Sign in to follow this  
MatthewDiaz

2d Sprite Speed

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:

[code]
// 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);
[/code]

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this