Box2D damper issue

I have a problem with Box2D, namely I want to make a damper/absorber for car in my game, everything works good but one part of damper fall out from the other like in movie: http://www.youtube.com/watch?v=gBPnfOLwbmg.

code of joint and bodies:

Code: Select all

BodyDef amorDef=new BodyDef();
amorDef.type=BodyType.DynamicBody;
amorDef.position.set(50, 100);
amorBody=world.createBody(amorDef);
PolygonShape ksztaltamor=new PolygonShape();
ksztaltamor.setAsBox(7.0f, 30.0f);
FixtureDef fixamorDef=new FixtureDef();
fixamorDef.shape=ksztaltamor;
fixamorDef.density = 3.1f;
fixamorDef.friction = 10f;
fixamorDef.restitution = 0.3f;
amorBody.createFixture(fixamorDef);

BodyDef amorDef2=new BodyDef();
amorDef2.type=BodyType.DynamicBody;
amorDef2.position.set(amorBody.getPosition().x, amorBody.getPosition().y+20);
amorBody2=world.createBody(amorDef2);
PolygonShape ksztaltamor2=new PolygonShape();
ksztaltamor2.setAsBox(10.0f, 30.0f);
FixtureDef fixamorDef2=new FixtureDef();
fixamorDef2.shape=ksztaltamor2;
fixamorDef2.density = 3.1f;
fixamorDef2.friction = 10f;
fixamorDef2.restitution = 0.3f;
amorBody2.createFixture(fixamorDef2);
amor1=new PrismaticJointDef();
amor1.initialize(amorBody, amorBody2, amorBody.getWorldCenter(), new Vector2(0,1));

amor1.lowerTranslation=-0.2f;
amor1.upperTranslation=0.0f;
amor1.enableLimit=true;
amor1.maxMotorForce=20.5f;
amor1.motorSpeed=4.0f;
amor1.enableMotor=true;
world.createJoint(amor1);

Thanks;)
You could try raising the density of the smaller pieces so that the bodies have more similar masses.
Unfortunatelly it doesn't help, I think that the problem is a horizontal bar has collision with smaller parts of absorber and thats why it falls out but i don't know how to change it cause this parts doesn't jointed to each other, so joint.collideConnected=false doesn't exist.
this is a code of bar and joints:

Code: Select all

   //bar
           BodyDef deskaDef2=new BodyDef();
           deskaDef2.type=BodyType.DynamicBody;
           deskaDef2.position.set(amorBody.getPosition().x+30, amorBody.getPosition().y+35);
           deskaBody2=world.createBody(deskaDef2);
           PolygonShape ksztaltDeska2=new PolygonShape();
           ksztaltDeska2.setAsBox(50.0f, 10.0f);
           FixtureDef fixDeskaDef2=new FixtureDef();
           fixDeskaDef2.shape=ksztaltDeska2;
           fixDeskaDef2.density = 7.0f;
           fixDeskaDef2.friction = 0.3f;
           deskaBody2.createFixture(fixDeskaDef2);

           //Joints
           spaw=new WeldJointDef();
           spaw2=new WeldJointDef();
           spaw.initialize(deskaBody2, amorBody4, deskaBody2.getWorldCenter().add(20,0));
           spaw2.initialize(deskaBody2, amorBody2, deskaBody2.getWorldCenter().add(-30,0));
           world.createJoint(spaw);
           world.createJoint(spaw2);
You could set the collision filter of those two fixtures so that they do not collide with each other. Look for info on 'categoryBits' and 'maskBits'.
https://www.iforce2d.net/b2dtut/collision-filtering

By the way, you can rotate videos on YouTube, under the 'Enhancements' section of the edit page for the video 🙂
It's work! 😉 Now i quite good, it's still possible to fall out the absorber but only in extremely conditions;)

But I have next problem:/
My absorbers get lower but then stay in this position instead of get higher, it's my settings:

Code: Select all

Horizontal Bar:
           fixDeskaDef2.density = 0.3f;

Smaler Part Of damper:
           fixamorDef3.density = 1.2f;
           fixamorDef3.friction = 10f;
           fixamorDef3.restitution = 0.3f;

Biger part od damper:
           fixamorDef4.density = 1.0f;
           fixamorDef4.friction = 10f;
           fixamorDef4.restitution = 0.3f;

Joint:
           amor1.lowerTranslation=-5.0f;
           amor1.upperTranslation=5.0f;
           amor1.enableLimit=true;
           amor1.maxMotorForce=10000.0f;
           amor1.motorSpeed=10000.0f;
           amor1.enableMotor=true;

Gravity:
           Vector2(0, -100)




Thanks 😉
I think you would need to dynamically change the motor settings (change them every time step) depending on the current position of the joint. For example if you are trying to simulate a spring, the force would be stronger when the spring is compressed more.
I have some trouble to dynamically change the motor settings, I came up with only one way to change it, I use LibGDX and in "onCreate()" I set up joints and after world.createJoint(Joint); i can't modify,
but "onRender()" i destory joints then modify and create again, but I think it's not professional way.
I don't know about libGDX, but the original C++ version has functions like SetMotorSpeed which you can use to change the joint settings after creating it.
In Java there is no function like this, maybe I have to update Box2d.
Thank you for all replays 😉
In libgdx these functions exist. I can't vouch for how well they work, but they are there.

Original thread: https://box2d.org/forum/viewtopic.php?t=9459