Sign in to follow this  

Pong ball movement

This topic is 4815 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

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

This topic is 4815 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.

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