Pergunta

I'm struggling to learn and understand box2d. Here is my problem:

I'm trying to make a simple car, the car body looks like this for now:

enter image description here

Now I'm trying to add a wheel, I create the wheel body and I assume I'll link the wheel to the car body using a b2RevolutionJoint.

Until I get to that part I just want to be able to position the wheel in its place, so in relation to the bodyCar Body. How do I do this correctly?

What I've seen online is something like this:

bodyDef2.position.Set(axle2->GetWorldCenter().x + 1.3*cos(-M_PI/3), axle2->GetWorldCenter().y + 0.3*sin(-M_PI/3));

However, I don't understand the cos and sin parts of that expression.

What I'm trying to do is this:

backWheelDef.position = b2Vec2(carBody->GetWorldCenter().x - 20.0/PTM_RATIO,carBody->GetWorldCenter().y);

Since my carBody vertices definition looks like this:

b2Vec2 vertices[] = {
        b2Vec2(100.0/PTM_RATIO,15.0/PTM_RATIO),
        b2Vec2(100.0/PTM_RATIO,30.0/PTM_RATIO),
        b2Vec2(50.0/PTM_RATIO,65.0/PTM_RATIO),
        b2Vec2(20.0/PTM_RATIO,65.0/PTM_RATIO),
        b2Vec2(0.0/PTM_RATIO,45.0/PTM_RATIO),
        b2Vec2(0.0/PTM_RATIO,15.0/PTM_RATIO)

    };

The result of the above way of positioning the wheel looks somehow correct (see below screenshot) but if you look at the above numbers it doesn't make much sense.

Since the carBody width is 100.0/PTM_RATIO I assume the GetWorldCenter().x would return a value equal to 50.0/PTM_RATIO.

So the wheel position would be after calculation at 30.0/PTM_RATIO, right?

Well, if you look at the result below the wheel x positioning seems to be at 20.0/PTM_RATIO.

enter image description here

Can someone please explain how things work with positioning bodies in relation to other bodies works in Box2D? Once I'm clear with these kind of things I'll move on to linking them using joints

Thanks a bunch!

LATER EDIT BASED ON iforce2d ANSWER:

Thank you iforce2d for answering and thank you for all the good insight you provide in the box2d world. What you're saying is perfectly true but this wasn't my question.

Once I've figured out that GetWorldCenter returns the center of gravity and GetPosition returns the position origin of the object it was easy to place my wheel exactly where I needed it to be in relationship to the carBody. Even though I still got the behaviour of it appearing under the body I knew this would be fixed once I add joints into equation.

Now I have another question that somehow relates to this positioning problem. Take a look at the image below:

enter image description here

Is this the right way my joints should look once I set their anchor points as below? I'm a little confused by them going from origin instead of them going from anchorPointA to anchorPointB

Thanks again!

Foi útil?

Solução

You are placing the wheel inside the car body to begin with, so in the first two or three time steps the two bodies will move to correct the overlap. It probably happens so fast that you don't see it, but the tell-tale signs are there, namely the wheel being positioned exactly at the edge of the car, and the car body being rotated a tiny bit.

If you pause the simulation (just don't call Step) you should see the bodies where you expect to see them.

Once you add the joint, the default behavior is for the two joined bodies not to collide with each other, so you wont see this problem.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top