Page 1 of 1
Simple bounding box collision question
Posted: Thu Apr 14, 2005 7:19 am
by Lucias
I feel kind of stupid for asking this, but I've been messing around with some bounding box collision stuff, and I'm not really succeeding. Basically I'm colliding 2 AABBs together, but I want them to stop when they collide, so I try to check which way they are colliding and push them back to right before they hit. I can't quite get this to work for some reason, I think it's because my brain doesn't work anymore. Is there anything in Ogre that can do this for me, or can someone point me to a good tutorial or something for this?
Posted: Thu Apr 14, 2005 9:24 am
by monster
You could just look at the intersection of the two bounding boxes and move the objects apart by the biggest overlap, or you could do something like this, assuming that box_a and box_b are your SceneNodes;
Code: Select all
AxisAlignedBox aab = box_a->_getWorldAABB().intersection(box_b->_getWorldAABB());
if(!aab.isNull())
{
Vector3 diff = box_b->getPosition() - box_a->getPosition();
Vector3 dir = diff.normalisedCopy();
Vector3 pene = aab.getMaximum() - aab.getMinimum();
Vector3 trans = dir * Math::Abs(pene.normalisedCopy().dotProduct(dir)) * pene.length() * 0.5;
box_a->translate(-trans);
box_b->translate(trans);
}
If box_a is the thing that always moves and box_b just needs to be shunted out of the way then don't multiply by 0.5 or translate box_a, that way box_b gets all the translation.