# A ball hits the corner slightly, where will it deflect?

I am currently making an iOS Game but I have a small problem.
The game is simply a ball which you can control to avoid obstacles and find the food. When the ball hits, e.g. the top or the bottom of an obstacle, it's easy, the y-speed is just inverted. Same with left and right, where the x-speed is inverted.

But what happens when the mass point of the ball doesn't touch a side of the obstacle, but its surface is? Where will it be deflected? What's the maths behind that?

Here's an image to illustrate the problem:

• Probably can be resolved in same way as with two colliding spheres, except one sphere is the corner point and with radius 0. Try this gamedev.stackexchange.com/questions/150322/… Jun 21, 2020 at 21:49

You can't just "flip velocity over y". To further that point, its obvious to see on the left and right sides of the box you would not flip over y, but instead flip over x.

On the corners you need to flip over a diagonal direction. This is where it becomes less obvious how to flip it. But it's still quite easy:

What you need is a function to flip over any direction. This is often referred to as a reflect function. This direction we call the normal, n.

On the top: n = (1, 0)

On the bottom: n = (-1, 0)

On the left: n = (-1, 0)

On the right: n = (1, 0)

On the corners: n = normalize(circle center - corner), this is the direction between the center of the circle and corner of the box, you must determine which corner to use

Now you have n. Here's how to reflect velocity over n:

reflect(vec2 vec, vec2 n)
// First project vec onto n, this gives the length of vec along the n axis
float nLength = dot(vec, n)
// Lets get that as a vector along n
vec2 vecN = nLength * n
// Now we want to know how much of vec is going along tan/perpendicular to n, easy, just remove vecN from vec
vec2 vecTan = vec - vecN
// These two should sum together such that vec = vecN + vecTan, but we actually want to flip the tangent/perpendicular part of vec, so negate
vec2 results = vecN - vecTan
return results