Vijay Patil
2008-09-03 12:12:52 UTC
Hi
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
collision.
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
code.
/* 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.
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
collision.
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
code.
/* 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
Vijay Patil