Jump to content
  • Advertisement
Sign in to follow this  
ProblemBaby

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Yes that is what I want but I dont know how to do it.
how sould I use atan2??

Share this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!