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: What happens on the right?

Thanks in advance for help!


1 Answer 1


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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .