Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

2D All the way! / Adding acceleration to a ball on sprite collision.

Years of Service
User Offline
Joined: 11th May 2011
Posted: 15th Jun 2011 20:06
So I am coding a simple pong game, and the last thing that i cant figure out is how to add acceleration to the ball as it hits the paddle.

for example if ball hits paddle ball movement speed should be speed in the begining + 1, if its hits second time speed should be speed + 2, and so on.

Any thoughts?
Grog Grueslayer
Valued Member
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 16th Jun 2011 04:56
I don't use DarkGDK but where your collision code for the paddle is where you add 1 to speed. But if that's the code for moving the ball you keep resetting the speed to 5 so any increases in speed somewhere else are nullified. Move the initial speed setting out of there.

Years of Service
User Offline
Joined: 10th Jun 2011
Posted: 17th Jun 2011 15:57
What above said ^

From the code I'm assuming ball is a class? If so, make speed a private variable, and set its initial value (5 in your example) in your constructor for the class.

Next, if move() is being called by a function inside the class, then you should be able to modify speed in that function, right before you call move().

If move() is being called by an object, say inside main(), then you need to create methods in your function to adjust the speed.

The second example does not require modification to your move() and its a bit more professional. Just remove the declaration of speed from the function, and make it a class variable. Similarly if needed, you can have a LowerSpeed() method. Hope this helps

Login to post a reply

Server time is: 2024-02-23 22:30:06
Your offset time is: 2024-02-23 22:30:06