January 27th, 2007 by Sharky

kick it on GameDevKicks.com 

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;

to:

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.

Average Rating: 4.7 out of 5 based on 240 user reviews.

Leave a Reply