coll2d: added bounding spheres to all Shapes

main
martin 2011-02-01 16:13:06 +01:00
parent dd9f2773f5
commit 95e709ce3f
2 changed files with 38 additions and 6 deletions

View File

@ -76,6 +76,7 @@ protected:
vector3d mVelocity; vector3d mVelocity;
float mAngle; float mAngle;
float mAngleVelocity; float mAngleVelocity;
float mBoundingRadius;
virtual void dummy() { virtual void dummy() {
} }
@ -84,13 +85,15 @@ public:
mPosition (0., 0., 0.), mPosition (0., 0., 0.),
mVelocity (0., 0., 0.), mVelocity (0., 0., 0.),
mAngle (0.), mAngle (0.),
mAngleVelocity (0.) { mAngleVelocity (0.),
mBoundingRadius (0.) {
} }
Shape (const Shape &shape): Shape (const Shape &shape):
mPosition (shape.mPosition), mPosition (shape.mPosition),
mVelocity (shape.mVelocity), mVelocity (shape.mVelocity),
mAngle (shape.mAngle), mAngle (shape.mAngle),
mAngleVelocity (shape.mAngleVelocity) mAngleVelocity (shape.mAngleVelocity),
mBoundingRadius (shape.mBoundingRadius)
{ } { }
virtual ~Shape () {}; virtual ~Shape () {};
@ -123,6 +126,12 @@ public:
float getAngleVelocity () { float getAngleVelocity () {
return mAngleVelocity; return mAngleVelocity;
} }
void setBoundingRadius (float bounding_radius) {
mBoundingRadius = bounding_radius;
}
float getBoundingRadius () {
return mBoundingRadius;
}
virtual void doPrintType() { virtual void doPrintType() {
std::cout << "Shape" << std::endl; std::cout << "Shape" << std::endl;
@ -160,6 +169,7 @@ public:
} }
mVertices = vertices; mVertices = vertices;
updateBoundingRadius();
} }
Polygon (const Polygon &polygon) : Shape (polygon) { Polygon (const Polygon &polygon) : Shape (polygon) {
mVerticeCount = polygon.mVerticeCount; mVerticeCount = polygon.mVerticeCount;
@ -167,6 +177,8 @@ public:
mVertices = new vector3d[mVerticeCount]; mVertices = new vector3d[mVerticeCount];
memcpy (mVertices, polygon.mVertices, sizeof (vector3d) * mVerticeCount); memcpy (mVertices, polygon.mVertices, sizeof (vector3d) * mVerticeCount);
mBoundingRadius = polygon.mBoundingRadius;
} }
virtual ~Polygon () { virtual ~Polygon () {
if (mFreeVertices == true && mVertices) if (mFreeVertices == true && mVertices)
@ -205,6 +217,7 @@ public:
std::cout << "mAngle = " << mAngle << std::endl; std::cout << "mAngle = " << mAngle << std::endl;
std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl; std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl;
std::cout << "mBoundingRadius = " << mBoundingRadius << std::endl;
} }
unsigned int getVerticeCount () { unsigned int getVerticeCount () {
@ -219,6 +232,20 @@ public:
void setVertice (unsigned int i, const vector3d &vertice) { void setVertice (unsigned int i, const vector3d &vertice) {
assert (i >= 0 && i < mVerticeCount); assert (i >= 0 && i < mVerticeCount);
mVertices[i] = vertice; 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, friend int check_collision_polygon_sphere(Shape *polygon_a,
@ -231,17 +258,20 @@ private:
public: public:
Sphere (float radius) { Sphere (float radius) {
mRadius = radius; mRadius = radius;
mBoundingRadius = radius;
Shape::setPosition (vector3d (0., 0., 0.)); Shape::setPosition (vector3d (0., 0., 0.));
} }
Sphere(float radius, const vector3d &position) { Sphere(float radius, const vector3d &position) {
mRadius = radius; mRadius = radius;
mBoundingRadius = radius;
Shape::setPosition(position); Shape::setPosition(position);
} }
Sphere (const Sphere &sphere): Sphere (const Sphere &sphere):
Shape (sphere), Shape (sphere),
mRadius (sphere.mRadius) { } mRadius (sphere.mRadius)
{ mBoundingRadius = mRadius; }
virtual Sphere* getCopy() { virtual Sphere* getCopy() {
Sphere* copy = new Sphere (*this); Sphere* copy = new Sphere (*this);
@ -269,7 +299,6 @@ public:
std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl; std::cout << "mAngleVelocity = " << mAngleVelocity << std::endl;
} }
void setRadius (float radius) { void setRadius (float radius) {
mRadius = radius; mRadius = radius;
} }

View File

@ -23,6 +23,9 @@ TEST ( SphereCopyConstructer ) {
velocity_b = sphere_b.getVelocity (); velocity_b = sphere_b.getVelocity ();
CHECK (velocity_a != velocity_b ); CHECK (velocity_a != velocity_b );
CHECK_EQUAL (123., sphere_a.getBoundingRadius());
CHECK_EQUAL (123., sphere_b.getBoundingRadius());
} }
TEST ( PolygonCopyConstructer ) { TEST ( PolygonCopyConstructer ) {
@ -47,6 +50,8 @@ TEST ( PolygonCopyConstructer ) {
CHECK (vertice1 == polygon_b.getVertice (1)); CHECK (vertice1 == polygon_b.getVertice (1));
CHECK (vertice2 == polygon_b.getVertice (2)); CHECK (vertice2 == polygon_b.getVertice (2));
CHECK_CLOSE (sqrt (2.), polygon_a.getBoundingRadius(), 1.0e-7);
polygon_b.setVelocity (vector3d (0., 0., 0.)); polygon_b.setVelocity (vector3d (0., 0., 0.));
velocity_a = polygon_a.getVelocity (); velocity_a = polygon_a.getVelocity ();
velocity_b = polygon_b.getVelocity (); velocity_b = polygon_b.getVelocity ();
@ -280,5 +285,3 @@ TEST ( SphereSetGetRadius ) {
sphere.setRadius (456.); sphere.setRadius (456.);
CHECK_EQUAL (456, sphere.getRadius()); CHECK_EQUAL (456, sphere.getRadius());
} }