Im currently working on a project that involves generating a random track (fixed increments on the Z axis and randomized X and Y to create a bumpy/bendy track) i have the track implemented using Catmull Rom interpolation which works as expected. This generates 9800 points which are in turn stored in a 2d array.
My main issue is that i am now attempting to path an object along the track (for camera usage and later an avatar to follow). I am currently using the Curve3D class as per msdn help http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.curve.aspx i have a function which assigns the position of the "testCube" based off the values time and the array with the catmull results in.
Now to the problem ; my cube paths down the track in a broad sense however as the track progresses it appears to jump backwards and forwards with increasing frequency. I have eliminated any other code as a possibility for interfering with it.
i am currently using:
public void UpdateMotionCube(GameTime gameTime1)
{
testCube.position = motionCubePosition.GetPointOnCurve((float)motionTime);
motionTime += gameTime1.ElapsedGameTime.TotalMilliseconds;
}
public void InitiateCurve()
{
float time = 0;
//for (int row = 0; row < 99; row++)
for (int col= 0; col < 99; col++)
{
//assigns positions to the new curve from drawLocValues
motionCubePosition.AddPoint(newTrack.basicPoints/*.drawlocValues*/[/*row,*/ col], time);
time += timeConst;
}
motionCubePosition.setTangents();
}
the basicPoints array holds the original 100 points i used for catmullRom interpolation and the drawLocValues are the results from the interpolation (100 and 10000 values respectively)
Any advice as to why the testCube isn't pathing straight down the track as opposed to heading in the correct direction but bouncing back and forwards would be greatly appreciated. As an aside the values for both of the vector arrays are correct as i have checked them (and the track itself draws correctly) i am also using CurveX.postloop.CurveLoopType.Linear inside the curve class for all xyz post and pre loop.
Thanks
EDIT: Set Tangents Code as requested:
public void setTangents()
{
CurveKey prev;
CurveKey current;
CurveKey next;
int prevIndex = 0;
int nextIndex = 0;
for (int i = 0; i < curveX.Keys.Count; i++)
{
prevIndex = i - 1;
if (prevIndex < 0)
{
prevIndex = i;
nextIndex = i + 1;
}
if (nextIndex == curveX.Keys.Count) nextIndex = i;
prev = curveX.Keys[prevIndex];
next = curveX.Keys[nextIndex];
current = curveX.Keys[i];
SetCurveKeyTangent(ref prev, ref current, ref next);
curveX.Keys[i] = current;
prev = curveY.Keys[prevIndex];
next = curveY.Keys[nextIndex];
current = curveY.Keys[i];
SetCurveKeyTangent(ref prev, ref current, ref next);
curveY.Keys[i] = current;
prev = curveZ.Keys[prevIndex];
next = curveZ.Keys[nextIndex];
current = curveZ.Keys[i];
SetCurveKeyTangent(ref prev, ref current, ref next);
curveZ.Key开发者_运维百科s[i] = current;
}
}
To answer your question , yes it is essentially a wrapper. The issue seems to lie in the testCube.position.Z having printed it out on screen it starts okay but as the time progresses it begins to double back on itself in ever increasing values , it maintains forward momentum generally speaking but for want of a better term it stutters forwards then backwards etc.
Thank you for the replies thus far
I'm guessing gameTime1.ElapsedGameTime.TotalMilliseconds is the elapsed time since UpdateMotionCube was last called?
- Surely
testCube.position =
should happen after you've updated the motion time? - Check your input: Print out your list of points and make sure they actually do progress.
- Check your input: Does the game time increment in a reasonable fasion?
- Print out the gametime, motion time and resulting points you retrieve to ensure that evrything's happening properly together.
精彩评论