Flatland

Documentation

SourceForge.net Logo

circle-circle.cpp

Go to the documentation of this file.
00001 // Summary: Test for circle-circle intersection and generate contacts.
00002 // Copyright: 2007  Philip Rideout.  All rights reserved.
00003 // License: see bsd-license.txt
00004 
00005 #include <flatland/shapes.hpp>
00006 #include <flatland/intersection.hpp>
00007 
00008 bool Flatland::Intersection::TestCircleCircle(const Geometry &g1, const Geometry &g2)
00009 {
00010     assert(g1.GetShape() == Shape::Circle);
00011     assert(g2.GetShape() == Shape::Circle);
00012     const Circle &c1 = static_cast<const Circle&>(g1);
00013     const Circle &c2 = static_cast<const Circle&>(g2);
00014 
00015     float distance = (c2.Center() - c1.Center()).length();
00016     return distance <= c1.Radius() + c2.Radius();
00017 }
00018 
00019 void Flatland::Intersection::FindCircleCircle(const Geometry &g1, const Geometry &g2, ContactList &contacts)
00020 {
00021     assert(g1.GetShape() == Shape::Circle);
00022     assert(g2.GetShape() == Shape::Circle);
00023     const Circle &c1 = static_cast<const Circle&>(g1);
00024     const Circle &c2 = static_cast<const Circle&>(g2);
00025 
00026     vec2 delta = c2.Center() - c1.Center();
00027     float radiusSum = c1.Radius() + c2.Radius();
00028     float distance = delta.length();
00029     if (distance > radiusSum)
00030         return;
00031 
00032     vec2 pos;
00033     vec2 normal;
00034     float depth;
00035 
00036     if (distance <= 0)
00037     {
00038         pos = c1.Center();
00039         normal = vec2(1, 0);
00040         depth = radiusSum;
00041     }
00042     else
00043     {
00044         normal = delta / distance;
00045         float k = 0.5 * (c2.Radius() - c1.Radius() - distance);
00046         pos = c1.Center() + normal * k;
00047         depth = radiusSum - distance;
00048     }
00049 
00050     contacts.AddContact(pos, -normal, depth);
00051 }

Generated on Sat Jan 13 17:20:21 2007 for Flatland by doxygen 1.5.1