I'm having a hard time trying to resolve the reflexion angles when the ball collides with one of the brick's corners (square vertices) in my brick breaker game.

The collision detection system is working fine, and I've already managed to resolve the collisions when the ball hits the brick from its sides. The reflection angles are working fine when the ball hits either the walls or the brick's sides, and I've even managed to add slight angle increments to these angles to prevent the ball to bounce indefinitely from one side to the other.

But when it comes to the brick's corners, things get messy. The collision resolution works fine to differentiate the sides from the corners. In general, I'm picking two coordinates x,y (new and previous) to determine the ball positions and using Math.atan2(newY-prevY, newX-prevX) function to get the angles to calculate cos and sin for spdX and spdY for the collision resolution on the walls and the brick's sides. For the corners part, I'm picking the newX,newY ball coordinate (center point) when it collides with one of the bricks corners and picking the x,y coordinate from this corner to determine the normal line, by using Math.atan2(newY-cornerY, newX-cornerX). Note that I've made the adjustments for each corner x,y to get the correct values (adding width to x for the right corners and height to y for the bottom corners). Then, I calculate the angle of incidence incAngle = Math.abs(Math.atan2(prevY-newY, prevX-newX) - normal) and the reflection angle refAngle = normal - incAngle. Lastly I calculate spdX = Math.cos(refAngle) and spdY = Math.sin(refAngle). It seems the mess happens when I calculate incAngle and refAngle. I've already inverted signs on both, used Math.abs(), and in all cases some of the reflection angles show fine, some of them even perfectly, but in certain positions, depending of the inciding angle, the reflection angle shows incorrectly. Definetly the angles sign issue still confuses me a lot.

Where am I doing wrong? Is there something missing? I'm developing my game from scratch using Java and Eclipse and am trying to make the code as simple as I can, since I don't understand much of those quite complicated complex Math equations (I had made some research about them, but did not understand a thing), and don't use external physics/collision engines in it.

Best regards.

  • \$\begingroup\$ Does the incorrect calculation appear in some specific cases, such as top-to-bottom collisions are fine, but bottom-to-top collisions are not correct? Maybe you can post the function to calculate the reflection angle here. \$\endgroup\$
    – Mangata
  • \$\begingroup\$ Can you show us your exact code? Also, when you say "ellipse", do you mean your ball can have a distorted aspect ratio? If not, it's better to use the words "circle" or "disc" — math with arbitrary ellipses can get significantly more complicated, complexity you can skip if you don't need it. \$\endgroup\$
    – DMGregory


You must log in to answer this question.

Browse other questions tagged .