我在努力做标题上说的。我有一个角色手上绑着一把枪,我试图让枪指向光标。我认为阻尼旋转弹簧是一个很好的方法,但事实证明并不是那么简单。枪是一个具有分段形状的动态实体,对于光标,我创建了一个静态实体,每个步骤都将其位置设置为鼠标位置。在
当我运行程序时,除了重力或碰撞的影响,枪根本不动。在
以下是相关代码:
# 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) )
编辑:
这里是我所有代码的主旨存储库:https://gist.github.com/4470807。 主回路在碎布玩偶.py. 在
也许你的问题出在弹簧参数上?除非枪非常轻,否则刚度和阻尼看起来很低。在
看看我昨天添加到pymunk的代码示例:http://code.google.com/p/pymunk/source/browse/trunk/examples/damped_rotary_spring_pointer.py
(当旋转在正负π之间翻转时,有一件事发生了,我将看看)
gist中代码的问题在于,你用两个关节将枪固定在手上,使它们保持在同一个位置和相同的旋转角度。但是,手是胭脂身体,不会旋转。因此,当枪被它和光标之间的弹簧拉动时,它就不会旋转,因为另一个关节更强。在
如果你不想把枪从接头上拆下来,你肯定可以。在
查看代码的一个固定分支,以了解确切的细节:https://gist.github.com/4505219
我发现问题后的一些故障排除技巧:
相关问题 更多 >
编程相关推荐