A bit of object collision help please

I'm working on an object collision engine for a game and I have run into a bit of a problem.

If I have 2 overlapping circles A and B, how far must A be moved in the direction of the normalized vector V so that the 2 circles are exactly touching?
Trigonometry is going to be your friend for that one.

The circles' radii will be two sides, while the distance between them will be the third. Then you need to do some math, and figure out the best way to push them apart.
Or even simpler, when circles are tangeant, the distance between their centers is the sum of each one's radius.
If it is smaller than that, there are in collision, and you need to get one back of the distance (radius1+radius2-distanceBetweenCenters) in the direction of the line between the centers
The length of the intersection is the same for both A and B, just the direction differs (which you get from the vector).

Then
let d = |p1-p2|, the length of the intersection is then
r1+r2-d (p1,p2 being the centers, r1 and r2 the radii)

Not sure if this still works if one circle is contained within the other though (I'd guess it doesn't). Can such a situation occur in your game?
Yes it can.

Also, I may not have been clear enough. The vector V is passed into the function as the direction that circle A is to be moved. I am not looking for the shortest distance out of the collision in any direction.
So you have circle A (coords Ax, Ay with radius Ar)
And circle B (Bx, By, Br)
You also have unit vector V (Vx, Vy)

A needs to be moved to a new position, C.
You need to find Cx and Cy, where C = A*V*dist
dist is unknown.

You know that the distance between C and B is equal to Ar+Br
You also know the distance between A and B because both center points are known.


You can form triangle ABC. You know the length of AB and BC. You also know the angle C (given vectors V and AB). You just need to find the length of AC (which == dist, which will tell you C's center point)

I'm pretty sure that given any 2 sides and at least one angle of a triangle you can get the length of the remaining side. But my geometry is pretty rusty.

EDIT:
Googling turned up this page which seems to be exactly what you need:

http://www.mathsisfun.com/algebra/trig-solving-ssa-triangles.html
Last edited on
Suppose, without lose of generality, that B=0
The candidate points for A, are in the line P_j = A_j + \alpha V_j

You want for the circles to be tangent, so |P| = Br + Ar
squaring both sides |P|^2 = (Br + Ar)^2

Expanding the left side
P_j P_j = A_j A_j + 2 A_j \alpha V_j + \alpha^2 V_j V_j = (Br + Ar)^2

As you see, you've got a quadratic equation. Solve for \alpha

Edit: if you are not familiar with summation convention,
dot(A,A) + 2\alpha dot(A,V) + \alpha^2 dot(V,V) = (Br + Ar)^2
where dot is the dot product.

Edit: Fixed some mistakes
Last edited on
The link was exactly what I needed. Thank you!
Topic archived. No new replies allowed.