Joint Anchor Issue [Solved]

Hello again.
After finally figuring out how to create joints properly, I created a top-down car.
The car consists of 5 boxes, 4 for the tires and one for the car body.
I created 2 revolute joints for the steering tires, and 2 prismatic joints for the back tires.
I anchored them as follows:
Code:
b2RevoluteJointDef leftFrontJointDef;
leftFrontJointDef.Initialize(leftFrontWheel, carBody, leftFrontWheel->GetWorldCenter());
leftFrontJointDef.enableMotor = true;
leftFrontJointDef.maxMotorTorque = 100;

leftJoint = world->CreateJoint(&leftJointDef);
leftRevJoint = static_cast<b2RevoluteJoint*>(leftJoint);

b2RevoluteJointDef rightFrontJointDef;
rightFrontJointDef.Initialize(rightFrontWheel, carBody, rightFrontWheel->GetWorldCenter());
rightFrontJointDef.enableMotor = true;
rightFrontJointDef.maxMotorTorque = 100;

rightJoint = world->CreateJoint(&rightJointDef);
rightRevJoint = static_cast<b2RevoluteJoint*>(rightJoint);

b2PrismaticJointDef leftRearJointDef;
leftRearJointDef.Initialize(leftRearWheel, carBody, leftRearWheel->GetWorldCenter(), b2Vec2(1,0));
leftRearJointDef.enableLimit = true;
leftRearJointDef.lowerTranslation = leftRearJointDef.upperTranslation = 0;

b2PrismaticJointDef rightRearJointDef;
rightRearJointDef.Initialize(rightRearWheel, carBody, rightRearWheel->GetWorldCenter(), b2Vec2(1,0));
rightRearJointDef.enableLimit = true;
rightRearJointDef.lowerTranslation = rightRearJointDef.upperTranslation = 0;


The positions are correct, in the game I rendered that shapes and it all aligns until I start driving the car!
I also tried not using the Initialize(...) function, but it yielded the same results: The joints all just spin around the center of the car body.
Actually, the wheels even steer properly, turning around the center of the wheel, but after taking a curve, they all shift out of alignment and spin around the center of the car.

Here are some images to help describe what I'm getting:

Image

Image

What should I do?

EDIT:
I actually tried out some more joints, putting in all kinds of anchor positions, but all produce the same behavior!
See below:

Code:
b2DistanceJointDef distTL;
distTL.Initialize(carBody, leftWheel, leftFrontWheelPosition, leftWheel->GetWorldCenter());
distTL.collideConnected = false;
world->CreateJoint(&distTL);

b2DistanceJointDef distTR;
distTR.Initialize(carBody, rightWheel, rightFrontWheelPosition, rightWheel->GetWorldCenter());
distTR.collideConnected = false;
world->CreateJoint(&distTR);

b2DistanceJointDef distBL;
distBL.Initialize(carBody, leftRearWheel, leftRearWheelPosition, leftRearWheel->GetWorldCenter());
distBL.collideConnected = false;
world->CreateJoint(&distBL);

b2DistanceJointDef distBR;
distBR.Initialize(carBody, rightRearWheel, rightRearWheelPosition, rightRearWheel->GetWorldCenter());
distBR.collideConnected = false;
world->CreateJoint(&distBR);


None have any effect at all!
Why do you need to have any joints at all for the rear wheels?

Your images are not showing, its a bit hard to visualize what you mean here...
Hmm, I don't know why the images aren't showing up for you...

https://1669622510138953627-a-1802744773732722657-s-sites.googlegroups.com/site/cireneikualsgameprogramming/game-uploads/carPic1.png?attachauth=ANoY7crzsW-TkqRxTpuNgI6WVWHnUHPu-aizEZyHMXOCZF9pWALfTt9I2lcZU7HhJwYTYW4WEREka-tWzhkVW9jp1GduZ5FxJ6FupW1edTKidDAKYU5wXyGwlK6mWTJsGmj0M7Pi2chfu3U_qecOMFNl3Vilcbs82lm5mDOJqg7qjZpn3wXj7ff-vXBKL-9lWvalnYbhYBReoS461ZGqdDeVIbhfp75I3wgk_mk5NP8KjCY6zy6Ns20%3D&attredirects=0

https://1669622510138953627-a-180274477 ... edirects=0

Does this work? The car is a little hard to see in the image.

About the prismatic joints, I honestly don't know! This came from a tutorial, so I assumed it was correct, I would have to check it again for the reason why it was chosen, but right now something is wrong with their website. My primary goal is to get the revolute joints working.
I tried replacing the prismatic joints with a more logical joint choice, a Weld joint, but they had absolutely no effect at all. I can't not use any joints at all, I need a separate body from which to remove the orthogonal velocity in order to properly simulate tires.
There is some sort of joint forming (because without it the tires just float around), but it ALWAYS orbits the center of the car, no matter which anchor point I give it. It is not how I am drawing it for sure, because the car drives as if its tires were orbiting it! And, like I said earlier, the tires still mysteriously steer around their center axes as if it were anchored there, but the joint just isn't keeping the two bodies together at any point besides the center point (is behaves more like a distance joint now). I may try just using an older version of box2d, this one is making me pull my hair out!
Dude... look carefully and thoughtfully at the positions of those wheels in the second pic.

My guess is that everything is working fine and you are just drawing things incorrectly. Specifically, your rectangles seem to be always drawn in their original rotation. The position of the wheels is exactly where I would expect them to be if the car body was rotated.

Have you tried using Box2d's debug draw feature?

I don't think you even need a weld joint for the rear wheels, do they ever move in relation to the car body? If not you could just attach them as fixtures to the car body itself.
I am drawing an entire map with static physics shapes (without joints) on them and it works fine, so the car must be drawing correctly. See the images below. Yes, the roads don't connect, they actually can, it is just the map file I am using that is set up like that (I was too lazy to use one with actual roads, so no corner pieces and no rotated tiles). Using "Debug Draw" still requires me to make the drawing functions (overload). Also, I have made games using Box2D without joints that work fine (side scrolling shooter where player is a body and so is the map and some crates). The drawing functions I used came from one of the included examples. There is a reason that tires are still parallel: they have joints, the joints just aren't working properly; without them, the tires go all over the place. As for the tires, they have to be separate bodies so I can cancel their orthogonal velocity to create a proper car-like driving behavior. The tires can actually slide up and down when I take curves or bump walls. Also, the car certainly drives as if its tires were spinning all over the place! I wouldn't have drawn the physics shapes if it drove correctly (It just had a jeep model on top of it before that).

Image with bodies drawn:
https://1669622510138953627-a-1802744773732722657-s-sites.googlegroups.com/site/cireneikualsgameprogramming/game-uploads/carGamePic1.png?attachauth=ANoY7cpV2zKPJo6PBFK3sjn0bfPL01pRsYYA9G0vNVvaVFAm9bDxIVyWKKy0IA2goVfRZqsZR3nm2MJ4q6ncS8SU5jd9thF4IyFOaAFLNHZhvhzkZyLQqEXp6NU-K0jl1OqdkUTexQ5anGZnG6HO1KdL3PUnhdLkRzhX4FHaFRPeNLydoP4ZOuRWSiPgfu85p3YwyrKxD2uuOF-n-G9Gxvjti8Pk_YP3-O7ExonUDzAQ2N1H8P6ud2I%3D&attredirects=0

Without:
https://1669622510138953627-a-1802744773732722657-s-sites.googlegroups.com/site/cireneikualsgameprogramming/game-uploads/carGamePic2.png?attachauth=ANoY7cq4jJB5BlMcPnRLIi9BfVi9pi0wLTxinEtCF4jZAjgC_tvBSkoezM9-obLc-nFTSgm7UY8oPHGjE6fFBfTcSgqTUoSV39hNdMbU-eMBRVJAQqgvuSV89R4HQwV-OC79by7sxaaIS42Ug1W8XLNg0MH5HN6wNdsF2pIIcLMfFCY0Hk64iDOEoxwy-f-MNU49O409nyLnf8LTc_7DDsPhsRBFxfUgxZ6s5o2sn_YsHTQEpIfU5R0%3D&attredirects=0

Any ideas what could be happening with those joints? Every other feature works fine.
Debug draw will handle the rotation for you which I think is your problem. None of your screenshots show any rotated rectangles at all.
Like I already said, the physics act the way they are drawn. The boxes collide appropriately with each other. For the rotation, look at a previous game I made which handles the physics (with no joints though, but ray casting and stuff for explosions, all working properly) and draws them just as I am drawing them now (both the car game and the game in the image use OpenGL):

Image

Here they work properly. In this game I also created an option to render the actual shapes so it was possible to view the capsule-like shape the player character had, which appeared correctly.

Here is the code rendering the car:

Code:
b2Vec2 position = carBody->GetPosition();
   float angle = carBody->GetAngle();
   glTranslatef(position.x, 0, position.y);
   glRotatef(toDegrees(angle), 0.0f, 1.0f, 0.0f);
   DrawPolygon(carBodyShapeDef.m_vertices, carBodyShapeDef.m_vertexCount, b2Color(0.4f, 0.6f, 0.3f));

   glPopMatrix();
   glPushMatrix();

   position = leftWheel->GetPosition();
   angle = leftWheel->GetAngle();
   glTranslatef(position.x, 0, position.y);
   glRotatef(toDegrees(angle), 0.0f, 1.0f, 0.0f);
   DrawPolygon(leftWheelShapeDef.m_vertices, leftWheelShapeDef.m_vertexCount, b2Color(0.4f, 0.6f, 0.3f));

   glPopMatrix();
   glPushMatrix();

   position = rightWheel->GetPosition();
   angle = rightWheel->GetAngle();
   glTranslatef(position.x, 0, position.y);
   glRotatef(toDegrees(angle), 0.0f, 1.0f, 0.0f);
   DrawPolygon(rightWheelShapeDef.m_vertices, rightWheelShapeDef.m_vertexCount, b2Color(0.4f, 0.6f, 0.3f));

   glPopMatrix();
   glPushMatrix();

   position = leftRearWheel->GetPosition();
   angle = leftRearWheel->GetAngle();
   glTranslatef(position.x, 0, position.y);
   glRotatef(toDegrees(angle), 0.0f, 1.0f, 0.0f);
   DrawPolygon(leftRearWheelShapeDef.m_vertices, leftRearWheelShapeDef.m_vertexCount, b2Color(0.4f, 0.6f, 0.3f));

   glPopMatrix();
   glPushMatrix();

   position = rightRearWheel->GetPosition();
   angle = rightRearWheel->GetAngle();
   glTranslatef(position.x, 0, position.y);
   glRotatef(toDegrees(angle), 0.0f, 1.0f, 0.0f);
   DrawPolygon(rightRearWheelShapeDef.m_vertices, rightRearWheelShapeDef.m_vertexCount, b2Color(0.4f, 0.6f, 0.3f));

   glPopMatrix();
   glPushMatrix();


Where Draw Polygon has the following implementation:

Code:
void DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
   glColor3f(color.r, color.g, color.b);
   glBegin(GL_LINE_LOOP);
   for (int32 i = 0; i < vertexCount; ++i)
   {
      glVertex3f(vertices[i].x, 0.1f, vertices[i].y);
   }
   glEnd();
}


However, this is all OpenGL, not Box2D. The previous function can actually be used to overload the debug drawing class, and I believe is used in the testbed. In my game however, since it is 3D, I cannot use it properly. Trust me, its my implementation of the physics, not the drawing! Otherwise, the car would drive properly and not like the tires are orbiting the car!
Yeah I agree that all looks fine...
What angle are you getting from GetAngle() ? Have you seen the car body or wheels rotate at all?
How about checking the differences to your other game, since obviously that one is working well.

By the way your last paragraph is a bit confusing - as you say the DrawPolygon function you have would fit straight into the debug draw overload, but for some reason you cannot use it... yet... you are using it?!
Yes, the tires DO rotate, when I steer the car and, well, when they start orbiting the car mysteriously. GetAngle() gives the correct angle, the car does turn, but not the way I want it to turn 😐. The other game uses a slightly older version of the engine I designed for this one. For the debug draw function, I did not actually use debug draw, only some of the implementations used in the test bed. I actually just now attempted to make use of the real debug draw, which required some modifications to the test bed implementations (z-axis, all shapes are on one plane), but for some reason, even after calling world.setDebugDraw and world.drawDebugData, Box2D never even calls any of my overloaded functions. Anyways, that isn't the problem, its the implementation of the physics. All of the boxes I am drawing react with each other perfectly, only the joints don't (the car crashes very well...).

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