00001
00002
00003
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 }