## Calculating direction of car from normal vector and angle

Apprentice
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 http://www.euclideanspace.com/maths/alge.../index.htm

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.
Code:
```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)...

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!
Member
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.