Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Gaming > Development Programming Misc > Re: Circle - Sq...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 12 of 12 Topic 948 of 1010
Post > Topic >>

Re: Circle - Square Collision Detection

by Anton <anton.txt@[EMAIL PROTECTED] > Oct 28, 2007 at 10:57 PM

Here's my perverted algorithm

__          __                                * Cc
Dl = (O,A); Dr = (O,B),                        \
A,B -- any two adjacent vertexes of the square. \
Cc -- center of the circle                       \
Rc -- its radius.                                 \
|V| -- stands for length of vector V.       '. A   \              B  .
                                              '.----\--------------.'
1. Quick check (as suggested above by John     |'.   \           .'|
                                      Nagle)   |  '.  \        .'  |
Suspicious for intersention:                   |    '. \ I   .'    |
                                               |      '.\  .'      |
|(O,Cc)| <= Rc + |(O,A)|                       |        '.'        |
                                               |   IV  .'O'.  II   |
If this is satisfied then further              |     .'     '.     |
calculation needed. Otherwise there's          |   .'   III   '.   |
no collision.                                  | .'             '. |
                                               .'-----------------'.
2. Precise calculation                       .'                     '.

2.1 Find the sign of scalar products:

Sl = sign[(O,Cc)X(Dl)];
Sr = sign[(O,Cc)X(Dr)];

2.2 Then find out in which quadrand the circle's center is located:

Case (Sl, Sr) of
(+,+): I;
(+,-): IV;
(-,+): II;
(-,-): III;

 2.3 Now that we know the quadrant, we can just find the distance
     between Cc and the corresponding side of the square:

     If this_distance <= Rc then collision.

// If you need to check for the circle being fully inside
// the square -- then check whther it's center is inside after
// step 1. Then, if there's no intersection and and center is
// inside, the whole circle is inside.
                                                       ^
Another way is to calculate only point-line distances: |n1
                                                   +-------+
(Choose normal vectors so that they all         n4 |       | n2
 point  outside the square)                    <---|       | --->
                                                   |       |
Using these normals, calculate:                    +-------+
(Di - signed distance from side i using normal         |n3
      vector ni)                                       '
                                                (normal vectors)
1. IF [((D1 <Rc) or (D3<Rc)) and
       ((D4<Rc ) and (D2<Rc]     or
      [((Abs(D2)<Rc) or (Abs(D4)<Rc)) and
       ((D1>0) and (D3>0))]
   THEN Collision //     including the "inside" case!
   ELSE No_Collision;

   This expression can be rewritten:
   (let's designate boolean values of Di<=Rc as Ai)

   Collided = A1*A2*A4 + A3*A2*A4 + A2*A1*A3 + A4*A1*A3;
                                             //(* = AND)
                                             //(+ = OR )
   This may speed up boolean evaluation.
   You begin to calculate this variables one by one,
   sequentially, and as soon as you gat Ai = FALSE, you
   may be sure that collision is possible only when
   the three other variables are TRUE.

   IF not A1 then COLLISION = (A2 and  A3 and A4) else
   IF not A2 then COLLISION = (A1 and A3 and A4) else
   IF not A3 then COLLISION = (A1 and A2 and A4) else
   IF not A4 then COLLISION = (A1 and A2 and A3) else
   COLLISION = TRUE; //...or the circle is inside the square
 




 12 Posts in Topic:
Circle - Square Collision Detection
"guitarstrummr@[EMAI  2007-10-15 20:25:18 
Re: Circle - Square Collision Detection
Luc The Perverse <atay  2007-10-16 08:20:43 
Re: Circle - Square Collision Detection
John Nagle <nagle@[EMA  2007-10-17 02:48:47 
Re: Circle - Square Collision Detection
Luc The Perverse <atay  2007-10-17 00:48:52 
Re: Circle - Square Collision Detection
Miss Elaine Eos <Misc@  2007-10-17 14:35:19 
Re: Circle - Square Collision Detection
John Nagle <nagle@[EMA  2007-10-17 08:09:10 
Re: Circle - Square Collision Detection
Miss Elaine Eos <Misc@  2007-10-17 15:24:12 
Re: Circle - Square Collision Detection
John Nagle <nagle@[EMA  2007-10-18 08:47:04 
Re: Circle - Square Collision Detection
Miss Elaine Eos <Misc@  2007-10-19 02:45:55 
Re: Circle - Square Collision Detection
John Nagle <nagle@[EMA  2007-10-21 04:43:06 
Re: Circle - Square Collision Detection
Richard James <IWillGe  2007-10-19 16:59:54 
Re: Circle - Square Collision Detection
Anton <anton.txt@[EMAI  2007-10-28 22:57:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 18 19:50:00 CDT 2008.