I had a question about what the best way is to move a sprite from point A to point B over a period of time. I have a method to do so as part of my sprite class, but I noticed it can sometimes be a bit jerky/rough looking when the translation is over a length of more then a few pixels. I was wondering if anyone could give me some suggestions on how to do it better. Basically, here is what I'm doing right now (it's in C# but the logic should be pretty straightforward):
My Sprite class has a method called SlideSprite that looks like this.
public void SlideSprite(Vector2 targetPosition, TickCount elapsedTime, TickCount currentTime)
{
this.targetPosition = targetPosition;
this.targettime = currentTime + elapsedTime;
_lastTranslationUpdate = currentTime;
if (elapsedTime > 0)
{
translationVector = Vector2.Subtract(targetPosition, this.Position);
translationVector = new Vector2(translationVector.X / elapsedTime, translationVector.Y / elapsedTime);
}
}
TickCount is just a measure of game time in milliseconds. targetPosition is the co-ordinates to move the sprite too, elapsed is how many milliseconds the sprite has to move to its target position, and currentTime is the current game time (again in ms).
The sprite implements another method called Update that is called whenever a drawing loop begins, but before any drawing occurs. The update method basically looks like this:
public virtual void Update(TickCount currentTime)
{
if (translationVector != Vector2.Zero)
{
TickCount elapsed = currentTime - _lastTranslationUpdate;
if (currentTime < targettime)
{
this.Position = Vector2.Add(this.Position, Vector2.Multiply(translationVector, elapsed));
}
else
{
this.Position = targetPosition;
translationVector = Vector2.Zero;
if (spritesettled != null)
spritesettled.Invoke();
}
_lastTranslationUpdate = currentTime;
//if (this.Position.X
}
if (animType != AnimType.None && isPlaying)
{
UpdateFrameIndex(currentTime);
}
}
Any tips on how to make this faster/smoother? I'm currently running my game at 60 FPS. I was thinking about splitting the Update component of the game into a seperate thread, and jsut making sure i add locks anywhere necessary.