简体   繁体   English

AndEngine Box2d - 如何避免联合“伸展”?

[英]AndEngine Box2d - how to avoid joint “stretching”?

I'm making a game, where you go on a bike through some hills. 我正在制作一个游戏,你骑自行车穿过一些山丘。 I have a problem with creating joints between bike's frame and it's wheels - the joints stretch somehow, they are not firm. 我有一个问题,就是在自行车架和它的车轮之间建立关节 - 关节会以某种方式伸展,它们不牢固。

I've tried some different joints with different setup, but it always happened. 我尝试了不同设置的不同关节,但总是发生。 I want no amortization, just firm joints between my bike and it's wheels. 我不需要摊销,只是我的自行车和车轮之间的紧密联系。

This is the setup, I use now: 这是设置,我现在使用:

    mFrame = new Frame(pX, pY, pPhysicsWorld);
    mWheelFront = new Wheel(pX + 47.0f, pY - 28.0f, pPhysicsWorld);
    mWheelRear = new Wheel(pX - 53.0f, pY - 28.0f, pPhysicsWorld);

    this.attachChild(mWheelFront);
    this.attachChild(mWheelRear);
    this.attachChild(mFrame);

    final RevoluteJointDef revoluteJointDefFront = new RevoluteJointDef();
    revoluteJointDefFront.initialize(this.mFrame.getBody(), this.mWheelFront.getBody(), this.mWheelFront.getBody().getWorldCenter());
    revoluteJointDefFront.collideConnected = false;
    revoluteJointDefFront.maxMotorTorque = 100f;
    pPhysicsWorld.createJoint(revoluteJointDefFront);

    final RevoluteJointDef revoluteJointDefRear = new RevoluteJointDef();
    revoluteJointDefRear.initialize(this.mFrame.getBody(), this.mWheelRear.getBody(), this.mWheelRear.getBody().getWorldCenter());
    revoluteJointDefRear.collideConnected = false;
    revoluteJointDefRear.maxMotorTorque = 100f;
    pPhysicsWorld.createJoint(revoluteJointDefRear);

What do I do wrong? 我做错了什么? How to get those "firm" joints? 如何获得那些“坚定”的关节?

Possibly solutions: 可能的解决方案:

  1. Make mass of the bike body and wheel not very different. 使自行车车身和车轮的质量差别不大。 They also should be not very big. 它们也应该不是很大。 According to box2d manual, stability degrades as the mass ratio passes 10:1. 根据box2d手册,当质量比超过10:1时,稳定性降低。 The most probably your problem is there. 最有可能你的问题就在那里。
  2. Scale the world, to make size of moving bodies between 0.1 and 10 meters. 缩放世界,使移动物体的大小在0.1到10米之间。 Box2D manual says: Box2D手册说:

    Continuous collision does not handle joints. 连续碰撞不能处理关节。 So you may see joint stretching on fast moving objects. 所以你可能会在快速移动的物体上看到关节伸展。

    If you are using bad scaling, then velocity maybe very high. 如果使用不良缩放,则速度可能非常高。

  3. Increase count of position and velocity iterations. 增加位置和速度迭代次数。 Box2D manual says: Box2D手册说:

    The suggested iteration count for Box2D is 8 for velocity and 3 for position. Box2D的建议迭代计数为8表示速度,3表示位置。 You can tune this number to your liking, just keep in mind that this has a trade-off between speed and accuracy. 您可以根据自己的喜好调整此数字,请记住,这需要在速度和准确度之间进行权衡。

  4. Make smaller time step (and increasing fps, to keep physic speed normal). 缩短时间步长(并增加fps,以保持物理速度正常)。 From my experience, time step 1/60 sec is good, 1/30 not so good, but still working, 1/20 quite unstable, and can simulate only simple models. 根据我的经验,时间步长1/60秒是好的,1/30不太好,但仍然有效,1/20相当不稳定,并且只能模拟简单的模型。

ps your ground body is not optimal :) There can be smaller count of fixtures. ps你的地面体不是最佳的:)固定装置的数量可以减少。 It is specially important, if you programming for mobile platforms. 如果您为移动平台编程,这一点尤为重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM