简体   繁体   中英

Sprite Speed on different Screen Sizes

I am trying to understand the concept of speed in a game which supports different screen sizes (in android).

Let's say I have a Sprite that should take 10 seconds from the right end in landscape mode to the left end. The Frame Rate is not steady so sometimes it's 30 for a while and then all of a sudden it changes to 10. But I still want my Sprite to take 10 seconds from one end to the other. How would I do that?

final int time = 10; //time from one end to the otehr
float baseSpeedX = screenWidth / time;
float currentFrameRate = 30.0; //just as an example
float baseFrameRate    = 50.0; //the frame rate we want
float factor = baseFrameRate / currentFrameRate;
playerSpeedX += baseSpeedX * factor;

This is what I understood:
You get the base speed by dividing the screen width by the time you want to get the sprite from one end to the other. The base speed is the speed your Sprite should add to your current location every second if a steady framerate is available. However, I don't know how to achieve that so I just get the current frame rate and divide the base frame rate , the frame rate we really want for our game, to get the factor we multiply our base speed with.

This would, if it really works, make the Sprite reach the other end in 10 seconds, right? Even if we have a frame rate of 10 for 2 seconds and then 50 after that for 8 seconds. The higher the frame rate now, the lower the Sprite increases in distance and the lower the frame rate, the higher the Sprite increases in distance.

If this all works, would it be something I could use in a game? Or is it totally wrong approach for getting the sprite speed?

You are on the right track

Generally when you are moving sprites, you need to make a way so that the sprite moves slower when there is a higher fps, and move quicker when there is a lower fps. This can be done by multiplying another variable called delta to your baseSpeed. Delta in this case is the time it takes from the current frame and the previous frame. With this, games with high fps will have a low delta because it takes a short amount of time to go from one frame to another. Following this pattern, a low fps game will see a high amount of delta. By calculating the delta, you can have uniformity that your sprite will be the same speed at different fps targets. You can usually calculate delta by subtracting the current time and the last rendered time.

In theory, if you use your baseSpeedX formula (from first glance, it seems sound) and multiply it with the delta, you will have your speed:

double delta = currentTime - timeOfLastFrame; // I don't know specifically what time libraries there are on your platform
double speed = baseSpeedX * delta;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM