• ### What is your GameDev Story?

This topic is 4750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, I'm trying to create an arkanoid clone and I've almost everything up and running. However I just can't get the bouncing of balls on the paddle right. When hitting bricks of the screen's boarders life is easy but when hitting the paddle. What is the basic concept behind it? I'm trying the following; when collision with paddle is detected I compute the x-coordinate of distance between ball's center and the paddle's center. Then I have a constant array of degrees and I do the following
static float degs[] = {
-86,
-85, -84, -83, -82, -81,
-80, -79, -77, -75, -73,
-71, -69, -67, -65, -73,
-60, -57, -54, -51, -48,
-45, -42, -39, -36, -33,
-30, -27, -24, -21, -18,
-15, -12, -9, -6, -3,
0,
3, 6, 9, 12, 15,
18, 21, 24, 27, 30,
33, 36, 39, 42, 45,
48, 51, 54, 57, 60,
63, 65, 67, 69, 71,
73, 75, 77, 79, 80,
81, 82, 83, 84, 85,
86
};
...
float rad = degs[18 + (int)dist] * 0.01745;
...

The vector computed is the velocity vector of the ball, while dist holds the ball/paddle distance mentioned above. The velocity vector is computed by taking vector (0,-4) and applying linear transformation for rotation applying the following matrix: | cos(x) -sin(x) | | 0 | | 4*sin(x) | | sin(x) cos(x) | X |-4 | = |-4*cos(x) | However things don't work as intended and balls follow their own crazy patterns :(. Any idea for a better algorithm? I'm really stuck with this and I can't understand if its my algorithm or floating point computational flaws.

##### Share on other sites
Just to clarify: you're having difficulties bouncing the ball off the paddle correctly?

If I make the simplifying assumptions that both the ball and the paddle are approximated by rectangles, then the following comes to mind:

1. Determine which side of the paddle the ball has collided with.
2. Reflect the ball's velocity vector off of this segment.

Item 1 is the hard part. The following method might suffice:

a. Determine the position of the ball at the previous frame somehow (e.g., by either storing it, or perhaps computing it using the velocity vector).
b. Determine which segment of the paddle's rectangle the line on which lie both the ball's current and previous positions intersects.
b1. If the line intersects two or more segments of the paddle, then use the segment which was closest to the ball's previous position.

##### Share on other sites
Thats an ... unusual way of doing the ball collision responce....

So in your system after the ball hits the paddle, it's new direction is entirely decided by where it hits the paddle. The starting velocity of the ball has absolutely no affect.
I haven't played Arkanoid so I dont know if this is the effect you want or not....

But in more traditional Pong sytle games, the ball velocity is reflected when it hits the paddle, not reset.
Just invert the Ycomponent of the velocity.
If you want to add a little bit of 'english' from the paddle, then you can maybe average what you're using with the Reflection version.

[Edited by - haphazardlynamed on January 21, 2006 9:07:20 PM]

##### Share on other sites
After reading haphazardlynamed's post, I realized that maybe you're looking for an algorithm for applying perturbations to the reflected velocity vector of the ball upon collision with the paddle.

If so, then this seems like a decent method. Imagine the rectangle of the paddle immersed within an ellipse. Reflect the ball's velocity vector over the normal to this ellipse rather than the normal to the rectangle.

If you work out all the math for this, you should conclude (provided I did it correctly [smile]) that the line (not vector) normal to the ellipse given x-coordinate x is parametrized by
n(t) = (x(t),y(t))wherex(t) = x * (1 + t * (h/w)) andy(t) = h * sqrt(1 - x^2/w^2) * (1 + t * (w/h))where w and h are the half-width and half-height of the ellipse, respectively. You should treat these as tuning constants to achieve the desired amount of perturbation.

You should be able to continue on from here.

HTH,
nilkn

##### Share on other sites
to haphazardlynamed:
actually in the arkanoid games the paddle does not reflect the velocity of the ball (games that I've played: jardinains, time breaker). Every other component does (bricks, borders) but the paddle does not. Otherwise arkanoids whould be rather static games, but they are much more chaotic.

to nilkn:
for you first post, actually the first one is not a problem (any more), the second is my problem and that because I don't want just a reflection

for the second post, I don't care at all for the old velocity of the ball, the new velocity is determined only by the point of the paddle it hit. I found the ellipse approach quite intresting, but what are the x and t on the right side of the equation?
x(t) = x * (1 + t * (h/w))

Thanks

##### Share on other sites
Quote:
 Original post by ThemisI found the ellipse approach quite intresting, but what are the x and t on the right side of the equation?x(t) = x * (1 + t * (h/w))

The x refers to the x-coordinate of a point on the rectangle of the paddle. n(t) is a parametrization for the normal to the ellipse at the point with an x-coordinate of x (so, equivalently, the point on the ellipse directly above the point on the rectangle). The t is just the parameter.

Remember, what I gave isn't a vector. It's the equation for the line which is normal to the ellipse in parametrized form.

Thank you!

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015834
×