I'm trying to implement some 2D vehicle physics. For this to work I have 2 wheels attached to the body of the car. After doing some collisions checks I place the wheels at their new position. Say the first wheel is at (-0.5, 1) and the second wheel is at (0.5, 1) I now have to calculate the angle and position of the body between the wheels. In the given example the angle should be 0. But how do I calculate the angle?

Because pictures make everything easier: The blue lines are the positions of the wheels and the pink line is the body. Given the two positions of the wheels, at what angle is the body in relation to the ground?

Situational sketch

  • \$\begingroup\$ I don't understand the image. Is this top-down view? Why is the wheel on the right larger than the one on the left? \$\endgroup\$ – bummzack Dec 15 '11 at 10:58
  • \$\begingroup\$ Hmm, yes the image isn't too clear, sorry. But I meant the blue lines to be vectors from Y=0 to indicate the position \$\endgroup\$ – Roy T. Dec 15 '11 at 11:02
  • 1
    \$\begingroup\$ Oh I see. So the pink line is the wheels axis? \$\endgroup\$ – bummzack Dec 15 '11 at 11:06
  • \$\begingroup\$ Yeah exactly that \$\endgroup\$ – Roy T. Dec 15 '11 at 11:28

All you need is atan2:

angle = atan2(frontwheel.y - rearwheel.y, frontwheel.x - rearwheel.x);
  • \$\begingroup\$ You're absolutely right, I was just thinking too difficult! \$\endgroup\$ – Roy T. Dec 15 '11 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.