Sign in to follow this  
Crusable77

Pong

Recommended Posts

Crusable77    594
Hello,

I am wanting to make a pong clone to test my new knowledge with Java and slick. i haven't started anything yet but im planning it out. I am just a it confused on how you get the direction the ball goes after you hit it with the paddle. I don't know anything about physics (im taking it in school though). do i just randomly generate a direction or is there some stupid long mathematical equation thanks for any help.

Share this post


Link to post
Share on other sites
Tudor Nita    121
[quote]"stupid long mathematical equation"[/quote]
That doesn't sound good regardless of how much I spin it around in my head.

Anyway, you could just go with some simple conditional statements and inverse the direction of the ball. If you want something more complex you'd have to do some vector maths with at least the ball's direction vector, the paddle's forward vector and the position the ball hit the paddle.


[source lang="cpp"] // edge of playing field
if (pong.ball.centre_y > 0.6f)
{
pong.ball_velocity_y = -ball_speed;
} else if (pong.ball.centre_y < -0.6f)
{
pong.ball_velocity_y = ball_speed;
}
// ball hit a paddle on the right side
if (pong.ball_velocity_x > 0 && collides(&pong.ball, &pong.bats[1]) )
{
pong.ball_velocity_x = -pong.ball_velocity_x;
}
// ball hit a paddle on the left side
if (pong.ball_velocity_x < 0 && collides(&pong.ball, &pong.bats[0]) )
{
pong.ball_velocity_x = -pong.ball_velocity_x;
}[/source] Edited by Tudor Nita

Share this post


Link to post
Share on other sites
Bacterius    13165
A simple model for a ball bouncing off a paddle (ignoring edge effects) is [url="http://en.wikipedia.org/wiki/Specular_reflection#Direction_of_reflection"]perfect reflection[/url]. If you use vectors, it has an elegant expression not at all complicated once you know some vector algebra. Note that if your paddle is perfectly vertical, this is equivalent to negating the x-component of the ball's velocity, so you could start by doing that instead and improve later on (perhaps by adding round edges).

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