Thursday 28 June 2012

Bounding Box and Collision Detection



The idea for this post came from a thread I spotted on the XNA Creators Club Forum. I read in the thread that you can't have your bounding box's rotate, now when I debug my collision stuff I tend to draw my bounding box's so I can see them in action and so I assumed that my bounding box's were rotating with my models. Turns out they're not. I guess I just assumed that the bounding box was part of the model, and so when I rotate the model the box rotates with it, this was re-enforced by me seeing the boxes that I have drawn rotate with the model. What is actually happening is the corner co-ords of the bounding box are being transformed in the shader that is used to rotate the model and so the link between bounding box and model orientation was made, but on the CPU the bounding box is still aligned to the world and not the object.



The models used are in X and FBX format (box2.fbx and sphere.x)

So in my attempt to try and get this to work, I have come across some new lingo; AABB, which is a Axis Aligned Bounding Box and OOBB, Object Orientated Bounding Box. The BoundingBox object we get with XNA s the former (AABB) so it is always aligned with the World axis and has no correlation with the objects rotation. Where as the latter takes into account the objects rotation.

I then thought, "If I transform the corner vectors with the world matrix, I could then have a OOBB!", alas, the native Intersects method of the XNA BoundingBox uses a AABB check no mater what I did with the co-ords. So this lead me to my current fudge :)



I guess the guys in the XNA Creators Club Forum are right to get true OOBB you need a third party physics component. Be nice to find out how this is done correctly though.

Basically I have taken each corner of the AABB and at that point put a BoundingSphere, so for each corner the collision method will check if any of the corners have collided with the other model.

In this example I have put the custom content pipeline to generate your bounding box data, in the model class I have put properties that then retrieve it and a method to manage the collision detection. This, as ever, is very basic but it illustrates regular bounds collision and shows how I have tried to implement a OOBB workaround.




GenericXNABoundingBoxExample.zip

No comments:

Post a Comment