Calculate the reflection of Ball [duplicate]

I have a collision in my game (see below) and a ball which is moving in a certainly speed to a random angle (x, y).

My problem is. If it touches the collision it should reflect.

I simply multiply the Y-value of my ball by -1 (negative 1), which works fine!

The main problem is my collision looks like this.

I made it like when the ball collides the red collider it should multiply reflect the y-value. If it collides the yellow collider it reflects x-value.

But... what happens when the ball coming from left to up (like on picture) and collides the red value. It just changes the y-value.

Maybe there's a way to make it easier, but I don't know it.

• Could we see your collision code? Also, are you using LibGDX or Unity? You can't use both at the same time. – Charanor May 9 '17 at 17:36
• I'm using libgdx. I don't have any code, this is my plan. How should it work, I just need a logic. – Eucar May 9 '17 at 18:11
• Unfortunately, we're not a code writing service. And how do you know it's going to be a problem if you don't have any code? Try to implement your idea and if you get stuck come back and ask a specific question about your problem instead :) – Charanor May 9 '17 at 18:14
• Thats the main problem. I don't know where to start. Should I create one collision or three colisions (up, right, bottom). – Eucar May 9 '17 at 18:22
• You need to include the notion of normals in your collision handling. The red edge has a normal pointing upward. In the case that your ball is moving in the same direction as the normal (dot product of velocity and normal > 0), you should do nothing. – Drew Cummins May 9 '17 at 20:00

1 Answer

First of all, we need to clarify collision term; Collision is the "event" that happens when two colliders overlap each other, hence a ball cannot touch the collision, the ball's collider is touching the red collider in this case, and the "collision" occurs.

Secondly, since you did not provide any code and ask the question logically, the ball ignores the yellow collider here most probably. Check your collider settings and collision handling back again, or put the code here so anyone can provide a solution