Sign in to follow this  
subflood

ball hitting paddle in breaout

Recommended Posts

Take a point directly below (or above) the centre of the paddle. Then get the vector from this point to the point of collision (collision point - point below center). Normalize this vector and then multiply it by the ball’s previous speed (length of previous velocity – you may want to add a small number on to this so as to increase the speed with each hit). This is the balls new velocity.
That is how I might go about it. If you need more information I’ll be happy to assist.

Hope this helps,
Jackson Allan

Share this post


Link to post
Share on other sites
jack_1313, I did something like this:

ball->xs = (ball->x - paddle->x2) * ball->xs;
ball->ys = ((ball->y + 21) - paddle->y) * ball->ys;

where xs = ball's horizontal velocity
ys = vertical velocity
x = current horizontal position of ball which is the collison point
y = same as x but this is the vertical position

x2 = left corner of paddle
y2 = botom of paddle

but all this does is exit out of my game loop which means it's failing right here:
if (ball->x < paddle->x1 || ball->x > paddle->x2);

Share this post


Link to post
Share on other sites
Hello again.

To normalize a vector is to make it a unit vector (length of 1.0). One can read a quick and to-the-point explanation of a 2d vector that will surely come in handy right here. Check the forum FAQ for more in-depth information.


/*
paddle->x = centre of paddle
paddle->y = bottom of paddle
ball->xs
ball->ys = velocity of ball
ball->x
ball->y = ball position (presumably point of collision)

*/


float vx;
float vy;
float vl;
float sl;

//Get the vector from the paddle bottom centre to the point of collision
vx = ball->x – paddle->x;
vy = ball->y – paddle->y;

//Get the length of the vector
vl = sqrt( vx * vx + vy * vy );

//Normalize this vector (make it’s length 1.0):
vx *= ( 1.0f / vl );
vy *= ( 1.0f / vl );

//Multiply it by the length of the ball’s previous velocity
sl = sqrt(ball->sx * ball->sx + ball->sy * ball->sy );
vx *= sl;
vy *= sl;

//This is the new velocity
ball->sx = vx;
ball->sy = vy;



That should do the trick (code untested).

Hope this helps,
Jackson Allan

Share this post


Link to post
Share on other sites
Ooops… I forgot to mention the final part:

vx = ball->x – paddle->x;
vy = ball->y – paddle->y;

Should be changed to something more like this:

vx = ball->x – paddle->x;
vy = ball->y – ( paddle->y + 50.0f );

Or some such depending on the size of the paddles and the angle variation you want in your game. Obviously the lager the number you add to paddle->y the lesser the variation. Sounds like you’ve already figured this one out though.

Anyhow, glad to be of help,
Jackson Allan

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