# Circle To Circle Collision

This topic is 5452 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi I just want to know a simple algoritm to find out the new direction after two circles have collide. Nothing about mass or different speed I just want to find out the new angle something that look quite realistic! Plz help (I know how to see if the circles overlap but after that iam lost)

##### Share on other sites
Vector d = c1.p-c2.p
d.Normalize()

##### Share on other sites
That seems a bit truncated there. DotP(c1.v,d)*d is the component of the velocity parallel to d and c1.v-DotP(c1.v,d)*d is the component perpendicular. A simple collision response is that the component parallel is reversed so c1.v=c1.v-2*DotP(c1.v,d)*d.

[Edited by - LilBudyWizer on September 27, 2004 8:20:57 PM]

##### Share on other sites
Um I don't understand your functions/classes in fact I doesnt work with classes at all. This is my variables

x1, y1, r1, direction1 // info about circle 1
x2, y2, r2, direction2 // info about circle 2

if (((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)) <= (r1+r2)*(r1+r2))
{
what should I do here with direction1/direction2
}

##### Share on other sites
I edited my post to use c1 and c2 like the first poster rather than cp1 and cp2.

The values x and y could be grouped together in a class, call that a vector. You can define the differance of two vectors as (x2,y2)-(x1,y1)=(x2-x1,y2-y1). You can define the magnitude of a vector as |(x,y)|=sqrt(x*x+y*y). You can define the multiplication of a vector by a scalar as c*(x,y)=(c*x,c*y). Then Normalize((x,y))=(1/|(x,y)|)*(x,y)=(x/|(x,y)|,y/|(x,y)|). You can define the scalar product as DotP((x1,y1),(x2.y2))=x1*x2+y1*y2.

Now c1.p is the position of the center of the first circle and c2.p is the position of the center of the second circle. So the condition of your if statement is DotP(c2.p-c1.p,c2.p-c1.p)<=(r1+r2)*(r1+r2). c1.v and c2.v are the velocity of the circles. They represent both the direction and speed of the circles. Given a speed s and a direction d measured as an angle with the x axis then c1.v=s*(cos(direction1),sin(direction1)). It is a change in position per unit of time given in terms of the change in x and y per unit of time rather than a direction and speed. What the unit of time is depends upon how you express s, i.e. miles per hour, pixels per second, etc. If your code is based upon using direction and speed you can get the direction back using atan2 and your speed remains unchanged by this collision response.

##### Share on other sites
Yes that is what I want but I dont know how to do it.
how sould I use atan2??

##### Share on other sites
Just atan2(y,x). That will give you the direction of a line from the origin to (x,y). The angle is between -pi (-180 degrees) to pi (180 degrees). All the trig functions in the C runtime library are in radians where 2pi is 360 degrees. Pi is defined in math.h as M_PI.

##### Share on other sites
Quote:
 Original post by ProblemBabyif (((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)) <= (r1+r2)*(r1+r2))

Couldn't that be reduced to

if (abs((x2-x1)+(y2-y1)) <= (r1+r2))

and still work? Or are you avoiding using abs() by squaring it?

##### Share on other sites
Its based on pythagoras theory
so that wont work

##### Share on other sites
if the distance between the two centre points is less than the sum of the radii (radiuses?) then the circles have colided.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 10
• 9
• 15
• 22