My game build releases have been a bit sparse lately. So you’re possibly wondering what I’ve been working on. Well, as the title suggests – Collision Detection.
The technique used in the current release of my game is as simple as they come, and it’s been bugging me. I haven’t released it yet, but the next release of my game will have much improved collision detection.
But boy, has it been a struggle! There has been a real lack of collision detection tutorials relating to 3D models under XNA, so I’ve had to learn the hard way. So I’m going to attempt to share what I’ve learnt, and hopefully save some other poor souls the pain.
First a caveat…
I don’t claim to be an expert on this subject at all – not by a long stretch. But hopefully I’ll be at least close to correct. Also, this is not really a tutorial either. Don’t expect a working sample with source code. I may get round to that later. Right now my code is a bit embarrassing, given the experimental nature of what I was trying to do.
these enhancements to my game are not yet released. So for now you’ll just have to trust me when I say it is an improvement. Stay tuned.
The Radius technique…primitive.
The simplest collision detection technique of all is to simply imagine a radius around all your object’s world positions. Then it is just a matter of checking whether any two objects have come within radius distance of each other. This is how my current release of Air Legends works, and if you watch closely it’ll be fairly evident. Depending on the orientation of the two planes a collision will still occur when clearly the planes were not even close.
A better alternative…
XNA provides two classes specifically for collision detection. BoundingSphere & BoundingBox. Both are non-visual, and contain an Intersects() method that will tell you if your BoundingBox/Sphere collides with another.
|//plane's been hit. Cue imminent death...|
What is not immediately obvious is this:
- it’s up to you to update the position of these objects, as the visual thing they represent moves around the the game’s world.
- You may even scale your model, so perhaps the BoundingBox/Sphere would need to be scaled too.
- Your game’s 3D model may also be rotated at times, so your collision detection needs to be be able to handle that.
There in lies a problem with using BoundingBox. BoundingBox is apparently an Axis Aligned Bounding Box (AABB). Not something I’d heard of until recently. I’m a bit sketchy on exactly what this means, but this is my take on it.
Imagine my plane model with an invisible box around it. This is our BoundingBox used for collision detection with other planes/bullets etc…
So what happens if our model is rotated? We want to rotate our invisible BoundingBox’s Min & Max corners to reflect this.
XNA has methods to transform these Vector3’s with rotation’s, so that’s achievable. We’d imagine our rotated BoundingBox to be something like this right?
Now you’re probably thinking, “How’s a sphere any different to the Radius technique mentioned above?”. And you’d be right. A single sphere around our model would be no improvement. However, you could break it up into multiple smaller BoundingSphere’s to represent different parts of the Model’s shape, and then we’d really be cooking!
It’s late, so I’ll stop there and continue in Part 2 soonish.
Ok, I couldn’t resist it. Here’s a sneak peak…