gpt4 book ai didi

当我旋转 body 时,Pymunk body 的形状不会旋转

转载 作者:行者123 更新时间:2023-12-02 05:03:35 29 4
gpt4 key购买 nike

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

这里是相关的旋转代码:

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)

最佳答案

碰撞树/哈希不会立即更新,需要先把空格往前踩。另一种更新碰撞数据的方法是调用 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>]

关于当我旋转 body 时,Pymunk body 的形状不会旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13983518/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com