Sign in to follow this  
johnstanp

Making collision detection possible for all derived classes of bounding volume class

Recommended Posts

I want to write a physical simulation program. There's going to be a collision detection part. My physical objects are going to be modeled by a class called Object. I want to store any kind of physical bounding volume inside of it. More, I want to have the possibility to detect collisions between different objects by detecting collision between their bounding volume. The bounding volumes will be "concrete" classes( like Sphere, Oriented_box, etc ): and each of them should be able to test collision with an instance of any other bounding volume. I first tried inheritance via an abstract base class from which all bounding volumes had been derived, but, obviously(?), I was not be able to test collision between bounding volumes using pointers to the base class... Is there a way to achieve what I want using inheritance? [Edited by - johnstanp on December 22, 2009 2:24:16 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by johnstanp
I want to write a physical simulation program. There's going to be a collision detection part.
My physical objects are going to be modeled by a class called Object. I want to store any kind of physical bounding volume inside of it. More, I want to have the possibility to detect collisions between different objects by detecting collision between their bounding volume. The bounding volumes will be "concrete" classes( like Sphere, Oriented_box, etc ): and each of them should be able to test collision with an instance of any other bounding volume.

I first tried inheritance via an abstract base class from which all bounding volumes had been derived, but, obviously(?), I was not be able to test collision between bounding volumes using pointers to the base class...
Is there a way to achieve what I want using inheritance?
Double dispatch?

Share this post


Link to post
Share on other sites
Quote:
Original post by Seol
Quote:
Original post by johnstanp
I want to write a physical simulation program. There's going to be a collision detection part.
My physical objects are going to be modeled by a class called Object. I want to store any kind of physical bounding volume inside of it. More, I want to have the possibility to detect collisions between different objects by detecting collision between their bounding volume. The bounding volumes will be "concrete" classes( like Sphere, Oriented_box, etc ): and each of them should be able to test collision with an instance of any other bounding volume.

I first tried inheritance via an abstract base class from which all bounding volumes had been derived, but, obviously(?), I was not be able to test collision between bounding volumes using pointers to the base class...
Is there a way to achieve what I want using inheritance?
Double dispatch?


Could you please elaborate?
I thought about embedding information inside ( bounding volume ) classes that could be retrieved at runtime when testing collision between two instances of different derived class...

What I want to achieve is this:

class Box;
class Collidable;
class Object;
class Sphere;

class Collidable{
public:
Collidable(){
}
};

class Sphere: public Collidable{
public:
Sphere();
};

class Box: public Collidable{
public:
Box();
};

class Object{
public:
Object( Collidable * bounding_geometry );

bool const collides( Object const & object )const{
return bounding_geometry_->collides(
object.bounding_geometry_
);
}

private:
Collidable * bounding_geometry_;
};


Share this post


Link to post
Share on other sites
Thanks for the help!


#ifndef COLLIDABLE_H
#define COLLIDABLE_H

namespace ngn{
class Box;
class Sphere;

class Collidable{
public:
Collidable();

virtual void collides( Sphere const & sphere )const;
virtual void collides( Box const & box )const;
virtual void collides( Collidable const & test )const;
};
}

#endif // COLLIDABLE_H





#include "Collidable.h"
#include "Box.h"
#include "Sphere.h"
#include <iostream>

namespace ngn{
Collidable::Collidable(){
}

void Collidable::collides( Sphere const & sphere )const{
std::cout << "collidable\n";
sphere.collides( *this );
}

void Collidable::collides( Box const & box )const{
std::cout << "collidable\n";
box.collides( *this );
}

void Collidable::collides( Collidable const & collidable )const{
std::cout << "collidable colliding with a collidable\n";
}
}





#ifndef SPHERE_H
#define SPHERE_H

#include "Collidable.h"

namespace ngn{
class Box;

class Sphere: public Collidable{
public:
Sphere();

void collides( Sphere const & sphere )const;
void collides( Box const & box )const;
void collides( Collidable const & test )const;
};
}

#endif // SPHERE_H




#include "Sphere.h"
#include "Box.h"
#include <iostream>

namespace ngn{
Sphere::Sphere(){
}

void Sphere::collides( Sphere const & sphere )const{
std::cout << "sphere colliding with a sphere\n";
}

void Sphere::collides( Box const & box )const{
std::cout << "sphere colliding with a box\n";
}

void Sphere::collides( Collidable const & test )const{
test.collides( *this );
}
}





#ifndef OBJECT_H
#define OBJECT_H

#include <boost/shared_ptr.hpp>

namespace ngn{
class Collidable;

class Object{
public:
Object( boost::shared_ptr< Collidable > const & bounding_geometry );

void collides( Object const & object )const;

private:
boost::shared_ptr< Collidable > bounding_geometry_;
};
}

#endif // OBJECT_H





#include "Object.h"
#include "Collidable.h"
#include <cassert>

namespace ngn{
Object::Object( boost::shared_ptr< Collidable > const & bounding_geometry )
:bounding_geometry_( bounding_geometry )
{
assert( bounding_geometry );
}

void Object::collides( Object const & object )const{
bounding_geometry_->collides( *object.bounding_geometry_ );
}
}





#include <boost/shared_ptr.hpp>
#include "Box.h"
#include "Collidable.h"
#include "Object.h"
#include "Sphere.h"



int main( int argc, char ** argv )
{
boost::shared_ptr< ngn::Collidable > box( new ngn::Box() );
ngn::Object object( box );
boost::shared_ptr< ngn::Collidable > sphere( new ngn::Sphere() );
ngn::Object object1( sphere );
object.collides( object1 );
object1.collides( object );

return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this