Question

I've created a simple Pong game and I move the ball by adding a variable ballSpeed to its position in my update game logic like:

void Update()
{
    ballPosition += ballSpeed;
}

The problem is that the ballSpeed is a D3DXVECTOR3 with componets: x = 10; y = 10; z = 0;

The thing is that when I increase the window's resolution from 640:480 to 1280:1024 the ball is moving slower because it takes more pixels to go.

And my question is: How can I make the ball move with the same speed regardless of the resolution of the monitor?

Was it helpful?

Solution 2

You can scale the speed by the ratio of the actual resolution to the development resolution. Since the gameplay in Pong is dominated by the horizontal motion, you can use the ratio of the widths:

ballSpeed = int(ballSpeed * width / 640.0);

You will probably want to do the same to the size of all the screen elements too, such as the ball.

This all assumes that you define "speed" as the time taken to cross the entire screen, not to cover a given number of inches. That calculation would depend on more than just the resolution.

OTHER TIPS

Perhaps you could introduce a new coordinate system; say from (0,0) to (1,1). Move the ball in this coordinate system, and then map positions in these coordinates to pixels in the screen to draw the ball. You'll want your coordinate system to have the same aspect ratio as the window.

You should first introduce a transform between screen coordinates and logical coordinates, and update the ball position not by a constant, but take in account the time since the last update, in order to properly simulate the speed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top