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 CNodeCon开发者_开发技巧trollerPieceWise::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 and t0
- the current parameter of the curve you can simply increment t0 by some little value epsilon
and 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 step
EDIT:
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
You need a parametric formulation of your curve.
The big question I have are
1) How do I orientate my object to face the tangent of the Bezier curve?
If you have the curve in parametric form, the tangential direction is the derivative of the position wrt. t.
2) How do I move that object along the curve without just setting objects position to that of a point on the bezier cuve?
I'm not sure I get your question - you'd increase t in your parametric form in a small step and update position and direction. You still have a degree of freedom there, btw - the "up" direction is not determined from the curve, so you'd need to take care of that too.
I'm assuming you have all points you need to define the bezier curve. Then you can compute every point on that curve. Calculate a suitable point taking into account the speed that the object should travel at, and the frame timing and you should have consistent movement.
The vector formed by the points from last and current frame can be used as a rough estimate of the tangent in most cases; e.g. when the curve does not bend too sharp.
Edit: also have a look here on how to calculate the length of a bezier curve. You will need to transform that, so you can calculate a point on your curve (or rather the t) for a given length. Then just move evenly distances in relation to time and you should be fine.
One approach is to calculate a pyramid of points. The bottom layer is your transformed control points. Now for a given t, and for each each pair of adjacent points, create a new point that is on this line segment weighted by t. This new set of points forms the next layer in the pyramid. Repeat until the current layer has only 1 point. This point is your position. Note that the second layer from the top has 2 points, and determines the tangent line.
精彩评论