Archive for January, 2007

XNA Collision Detection for 3D models – tutorial complete!

Tuesday, January 30th, 2007

Well it ended up being 3 parts instead of 2. (techinical difficulties with Windows Live Writer, and me ranting on too much)

Anyway, blog form is not ideal for tutorials, so eventually I might make pages out of them.

In the meantime I’ve collated links to the 3 parts & the sample code here.

Plane in BoundingBox


XNA Collision Detection for 3D models – Part 3

Tuesday, January 30th, 2007

After some technical difficulties with Windows Live Writer, I think I’m back in action.


So continuing from where I left off in Part 2 (part 1 is here)…

What about this BoundingPart class then?

As mentioned earlier it wraps a Collection of BoundingSphere’s. Well actually it contains two Collections of BoundingSpheres.

  1. One collection is the sphere’s populated as we saw above. These sphere’s coordinates are in Object Space and are populated when the Model is first loaded.
  2. The second collection is for holding Transformed versions of these spheres. Their coordinates will be transformed into World Space, and the Radius scaled on each frame of our game. Think of these as being the Spheres as they are positioned and scaled in our game’s world. This is necessary so we can test for Collisions! Object Space simply wouldn’t cut the mustard, since it would be equivalent to having all our models stacked on top of each other at the same (0,0,0) position. They’d always be colliding like some kind of roman orgy. World Space puts them in their rightful place with their hands in their pockets.


XNA Collision Detection for 3D models – Part 2

Tuesday, January 30th, 2007

Hi there.

In Part 1 of this tutorial we looked briefly at one of the simplest techniques for collision detections – the radius (forgive me, if there’s a fancy name for it). A perfectly adequate solution for some games.

We also looked into what XNA has to offer out of the box, and why the BoundingBox class may not be suitable for some games, due to it’s Axis Aligned nature, and how that doesn’t work so well with rotated models.

If you missed part 1, check it out here

Meanwhile, lets get on with this 2nd, and if all goes well, final part. By the end of this I hope I’ll have shared some sort of solution. I see it as an interim solution as I really hope Microsoft will address this in a future version of XNA.

In Part 1 I mentioned that there would be no working code sample. I lied. I found it so difficult to explain it all from bits and pieces of my game’s code, that I bit the bullet and made a working sample.

As you may already be aware, I’ve published this sample on the blog already. Feel free to grab it over here. I will be refering to code in the sample throughout the rest of this tutorial.

Bye bye BoundingBox, hello BoundingSphere.

Besides BoundingBox, XNA provides an BoundingSphere class for collision detection. (more…)

Problems anyone?

Monday, January 29th, 2007

Can people please let me know (via blog comments) if they are having problems with either…

  1. my latest “Air Legends” release?
  2. or my Collision Detection sample code?

I’ve had one report of a problem with “Air Legends” crashing after choosing from the menu screen.  Works fine for myself & some others.

Also Ultrahead alerted me to a problem he was having running my Collision Detection sample code (v1.0.0.1).

While it worked fine for me, a crash was occuring on the DrawIndexedPrimitives lines that actually draws the spheres. I wish I could understand why it would crash. My code for rendering the spheres was taken pretty much straight from an XNA help file example. 

While investigating the problem I ended up changing the code a little.

  • I had been using a LineList, but in retrospect a LineStrip seems simpler, and perfect for a list of connected lines. Once again, I based my code on the XNA help file example.

I’ve just uploaded a new version (v1.0.0.2). I’ll be interested to hear if it also gives anyone grief. 

If it does please do let me know. And as a work-around if it also fails, I’m guessing that changing these lines of code…

        0, // vertex buffer offset to add to each element of the index buffer
        0, // minimum vertex index
        CIRCLE_NUM_POINTS + 1, // number of vertices. If this gets an exception for you try changing it to 0. Seems to work just as well.
        0, // first index element to read
        CIRCLE_NUM_POINTS); // number of primitives to draw


        0, // vertex buffer offset to add to each element of the index buffer
        0, // minimum vertex index
        0, // number of vertices. If this gets an exception for you try changing it to 0. Seems to work just as well.
        0, // first index element to read
        CIRCLE_NUM_POINTS); // number of primitives to draw

…will probably solve the problem (much like the solution Ultrahead found with my original code). The “number of vertices” parameter seems to be quite optional. Zero works fine for me too.  Kinda bugs me though. I hate this uncertainty.

Meanwhile, I should really get on with Part 2 of the tutorial, perhaps even some Battlefield 2142…  ;)

New release of XNA game "Sharky’s Air Legends". Now with improved collision detection

Sunday, January 28th, 2007

I’ve just uploaded a new build of “Air Legends”.

The only change in this release is that I’ve incorporated the improved collision detection that I’ve been blogging about so much lately. If you’ve played the previous versions I’m sure you’ll notice it’s a lot better now.

You should be able to survive much closer flybys now than before.  More of your bullets will miss now. For obvious reasons really – the collision zones are much tighter and more realistic than before.  But also, and this one was slightly unexpected, some bullets will fly right over or behind a wing missing altogether.  This is not a fault.  Wings are narrow – bullets are small and could go either side of a wing depending on the planes roll.

You can get the new release from the Latest Builds page here.

XNA Collision Detection for 3D models – dodgy hack solved, source fixed and uploaded!

Saturday, January 27th, 2007

Shawn Hargreaves (of the XNA team) gave me a nice simple solution to the small hack I had in my code.

In your BoundingPart.Transform method, you build up a full matrix to transform the bounding sphere center point, but don’t take that matrix scaling into account when updating the bounding sphere radius.

There isn’t a really general way of extracting scaling information from a matrix in XNA at the moment (this is something we’re planning on adding for the next version), but a good workaround is to take the length of the Matrix.Forward vector. That will be accurate as long as the matrix doesn’t include any shearing on non-uniform scaling, so it works fine for pretty much everything you are likely to encounter in real life model data.

I changed your:

bsTransformed.Radius = bs.Radius * scale * DODGY_HACK_MAGIC_NUMBER;


bsTransformed.Radius = bs.Radius * localWorld.Forward.Length();

which will hopefully be more consistent with different meshes as well

I’ve tested it and it works sweet.  The new version of the sample is now available here.

XNA Collision Detection for 3D models – source code

Saturday, January 27th, 2007

As promised I’ve uploaded the working sample solution that I will explain later in Part 2 of the tutorial…coming soon!

I’ve also added a Tutorials section on the site so it should be easy enough to navigate.

Enough talk… go get the source

If anyone finds a solution to the one small hack in my solution, please let me know!

Update: I forgot to mention the sample’s controls & that there is a Mouse moveable black Cursor that goes White when colliding. The controls are…

  • Pitch model: XBOX 360 Left Thumbstick Forward/Backwards or Keyboard W/S
  • Roll model: XBOX 360 Left Thumbstick Left/Right or Keyboard A/D
  • Yaw model: XBOX 360 Right Thumbstick Left/Right or Keyboard Q/E
  • Scale model: XBOX 360 Left/Right Trigger or Keyboard LeftShift/LeftCtrl
  • Move 3D cursor: Mouse
  • Move 3D cursor forward/backwards: Mouse Scrollwheel Forwards/Backwards
  • Screenshot and XML Dump (put in \Screenshots sub directory): Keyboard PrintScrn

Note: my Mouse code is very rushed and obviously not quite right yet.  Suggestions welcome. ;)

Part 2 of the tutorial is coming… well soon-ish ("XNA Collision Detection for 3D models")

Friday, January 26th, 2007

G’day folks.

Quick update…

I’ve been working hard to get Part 2 in a state ready to publish.  While I’ve got a working solution in my game It’s been painfully hard to write a tutorial showing snippets. So I’ve had to bite the bullet and make a working sample to share.

That has been worthwhile though. In the process I’ve cleaned up a lot of ugly code, or stuff related to “Air Legends” current architecture, that just complicates things.

It has also highlighted some problems that I’ve fixed.  There is one remaining problem – some models seem to behave differently. Something to do with their BoneTransforms I believe.  I’ve found a simple, but less than ideal solution (aka. a dodgy hack), but IT WORKS reliably!!

I think I’ll post the working sample code in the next couple of days.  (Maybe someone can point out solutions to the dodgy hack)

I’ll follow that up with Part 2 of the tutorial to actually explain it all.

An update on Kukyona’s XNA Terrain component – Shadow maps!

Sunday, January 21st, 2007

Howdy all.

Kukyona at CodeAnxiety has posted another update on the Terrain Component for XNA. It’s looking very cool indeed.  Check it out here

XNA Terrain component, now with Shadow maps

p.s. I’m still working on Part 2 of my XNA Collision Detection tutorial.

XNA Collision Detection for 3D models – Part 1

Thursday, January 18th, 2007

Greetings earthlings.

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.

Second caveat…

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. (more…)