Simple bounding box collision question
-
- Gnoblar
- Posts: 6
- Joined: Fri Apr 08, 2005 6:05 am
Simple bounding box collision question
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?
-
- OGRE Community Helper
- Posts: 1098
- Joined: Mon Sep 22, 2003 2:40 am
- Location: Melbourne, Australia
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;
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.
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);
}