Download the cube class required for this tutorial here: CollisionCube.cs

Basically, when doing collision at the bone level, you want to get some sort of bounding structure for a bone. There are many ways to do this - you can get a bounding box, bounding sphere, bounding rectangular prism, etc.

This tutorial will show you how to use bounding boxes.
  • This tutorial builds off of the running dwarf tutorial, so you must complete the Making a Dwarf Realistically Walk and Run tutorial up to at least making the dwarf walk around.
  • I created a Cube class that helps for our purposes. Showing how to create it would be more trouble than it's worth - it's just grunt work. The cube allows us to render a visualization of a colored cube based on a world matrix, and also allows us to get an axis aligned bounding box for the cube.
  • Download the CollisionCube.cs file (CollisionCube.cs) and add the class to your file by pasting the code into Game1.cs or creating a new class in your project and pasting the code there.
  • First lets create another dwarf:
    // Add this as a member variable
    ModelAnimator otherDwarf;

    // Add this to LoadGraphicsContent
    otherDwarf = new ModelAnimator(this, model);
    otherDwarf.World = Matrix.CreateTranslation(5, 0, 5);
  • Now lets create the cube objects to represent the collision bounding boxes for our player dwarf's head and for the new dwarf's left elbow:
    // Add these as member variables
    Cube leftElbow, head;
  • Notice in this next snippet that the cube side length for the head is 1.2f and .6f for the elbow. Really, it takes trial and error to determine the best dimensions for a bone bounding box. Many times, you will want to offset the bounding box so it's center doesn't coincide with the bone. Add the following code:
    // Add this in load graphics content
    head = new Cube(graphics.GraphicsDevice, Color.Red, 1.2f);
    leftElbow = new Cube(graphics.GraphicsDevice, Color.Green, .6f);
    head.Projection = projection;
    leftElbow.Projection = projection;
  • We want to draw the Cubes, but we have to set the World transforms for the cubes to be equal to the their respective bone transforms. One way to get the absolute transform for a bone at the current frame is by calling ModelAnimator.GetCurrentTransform:
    // Add this to the Draw method
    head.View = view;
    head.World =
        dwarfAnimator.GetAbsoluteTransform(
        dwarfAnimator.BonePoses["head"].Index) * dwarfAnimator.World;
        head.Draw();

    leftElbow.View = view;
    leftElbow.World =
        otherDwarf.GetAbsoluteTransform(
        otherDwarf.BonePoses["lelbo"].Index) * otherDwarf.World;
    leftElbow.Draw();
  • Finally, let's do something if our player's head rams into the idle dwarf's elbow. We'll make the elbow green when it is not colliding and blue when it is. I added this functionality to the Cube class:
    // Add this to the Draw method
    if (head.GetBoundingBox().Intersects(leftElbow.GetBoundingBox()))
    {
        leftElbow.Color = Color.Blue;
    }
    else
    {
        leftElbow.Color = Color.Green;
    }

}}

Last edited Mar 8, 2007 at 3:38 PM by dastle, version 7

Comments

No comments yet.