MULTIMEDIA

Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies

2/26/2011 5:09:44 PM

Bounding Spheres

A bounding sphere can be defined as a center position and a radius, where a radius represents the size of the sphere from the center to the outer surface. This is shown in Figure 3. Keep in mind that the difference between the radius and the diameter is that the diameter is the size of the sphere from one end of the surface to the other side as it crosses the center. The radius is essentially half the diameter.

Figure 3. A sphere defined by a position and a radius.


The benefit of using spheres is that mathematically you can test for things such as collisions and visibility faster than you can test boxes or other shapes. The downside to spheres is that depending on the shape, there can be more wasted space within the sphere’s volume, or even the box. An example of this is shown in Figure 4, in which the same object has a box around it (left) and a sphere (right).

Figure 4. An object that wastes less space using a box (left) than a sphere (right).


Because one shape (depending on the object) can lead to more wasted space than other shapes, using them can also be a little less accurate in various tests. For example, in Figure 5 two objects might register collision when testing only their bounding spheres when in reality they do not touch. There are solutions to this, which will be discussed in the next section, “Bounding Hierarchies.”

Figure 5. Objects that register as colliding even though they are not.


To calculate a bounding sphere in DirectX, we can use the function D3DXComputeBoundingSphere(), which takes as parameters a list of 3D vectors that represent the entire model, the total number of vertices in the model, the size of each vertex, and the output addresses that will store the center position of the model or bounding sphere and the bounding sphere’s radius. The D3DXComputeBoundingSphere() function has the following function prototype according to the DirectX SDK.

HRESULT D3DXComputeBoundingSphere(
CONST D3DXVECTOR3 *pFirstPosition,
DWORD NumVertices,
DWORD dwStride,
D3DXVECTOR3 *pCenter,
FLOAT *pRadius
);

You can test a ray for collision with the bounding sphere by calling the D3DXSphereBoundProbe() function, which takes as parameters the center position and radius of the sphere and the ray’s position and direction. The D3DXSphereBoundProbe() function has the following function prototype according to the DirectX SDK.

BOOL D3DXSphereBoundProbe(
CONST D3DXVECTOR3 *pCenter,
FLOAT Radius,
CONST D3DXVECTOR3 *pRayPosition,
CONST D3DXVECTOR3 *pRayDirection
);

Bounding Hierarchies

Using one of the simple shades as bounding geometry can lead to very fast tests such as for collision detection, but these tests are not the most accurate in many situations. Not only can there be wasted space within the volume of the shape, but it is difficult to determine much information from tests on a single bounding geometry. For example, in a first-person shooter game, what if you wanted to know if the player was shot in the arm, leg, head, and so on so that an appropriate animation can be played?

The solution is to use bounding hierarchies. The simple method requires you to break your model into pieces, each of which can have its own bounding geometry. For example, take a 3D character model. You can place bounding boxes around the arms, legs, feet, head, torso, hands, and so forth. This makes it much easier to detect the specific region of impact because more bounding geometry pieces are used in the model (see Figure 6). The amount of detail needed determines what parts of the model have their own bounding geometry. If you need to know which finger was hit, you could have bounding geometry around each finger of both hands, even if that might be overkill in most games.

Figure 6. A hierarchy of bounding geometry.


Also, you can efficiently combine a hierarchy of geometry with one large bounding box or sphere to improve performance when you must use the hierarchy for specific or more accurate tests. For example, you can use the larger bounding box or sphere of a model to determine if something is possible (e.g., possible collision, possible visibility, possible occlusion) and then use the bounding hierarchy to obtain more accurate results if that first test passes. This method can be very useful in games for which you must have more detailed information (such as a fighting game) if you don’t want to do those more accurate tests unless they are necessary.

If you do not need to go as far as bounding geometry hierarchies, you can take just a bounding box and sphere and use those. For example, you can use the very fast bounding sphere to determine if something is possible and then test the bounding box if the first test passes to get slightly more accurate results. If that is accurate enough for the application you are developing, you could stop there and assume that whatever you are testing is true.

Other  
  •  Programming with DirectX : Game Math - Matrices
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 2) - Jittering
  •  iPhone 3D Programming : Anti-Aliasing Tricks with Offscreen FBOs (part 1) - A Super Simple Sample App for Supersampling
  •  Building LOB Applications : Navigating RIA LOB Data
  •  Building LOB Applications : Databinding in XAML
  •  Microsoft XNA Game Studio 3.0 : Program Bugs
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Adding Vibration
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard
  •  iPhone 3D Programming : Blending and Augmented Reality - Stencil Alternatives for Older iPhones
  •  iPhone 3D Programming : Blending and Augmented Reality - Poor Man’s Reflection with the Stencil Buffer
  •  Microsoft XNA Game Studio 3.0 : Getting Player Input - Reading a Gamepad
  •  iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  Building LOB Applications : Using Visual Studio 2010 WCF RIA Data Services Tooling
  •  Building LOB Applications : Implementing CRUD Operations in WCF Data Services
  •  iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 3)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
  •  
    Top 10
    Intel SSD 335 Series - Better Under The Hood
    Upgrade Your Mice & Keyboards – May 2013
    Printer Upkeep: Inkjet Printer Maintenance Tips
    Printers: Inkjet vs. Laser, And More
    WD Black 4TB - 4TB Storage Goes Mainstream
    Smart Phones: Bigger, Faster, And Better Than Ever
    Choice Exotica Well Tempered Versalex Turntable (Part 2)
    Choice Exotica Well Tempered Versalex Turntable (Part 1)
    Pre/ Power Amplifier - Nagra Jazz/ MSA (Part 2)
    Pre/ Power Amplifier - Nagra Jazz/ MSA (Part 1)
    Most View
    Hashing Algorithms: Extending the .NET Framework (part 1)
    Motorola Razr
    The Second BlackBerry Developers Conference Asia (Part 2)
    IIS 7.0 : Performance and Tuning - Network
    .NET Compact Framework : Font Selection
    Programming the Mobile Web : Mobile Rich Internet Applications (part 1) - JavaScript UI Libraries
    Windows 8 All-In-One PCs On Test (Part 1) - Dell XPS One 27, Samsung Series 7 All-in-One PC
    Learn How To … Boost Your Protection Against PC Viruses
    Visual Studio Team System 2008 : TFS reports for testing - Bugs
    Oracle Coherence 3.5 : Installing Coherence, Starting up the Coherence cluster
    Dell S2740L - A Beautifully Crafted 27-inch IPS Monitor
    Photoshop School: Use Cloning To Clean Up Coastal Scenes
    Processor Group Test (Part 6) - Intel Core i7-3930K
    Programming with DirectX : Game Math - Bounding Geometry (part 2) - Bounding Spheres & Bounding Hierarchies
    Windows 7 : Mapping Your Networking Infrastructure (part 1) - Using the Network and Sharing Center
    Microsoft Content Management Server : Building SharePoint Web Parts - The SharePoint MCMS Navigation Control, Creating the Web Part Project
    Cooler Master Hyper T4 - A Step Up In Budget Cooling
    SQL Server 2008 : Index design and maintenance - Managing statistics
    Acer Aspire 5560G
    Visual Studio 2010 : Managing Extensions with the Extension Manager, Managing Add-Ins with the Add-In Manager