[osg-users] Collision of Spheres

Vijay Patil vijay.patil at gmail.com
Thu Sep 4 04:26:24 PDT 2008


Thank you Paul for your inputs.

On Wed, Sep 3, 2008 at 11:24 PM, Paul Martz <pmartz at skew-matrix.com> wrote:
>> I haven't looked at the code specifically, but I think this
>> is the assumption which turns out to be false. The Drawables
>> have bounding boxes, and then the Geodes containing them have
>> bounding spheres which are built to encompass the Drawables'
>> bounding boxes. Hence, the Geode's bounding sphere will be
>> larger than the original sphere because it has to encompass
>> the sphere's bounding box, which has to encompass the sphere itself.
>
> J-S is right here. But, there is a ShapeVisitor class and a
> ConstShapeVisitor class that you can use to create a more accurate bounding
> sphere, as in the code below... (Coding this off the top of my head, so
> forgive typos, thanks.)
>   -Paul
>
> class SphereBoundVisitor : public osg::ConstShapeVisitor
> {
> public:
>    SphereBoundVisitor() {}
>    virtual ~SphereBoundVisitor() {}
>
>    virtual void apply( const osg::Shape& s )
>    {
>        osg::notify( osg::WARN ) << "Unknown shape." << std::endl;
>    }
>    virtual void apply( const osg::Sphere& s )
>    {
>        osg::notify( osg::INFO ) << "Found Sphere." << std::endl;
>
>        osg::Vec3 c = s.getCenter();
>        float radius = s.getRadius();
>        _bs = osg::BoundingSphere( c, r );
>    }
>
>    osg::BoundingSphere _bs;
> };
>
> _______________________________________________
> osg-users mailing list
> osg-users at lists.openscenegraph.org
> http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
>

This gives me a simpler idea. Since I am storing x, y, z co-ordinates
of the sphere is external ( to SG) objects (Car objects). I could just
use/access that information to create BoundingSphere objects of
desired center and radius, as following code demonstrates:

       /* Check for intersection of one object with every other. */
            for(int i = 0; i < (int)carlist.size(); i++) {
                for(int j = i+1; j < (int)carlist.size(); j++) {

                    /* Use pointer to MatrixTransform to *create*
bound objects. */
                    osg::Vec3d c1(carlist[i]->x, 0.0, carlist[i]->z);
                    osg::BoundingSphere bs1(c1, RAD);

                    osg::Vec3d c2(carlist[j]->x, 0.0, carlist[j]->z);
                    osg::BoundingSphere bs2(c2, RAD);

                    if(bs1.intersects(bs2)) {
                        /* Collision detection. Simply reverse direction. */
                        carlist[i]->dx =  -carlist[i]->dx;
                        carlist[i]->dz =  -carlist[i]->dz;
                        carlist[j]->dx =  -carlist[j]->dx;
                        carlist[j]->dz =  -carlist[j]->dz;
                        break;
                    }
                }
            }


I tried this and it's working fine. Attached is complete code listing.
Next step for me would to study physics behind "elastic collision" and
implement correct deflection.


-- 
Vijay Patil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: accurate-cd.cpp
Type: text/x-c++src
Size: 4104 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080904/f1970e60/attachment-0003.cpp>


More information about the osg-users mailing list