Added circle on ractangle collision
authorIvan Hernandez <iturtleman128@gmail.com>
Mon, 31 Jan 2011 23:19:07 +0000 (17:19 -0600)
committerIvan Hernandez <iturtleman128@gmail.com>
Mon, 31 Jan 2011 23:19:07 +0000 (17:19 -0600)
Collision.cpp

index 061b432..b49c51c 100644 (file)
@@ -210,9 +210,9 @@ bool CollisionRectangle::collision(const Collision *c, const Point2D cPos, const
                return !(outsideY || outsideX);
        }
        else if(dynamic_cast<const CollisionCircle*>(c)){
-               c->collision(this, cPos, pos);/// \todo this flips a shit!!!!!!
+               return c->collision(this, cPos, pos);
        }
-       
+       //if something breaks bad
        return false;
 }
 
@@ -233,5 +233,24 @@ bool CollisionCircle::collision(const Collision *c, const Point2D cPos, const Po
 
 bool CollisionCircle::collision(const CollisionRectangle* c, const Point2D cPos, const Point2D pos) const {
        ///check rect vs circle \todo implement
+       Point2D rectCent = cPos + c->mPos;//center of rect
+       Point2D tl = rectCent + Point2D(-c->width/2,-c->height/2);//top left of rect
+       Point2D bl = rectCent + Point2D(-c->width/2, c->height/2);//bottom left of rect
+       Point2D tr = rectCent + Point2D( c->width/2,-c->height/2);//top right of rect
+       Point2D br = rectCent + Point2D( c->width/2, c->height/2);//bottom right of rect
+       //Are any of the points inside the circle?
+       if( ((tl.x - mPos.x)*(tl.x - mPos.x) + (tl.y - mPos.y)*(tl.y - mPos.y) - radius*radius) <= 0 || 
+               ((tr.x - mPos.x)*(tr.x - mPos.x) + (tr.y - mPos.y)*(tr.y - mPos.y) - radius*radius) <= 0 ||
+               ((bl.x - mPos.x)*(bl.x - mPos.x) + (bl.y - mPos.y)*(bl.y - mPos.y) - radius*radius) <= 0 ||
+               ((br.x - mPos.x)*(br.x - mPos.x) + (br.y - mPos.y)*(br.y - mPos.y) - radius*radius) <= 0 )
+               return true;
+       //Is the circle inside the rectangle? (considers bounds)
+       else if( !(
+               mPos.x + radius < tl.x || 
+               mPos.x - radius > tr.x || 
+               mPos.y + radius < tl.y || 
+               mPos.y - radius > bl.y ))
+               return true;
+       //It's not in there 
        return false;
-}
\ No newline at end of file
+}