3

在 Pymunk 中,当我旋转一个物体时,它的形状并没有旋转。当我施加脉冲时,形状会像预期的那样同步移动。我的谷歌搜索表明当身体旋转时,身体的形状应该是旋转的。我从根本上误解了轮换吗?

这是相关的轮换代码:

def selectEntity(self, location):
    shapes = self.space.point_query(location)
    bodies = set()
    for shape in shapes:
        bodies.add(shape.body)
    for body in bodies:
        body.angle += 1.57079633 # + 90 degrees

下面是初始化代码:

def _addShip(self, mass, center, angle = 0.):
    radius = 10

    groupId = self.getNextBodyId() # shapes in the same group do not generate collisions
    body = pymunk.Body(mass, pymunk.moment_for_circle(mass, radius / 10, radius)) # mass, inner radius, outer radius, offset
    body.angle = angle

    partOne = pymunk.Circle(body, radius, center)
    partOne.group = groupId
    partOne.color = THECOLORS['blue']
    partOne.friction = .8

    partTwo = pymunk.Circle(body, radius, (center[0], center[1] + 20))
    partTwo.group = groupId
    partTwo.color = THECOLORS['blue']
    partTwo.friction = .8

    ship = (partOne, partTwo, body)
    self.space.add(*ship)
4

1 回答 1

0

碰撞树/哈希不会立即更新,您需要先向前推进空间。另一种更新碰撞数据的方法是调用 Space.reindex_shape(shape_that_has_been_moved)。

请参阅此示例:

>>> from pymunk import *
>>> s = Space()
>>> b = Body(1,1)
>>> c1 = Circle(b, 10, (-10,0))
>>> c2 = Circle(b, 10, (10,0))
>>> s.add(b,c1,c2)
>>> s.step(.1)
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]
>>> b.angle
0.0
>>> b.angle = 1.57
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]
>>> s.step(.1)
>>> s.point_query((-15,0))
[]
>>> s.point_query((0,-15))
[<pymunk.Circle object at 0x02264690>]
>>> b.angle = 0
>>> s.point_query((-15,0))
[]
>>> s.reindex_shape(c1)
>>> s.reindex_shape(c2)
>>> s.point_query((-15,0))
[<pymunk.Circle object at 0x02264690>]
于 2012-12-22T21:37:47.327 回答