# how to give a Direction

## Recommended Posts

i am working in 3d space , what i am really doing is that i am moving a car along a cubic bezier curve acoording to a scalar which varies between 0 and 1 but my car is not taking the correct direction of the path so how can i solve this problem and give it the correct direction ?? i tried to calculate the angle of the direction using the DOT product and make rotation around the vertical axis but it is not working because i always get a positive angle and i cannot know to which side to rotate the car to LEFT or to RIGHT

##### Share on other sites
haegarr    7372
Evaluating the spline with the value [0,1] yields in a position only. What you need is to compute also the tangent vector of the spline at that position. Those tangent is then to be used as "line of sight". Together with the up vector you then could compute a rotation.

##### Share on other sites
it is hard to determine the tangent to the sepline at a specific point isn't ?? but suppose i have the approximate direction what do u mean by
[qoute]
Together with the up vector you then could compute a rotation.
[/qoute]

if u mean to use the dot product to calculate the angle , it will not work because what i said about if i should rotate left or right

##### Share on other sites
haegarr    7372
Nope, I don't mean the angle, at least not explicitely.

Assuming you are able to use a matrix as rotation as is often the case (e.g. it is w/ the std gfx APIs). Then a rotation is nothing else as the basis of a rotated vector space. E.g. if you do a heading rotation by 'a' radians, then the rotation matrix in homogeneous co-ordinates would be (as an example)
[ cos a  0  sin a  0 ][   0    1    0    0 ][-sin a  0  cos a  0 ][   0    0    0    1 ]

if using column vectors.

One could interpret this matrix as follows: The second column is the up vector, the third column the "look at" vector, and the first is the "right" vector, resulting from a cross product "look at cross up". (I use this definition since my camera looks along the z axis; you could similarly use the x axis as "look at", if you wish).

Another possibility is to use the standard z axis (as unrotated "look at") and the approx actual "look at" (as rotated "look at") to compute a pivot/angle or else quaternion rotation from. From that also a rotation matrix could be computed, or the pivot/angle pair could be directly use with OpenGL's glRotate routine or D3D's D3DXMatrixRotationAxis.

It depends on what way is appropriate for you to represent rotations.

EDIT: It may be better to use either pivot/angle or quaternion than to compute the rotation matrix directly. In the latter case one has to guarantee orthogonality, e.g. due to "backward" computing of the up vector after having computed the vector to the right and some normalization (but would work, too).

[Edited by - haegarr on November 23, 2005 3:33:10 AM]

##### Share on other sites
haegarr    7372
Appendix

Quote:
 Original post by jad_salloumif u mean to use the dot product to calculate the angle , it will not work because what i said about if i should rotate left or right

The angle is not the only thing you could compute from two vectors. The cosine is symmetric to the axis, and so is not sufficient to yield in the sign, right you are. It could also be seen from the fact that it makes no difference whether you are computing v1 dot v2 or else v2 dot v1.

However, you could also compute the cross product to yield in the pivot vector of rotation. The cross product has the sine inside, and hence the sign of the angle plays a role: sign reversal forces the pivot to point into the opposite direction.

This stuff is in fact how to compute the pivot/angle rotation representaton that I've already mentioned in my post above. However, I wanted to make it clear.