我正在努力按照标题所说的去做。我有一个手持枪的角色,我试图让枪指向光标。我认为 DampedRotarySpring 会是一个不错的方法,但事实证明并不是那么简单。枪是一个具有 Segment 形状的动态体,对于光标,我创建了一个静态体,我在每一步都使用 pygame 将其位置设置为鼠标位置。
当我运行程序时,除了重力或碰撞的影响之外,枪根本不动。
以下是相关代码:
# add crosshairs at the location of the mouse
pointer_body = pymunk.Body()
pointer_shape1 = pymunk.Segment(pointer_body, (0,CROSSHAIRS_SIZE), (0,-CROSSHAIRS_SIZE), 1) # vertical segment
pointer_shape2 = pymunk.Segment(pointer_body, (-CROSSHAIRS_SIZE,0), (CROSSHAIRS_SIZE,0), 1) # horizontal segment
# add a spring that will angle the gun toward the mouse
spring = pymunk.DampedRotarySpring(me.gun.body, pointer_body, 0, 0.01, 1)
space.add(pointer_shape1, pointer_shape2, spring)
while True:
# handle event queue
for event in pygame.event.get():
if event.type == pygame.MOUSEMOTION:
from math import atan2
# update location of pointer
pointer_body.position = flipy(pygame.mouse.get_pos())
pointer_body.angle = atan2( (pointer_body.position.y - me.gun.body.position.y), (pointer_body.position.x - me.gun.body.position.x) )
编辑:
这是我所有代码的 Gist 存储库:https ://gist.github.com/4470807 。主循环在 ragdoll.py 中。