ball hitting paddle in breaout
In the game breakout how am I supposed to get the new value of x and y for the ball when it hits the paddle at a certain spot?
You can set up different bounding boxes around your paddle and depends on where the ball hit you can make you decision and position the x and y in the correct location.
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
That is how I might go about it. If you need more information I’ll be happy to assist.
Hope this helps,
Jackson Allan
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);
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);
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.
That should do the trick (code untested).
Hope this helps,
Jackson Allan
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 paddlepaddle->y = bottom of paddleball->xsball->ys = velocity of ballball->xball->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 collisionvx = ball->x – paddle->x;vy = ball->y – paddle->y;//Get the length of the vectorvl = 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 velocitysl = sqrt(ball->sx * ball->sx + ball->sy * ball->sy );vx *= sl;vy *= sl;//This is the new velocityball->sx = vx;ball->sy = vy;
That should do the trick (code untested).
Hope this helps,
Jackson Allan
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
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement