Hey guys, so I’m trying to implement something simple where an object will jump around between a N number of control points (i use 8) and at the same time trying to learn exactly how this interpolation works. Below is code for what I have generating the vector that i’m constantly translating to in update
Vector3 ComputePointOnCatmull(double u, int segNumber)
{
Vector3 travelpoint = new Vector3();
Vector3 temp1 = new Vector3();
Vector3 temp2 = new Vector3();
Vector3 temp3 = new Vector3();
Vector3 temp4 = new Vector3();
//Debug.Log(u);
//Debug.Log (segNumber);
int p0=0;
int p1=0;
int p2=0;
int p3=0;
if (segNumber == 0)
{
p0=7;
p1=0;
p2=1;
p3=2;
}
else if (segNumber ==7)
{
p0=6;
p1=7;
p2=0;
p3=1;
}
else if (segNumber ==6)
{
p0=5;
p1=6;
p2=7;
p3=0;
}
else
{
p0=segNumber-1;
p1=segNumber;
p2=segNumber+1;
p3=segNumber+2;
}
temp1 = 2f*controlPoints[p1];
temp2 = (controlPoints[p2]-controlPoints[p0])*(float)u;
temp3 = ((2f*controlPoints[p0])-(5f*controlPoints[1])+(4f*controlPoints[p2])-controlPoints[p3])*(float)(u*u);
temp1 = ((3f*controlPoints[p1])-(controlPoints[p0])-(3f*controlPoints[p2])+(controlPoints[p3]))*(float)(u*u*u);
travelpoint = 0.5f * (temp1+temp2+temp3+temp4);
return point;
}
u is a constantly being updated by a counter of .01 and goes from 0.0 to 1.0, segNumber goes from 0.0 to 7.9, controlpoints are the vector locations of where i’m traveling to and interpolating between.
the temps just break up the math behind the equation for a catmull rom (i believe, trying to get it from this site Introduction to Catmull-Rom Splines)
basicaly when this is run, every segment starts at 0 and flys randomly off on some curve and when it reaches the next segment it starts back at 0,0,0 which is not what i want. it doesn’t appear to hit any points, and it shouldn’t be starting at 0,0,0 everytime but i think thats because when u = 0 it resets the travelpoint vector to 0, but when i tried to force when u = 0 change u to 0.01 it still didn’t work. I feel like theres some fundamental piece missing as i’ve checked the equation and i think it’s right.
Let me know if anyone has any ideas.