Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Best Way To Translate (move) A Sprite?  (Read 2176 times)

0 Members and 1 Guest are viewing this topic.

Megatron

  • Newbie
  • *
  • Posts: 22
    • View Profile
Best Way To Translate (move) A Sprite?
« on: November 21, 2010, 08:54:29 am »
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.

Code: [Select]
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:

Code: [Select]
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.

Zamadatix

  • Newbie
  • *
  • Posts: 27
    • View Profile
Best Way To Translate (move) A Sprite?
« Reply #1 on: November 21, 2010, 04:22:04 pm »
So you need to move a sprite between 2 points on a euclidian plane, simply and fast, and the person helping you hates working with vectors ;).

First we need to find the total length it will travel (x2-x1) on each axis (y2-y1). then we need to find out what percentage of time has passed (currentTime/totalTime) and then multiply the two to find how far it is on its path on each axis then add it to the starting points.

Use floats for all variables.

Sprite.SetPosition(x1+(x2-x1)*(currentTime/totalTime),y1+(y2-y1)*(currentTime/totalTime));

you can get total time from Refreshrate*seconds and current time be ticking up once per frame

 

anything