Jump to content
• Advertisement

Archived

This topic is now archived and is closed to further replies.

Help with pong

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey I''m currently making pong, and have only made the ball to move so far. It works exactly as I want it to, but I feel there is a much better way of getting the direction of the ball. So far I have an enum: enum BALL_DIRECTION { WEST,EAST,NORTH_EAST and so on.... and an int: int ball_direction; and to move the ball I am doing this: switch (ball_direction) { case WEST: x--; break; case NORTH_EAST: x++; y--; break; } and so on......... Could anyone give me advice on doing this a better way, I''ve heard other people using a ball velocity but i dont understand how to use it. (like how to calculate it). Please, any help would be appreciated!

Share this post

Share on other sites
Advertisement
You could store the north-south and the east-west components of the ball in two variables, usually x and y. x in this case represents the horizontal movement. So, a positive x would be, uh... east (I always get east and west mixed up). Same thing with the y component. This is way more efficient than just using eight different possible directions, I think. To move the ball north-east, you would combine x=1 and y=-1 (=up). You could also move the ball a little faster by setting x=2, or something like that. Hope you get the point

A slightly different way would be to store the direction of the movement and then the speed (scalar portion of the vector), but that would make the collision in a Pong-like game unnecessarily complicated...

For simple collision detection in your game, you'd simply have to invert the x component of the velocity when the ball hits the left or right border of the screen, or invert the y component when it hits the bottom or the top.

Also, when you're movement code is somewhat working, I'd consider using time-based movement, because the way you do your movement right now, the ball will move faster on faster machines.

[edited by - Wutz2003 on January 28, 2003 10:10:24 AM]

Share this post

Share on other sites
Personally I''d use a direction vector, by some called the velocity vector.

You will then have a vector called "position", which has an x and y component, and a vector "direction", which also has an x and y component.
Say, we move the ball one frame... what happens... the ball will move into the direction of the direction-vector. So you add the direction to the position, and tada... you ball has moved. You probably use something similar now (with the x and y I see in your code)
This x and y component can be of any value. The bigger the components, the faster the ball will move. This should all sound familiar.

When the ball bounces off the wall you have to INVERT the desired component of the direction (The ball changes direction).
You can best draw this on paper, but when the ball bounces against the top or bottom of the level, the y component of the direction-vector is inverted (multiplied with -1). So, if it was moving up, it will now be moving down. The X-component will remain the same.
If the ball bounces off a paddle, the x component of the direction vector has to be inverted. The direction over the y will remain the same.

You won''t need to know if the ball is going east or south or west, or in whatever direction... if it comes near the wall, you simply invert the right component. So, regardless of you ball moving south or north, it will always go the opposite direction.

______________________________
"A computer is meant to be a big calculator, not a storage device"
Struct.m33p.net

Share this post

Share on other sites
Hey thanks a lot guys! (or gals)

I really appreciate your replies, it has helped me out a lot!

Thanks again!

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
Rutin
21
3. 3
JoeJ
18
4. 4
5. 5
gaxio
12
• Advertisement

• 14
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631724
• Total Posts
3001897
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!