I'm facing an issue regarding the tangent when moving along my cubic bezier curve within 3D space where (x,z) are used rather than (x,y). An image of the curve has been added.
The issue occurs when the entity hits the red line indicated on the image. The tangent begins progressively from 0.0 to 1.0 but then hits the red line and starts to progressively decrease from 1.0 to 0.0, causing the entity's direction to flip.
Currently this snipped is responsible for the tangent:
const Vector3 Class::getTangent(const float s) const
{
const float t { 1 - s };
return (3 * t * t * (points.at(1) - points.at(0)) +
6 * s * t * (points.at(2) - points.at(1)) +
3 * s * s * (points.at(3) - points.at(2)));
}
I then use this function for:
Vector3 firstDerivative = curve.getTangent(segment);
firstDerivative.normalise();
Vector3 normal = firstDerivative.crossProduct(Vector3(0, 0, 1)); // Z = forward
normal.normalise();
Vector3 b = firstDerivative.crossProduct(normal);
b.normalise();
Then I simple use b
to set the entities direction along the curve. I did create a simple/dirty fix by switching the sign of b
to -b
when the point which flips the entity is reached. Now despite this "fixing" the issue I believe if the curve changes the fix would cause problems later.
So if anyone can spot where I may have gone wrong or what I may need to do to fix this, any help would be great and appreciated. Please could you also provide examples of your solution with the given problem, either via pseudo or c++ code.