codesamples >> quantum collision class

Quantum: CollisionBox class

Project:

Quantum

Engine:

XNA

Code language:

C#

Class description:

The collision system in Quantum is box-based: based on width, depth and height parameters we generate a collisionbox per colliding object in the world of Quantum.

CollisionBox.cs

// ...            
public void VigorCollisionBoxHandling(BoundingBox boundingBox1)
{
    // X - axis
    // Object is in the MIDDLE of the perpendicular face (Z)
    bool middleZ = m_Vigor.BoundingBox.Min.Z > boundingBox1.Min.Z && 
    m_Vigor.BoundingBox.Max.Z < boundingBox1.Max.Z;

    // Object is in contact with the RIGHT corner (Z)
    bool rightCornerZ = m_Vigor.BoundingBox.Max.Z > boundingBox1.Min.Z && 
    m_Vigor.BoundingBox.Max.Z < boundingBox1.Max.Z;

    // Object is in contact with the LEFT corner (Z)
    bool leftcornerZ = m_Vigor.BoundingBox.Min.Z > boundingBox1.Min.Z && 
    m_Vigor.BoundingBox.Min.Z < boundingBox1.Max.Z;

    // If the object is in specific area (Z) and the X-value is greater than the front face, collision with
     FRONT face (X)
    if ((middleZ || rightCornerZ || leftcornerZ) 
    && m_Vigor.BoundingBox.Max.X > boundingBox1.Min.X && 
    m_Vigor.BoundingBox.Min.X < boundingBox1.Min.X)
    {
        Vector3 Position = m_Vigor.Position;
        Position.X = boundingBox1.Min.X - m_CollisionOffset;
        m_Vigor.Position = Position;
        //Trace.WriteLine("Front face (X) Collision detected");
    }

    // If the object is in specific area (Z) and the X-value is smaller than the back face, collision with 
    BACK face (X)
    if ((middleZ || rightCornerZ || leftcornerZ) 
    && m_Vigor.BoundingBox.Min.X < boundingBox1.Max.X && 
    m_Vigor.BoundingBox.Max.X > boundingBox1.Max.X)
    {
        Vector3 Position = m_Vigor.Position;
        Position.X = boundingBox1.Max.X + m_CollisionOffset;
        m_Vigor.Position = Position;
        //Trace.WriteLine("Back face (X) Collision detected");
    }

    // Z - axis
    // Object is in the MIDDLE of the perpendicular face (X)
    bool inbetweenX = m_Vigor.BoundingBox.Min.X > boundingBox1.Min.X && 
    m_Vigor.BoundingBox.Max.X < boundingBox1.Max.X;

    // Object is in contact with the RIGHT corner (X)
    bool rightcornerX = m_Vigor.BoundingBox.Max.X > boundingBox1.Min.X && 
    m_Vigor.BoundingBox.Max.X < boundingBox1.Max.X;

    // Object is in contact with the LEFT corner (X)
    bool leftcornerX = m_Vigor.BoundingBox.Min.X > boundingBox1.Min.X && 
    m_Vigor.BoundingBox.Min.X < boundingBox1.Max.X;

    // If the object is in specific area (X) and the Z-value is greater than the front face, collision with 
    FRONT face (Z)
    if ((inbetweenX || rightcornerX || leftcornerX) 
    && m_Vigor.BoundingBox.Max.Z > boundingBox1.Min.Z && m_Vigor.BoundingBox.Min.Z < boundingBox1.Min.Z)
    {
        Vector3 Position = m_Vigor.Position;
        Position.Z = boundingBox1.Min.Z - m_CollisionOffset;
        m_Vigor.Position = Position;
        //Trace.WriteLine("Front face (Z) Collision detected");
    }

    // If the object is in specific area (X) and the Z-value is smaller than the front face, collision with 
    BACK face (Z)
    if ((inbetweenX || rightcornerX || leftcornerX) 
    && m_Vigor.BoundingBox.Min.Z < boundingBox1.Max.Z && m_Vigor.BoundingBox.Max.Z > boundingBox1.Max.Z)
    {
        Vector3 Position = m_Vigor.Position;
        Position.Z = boundingBox1.Max.Z + m_CollisionOffset;
        m_Vigor.Position = Position;
        //Trace.WriteLine("Back face (Z) Collision detected");
    }
}
// ...

QuantumGame.cs

// ...
// CollisionBox handling for Vigor

// CollisionBox handling between Vigor and Buildings
for (int index = 0; index < m_AmountOfBuildings; ++index)
{
    m_CollisionBoxClass.VigorCollisionBoxHandling(m_Building[index].BoundingBox);
}
// ...