0
\$\begingroup\$

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

\$\endgroup\$
4
  • \$\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
    Commented Dec 15, 2011 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.
    Commented Dec 15, 2011 at 11:02
  • 1
    \$\begingroup\$ Oh I see. So the pink line is the wheels axis? \$\endgroup\$
    – bummzack
    Commented Dec 15, 2011 at 11:06
  • \$\begingroup\$ Yeah exactly that \$\endgroup\$
    – Roy T.
    Commented Dec 15, 2011 at 11:28

1 Answer 1

1
\$\begingroup\$

All you need is atan2:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .