diff --git a/engine/libraries/coll2d/include/coll2d.h b/engine/libraries/coll2d/include/coll2d.h index 2693a40..351de72 100644 --- a/engine/libraries/coll2d/include/coll2d.h +++ b/engine/libraries/coll2d/include/coll2d.h @@ -76,6 +76,7 @@ protected: vector3d mVelocity; float mAngle; float mAngleVelocity; + float mBoundingRadius; virtual void dummy() { } @@ -84,13 +85,15 @@ public: mPosition (0., 0., 0.), mVelocity (0., 0., 0.), mAngle (0.), - mAngleVelocity (0.) { + mAngleVelocity (0.), + mBoundingRadius (0.) { } Shape (const Shape &shape): mPosition (shape.mPosition), mVelocity (shape.mVelocity), mAngle (shape.mAngle), - mAngleVelocity (shape.mAngleVelocity) + mAngleVelocity (shape.mAngleVelocity), + mBoundingRadius (shape.mBoundingRadius) { } virtual ~Shape () {}; @@ -123,6 +126,12 @@ public: float getAngleVelocity () { return mAngleVelocity; } + void setBoundingRadius (float bounding_radius) { + mBoundingRadius = bounding_radius; + } + float getBoundingRadius () { + return mBoundingRadius; + } virtual void doPrintType() { std::cout << "Shape" << std::endl; @@ -160,6 +169,7 @@ public: } mVertices = vertices; + updateBoundingRadius(); } Polygon (const Polygon &polygon) : Shape (polygon) { mVerticeCount = polygon.mVerticeCount; @@ -167,6 +177,8 @@ public: mVertices = new vector3d[mVerticeCount]; memcpy (mVertices, polygon.mVertices, sizeof (vector3d) * mVerticeCount); + + mBoundingRadius = polygon.mBoundingRadius; } virtual ~Polygon () { if (mFreeVertices == true && mVertices) @@ -205,6 +217,7 @@ public: std::cout << "mAngle = " << mAngle << std::endl; std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl; + std::cout << "mBoundingRadius = " << mBoundingRadius << std::endl; } unsigned int getVerticeCount () { @@ -219,6 +232,20 @@ public: void setVertice (unsigned int i, const vector3d &vertice) { assert (i >= 0 && i < mVerticeCount); mVertices[i] = vertice; + + updateBoundingRadius(); + } + + void updateBoundingRadius () { + unsigned int i; + float max_vertice_dist2 = 0.; + for (i = 0; i < mVerticeCount; i++) { + float vertice_dist2 = mVertices[i].length2(); + if (vertice_dist2 > max_vertice_dist2) + max_vertice_dist2 = vertice_dist2; + } + + mBoundingRadius = sqrt (max_vertice_dist2); } friend int check_collision_polygon_sphere(Shape *polygon_a, @@ -231,17 +258,20 @@ private: public: Sphere (float radius) { mRadius = radius; + mBoundingRadius = radius; Shape::setPosition (vector3d (0., 0., 0.)); } Sphere(float radius, const vector3d &position) { mRadius = radius; + mBoundingRadius = radius; Shape::setPosition(position); } Sphere (const Sphere &sphere): Shape (sphere), - mRadius (sphere.mRadius) { } + mRadius (sphere.mRadius) + { mBoundingRadius = mRadius; } virtual Sphere* getCopy() { Sphere* copy = new Sphere (*this); @@ -269,7 +299,6 @@ public: std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl; } - void setRadius (float radius) { mRadius = radius; } diff --git a/engine/libraries/coll2d/tests/general.cc b/engine/libraries/coll2d/tests/general.cc index f68289a..ad9276c 100644 --- a/engine/libraries/coll2d/tests/general.cc +++ b/engine/libraries/coll2d/tests/general.cc @@ -23,6 +23,9 @@ TEST ( SphereCopyConstructer ) { velocity_b = sphere_b.getVelocity (); CHECK (velocity_a != velocity_b ); + + CHECK_EQUAL (123., sphere_a.getBoundingRadius()); + CHECK_EQUAL (123., sphere_b.getBoundingRadius()); } TEST ( PolygonCopyConstructer ) { @@ -47,6 +50,8 @@ TEST ( PolygonCopyConstructer ) { CHECK (vertice1 == polygon_b.getVertice (1)); CHECK (vertice2 == polygon_b.getVertice (2)); + CHECK_CLOSE (sqrt (2.), polygon_a.getBoundingRadius(), 1.0e-7); + polygon_b.setVelocity (vector3d (0., 0., 0.)); velocity_a = polygon_a.getVelocity (); velocity_b = polygon_b.getVelocity (); @@ -280,5 +285,3 @@ TEST ( SphereSetGetRadius ) { sphere.setRadius (456.); CHECK_EQUAL (456, sphere.getRadius()); } - -