如何在 pybox2d 中实现 MouseJoint?
我正在尝试在 pybox2d 中用鼠标移动动态主体。以下是我尝试过的最小的、可重现的代码。
import pyglet
import Box2D
from Box2D import *
window = pyglet.window.Window(resizable=True)
object = pyglet.shapes.Rectangle(x=0,y=0,width=20,height=20,color=(255,255,0))
ground = pyglet.shapes.Rectangle(x=0,y=0,width=20,height=20,color=(255,0,0))
world = Box2D.b2World()
ground_body = world.CreateStaticBody()
object_body = world.CreateDynamicBody(position=(1, 0.25),shapes=b2PolygonShape(box=(0.1,0.1)))
support_body = world.CreateStaticBody(position=(0, 0),shapes=b2PolygonShape(box=(5,0.1)))
mouse_joint = None
object_body.awake = True
@window.event
def on_draw():
window.clear()
object.draw()
@window.event
def on_mouse_press(x, y, button, modifiers):
global mouse_joint
mouse_joint = world.CreateMouseJoint(bodyA=ground_body,bodyB=object_body, target=object_body.position)
object_body.awake = True
@window.event
def on_mouse_release(x, y, button, modifiers):
global mouse_joint
if mouse_joint:
world.DestroyJoint(mouse_joint)
mouse_joint = None
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
if mouse_joint:
mouse_joint.target = (x/100,y/100) # Taking one meter as 100 pixels
def update(dt):
world.Step(dt, 5, 5)
if mouse_joint:
print(mouse_joint.target, object_body.position.x, object_body.position.y)
object.x = (object_body.position.x-0.1) * 100
object.y = (object_body.position.y-0.1) * 100
pyglet.clock.schedule_interval(update, 1/60.)
pyglet.app.run()
但该对象不跟随鼠标。它停留在支撑体上,就好像不受鼠标关节的影响一样。我该如何解决这个错误?提前致谢!
I am trying to move a dynamic body with mouse in pybox2d. Following is the minimal, reproducible code that I tried.
import pyglet
import Box2D
from Box2D import *
window = pyglet.window.Window(resizable=True)
object = pyglet.shapes.Rectangle(x=0,y=0,width=20,height=20,color=(255,255,0))
ground = pyglet.shapes.Rectangle(x=0,y=0,width=20,height=20,color=(255,0,0))
world = Box2D.b2World()
ground_body = world.CreateStaticBody()
object_body = world.CreateDynamicBody(position=(1, 0.25),shapes=b2PolygonShape(box=(0.1,0.1)))
support_body = world.CreateStaticBody(position=(0, 0),shapes=b2PolygonShape(box=(5,0.1)))
mouse_joint = None
object_body.awake = True
@window.event
def on_draw():
window.clear()
object.draw()
@window.event
def on_mouse_press(x, y, button, modifiers):
global mouse_joint
mouse_joint = world.CreateMouseJoint(bodyA=ground_body,bodyB=object_body, target=object_body.position)
object_body.awake = True
@window.event
def on_mouse_release(x, y, button, modifiers):
global mouse_joint
if mouse_joint:
world.DestroyJoint(mouse_joint)
mouse_joint = None
@window.event
def on_mouse_drag(x, y, dx, dy, buttons, modifiers):
if mouse_joint:
mouse_joint.target = (x/100,y/100) # Taking one meter as 100 pixels
def update(dt):
world.Step(dt, 5, 5)
if mouse_joint:
print(mouse_joint.target, object_body.position.x, object_body.position.y)
object.x = (object_body.position.x-0.1) * 100
object.y = (object_body.position.y-0.1) * 100
pyglet.clock.schedule_interval(update, 1/60.)
pyglet.app.run()
But the object is not following the mouse. It stays on the support body as if it is not affected by the mouse joint. How can I solve this bug? Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过向 CreateMouseJoint 函数调用添加 maxForce 参数解决了这个问题。
I solved this issue by adding a maxForce parameter to the CreateMouseJoint function call.