Circle To Circle Collision

Started by
9 comments, last by Sweenie 19 years, 6 months ago
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)
Problems every where
Advertisement
Vector d = c1.p-c2.p
d.Normalize()
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]
Keys to success: Ability, ambition and opportunity.
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
}
Problems every where
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.
Keys to success: Ability, ambition and opportunity.
Yes that is what I want but I dont know how to do it.
how sould I use atan2??
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.
Keys to success: Ability, ambition and opportunity.
Quote:Original post by ProblemBaby
if (((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?
Its based on pythagoras theory
so that wont work
Problems every where
if the distance between the two centre points is less than the sum of the radii (radiuses?) then the circles have colided.

This topic is closed to new replies.

Advertisement