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)...

[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
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Smooth acceleration in every direction. Honey Sharma 1 4,868 Aug 16, 2011 01:37 AM
Last Post: PoseMotion
  Formula for converting angle to vector? komirad 2 11,402 Jul 29, 2011 07:29 AM
Last Post: ThemsAllTook
  newbie request direction Teehee 3 6,476 Jul 27, 2011 09:57 AM
Last Post: AndyKorth
  give direction to sprite termitis 9 10,819 Mar 11, 2011 02:38 PM
Last Post: compiler
  Direction of normal Miglu 3 4,022 Oct 4, 2010 05:08 PM
Last Post: Skorche