If you’ve been forllowing this blog you’ll know i’ve been making a game. It originally started as an assignment for my XNA development class, but I grew attached to it and have turned it into its own project. Anyway, if you want to know more read the post below this one!
When beginning to do 3D collision detection, i thought the easiest way would be the “bounding box” method. If you don’t know what that is and would like to know more, just google it, there are tons of tutorials. However, for my particular use, this was no good, and it takes a little extra time to code it all out.
If you are using simple models in your game (currently mine just uses cubes to illustrate the basic gameplay) then you can probably use the other method of bounding sphere, thats already built into XNA! When i found this it was like a life saver, although I already coded the bounding box, i removed it and replaced a few lines and I was ready to go.
Basically for each model you load in XNA, the framework automatically creates a bounding sphere for that model. What most people have a hard time understanding is that if you move the model in game, then the sphere doesnt follow. So really if you drew all your bounding spheres they would probably be right where you loaded the model. So remember when using model.BoundingSphere() you have to create a new bounding sphere to move around. It’s not as hard as you think here are some examples:
In the variables for a troop i have this:
public BoundingSphere bs;
public BoundingSphere range;
“Bs” is the actual bounding sphere of my “troop” model. I just steal the position of my troop when i first load it to give the bounding sphere the proper location. The “range” bounding sphere is just another way of showing you what the bounding sphere can be used for. Each of the weapons have a range, and this sphere’s radius, is the number range of that weapon. So if a target is in range, we can shoot at it! Kind of neat to use bounding sphere’s in that way.So how do we use a bounding sphere and why are they so easy? Here is some more code to initialize the sphere:
foreach (ModelMesh mesh in model.Meshes)
{
Vector3 center = mesh.BoundingSphere.Center;
float radius = mesh.BoundingSphere.Radius * scale;
center.X += position.X;
center.Y += position.Y;
center.X += .5f;
center.Y += 2f;
bs = new BoundingSphere(center, radius);
range = new BoundingSphere(center, w.getRadius);
}
It looks like a lot but it isn’t i promise. Bounding sphere’s are actually in the mesh part of a model, for my classes they all hold their own model, and when i draw i just tell my application what were drawing and it has the model. So, were going through all the mesh parts and moving there bounding box accodingly, for mine we only have one, and your method might have to be different for multiple mesh parts to a single mesh.
Each bounding sphere has a center, and a radius. The center is an XYZ coordinate, and the radius is a float saying how large it needs to be, so in essence my range is just a different float for each weapon. Once you set all that you should have your sphere moving with your model in no time.So now my troop has a bounding sphere, we have to make sure we move that with the troop so whenever we make the troop move, make the sphere move too. Thats the most important don’t forget to do that or you’ll have a hell of a time wondering why you’re shooting something and it never collides.
Alright, now for the second part! I have another class, this is a projectile class that i use to show a yellow “bullet” firing. These are really small low poly models that also have a bounding sphere. It also moves with the bullet as it is fired; so how exactly do we check to see if a bullet hit something? Oh, how you wonder. Luckily you are using the bounding sphere and it’s SO simple:
bs.Intersects(troop.Target.BoundingSphere)
Don’t get consfused, i pulled this from my projectile class so “bs” is the bounding sphere of the bullet. In my game, each troop is shooting at a target troop, not just a random area, so were getting the target troop of the bullets owner. Basicall, whoever shot the bullet, were getting who they are shooting it at. Once we have that we just check to make sure the bounding sphere for the bullet, intersects the bounding shpere of their target, and if it does we lower the targets health.Now this is really simple and it only works if they hit their target, if another enemy happens to cross the line of fire, the bullet would continue until it either hit’s or misses it’s target. You can extend this to have more functionality, so if anything crosses the bullet it’s going to get hit.