pymunk中自上而下的摩擦力

发布于 2024-10-20 08:35:13 字数 1118 浏览 4 评论 0原文

几天来一直在努力解决这个问题,很难在网上找到代码示例。

我正在制作一款自上而下的游戏,但无法让玩家通过按键移动。目前我正在使用 add_forceadd_impulse 将玩家朝某个方向移动,但玩家不会停止。 我读过有关使用空间和玩家之间的表面摩擦来模拟摩擦的内容,以下是在 tank.c 演示。

然而,我对 API 的理解还不足以将这段代码从chipmunk移植到pymunk中。

cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero));

到目前为止,我的东西看起来像这样:

class Player(PhysicalObject):
    BASE_SPEED = 5
    VISIBLE_RANGE = 400
    def __init__(self, image, position, world, movementType=None):
        PhysicalObject.__init__(self, image, position, world)
        self.mass = 100
        self.CreateBody()
        self.controlBody = pymunk.Body(pymunk.inf, pymunk.inf)
        self.joint = pymunk.PivotJoint(self.body, self.controlBody, (0,0))
        self.joint.max_force = 100
        self.joint.bias_coef = 0
        world.space.add(self.joint)

我不知道如何将空间/玩家的约束添加到空间中。

(需要有 1500+ 代表的人为这个问题创建一个 pymunk 标签)。

Been struggling with this for a couple of days, hard to find code examples on the net.

I'm making a topdown game and having trouble getting the player to move on key press. At the moment i'm using add_force or add_impulse to move the player in a direction, but the player doesn't stop.
I've read about using surface friction between the space and the player to simulate friction and here is how it's done in the tank.c demo.

However I don't understand the API enough to port this code from chipmunk into pymunk.

cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero));

So far, I have something that looks like this:

class Player(PhysicalObject):
    BASE_SPEED = 5
    VISIBLE_RANGE = 400
    def __init__(self, image, position, world, movementType=None):
        PhysicalObject.__init__(self, image, position, world)
        self.mass = 100
        self.CreateBody()
        self.controlBody = pymunk.Body(pymunk.inf, pymunk.inf)
        self.joint = pymunk.PivotJoint(self.body, self.controlBody, (0,0))
        self.joint.max_force = 100
        self.joint.bias_coef = 0
        world.space.add(self.joint)

I don't know how to add the constraint of the space/player to the space.

(Need someone with 1500+ rep to create a pymunk tag for this question).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

記憶穿過時間隧道 2024-10-27 08:35:13

Joe 将问题交叉发布到 Chipmunk/pymunk 论坛,并在那里得到了更多答案。 http://www.slembcke.net/forums/viewtopic.php?f=1&t=1450&start=0&st=0&sk=t&sd=a

我已经粘贴/编辑了部分内容我在下面的论坛中的回答:

#As pymunk is python and not C, the constructor to PivotJoint is defined as
def __init__(self, a, b, *args):
    pass

#and the straight conversion from c to python is
pivot1 = PivotJoint(tankControlBody, tankBody, Vec2d.zero(), Vec2d.zero())
# but ofc it works equally well with 0 tuples instead of the zero() methods:
pivot2 = PivotJoint(tankControlBody, tankBody, (0,0), (0,0))

mySpace.add(pivot1, pivot2)

根据您是否向 args 发送一个或两个参数,它将使用 C 接口中的 cpPivotJointNew 或 cpPivotJointNew2 方法来创建关节。这两种方法之间的区别在于 cpPivotJointNew 想要一个枢轴点作为参数,而 cpPivotJointNew2 想要两个锚点。因此,如果您发送一个 Vec2d pymunk 将使用 cpPivotJointNew,但如果您发送两个 Vec2d 它将使用 cpPivotJointNew2。

完整的 PivotJoint 构造函数文档位于:PivotJoint 构造函数文档

Joe crossposted the question to the Chipmunk/pymunk forum, and it got a couple of more answers there. http://www.slembcke.net/forums/viewtopic.php?f=1&t=1450&start=0&st=0&sk=t&sd=a

Ive pasted/edited in parts of my answer from the forum below:

#As pymunk is python and not C, the constructor to PivotJoint is defined as
def __init__(self, a, b, *args):
    pass

#and the straight conversion from c to python is
pivot1 = PivotJoint(tankControlBody, tankBody, Vec2d.zero(), Vec2d.zero())
# but ofc it works equally well with 0 tuples instead of the zero() methods:
pivot2 = PivotJoint(tankControlBody, tankBody, (0,0), (0,0))

mySpace.add(pivot1, pivot2)

Depending on if you send in one or two arguments to args, it will either use the cpPivotJointNew or cpPivotJointNew2 method in the C interface to create the joint. The difference between these two methods is that cpPivotJointNew want one pivot point as argument, and the cpPivotJointNew2 want two anchor points. So, if you send in one Vec2d pymunk will use cpPivotJointNew, but if you send in two Vec2d it will use cpPivotJointNew2.

Full PivotJoint constructor documentation is here: PivotJoint constructor docs

请帮我爱他 2024-10-27 08:35:13

我对您提到的任何一个系统都不熟悉,但可能相关的一些基本游戏理念是:

  • 如果您添加影响运动的力(或冲动),为了使实体停止,您还必须接受它离开。在我的游戏中,如果我有一个函数 AddImpulse()/AddForce(),我就会有一个相应的函数,例如 Apply_Friction(),它将根据您想要的程度(基于地形?)反转效果,直到移动速度为零或更低。我个人不会费心使用这种移动方法,除非游戏需要,因为它可以添加更多值得每次更新的计算。

  • 应该有某种方法来跟踪 KeyPressed 和/或 KeyPosition,然后使用这些 x/y 坐标根据玩家速度递增。在不知道您已经尝试过什么或 API 为您做了多少的情况下,很难说更多。

希望这有帮助。如果这是您已经知道的内容,请忽略它。

I'm not familiar with either system you've mentioned, but some basic game ideas that may relate are:

  • If you add a force (or impulse) which affects movement, for the entity to stop, you must also take it away. In my games if I had a function AddImpulse()/AddForce() I would have a corresponding one such as Apply_Friction() which would reverse the effect by however much you want (based on terrain?) until movespeed is zero or less. I personally wouldn't bother with this method for movement unless needed for gameplay since it can add more computations that its worth each update.

  • There should be some way to track KeyPressed and/or KeyPosition and then using those x/y coordinates are incrememnted based on player speed. Without knowing what you've tried or how much the API does for you, it's hard to really say more.

Hope this helps. If this is stuff you already knew kindly ignore it.

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