Hi:
I'd like to add Physics Simulation to this wonderful little engine with BulletX from the XNADev.ru guys. I added a Physics manager class.
Here's the code:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using HMEngine.HMObjects;
using XnaDevRu.BulletX;
using XnaDevRu.BulletX.Dynamics;
namespace HMEngine.HMPhysics
{
public class HMPhysicsManager
{
protected DiscreteDynamicsWorld world;
protected CollisionDispatcher dispatcher;
protected OverlappingPairCache pairCache;
protected SequentialImpulseConstraintSolver solver;
protected Vector3 gravity;
protected CollisionShape groundPlane;
public Matrix groundTransformationMatrix;
public List<RigidBody> bodies = new List<RigidBody>();
//constructor
public HMPhysicsManager()
{
dispatcher = new CollisionDispatcher();
dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Cylinder, new SphereTriangleCollisionAlgorithm.CreateFunc());
dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Sphere, new SphereSphereCollisionAlgorithm.CreateFunc());
dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.Sphere, BroadphaseNativeTypes.Box, new SphereBoxCollisionAlgorithm.CreateFunc());
dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeTypes.StaticPlane, BroadphaseNativeTypes.Sphere, new SphereTriangleCollisionAlgorithm.CreateFunc());
solver = new SequentialImpulseConstraintSolver();
pairCache = new SimpleBroadphase();
gravity = new Vector3(0, -9.8f, 0);
world = new DiscreteDynamicsWorld(dispatcher, pairCache,solver);
world.Gravity = gravity;
//groundPlane = new BoxShape(new Vector3(50, 50, 1));
groundTransformationMatrix = Matrix.Identity;
groundTransformationMatrix.Translation = new Vector3(0, 0, 0);
//bodies.Add(localCreateRigidBody(0,groundTransformationMatrix,groundPlane));
}
//funcs
private RigidBody localCreateRigidBody(float mass, Matrix trans, CollisionShape shape)
{
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0);
Vector3 localInertia = new Vector3();
if (isDynamic)
shape.CalculateLocalInertia(mass, out localInertia);
DefaultMotionState ms = new DefaultMotionState(trans, Matrix.Identity);
RigidBody body = new RigidBody(mass, ms, shape, localInertia, 0, 0, 0.5f, 0);
World.AddRigidBody(body);
return body;
}
//public propeties
public Matrix Transform
{
set { groundTransformationMatrix = value; }
get { return groundTransformationMatrix; }
}
public DiscreteDynamicsWorld World
{
set { world = value; }
get { return world; }
}
public Vector3 Gravity
{
set { World.Gravity = gravity = value; }
get { return gravity; }
}
}
}
Now I wonder: each model I want to simulate physics on should have it's own CollisionShape attached to it some how, no? Even if it's invisible...
should I put this CollisionShape, as a variable, in the HMModel class? It shouldn't be constant, right?
or maybe some sort of IHMPhysics interface for the HMModel class?
or maybe even a PhysicsModel class that inherits from HMModel class?
I'm in doubt as to where to put this CollisionShape so I can make a RigidBody with it... AND keep it attached to the model.
any help in this matter would be appreciated!

I just love this engine and woujld like to expand it.
