Li : Dev Actionscript Development Blog

19Oct/09Off

OBB Collision Detection

Click on image to view the demo (view source enabled).

Its just a basic implementation of the method of separating axes for 3D OBB's (oriented bounding boxes). It detects collisions between cubes in a way that is slightly more advanced than spherical collisions, which are merely based on distances and radii. Collisions are not resolved in the demo though, only detected.

The point of the experiment is to show that it isn't hard at all to detect collisions between 3D objects if considering only their bounding boxes for their 'physical volume' is enough; if we just need to detect if objects are colliding or not, we don't actually need to bring in a heavy, cpu thirsty, 3D physics engine.

The code is not optimized (in fact quite the contrary so that its easy to read), but it runs quite smoothly with 15 cubes, where each cube is checked with all others for collision. If, for example, a character needed to be checked for collisions against static objects in a 3D scene, we would just need to compare his bounding box with the scene's static bounding boxes, so a large number of physically active objects could be used to describe a pretty complex scene. Furthermore, if any of these boxes were axis aligned, they are no longer OBB's, but rather AABB's (axis aligned bounding boxes) and collision checks could be made much faster. Something like this I imagine:

return (obj1MinX < obj2MaxX) && (obj1MaxX > obj2MinX) &&
(obj1MinY < obj2MaxY) && (obj1MaxY > obj2MinY) &&
(obj1MinZ < obj2MaxZ) && (obj1MaxZ > obj2MinZ);

The experiment is clarifying in understanding how the method of separating axis works, and I guess it gave me a quick glimpse at how a advanced physics engines like JigLibFlash might work. The code is heavily commented, so have a look if you're interested in understanding OBB collisions.

Filed under: 3D, Tutorials Comments Off
Comments (4) Trackbacks (1)
  1. broken link…

  2. Oops… Should be fixed now, just in case, this is the url for the demo: http://www.lidev.com.ar/demos/away3d/obbs1/

  3. awesome. now make them bounce off each other and you have a jenga simulator :)

  4. very neat :)

    if it’s not too hard to solve collisions, it would be much more useful than an ‘exact’ (= heavy as hell) 3D physics engine like jiglib.

    I don’t think that simulations (especially in flash) really need accurate collision detetcion. Oriented boxes should do in many cases.

    well done anyway keep up!