[osg-users] Collision of Spheres

Vijay Patil vijay.patil at gmail.com
Wed Sep 3 05:12:52 PDT 2008


I am trying to implement simple collision detection using OSG. Just
few balls/spheres enclosed in a closed box colliding with each other
and deflecting.
Since the objects are spheres, I used BoundingSphere objects to detect

I expect the bounding sphere to coincide with actual spherical object,
so collision should be accurate, That's where I face the problem. The
the collision is being detected (intersects true) some distance
*before* balls actually touch with each other. I want it to detect the
collision (intersects true) when they touch with each other. Is there
anything I might be missing ?  Following is relevant segment of the

       /* 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 obtain bound
objects. */
                    const osg::BoundingSphere& bs1 = carlist[i]->mt->getBound();
                    const osg::BoundingSphere& bs2 = carlist[j]->mt->getBound();

                    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;

Attached is complete code listing.

Vijay Patil
-------------- next part --------------
A non-text attachment was scrubbed...
Name: collision.cpp
Type: text/x-c++src
Size: 4030 bytes
Desc: not available
URL: <http://lists.openscenegraph.org/pipermail/osg-users-openscenegraph.org/attachments/20080903/fa20e9ea/attachment-0002.cpp>

More information about the osg-users mailing list