Recommended Posts

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?

Share on other sites
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.

Share on other sites
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 on other sites
What does it mean to normalize a vector?

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

but all this does is exit out of my game loop which means it's failing right here:

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

Share on other sites
wow thanks, I made a few changes and everything works well now.

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

Should be changed to something more like this:

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

Create an account

Register a new account

• Forum Statistics

• Total Topics
628370
• Total Posts
2982300

• 10
• 9
• 13
• 24
• 11