Sign in to follow this  
andyb716

Pong ball movement

Recommended Posts

andyb716    100
Hi I've made a pong game in directx9 and I got movement with the pong ball but the movement is repeated in a patern that is obvious to learn. Does anyone know a good way to make it bounce of tha walls and paddle at different angles. By the way I suck at geometry. I've tried to add an extra move to the x and y values but that but makes the ball go faster and slower, not a real definate speed. Any help would be appreciated thanks. If you want to see what I am doing you can download the source and exe. I programed it in c++ 6 and directx 9. Test Pong Download

Share this post


Link to post
Share on other sites
oliii    2196
you have acceleration and slowdowns because adding some small deflection changes the magnitude of the velocity (magnitude = length = norm of a vector).

so, you need to 're-normalise' the velocity to conserve kinetic energy (the ball will move at constant speed).

two solutions,

1 add the deflections, but renromalise the velocity
2) rotate the velocity vector by 'x' degrees (which I think is better.

both refer to vector maths, with 2 adding a bit of trigonometry into the mix. if you don;t know vector maths or anything I just said above, I strongly suggest you look into it.

http://www.gamedev.net/reference/articles/article1832.asp

since it;s a simple solution, to achieve 1, all you need to do is

1)

float speed = Velocity.Magnitude();
Velocity += Vector(frand(3) - frand(3), frand(3) - frand(3)); // max of 3 pixels deflection.
Velocity *= (speed / Velocity.Magnitude());



2)

RotateVector(Velocity, frand(10)-frand(10)); // rotate vector by -10->10 degrees

float Pi()
{
static const float pi = (float) atan(1.0f) * 4.0f;
return pi;
}

float DegreesToRadians(float deg)
{
static const float deg_to_rad_constant = Pi() / 180.0f;
return deg * deg_to_rad_constant;
}

void RotateVector(Vector& V, float degrees)
{
float rad = DegToRad(degrees);
float co = cos(rad);
float si = sin(rad);

float x = V.x * co + V.y * si;
float y =-V.x * si + V.y * co;

V.x = x;
V.y = y;
}

Share this post


Link to post
Share on other sites
andyb716    100
So I set the speed of the ball to 10.0f, and then I pass into the rotate function 90 degrees, the ball would go straight up right. I can't seem to get that to work.

Share this post


Link to post
Share on other sites
LilBudyWizer    491
No, that function causes you to face a direction relative to the direction you were facing. The angle is how many degrees to the left you want to turn. If you want to face a direction relative to right on the screen then you feed in (1,0). If you want the angle to be how much to the right to turn then reverse the sign on the si terms. Left and right here assuming the coordinates increase from top/left to bottom/right as is normal in Windows.

Share this post


Link to post
Share on other sites
andyb716    100
How would I calculate the new Angle with the ball hitting the wall at a certain angle. Like for example the ball hits the top wall going right so that means the x value is positive and the y value is negative, and the angle it hits at is 45 degrees how would I calculate the opposite degree so I could rotate the ball x and y?

Share this post


Link to post
Share on other sites
andyb716    100
I read this article http://www.gamedev.net/reference/articles/article1832.asp
and it doesn't make any sense to me. So can someone please help me.

Share this post


Link to post
Share on other sites
grhodes_at_work    1385
Have you tried searching the forum archives? I just searched on the word "Pong" and there are about 50 different threads going back to 2001. If I were you, I would *seriously* take a look at all these past discussions!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this