[SOLVED] Help in Pong Physics
Moderator: Coders of Rage
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
[SOLVED] Help in Pong Physics
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.
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.
- davidthefat
- 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
http://en.wikipedia.org/wiki/Kinematicsxx6heartless6xx 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.
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: Help in Pong Physics
How can I apply these equations into a program?davidthefat wrote:http://en.wikipedia.org/wiki/Kinematicsxx6heartless6xx 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.
Re: Help in Pong Physics
there are loads of pong examples for all programming languages.
just google "Pong tutorial <my language>"
i mean seriously, have you even looked ?
just google "Pong tutorial <my language>"
i mean seriously, have you even looked ?
- dandymcgee
- 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
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:
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!
- Ginto8
- 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
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.dandymcgee wrote:if it hits a paddle reverse the x velocity.
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.
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: Help in Pong Physics
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?Ginto8 wrote: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.dandymcgee wrote:if it hits a paddle reverse the x velocity.
- Ginto8
- 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
well, if you have the (x,y) coordinates of the ball's center, and the (x,y) coordinates of the paddle's center, then
if you want to increase the speed, follow this algorithm:
Code: Select all
xvel = ball.x - paddle.x;
yvel = ball.y - paddle.y
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.
- xx6heartless6xx
- Chaos Rift Cool Newbie
- Posts: 80
- Joined: Wed Feb 02, 2011 9:42 pm
Re: Help in Pong Physics
Thanks man, I'll definitely try this out.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, thenif you want to increase the speed, follow this algorithm:Code: Select all
xvel = ball.x - paddle.x; yvel = ball.y - paddle.y
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;
- dandymcgee
- 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
Oh right, pong doesn't use real physics. :PGinto8 wrote: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.dandymcgee wrote:if it hits a paddle reverse the x velocity.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!