简体   繁体   English

Box2D:绳索摆(不停)

[英]Box2D: Rope pendulum (no stop)

I would like to make a scene where there is a "pendulum" that oscillates continuously, no stop. 我想做一个场景,那里有一个“钟摆”不断振荡,没有停止。 I have uploaded an image for better clarity. 我上传了一张图片,以提高清晰度。 So i try to use Box2D joints. 所以我尝试使用Box2D关节。 For example: 例如:

   RevoluteJointDef revDef = new RevoluteJointDef();
     revDef.initialize(ball, box, ball.getWorldCenter());
     revDef.lowerAngle = 0 * MathUtils.degreesToRadians;
     revDef.upperAngle = 180 * MathUtils.degreesToRadians;
     revDef.enableLimit = true;
     revDef.maxMotorTorque = 10.0f;
     revDef.motorSpeed = 2.0f;
     revDef.enableMotor = true;

  revoluteJoint = (RevoluteJoint)world.createJoint(revDef);

But it doesn't work. 但这是行不通的。 If i comment limits and motor lines i obtain the same result that i obtain when these lines are uncommented. 如果我注释极限和运动线,则我获得的结果与未注释这些行时的结果相同。 Although motor is enabled, it seems not work. 虽然启用了电动机,但似乎无法正常工作。

PS The motor must stop when the user releases the box by press a button. PS当用户按下按钮释放盒子时,电动机必须停止。 So the box falls to the ground due to the force of gravity. 因此,箱子由于重力而掉落到地面上。

Can someone help me? 有人能帮我吗? Thanks!! 谢谢!!

Scene image 场景图片

I don't think you need a revolute joint for this, but a rope joint (b2RopeJoint). 我认为您不需要为此使用旋转接头,而需要使用绳索接头(b2RopeJoint)。 A revolute joint will make the two object rotate around a single point. 旋转关节将使两个对象绕单个点旋转。 The rope joint will keep one swinging from another like a pendulum. 绳索接头将使一个摆杆像另一个摆杆一样摆动。

You need to make the pendulum attach with a single rope joint to a static body. 您需要使用单个绳索接头将摆锤固定到静态物体上。 Then cut the rope joint when you want it to fall. 然后,当您希望其掉落时,将其剪断。 If gravity is turned on and you don't have any retarding forces, the pendulum should continue indefinitely (or for a really long time depending on numerics). 如果打开重力并且您没有任何减速力,则摆锤应无限期继续(或根据数字持续很长时间)。

Take a look at this post that was just done for something like this. 看一下这篇文章 ,它只是为这样的事情完成的。 Note that the code is also posted on github here . 注意,代码也发布在github上 In that case, there were two extra rope joints added to constrain the body so that it could not move beyond the end of the initial swing. 在那种情况下,增加了两个额外的绳索接头以约束身体,使其无法移动到初始挥杆结束之后。 I don't think you need those. 我认为您不需要这些。

To create the pendulum yourself, use something like: 要自己创建钟摆,请使用以下方法:

   // Calculate the local position of the
   // top of screen in the local space
   // of the ground box.
   CCSize scrSize = CCDirector::sharedDirector()->getWinSize();
   b2Vec2 groundWorldPos = b2Vec2((scrSize.width/2)/PTM_RATIO,(scrSize.height)/PTM_RATIO);
   b2Vec2 groundLocalPos = m_pGround->GetLocalPoint(groundWorldPos);

   // Now create the main swinging joint.
   b2RopeJointDef jointDef;
   jointDef.bodyA = m_pGround;
   jointDef.bodyB = body;
   jointDef.localAnchorA = groundLocalPos;
   jointDef.localAnchorB = b2Vec2(0.0f,0.0f);
   jointDef.maxLength = (groundWorldPos-body->GetWorldCenter()).Length();
   jointDef.collideConnected = true;
   world->CreateJoint(&jointDef);

NOTE This is in C++, not java (for libgdx), but the approach should be sound and you just have to map the "->" onto the "." 注意这是在C ++中,而不是Java(对于libgdx),但是这种方法应该是正确的,您只需要将“->”映射到“”即可。 where needed. 在需要的地方。

In my example, it ended up looking like this (image liberated from other posted answer): 在我的示例中,它最终看起来像这样(图像从其他发布的答案中解放出来):

在此处输入图片说明

Was this helpful? 这个有帮助吗?

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

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