How to move a ball continuously in 2D scrolling game ?

Hello people,

I have just started with learning how to make basic games in c++.

The situation is , I have a ball which should move on a flat horizontal surface which i have created in a display of 800 x 400 . Right now!I am able to make it move left or right in direction using key inputs at a fixed speed.

But I want to make it move horizontally at a constant velocity without using keys. Like in copter game.

It should be able to move automatically , towards the right side.

Increment x position by speed variable does the trick but When I used it with keys I used to limit the movement like.

ball.x += ball.speed;
if(ball.x > 750)
ball.x = 750; // because my screen width is 800.



http://www.dailymotion.com/video/xkq4i6_vid-20110826-093152_videogames#! < this video will show what I really want to do with the ball

If you saw the game video in the link i gave, the ball kept moving with constant velocity. But it never went out of bound. What is keeping it from not getting out of display screen. Instead! its moving continuously... but stays almost at the center of screen. How?

Would be good to get inputs from C++ experts here. What should I do?
Last edited on
If you want to move it just set a constant speed variable and continue to add it to the x position. (You should be doing almost the same thing with input anyway....so just remove the input statements..?)

I would also look up frame independent movement so that wont cause problems for you later. Here is one example https://www.scirra.com/tutorials/67/delta-time-and-framerate-independence

Other than that I am not sure what you need advice on so maybe explaining slightly more might help.
Yeah! increment x position by speed variable does the trick but When I used it with keys I used to limit the movement like.

ball.x += ball.speed;
if(ball.x > 750)
ball.x = 750; // because my screen width is 800.


If you saw the game video in the link i gave, the ball kept moving with constant velocity. But it never went out of bound. What is keeping it from not getting out of display screen. Instead! its moving continuously... but stays almost at the center of screen. How?
Last edited on
closed account (j3Rz8vqX)
I am able to make it move in left and right directions using key inputs at a fixed speed.
But now i want to make it move horizontally at a constant velocity without using keys. Like in copter game.
It should be able to move automatically, towards the right side.


Are you referring to simultaneously movement during other processing (if the user doesn't input anything, it would still move) or are you trying to design a momentum (to force the ball to move at consistent speed)?

Your question may be similar to this post:
http://www.cplusplus.com/forum/beginner/120856/
Last edited on
Yes! its the simultaneous movement that i want.

But while simultaneous movement I dunno how to keep it at the center of the screen. Because in normal scenario if its moving on its own, it will reach the right bound of my screen and then it will go out of bound i.e out of my screen.

Have a look at video:

This is how i want my ball to move.

http://www.dailymotion.com/video/xkq4i6_vid-20110826-093152_videogames#!
Oh I see what you want (your topic title is kind of misleading..it's not the velocity you are concerned about).

Normally this is handled by setting up a camera object that follows your players position during the level. You then offset every graphic on the screen (including the player) by that camera so that it all shows up as you move right and your player continues to stay in the middle.

It isn't the easiest thing to explain in a few words but this is how I first started learning about it. http://lazyfoo.net/SDL_tutorials/lesson21/
If you search 2D Scrolling Camera or anything similar in google you will find many more tutorials on how to do it as well.

Hopefully that helps.
Last edited on
Thanx James, You cleared the fog over this scene. I will dig deeper into the topic of 2D scrolling Camera.

You just decremented my problem counter . :)

Problems - - ;


ball.pos.x = background.center.x;

Why did I use background.center ? Notice that when the ball collides with an object, the background moves backward as well, so for the ball to stay in the center of the screen, you simply set it's x value to be the same as the background.center x value and this should achieve what you want.

I'm assuming the background is a sprite sheet. So when the "speed" of the ball will depend on how fast you want the sprites on the sprite sheet to be drawn.
Topic archived. No new replies allowed.