# Rotating a body in the direction of its trajectory

Hi,

I am writing a rocket simulator and I am moving the rocket along a trajectory.
I would like the rocket to point in the direction of the tangent to the trajectory.

The trajectory happens to be such that Y(t) is a parabola and X(t), Z(t) are linear, but I would like this to work for any trajectory in 3D space.

I tried using the glRotate function with angles I calculated from the slopes of x(t), y(t), z(t).
For instance, the slope of Y(X) can be calculated using the arctan of dy/dt divided by dx/dt.
This worked for some trajectories but not for all.

I have seen a few solutions that seem to involve a lot of calculations, but intuitively I assume that there must be a simple solution I am missing.

For instance, as I know where the rocket is at T and at T + dT, can I not just rotate the rocket so that it points from:
{x(t), y(t), z(t)} to {x(t+dt), y(t+dt), z(t+dt)} ?

Thanks for any tips on how to do this

David

Assume rocket is modeled with X axis through the nose, and you want Z to be in the orbit plane as the rocket propagates.
So with no rotations, the rocket lies in the horizontal plane pointed eastward.
Code something similar to below works for me.
If the rocket isn’t modeled as above, put in some rotations just above the Draw_Rocket call to orient it properly.
This is all there is to it. Note that rockets usually deviate from this attitude a bit (angle of attack).

``````
dx = x(t+dt) - x(t);
dy = y(t+dt) - y(t);
dz = z(t+dt) - z(t);

dq = sqrt (dx*dx + dy*dy);

az = atan (dy/dx);
el = atan (dz/dq);

glPushMatrix();
glRotate (az, 0,0,1);   // Azimuth
glRotate (el, 0,1,0);   // Flight path angle.
Draw_Rocket ();
glPopMatrix();

``````