# 2d Sprite Speed

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

## 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 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 { ... private: 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!

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×