I am attempting to construct a roller coaster simulator. Each track piece is defined by a right handed pair of vectors. A direction vector, and up vector and a normal vector. I can use the direction vector ok, but how do I calculate the up vector from the directional vector?
At the moment is fixed at (0, 1, 0) however I need to calculate it according to the directional vector so it is always at a right angle to the directional vector. Reason for this is for lighting and also so the first person cam can take the up vector for each track piece.
To keep the Up vector perpendicular to the Front vector use the following. This should work so long as the front vector has not rotated so much that it is parallel to the CurrentUp vector.
Right = Front x CurrentUp
NewUp = Right x Front
Rotating it around the Front vector by an angle A is given by the following. |CurrentUp| is the length of the CurrentUp vector. However, if you use this you will have to normalise / correct the length of the NewUp vector after a couple thousand rotations. The calculations give rounding errors after a while.
Here’s my first suggestion: Rename your vectors! Use direction, left, and up (or normal). A “normal” vector is a specific vector which is not the same as your “normal” vector. You will save yourself (and everybody else) a lot of confusion.
If you have two vectors, you can get the third using the cross-product.