# Circle collision help

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

## Recommended Posts

ok i wana do circle circle collision using a swept circle ive been using a gamasutra article to help me get this far but i am stuck at this point so far i have 2 spheres A and B where A0 is begin pos and A1 is the end pos as well as with B0 and B1 so i define A(t) = A0 + t(A1 - A0) and B(t) = B0 + t(B1 - B0) where t is the normalized time step so that ( 0 <= t <= 1 ) let |Vector| = the Lenght of Vector so the distance squared between the segments at any point in time is |B(t)-A(t)|^2 given that ra = radius of a and rb = radius of b to find the point of intersection i only need to find where |B(t)-A(t)|^2 = ( ra + rb )^2 rewriting using the original equations i get |(B0 + t(B1 - B0))-(A0 + t(A1 - A0))|^2 - ( ra + rb )^2 = 0 witch when expanded is quadratic and can be solved using the quadratic formula My problem is how to expand the equation using vectors. you cant multiply vectors and im not even looking for vectors but the length squared. the example was using dot product and it didnt seem to follow "rules" of number arithmatic. Could anyone give me some help on rewriting it in the form at^2 + bt + c So far i have that the actual positions of the vectors dont matter just there relative distances so if i translate all the vectors by one of the vectors i get a free zero ill pick B0 and i get an equation that looks like | Ut - (V0 + t(V1 - V0)) |^2 - ( ra + rb )^2 = 0 but this doesnt even look right

##### Share on other sites
|(B0 + t(B1 - B0))-(A0 + t(A1 - A0))|^2 - ( ra + rb )^2 = 0

You have to insert the vectors and divide the equation up to two, one for you X and one for your Y, so you get:

|(B0x + t(B1x - B0x))-(A0x + t(A1x - A0x))|^2 - ( ra + rb )^2 = 0
|(B0y + t(B1y - B0y))-(A0y + t(A1y - A0y))|^2 - ( ra + rb )^2 = 0

Quote:
 Original post by Raymond_Porter420My problem is how to expand the equation using vectors. you cant multiply vectors and im not even looking for vectors but the length squared. the example was using dot product and it didnt seem to follow "rules"of number arithmatic. Could anyone give me some help on rewriting it in the form
When you square a vector, you dot (don't know if it's the right word) it with itself.

the dot product:
a dot b = a1b1 + a2b2a dot a = a1a1 + a2a2 = 2a1 + 2a2

So looking at it:
|B0 + tB1 - tB0 - A0 - tA1 - tA0|^2 =2(B0x + t(B1x - B0x) - A0x - t(A1x - A0x)) + 2(B0y + t(B1y - B0y) - A0y - t(A1y - A0y))Now insert that, and you get:(2tB1x - (t-1)2B0x - 2tA1x - 2(t+1)A0x + 2tB1y - (t-1)2B0y - 2tA1y - (t+1)2A0y) - ( ra + rb )^2 = 0

Which can be simplified, but I'm not the man to do it [smile]

[Edited by - Coward on March 30, 2005 3:36:20 PM]

##### Share on other sites
This code almost directly corresponds to what is in the paper. But I removed the code for collision response. See how it compares to what you've wrote.

This is returned and stores the results of the functionstruct pCDI_MovCirCir{   bool collision; // Did they ever collide?   Vector np1, np2; // The new positions for circle 1 and 2};// Collision detection for two moving circles// p1, p2 - positions of circle 1 and 2 respectively// r1, r2 - radii ...// v1, v2 - velocities ....pCDI_MovCirCir pColDet_MovCirCir (Vector p1, Vector p2,   float r1, float r2, Vector v1, Vector v2){   pCDI_MovCirCir ret; // The results to be returned   // Check if circle 1 is heading in circle 2's direction   Vector relv = v1 - v2; // Relative velocity of A to B   Vector D = p2 - p1; // Direction of A to B   if (relv.dot (D) < 0)   {      ret.collision = false; // No collision will occur      return ret;   }   // Check if circle 1 will ever reach circle 2 in distance   float Dlen = D.len ();   float relvlensq = relv.x * relv.x + relv.y * relv.y;   if (relvlensq <= Square(Dlen - r1 - r2))   {      ret.collision = false;      return ret;   }   // Check if the closest circle 1 will ever get to circle 2   // is greater then the sum of their radii   float relvlen = sqrt (relvlensq);   Vector N = relv / relvlen;   float Elen = D.dot (N);   float Flensq = Dlen * Dlen - Elen * Elen;   float Rlensq = Square(r1 + r2);   if (Flensq > Rlensq)   {      ret.collision = false;      return ret;   }   // Calculate the new positions of the circles   float tsq = Rlensq - Flensq;   if (tsq > relvlensq)   {      ret.collision = false;      return ret;   }   else ret.collision = true;   float t = sqrt (tsq);   float td = (Elen - t) / relvlen;   ret.d1 = v1 * td;   ret.d2 = v2 * td;   ret.np1 = p1 + ret.d1;   ret.np2 = p2 + ret.d2;   return ret;}

##### Share on other sites
Raymond, the expression is not a squaring of a vector, which, as you say, is impossible. You are squaring the length (norm) of a vector (note the vertical bars surrounding the vector expression). That is, you have an expression of the form |X|^2 for some vector expression X. Well, |X|^2 simplifies to Dot(X,X), that is, the dot product of X with itself. Hopefully everything should now fall into place!

1. 1
Rutin
47
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632994
• Total Posts
3009766
• ### Who's Online (See full list)

There are no registered users currently online

×