Jump to content
  • Advertisement
Sign in to follow this  

2d Sprite Speed

This topic is 2822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In the platformer I am making i incorporated a run button to have the player sprite move faster when pressed. How should faster speed be calculated? Should it be drawing changes of the sprite's positions at the same intervals as before but just with more pixels in between each? I currently do this and have the sprite move 60 times each second. So I am guessing i can't speed him up by increasing the amount of times he moves each second instead? If I moved him 90 times in 1 sec wouldn't that cause visual problems with future additions I may make including adding vsync support?

Share this post

Link to post
Share on other sites
You might want to change how you calculate the speed of objects. Instead of using how often you update per second, you should use a typical unit of speed: distance/time. Typically, I you could use pixels/second. For large world with their own world coordinates, it should be your world unit/seconds.

This way you would use the delta time since the last update, and figure out where the objects move based o it's current velocity. For example, assuming using World Coordinates:

// This assume the ScreenEntity class has an X and Y velocity, as well as an X and Y location.
class ScreenEntity
double XVelocity;
double YVelocity;
double XLocation;
double YLocation;

Sprite *EntitySprite;

void ScreenEntity::UpdateAndDraw(double TimeSinceLastUpdate)
double ScreenX;
double ScreenY;

// Move this object based on it's current velocity
XLocation += XVelocity * TimeSinceLastUpdate;
YLocation += YVelocity * TimeSinceLastUpdate;

// Draw it at the current screen Coordinates, ScreenX and ScreenY must be passed by reference
// This function knows where the camera is in the world, and can give you screen Coordinates for this object
ConvertWorldCoordsToScreen(XLocation, YLocation, ScreenX, ScreenY);

Sprite->Draw(ScreenX, ScreenY);

You would call this function every loop, and pass in the time difference since the last call. This way, you only have to set the X and Y Velocity, and your object will move each time you call this. Give it a big positive X Velocity, and it will move faster to the right. Give it a small negative X, and it will move slow to the left.

Hope that helps!

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!