Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pong concepts.

Started by zergdeath1 Nov 20, 2004 at 10:20 AM 4 replies 800+ views
Original Post
zergdeath1
zergdeath1
Maybe I am not ready to make pong but I do not understand the concepts/physics of the ball. Basically how do you get the compiler to know whena ball hit here have it move this fast in this direction. My only Idea was to have 4 functions, leftplus,leftminus,rightplus,rightminus. standing for the differant quadrants on a plane, then allways have it move in the same direction in those quadrants. But after thinking about it that wouln't work either unless I used about a million switch statement. So is there just some formula I dont know, im in 10 grade and was forced into chemistry not physics.
Rob Loach
Rob Loach
Check out mine. Very simple and horrendous game code, but it works.

I would recommend one of two options:
1) Use angles and basic trig to figure out where the ball is going from the angle and speed.
2) Use speedX and speedY.
Rob Loach [Website] [Projects] [
GameDev135
GameDev135
My pong, was designed this way (very sloppily im sure as pong was my first game as well)

So keep track of a point (x,y) = its position.
Then also keep track of another point velocity(x,y) = its velocity. Unlike speed, velocity can be negative.

Every frame/loop call a UpdatePosition() function which says point = point + velocity. (That is pseudocode as depending on your language, you may or may not be able to just add 2 points to each other in this form---that is you may have to add the x and y coordinates separately.)

Then in terms of bouncing off walls....I made mine very simple. I would just say if it hits a wall, switch the sign of the velocity. So if it hits a left or right wall, switch velocity.x, if it hits a top or bottom wall, switch the sign of velocity.y.

Actually getting it to know when it will hit the wall might be a little more difficult. You want it to display actually hitting the wall. My first thought would be for 1 frame to set the velocity of the ball equal to the distance between the ball and the wall (when it is close enough to the wall it will pass it) But this won't quite work, because then you are making the velocity a smaller number, and when you switch the sign, it will still be messed up.

So maybe do something like have your UpdatePosition() function do something a little different. How about actually detect the collision in there? (weird way of coding, maybe someone has a better idea)--So in there say,
if (ball_coordinates + ball_velocity pass a wall) ---> then make the ball_coorinates equal to a wall coordinate....otherwise add the velocity to the position

The other thing you can do. In real pong, the ball's speed increases whenever it hits a wall. So you can also increase the magnitude of the velocity after changing its sign. How about say something like -- velocity.x = velocity.x *1.1 and velocity.y = velocity.y * 1.1 (i think in real pong both velocities change). Note that to use the 1.1 you need to have the velocity and coordinates stored as a double---so it might make more sense to just figure out what the correct initial velocity for the ball is and just add a good amount each time instead of *1.1 (note that then you do need a switch statement for whichever wall it hits--but its not a very big one)

---Dan
Feel free to email me at NYYanks432@hotmail.com if you have any questions
zergdeath1
zergdeath1
Thanks that makes sense, I am only in Alg2/Trig honors so we are not to the trig stuff yet, so the other explaination kinda confuses me but i get the velocity stuff.
mxrss
mxrss
You could do that or you could just invese the values of x and y speed bassed on collision. For example if the ball is goin +x then when it hits the paddle inverse the x and y so it would be going -x. Pong really does not require trigometry functions and can be made by just using inversion. Alot of games do it and it provides for a clean effective effect. Once you have this you can then move onto the trig.

- Mike
Drakkcon
Drakkcon
Yeah, do what they said. Forget trig and use XVelocity and YVelocity. I just learned basic trig in my 9th grade geometry class, so I was trying to apply it to pong. Well, it wasn't fun, especially because the c standard library uses radians. Just use Cartesian coords.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.