如何在 pybox2d 中实现 MouseJoint?

发布于 2025-01-14 18:52:44 字数 1643 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2025-01-21 18:52:44

我通过向 CreateMouseJoint 函数调用添加 maxForce 参数解决了这个问题。

mouse_joint = world.CreateMouseJoint(bodyA=ground_body,bodyB=object_body, target=object_body.position,maxForce=1000)

I solved this issue by adding a maxForce parameter to the CreateMouseJoint function call.

mouse_joint = world.CreateMouseJoint(bodyA=ground_body,bodyB=object_body, target=object_body.position,maxForce=1000)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文