Extending the SceneManager and introducing….

Yes, I’ve finally managed got around to implementing BEPU Physics. This has meant introducing a new “PhysicsManager” class and extending the “SceneManager” class, but it’s worth it. If you haven’t already, download the latest code from the link above (I’m using 1.1.0). In the “ProjectVanquish” project, add a reference to the BEPU DLL.

We’ll start with the new “PhysicsManager” class. In the “Core” folder, add a new class called “PhysicsManager” and include the following namespaces:

using BEPUphysics;
using BEPUphysics.Constraints;
using BEPUphysics.Settings;
using Microsoft.Xna.Framework;
using ProjectVanquish.Models;

Add the following variables:

private Space space;
private IList<PhysicsObject> physicsObjects; 

PhysicsObject doesn’t exist yet, so you’ll have to fight with Visual Studio for the time being with regards the Intellisense. Let’s create the constructor:

public PhysicsManager(Vector3 gravity)
{
    space = new Space();
    space.ForceUpdater.Gravity = gravity;
    SolverSettings.DefaultMinimumIterations = 2;
    MotionSettings.DefaultPositionUpdateMode = BEPUphysics.PositionUpdating.PositionUpdateMode.Continuous;
    MotionSettings.UseExtraExpansionForContinuousBoundingBoxes = true;
    MotionSettings.CoreShapeScaling = 0.99f;
    space.Solver.IterationLimit = 20;

    // Check if we can use mutli-threading
    if (Environment.ProcessorCount > 1)
    {
        for (int i = 0; i < Environment.ProcessorCount; i++)
            space.ThreadManager.AddThread();

        space.BroadPhase.AllowMultithreading = true;
    }

    physicsObjects = new List<PhysicsObject>();
} 

In the constructor we are instantiating a new BEPU physics object and setting some default values. We also check to see if we can use multi-threading that the BEPU engine now supports. Straight forward enough. We’ll add some properties:

public IList<PhysicsObject> PhysicsObjects 
{ 
    get { return physicsObjects; } 
}

public Space Space 
{ 
    get { return space; } 
} 

This will be used in the “SceneManager” class. Lastly, we just need an “Update” method:

public void Update(GameTime gameTime)
{
    space.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
} 

Excellent. That is our “PhysicsManager” class complete. Let’s extend our “SceneManager” class to use this new class. Open the “SceneManager” class and declare a new variable:

static PhysicsManager physicsManager;

In the constructor, we’ll instantiate it:

physicsManager = new PhysicsManager(new Vector3(0, -9.81f, 0));

This will create an earth-like gravity. We’ll create a static property so we can access this from the “PhysicsManager” class:

public static PhysicsManager PhysicsManager 
{ 
    get { return physicsManager; } 
}

The last part of the “SceneManager” class is to update the “PhysicsManager” in the “Update” method:

physicsManager.Update(gameTime);

That’s all we need to do with the “SceneManager”. The last thing on the check-list is the new “PhysicsObject” class. Add a new class in the “Models” folder called “PhysicsObject” and add in the following namespaces:

using BEPUphysics.Entities;
using BEPUphysics.Entities.Prefabs;
using BEPUphysics.MathExtensions;
using Microsoft.Xna.Framework;
using ProjectVanquish.Core;

Make the class public abstract:

public abstract class PhysicsObject

Declare the following variables:

bool movable = false;
Entity entity;
float mass = 1f;

Add a constructor:

public PhysicsObject(bool isMovable)
{
    movable = isMovable;
    InitializeEntity();
} 

In the constructor, we are declaring if the object is movable or static. We are also calling the “InitializeEntity” method. We have 2 methods to add:

protected void InitializeEntity()
{
    entity = new Box(Vector3.Zero, 0f, 0f, 0f, mass);
    SceneManager.PhysicsManager.PhysicsObjects.Add(this);
}

public void Remove()
{
    entity.Space.Remove(entity);
    SceneManager.PhysicsManager.PhysicsObjects.Remove(this);
}

The first initializes a new “Box” object entity and adds it to the “PhysicsManager” list, whilst the second removes the object. That’s all for now with this class. We will be returning to it to add properties later on, but for now, you have successfully integrated BEPU Physics into the engine.