3
votes

Working with iPhone and Objective C.

I am working on a game and I need to correctly reflect a ball off a circle object. I am trying to do it as a line and circle intersection. I have my ball position outside the circle and I have the new ball position that would be inside the circle at the next draw update. I know the intersect point of the line (ball path) and the circle. Now I want to rotate the ending point of the ball path about the intersection point to get the correct angle of reflection off the tangent.

The following are known:

  • ball current x,y
  • ball end x,y
  • ball radius
  • circle center x,y
  • circle radius
  • intersection point of ball path and circle x and y

I know I need to find the angle of incidence between the tangent line and the incoming ball path which will also equal my angle of reflection. I think once I know those two angles I can subtract them from 180 to get my rotation angle then rotate my end point about the angle of intersection by that amount. I just don't know how.

2

2 Answers

4
votes

First, you should note that the center of the ball doesn't have to be inside of the circle to indicate that there's a reflection or bounce. As long as the distance between ball center and circle is less than the radius of the ball, there will be a bounce.

If the radius of the circle is R and the radius of the ball is r, things are simplified if you convert to the case where the circle has radius R+r and the ball has radius 0. For the purposes of collision detection and reflection/bouncing, this is equivalent.

If you have the point of intersection between the (enlarged) circle and the ball's path, you can easily compute the normal N to the circle at that point (it is the unit vector in the direction from the center of the circle to the collision point).

For an incoming vector V the reflected vector is V-2(NV) N, where (NV) is the dot product. For this problem, the incoming vector V is the vector from the intersection point to the point inside the circle.

As for the reflection formula given above, it is relatively easy to derive using vector math, but you can also Google search terms like "calculate reflection vector". The signs in the formula will vary with the assumed directions of V and N. Mathworld has a derivation although, as noted, the signs are different.

0
votes

I only know the solution to the geometry part.
Let:
r1 => Radius of ball
r2 => Radius of circle

You can calculate the distance between the two circles by using Pythagoras theorem.
If the distance is less than the r1+r2 then do the collision.

For the collision,I would refer you Here. It's in python but I think it should give you an idea of what to do. Hopefully, even implement it in Objective C. The Tutorial By PeterCollingRidge.