Calculating direction of car from normal vector and angle

Posts: 10
Joined: 2006.10
Post: #1
For an assignment, I have to program a racing game

At the moment I try calculating the angle from the normal vector to the surface and the rotation angle.

I am trying to use the formula for the rotation from

As the 'forward' vector is (0, 0, -1), it should just be a matter of taking the last column?

The angle is expressed as the angle between the normal and the y axis.
float theta = acos(myCar->n[1]) + myCar->orientation->y;
myCar->d[0] = -(myCar->n[1] * sin(theta) + (1-cos(theta))*myCar->n[2]*myCar->n[0]);
myCar->d[1] = -(-myCar->n[0]*sin(theta) + (1-cos(theta))*myCar->n[1]*myCar->n[2]);
myCar->d[2] = -(1+ (1-cos(theta))*(myCar->n[2]*myCar->n[2]-1));

Where d is the direction vector (shown by green) and n is the normal vector for the car (shown by blue)...

[Image: dvector.png]

Anyway, when I draw the (scaled) direction vector I get what you see below. The green line should point straight ahead, parallel to sides of the car's bounding box. It works when the car is on the flat, but then goes funny when it is on a hill, although as you can see the bounding box and the normal vectors look correct.

It isn't a issue with degrees/radians as angles are stored as radians....

Special thanks to those who helped with my last question, you can see the background image being used!
Quote this message in a reply
Posts: 161
Joined: 2005.07
Post: #2
I doubt this is the 'correct' way to do it, but I'd do something crazy like this:

1) call glPushMatrix to save the current matrix
2) glLoadIdentity, then call the glRotates that you use to rotate the car model into position
3) use glGetFloatv(GL_MODELVIEW_MATRIX, mat) to store the current OpenGL matrix
4) Take a (0, 0, 1) vector and multiply it by that matrix.
5) glPopMatrix to restore the settings

That should make the vector point forward from the front of the car. There are probably faster ways, though - I just wanted to show one way that should work.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Formula for converting angle to vector? komirad 2 14,485 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  Direction of normal Miglu 3 5,396 Oct 4, 2010 05:08 PM
Last Post: Skorche
  Question Regarding the Reflect Angle of a Transition iBaby 3 5,556 Apr 27, 2010 03:15 PM
Last Post: JustinFic
  Direction formula? TimMcD 2 7,496 Nov 11, 2009 11:42 PM
Last Post: TimMcD
  ending location from angle and speed Kazooless 5 6,962 Apr 3, 2009 02:40 PM
Last Post: Gillissie