This is my first post at this section (XNA and game development). I'm trying to get something the below image. As you can see, there's a highway 开发者_高级运维and inside of it, there'll be some objects moving (millisecond). I guess the streeth behavier is like a pipeline. When the highway loads an object, it appears at the beggining and it'll be moving through the highware until it arrives in the another extreme of the highway.
My main problem is, how can I do to move several objects only inside of the highway?
Thanks in advance.
You need a list of points and a list of sprites
class Path
{
List<Vector2> Points;
float[] Lengths;
Vector2[] Directions;
void Build()
{
Lengths = new float[Points.Count-1];
Directions = new float[Points.Count-1];
for (int i=0; i<Points.Count-1;i++)
{
Directions[i] = Points[i+1] - Points[i];
Lengths[i] = Directions[i].Length();
Directions[i].Normalize();
}
}
}
class Sprite
{
Vector2 Position;
float StagePos;
int StageIndex;
Path Path;
float Speed;
void Update(float Seconds)
{
if (StageIndex!=Path.Points.Count-1)
{
StagePos += Speed * Seconds;
while (StagePos>Path.Lengths[StageIndex])
{
StagePos -= Path.Lengths[StageIndex];
StageIndex++;
if (StageIndex == Path.Points.Count-1)
{
Position = Path.Points[StageIndex];
return;
}
}
Position = Path.Points[StageIndex] + Directions[StageIndex] * StagePos;
}
}
}
精彩评论