I have an object with which I would like to make follow a bezier curve and am a little lost right now as to how to make it do that based on time rather than the points that make up the curve.
.::Current System::.
Each object in my scene graph is made from position, rotation and scale vectors. These vectors are used to form their corresponding matrices: scale, rotation and translation. Which are then multiplied in that order to form the local transform matrix.
A world transform (Usually the identity matrix) is then multiplied against the local matrix transform.
class CObject
{
public:
// Local transform functions
Matrix4f GetLocalTransform() const;
void SetPosition(const Vector3f& pos);
void SetRotation(const Vector3f& rot);
void SetScale(const Vector3f& scale);
// Local transform
Matrix4f m_local;
Vector3f m_localPostion;
Vector3f m_localRotation; // rotation in degrees (xrot, yrot, zrot)
Vector3f m_localScale;
}
Matrix4f CObject::GetLocalTransform()
{
Matrix4f out(Matrix4f::IDENTITY);
Matrix4f scale(), rotation(), translation();
scale.SetScale(m_localScale);
rotation.SetRotationDegrees(m_localRotation);
translation.SetTranslation(m_localTranslation);
out = scale * rotation * translation;
}
The big question I have are
1) How do I orientate my object to face the tangent of the Bezier curve?
2) How do I move that object along the curve without just setting objects position to that of a point on the bezier cuve?
Heres an overview of the function thus far
void CNodeControllerPieceWise::AnimateNode(CObject* pSpatial, double deltaTime)
{
// Get object latest pos.
Vector3f posDelta = pSpatial->GetWorldTransform().GetTranslation();
// Get postion on curve
Vector3f pos = curve.GetPosition(m_t);
// Get tangent of curve
Vector3f tangent = curve.GetFirstDerivative(m_t);
}
Edit: sorry its not very clear. I’ve been working on this for ages and its making my brain turn to mush.
I want the object to be attached to the curve and face the direction of the curve.
As for movement, I want to object to follow the curve based on the time this way it creates smooth movement throughout the curve.
You should have a curve in parametric form and use derivative vector to evaluate the rotation of your object (rotation angle = derivative angle) as @etarion said.
To move the object on a curve with a desired velocity ( i think it is the think you want) each simulation step you shoud estimate the distance the point should move on this step.
The simplest estimation is
dist = derivative.length()*TIMER_STEP. When you know the dist should be traversed on the current step andt0– the current parameter of the curve you can simply increment t0 by some little valueepsilonand check the traversed distance is still smaller then estimated. Repeat this until the traversed distance (while increasing t0) is >= estimated. This will be the new current parameter t0 for the next stepEDIT:
Didn’t notice first you are in 3d. In 3d space you can’t unambiguously define the position of an object on a curve even if you know the initial position. Just imagine your curve is a line – the object still can rotate around the line. This angle is not defined by a curve.
I would do something like this. Let’s bind a vector to the object so that in the beginning of the movement (curve parameter t = 0 for example) the object vector direction coincide with the derivative vector. Then during the movement this vector should still coincide with the derivative in each point of a curve. So you will know this object vector and will be able to setup your object according to this vector. But you will still have one degree of freedom.
For example you can say that object does not rotate around this vector.
Knowing the object vector and the angle of rotation around it you can restore the object orientation in 3d world.
PS: such object vector and a rotation angle around it is called quaternion – so you can use quaternion math (simple copy the required formula) to calculate the object rotation matrix!
here are the formulas http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToMatrix/index.htm