我该如何将pyglet精灵绑定到pymunk物体上,以便当物体旋转时,精灵也会旋转?
没有内置同步功能,所以每帧都需要自己完成。但是不用担心,这很容易。
如果你的身体位置在形状的中央,并且图像大小相同,那么你需要两件事。首先,将图像锚点设置为其大小的一半。然后在更新方法中循环要同步的身体,并将精灵位置设置为身体位置,将精灵旋转设置为转换为度数的身体旋转。你可能还需要将其旋转180度(如果你的模型被翻转了),和/或者反转旋转角度。
代码如下:
img = pyglet.image.load('img.png')
img.anchor_x = img.width/2
img.anchor_y = img.height/2
sprite = pyglet.sprite.Sprite(img)
sprite.body = body
def update(dt):
sprite.rotation = math.degrees(-sprite.body.angle)
sprite.set_position(sprite.body.position.x, sprite.body.position.y)
关于完整的示例,请查看我创建的这个示例:https://github.com/viblo/pymunk/blob/master/examples/using_sprites_pyglet.py
(我是pymunk的作者)
+ 180
真的需要吗?我没有加它反而得到了更好的结果。 - Flimm