Force placing pieces of character

Hi guys, I am having some difficulties trying to assemble a car here. It has a rectangle, two axles and two wheels, as seen in this image:

Image

Well, the problem is, I put the rectangle, then the axles with a prismatic joint tied to the rectangle, and then the wheels with a revolute joint to the axles, it works good, but the moment the simulation starts the car suffers an up force on the front part that sometimes puts it upside down, as seen here (just after the simulation starts):

Image

I think it has something to do with where I place the bodies before the simulation starts and how the joints put the bodies on place, but right now I am putting them exactly where they go.

Please if there is a need of code to know what is wrong ask specifically for that code because I don't want to make a huge post here.
It's hard to say for sure but usually these forces coming out of nowhere like this indicate that you have two (or more) joints set up in a situation where they can never both satisfy their constraints. My first suggestion would be to try the wheel joint instead of the prismatic/revolute combo. If you cannot use the wheel joint, try adding the joints one by one and maybe you will find one that causes things to go wrong.
I checked it and I had indeed that situation. Fixed the bug, and the problem persisted but in a smaller scale. I turned off the motor of the wheels and it disappeared completely, so I guess it is normal to happen this. Now I have this other issue, maybe related, the car goes crazy jumping around, it is like an explosion just after starting the simulation, and the car goes away very fast, here is an image to explain it:

Image

This only happens with specific deltas or sometimes slow cpu smartphones. I caught the image with max delta 0.008 and with vsync activated at the same time on a laptop, if it were caused by the simulation not having enough time to catch up the time, I would say it would happen with no vsync (so the pc is more loaded).

I will take a look at the wheel joint to see if I can get rid of this problem with that, but it would be nice to see what is the source of this problem.

UPDATE: It looks it is because the axles start jumping like crazy when the delta is that hight (even without wheels). Sometimes even after stabilizing it jumps like crazy. With 0.001 it looks like the simulation of the joint is a lot smoother. Is it normal that this level of step is required or could it be because I am doing something wrong?
How heavy is the intermediate body (the bridge between the prismatic/revolute joints) relative to the other bodies? If the intermediate body is significantly lighter than the other bodies it becomes the subject of an unresolvable tug-of-war between them. The car body tells it 'go here' and the wheel tells it 'go there'.... etc. You could try adjusting the densities so that the masses of all the bodies are similar.

But really I would just try the wheel joint if it's available (afaik some ports do not have it yet) because it's purpose built to handle just this situation.
dyeray: I think I just heard you say at least 1 problem: You'll need to use a fixed timestep for good results with Box2D.
irresistible force wrote:
How heavy is the intermediate body (the bridge between the prismatic/revolute joints) relative to the other bodies? If the intermediate body is significantly lighter than the other bodies it becomes the subject of an unresolvable tug-of-war between them. The car body tells it 'go here' and the wheel tells it 'go there'.... etc. You could try adjusting the densities so that the masses of all the bodies are similar.

But really I would just try the wheel joint if it's available (afaik some ports do not have it yet) because it's purpose built to handle just this situation.


It looks I have the wheeljoint on my port so I will just try it. The problem is not the weight, I've put both densities to 1 and it is the same, now I am testing it without wheels, only main body and prismatic joints.

I am going to put the code for the prismatic joints to check if I have maybe a trivial bug.

Code:
      PolygonShape axleShape = new PolygonShape();
      axleShape.setAsBox(0.1f,0.1f);
      FixtureDef axleFixture = new FixtureDef();
      axleFixture.density = 0.5f;
      axleFixture.density = 1;
      axleFixture.friction = 3;
      axleFixture.restitution = 0.3f;
      axleFixture.shape = axleShape;
      axleFixture.filter.groupIndex = -1;
      BodyDef axleBodyDef = new BodyDef();
      axleBodyDef.type = BodyType.DynamicBody;
      axleBodyDef.position.set(startPosition.x,startPosition.y);
      Body rearWheelSupport = physicsWorld.createBody(axleBodyDef);
      rearWheelSupport.createFixture(axleFixture);
      axleBodyDef.position.set(startPosition.x + m_size.x, startPosition.y);
      frontWheelSupport = physicsWorld.createBody(axleBodyDef);
      frontWheelSupport.createFixture(axleFixture);
      PrismaticJointDef prismaticJointDef = new PrismaticJointDef();
      prismaticJointDef.enableMotor = false;
      prismaticJointDef.maxMotorForce = 5;
      prismaticJointDef.motorSpeed = 5;
      prismaticJointDef.bodyA = body;
      prismaticJointDef.bodyB = rearWheelSupport;
      prismaticJointDef.collideConnected = false;
      prismaticJointDef.localAxisA.x = 0;
      prismaticJointDef.localAxisA.y = -1;
      prismaticJointDef.localAnchorA.x = 0;
      prismaticJointDef.localAnchorA.y = 0;
      prismaticJointDef.localAnchorB.x = 0;
      prismaticJointDef.localAnchorB.y = 0;
      prismaticJointDef.enableLimit = true;
      prismaticJointDef.lowerTranslation = 0.0f;
      prismaticJointDef.upperTranslation = 0.1f;
      rearShockJoint = (PrismaticJoint)physicsWorld.createJoint( prismaticJointDef );
      prismaticJointDef.bodyB = frontWheelSupport;
      prismaticJointDef.localAnchorA.x = m_size.x;
      frontShockJoint = (PrismaticJoint)physicsWorld.createJoint( prismaticJointDef );


pTymN wrote:
dyeray: I think I just heard you say at least 1 problem: You'll need to use a fixed timestep for good results with Box2D.


I don't have exactly that but almost, only the fastest computers won't use maxdelta, I am using what is called here semi-fixed timestep: http://gafferongames.com/game-physics/f ... -timestep/
Thanks! with the wheeljoint the problem disappears, and now it works better on lower performance devices.

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