# [.net] Calculate world space bounding sphere / box

This topic is 4118 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello world, :) I have some nicely working code in my Managed DirectX 1.1 game engine that calculates bounding spheres/boxes for meshes in Model Space:
public static BoundingSphere Compute(Mesh mesh)
{ // Note: BoundingSphere is simply a struct to keep Vector3 Center and float Radius together
GraphicsStream data = null;
try
{
// Lock the vertex buffer

// Now compute the bounding sphere
Vector3 sphereCenter;
Geometry.ComputeBoundingSphere(data, mesh.NumberVertices, mesh.VertexFormat, out sphereCenter);
// Note: Equivalent code for Geometry.ComputeBoundingBox also exists
}
finally
{
// Make sure to unlock the vertex buffer
if (data != null)
{
mesh.UnlockVertexBuffer();
data.Dispose();
}
}
}

However for stuff like frustum culling, intersect tests (for picking) and so on I need these bounding bodies in World Space. Obviously I can't simply transform things like an axis-aligned bounding box (can't be rotated properly) from Model Space to World Space. Instead I'd like to generate a bounding sphere/box that's in World Space in the first place. However, so far, I have absolutely no clue how I could do this. Could somebody give me a hint how I can generate bounding bodies with the World Transformation already applied? Regards, Bastian

##### Share on other sites
How can you generate a world space AABB/bounding sphere for an object that's moving or rotating? Obviously the object's AABB/bounding sphere must move and rotate with it (a bounding sphere just has to move and doesn't need to rotate). A simple way to rebuild an AABB is to apply the world matrix to it's 8 points and then build an AABB from these 8 points, but here you can find some code that uses a more efficient method.

[Edited by - Gage64 on June 13, 2007 1:43:15 AM]

##### Share on other sites
@Gage64: Thanks for the hint. The problem with rotating a body inside a bounding sphere is that the body might be asymmetrical. The centre of the bounding sphere doesn't necessarily also have to be the origin (rotation centre) of a body.

Anyhow, I finally realised there's a much easier to implement picking than adapting my bounding bodies to World Space. Instead I simply transform my picking ray to Model Space and perform the intersect test there.

##### Share on other sites
I think this approach is less efficient because you would have to do it for each object but world-space bounding boxes/spheres are usually needed anyway (for frustum culling/collision detection, ...), so performing the test in world space is probably better.

##### Share on other sites
For "transformig" bounding spheres I now use this workaround:
public BoundingSphere ToWorld(Vector3 scale, Quaternion rotation, Vector3 position){    // Scale, rotate and transfrom the center of the bounding sphere    Vector3 newCenter =        Vector3.TransformCoordinate(Center, Matrix.Scaling(scale) * Matrix.RotationQuaternion(rotation)) + position;    // Shift the sphere by the position of the body    // Then scale the sphere according to the object's scaling    return new BoundingSphere(newCenter, Radius * MathHelper.GetMaxComponent(scale));    // Note: MathHelper.GetMaxComponent returns the largest of the three components of a Vector3}

Quote:
 Original post by Gage64A simple way to rebuild an AABB is to apply the world matrix to it's 8 points and then build an AABB from these 8 points

Do you know whether it's possible to use Managed DirectX's function for this?
Or do I need to implement the code for this manually?
So far I've only been able to apply Geometry.ComputeBoundingThingy to untransformed vertex information in meshes and vertex buffers

##### Share on other sites
Quote:
 Do you know whether it's possible to use Managed DirectX's function for this?

Probably not, but the link I posted above has a simple (and efficient) method to do this (plus some other useful stuff).

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
frob
13

• 9
• 15
• 10
• 9
• 17
• ### Forum Statistics

• Total Topics
632602
• Total Posts
3007363

×