[SOLVED] Help in Pong Physics

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

[SOLVED] Help in Pong Physics

Post by xx6heartless6xx »

I want to make a Pong game, but I have no idea how to make the gravity in the game that pulls the ball towards the floor and sends it flying in different directions when it is hit.

Can you guys point me to a good site or can you guys just start me off and I'll take it from there.
Last edited by xx6heartless6xx on Wed Feb 23, 2011 9:38 pm, edited 1 time in total.
User avatar
davidthefat
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 529
Joined: Mon Nov 10, 2008 3:51 pm
Current Project: Fully Autonomous Robot
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: California
Contact:

Re: Help in Pong Physics

Post by davidthefat »

xx6heartless6xx wrote:I want to make a Pong game, but I have no idea how to make the gravity in the game that pulls the ball towards the floor and sends it flying in different directions when it is hit.

Can you guys point me to a good site or can you guys just start me off and I'll take it from there.
http://en.wikipedia.org/wiki/Kinematics
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: Help in Pong Physics

Post by xx6heartless6xx »

davidthefat wrote:
xx6heartless6xx wrote:I want to make a Pong game, but I have no idea how to make the gravity in the game that pulls the ball towards the floor and sends it flying in different directions when it is hit.

Can you guys point me to a good site or can you guys just start me off and I'll take it from there.
http://en.wikipedia.org/wiki/Kinematics
How can I apply these equations into a program?
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Help in Pong Physics

Post by k1net1k »

there are loads of pong examples for all programming languages.
just google "Pong tutorial <my language>"
i mean seriously, have you even looked ?
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help in Pong Physics

Post by dandymcgee »

There's not really any gravity in pong. If it hits the top or bottom of the screen reverse the y velocity, if it hits a paddle reverse the x velocity.

Each frame update the position based on the current velocity:

Code: Select all

//Update position
ball.position.x = ball.position.x + ball.velocity.x;
ball.position.y = ball.position.y + ball.velocity.y;
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help in Pong Physics

Post by Ginto8 »

dandymcgee wrote:if it hits a paddle reverse the x velocity.
Actually, it also changes the y velocity. It goes bouncing off at an angle that's determined by where on the paddle it hits. What you might want to do is make the ball move along the vector from the center of the paddle to the center of the ball. It should be a fairly realistic representation, while still being very easy to make.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: Help in Pong Physics

Post by xx6heartless6xx »

Ginto8 wrote:
dandymcgee wrote:if it hits a paddle reverse the x velocity.
Actually, it also changes the y velocity. It goes bouncing off at an angle that's determined by where on the paddle it hits. What you might want to do is make the ball move along the vector from the center of the paddle to the center of the ball. It should be a fairly realistic representation, while still being very easy to make.
Making the ball go in a reverse direction makes some sense. How would I make the ball move along a vector? You say its still very easy to make but can you explain some more?
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Help in Pong Physics

Post by Ginto8 »

well, if you have the (x,y) coordinates of the ball's center, and the (x,y) coordinates of the paddle's center, then

Code: Select all

xvel = ball.x - paddle.x;
yvel = ball.y - paddle.y
if you want to increase the speed, follow this algorithm:

Code: Select all

float mag = sqrt(xvel*xvel+yvel*yvel); // get the vector's magnitude
// next two lines normalize the vector (give it a length of 1)
xvel /= mag;
yvel /= mag;
// Now, scale the vector to the "speed" you want
xvel *= speed;
yvel *= speed;
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
xx6heartless6xx
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 80
Joined: Wed Feb 02, 2011 9:42 pm

Re: Help in Pong Physics

Post by xx6heartless6xx »

Ginto8 wrote:well, if you have the (x,y) coordinates of the ball's center, and the (x,y) coordinates of the paddle's center, then

Code: Select all

xvel = ball.x - paddle.x;
yvel = ball.y - paddle.y
if you want to increase the speed, follow this algorithm:

Code: Select all

float mag = sqrt(xvel*xvel+yvel*yvel); // get the vector's magnitude
// next two lines normalize the vector (give it a length of 1)
xvel /= mag;
yvel /= mag;
// Now, scale the vector to the "speed" you want
xvel *= speed;
yvel *= speed;
Thanks man, I'll definitely try this out.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Help in Pong Physics

Post by dandymcgee »

Ginto8 wrote:
dandymcgee wrote:if it hits a paddle reverse the x velocity.
Actually, it also changes the y velocity. It goes bouncing off at an angle that's determined by where on the paddle it hits. What you might want to do is make the ball move along the vector from the center of the paddle to the center of the ball. It should be a fairly realistic representation, while still being very easy to make.
Oh right, pong doesn't use real physics. :P
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply