Search code examples
c++mathopenglanimationcurve

Curve Trajectory object orientation


I have a spaceship in a 3D environnement. I drew a B-Spline curve using control points (converted to a Bezier curve to be able to draw it). Each time the draw event is called, the ship is at the point (0,0,0) and is translated to the next coordinate on the curve. Now, before doing this translation, I would need to make a rotation so that the ship's orientation is along the curve's tangent at that point.

I'm able to approximate the tangent by finding a point on the curve that is near the current one by doing this. I use OpenGL in C++

float mDifference[1][3] = {nearPoint[0][0] - currentPosition[0][0],
                           nearPoint[0][1] - currentPosition[0][1],
                           nearPoint[0][2] - currentPosition[0][2]};

float norm = sqrt(mDifference[0][0] * mDifference[0][0] 
             + mDifference[0][1] * mDifference[0][1]
             + mDifference[0][2] * mDifference[0][2]);

float tangent[1][3] = { mDifference[0][0] / norm,
                        mDifference[0][1] / norm,
                        mDifference[0][2] / norm};

//tangent = rotationVector?
spacecraftTransformGroup->setRotationVector(tangent[0][0],tangent[0][1],tangent[0][2]);

I think the rotation vector is the tangent, but can't find the angle needed to rotate the ship. How can I rotate the ship to align it with the tangent?


Solution

  • Well an orthonormal (ie orientation) matrix is made up of the following:

    Direction, Tangent, BiTangent (Up vector).

    So if you have your direction and tangent you can cross product to get your bitangent and form your matrix from the 3 direction vectors (D, T, B) and position vector (P) as follows

     Tx, Bx, Dx, Px
     Ty, By, Dy, Py
     Tz, Bz, Dz, Pz
      0,  0,  0,  1
    

    Your object is now oriented along the direction vector ...