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!

0

Share this post

Link to post

Share on other sites

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]

0

Share this post

Link to 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