As you may have seen a post on how to generate bounding box data in 4.0 was required as some of the old tutorials just don’t seem to be cutting it any more. The pipeline code in this sample will also work in 3.1 as I took it from my 3.1 instancing project.
I am also going to show you how you can extend the properties of the pipeline so you can add in a logging switch, some times it’s a pain in the back side to code for the pipeline so it’s nice to be able to add some way of pushing some debug out put.
So, first off, extending the pipeline so we can switch logging on and off per model. First we need to add two references to the pipeline project, System.Drawing and System.Drawing.Design
This will allow us to use the types converters we want, we then set up a bool called enableLogging and an associated field called EnableLogging
This will allow us to switch the logging on and off per model in the IDE (VS 2010) which will look like this
Now we need a way to write the logging out, you could output to the debug window if you like, but I prefer to write it out to a file, so I have created a class to do this, naturally you can do both if you like :)
And now onto the core of this post, the capture of bounds data for the model, I first create lists for the bounds data, so at the top of the pipeline ModelProcessor class I have this
So, now I can hold a bounding box and sphere per mesh in the model. In the process method I set up a dictionary that can be used to store this data and be placed in the models Tag field so it can then be accessed later in your game code. I then call the base Process method before calling my GenerateData method. I then store the bounds data in the Tag object.
So, the meat and potatoes of the method is in GenerateData, this method checks if the asset is indeed a mesh and for each GeometryContent object in that mesh sets up a min and max value, it then loops through all the vertices by index (this is legacy from my original processor), get’s it’s position, calculates if that is greater or smaller than the last recorded min/max and store it, once all the vertices are read it then stores the final min.max data in the bounding lists. The method then does the same for all children.
And that’s it, we can now get at this data in game. In the sample I have done it when the mesh is loaded to get the bounding box volume like this
I can then, during the update set the bounding box like this
and I can now use this to check for collision.
As ever , C&C welcome :)
Sample code can be found here.
Multiple Mesh Bounding Box’s
As we can see from the comments below, this post skimped on the use of mesh’s that have more than one bounding volume, so a bounding volume per mesh in mesh. So to get this going you need to make a couple of modifications to the Base3DObject class.
First off, you need to make the volume and bounds fields to be List of BoundingBox’s
Now, in the update call, we change how we load the volumes up
and also how we calculate the bounds list used for collision data like this
And finally in the Collide call we have to check ALL the bounding volumes
And we should end up with something like this
Hope this addition to the post has helped :D