Rendering Bounding Boxes

From XNAWiki
Jump to: navigation, search

A Bounding Box is a axis aligned area of space that is in the shape of a box, This means that each face of the Bounding Box is riding on the X, Y, or Z Axis.

BoundingBox myBoundingBox = new BoundingBox(new Vector3(0, 0, 0), new Vector3(4, 4, 4));

The above code creates a Bounding Box named myBoundingBox. The constructor of the BoundingBox Class takes in two Vector3s, a min value and a max value. The first value can be the back top left corner of your 3D object and the second value can be the front right bottom corner of your 3D object.

The Function

public bool Intersects()

Allows the user to check if the box is intersecting another box, ray or sphere.

This is a basic static class that allows easy rendering of BoundingBoxs.

/// <summary>
    /// Provides a set of methods for the rendering BoundingBoxes.
    /// </summary>
    public static class BoundingBoxRenderer
    {
        #region Fields
 
        static VertexPositionColor[] verts = new VertexPositionColor[8];
        static int[] indices = new int[]
        {
            0, 1,
            1, 2,
            2, 3,
            3, 0,
            0, 4,
            1, 5,
            2, 6,
            3, 7,
            4, 5,
            5, 6,
            6, 7,
            7, 4,
        };
 
        static BasicEffect effect;
        static VertexBuffer vertex_Buffer;
 
        #endregion
 
        /// <summary>
        /// Renders the bounding box for debugging purposes.
        /// </summary>
        /// <param name="box">The box to render.</param>
        /// <param name="graphicsDevice">The graphics device to use when rendering.</param>
        /// <param name="view">The current view matrix.</param>
        /// <param name="projection">The current projection matrix.</param>
        /// <param name="color">The color to use for drawing the lines of the box.</param>
        public static void Render(
            BoundingBox box,
            GraphicsDevice graphicsDevice,
            Matrix view,
            Matrix projection,
            Color color)
        {
            if (effect == null)
            {
                effect = new BasicEffect(graphicsDevice);
                effect.VertexColorEnabled = true;
                effect.LightingEnabled = false;
            }
 
            Vector3[] corners = box.GetCorners();
 
            for (int i = 0; i < 8; i++)
            {
                verts[i].Position = corners[i];
                verts[i].Color = color;
            }
 
            vertex_Buffer = new VertexBuffer(graphicsDevice, typeof(VertexPositionColor), verts.Length, BufferUsage.WriteOnly);
            vertex_Buffer.SetData<VertexPositionColor>(verts);
            graphicsDevice.SetVertexBuffer(vertex_Buffer);
 
            effect.View = view;
            effect.Projection = projection;
 
            effect.CurrentTechnique.Passes[0].Apply();
 
            foreach (EffectPass pass in effect.CurrentTechnique.Passes)
            {
                graphicsDevice.DrawUserIndexedPrimitives(
                    PrimitiveType.LineList,
                    verts,
                    0,
                    8,
                    indices,
                    0,
                    indices.Length / 2);
            }
        }
    }