public Simulation Create(PhysicsProcessor sceneProcessor, PhysicsEngineFlags flags = PhysicsEngineFlags.None) { var scene = new PhysicsScene { Processor = sceneProcessor, Simulation = new Simulation(sceneProcessor, flags) }; lock (this) { scenes.Add(scene); } return(scene.Simulation); }
public void Release(PhysicsProcessor processor) { lock (this) { var scene = scenes.SingleOrDefault(x => x.Processor == processor); if (scene == null) { return; } scenes.Remove(scene); scene.Simulation.Dispose(); } }
/// <summary> /// Initializes the Physics engine using the specified flags. /// </summary> /// <param name="processor"></param> /// <param name="flags">The flags.</param> /// <exception cref="System.NotImplementedException">SoftBody processing is not yet available</exception> internal Simulation(PhysicsProcessor processor, PhysicsEngineFlags flags = PhysicsEngineFlags.None) { this.processor = processor; if (flags == PhysicsEngineFlags.None) { if (OnSimulationCreation != null) { flags = OnSimulationCreation(); } } MaxSubSteps = 1; FixedTimeStep = 1.0f / 60.0f; collisionConfiguration = new BulletSharp.DefaultCollisionConfiguration(); dispatcher = new BulletSharp.CollisionDispatcher(collisionConfiguration); broadphase = new BulletSharp.DbvtBroadphase(); //this allows characters to have proper physics behavior broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new BulletSharp.GhostPairCallback()); //2D pipeline var simplex = new BulletSharp.VoronoiSimplexSolver(); var pdSolver = new BulletSharp.MinkowskiPenetrationDepthSolver(); var convexAlgo = new BulletSharp.Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver); dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Convex2DShape, BulletSharp.BroadphaseNativeType.Convex2DShape, convexAlgo); //this is the ONLY one that we are actually using dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Box2DShape, BulletSharp.BroadphaseNativeType.Convex2DShape, convexAlgo); dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Convex2DShape, BulletSharp.BroadphaseNativeType.Box2DShape, convexAlgo); dispatcher.RegisterCollisionCreateFunc(BulletSharp.BroadphaseNativeType.Box2DShape, BulletSharp.BroadphaseNativeType.Box2DShape, new BulletSharp.Box2DBox2DCollisionAlgorithm.CreateFunc()); //~2D pipeline //default solver var solver = new BulletSharp.SequentialImpulseConstraintSolver(); if (flags.HasFlag(PhysicsEngineFlags.CollisionsOnly)) { collisionWorld = new BulletSharp.CollisionWorld(dispatcher, broadphase, collisionConfiguration); } else if (flags.HasFlag(PhysicsEngineFlags.SoftBodySupport)) { //mSoftRigidDynamicsWorld = new BulletSharp.SoftBody.SoftRigidDynamicsWorld(mDispatcher, mBroadphase, solver, mCollisionConf); //mDiscreteDynamicsWorld = mSoftRigidDynamicsWorld; //mCollisionWorld = mSoftRigidDynamicsWorld; throw new NotImplementedException("SoftBody processing is not yet available"); } else { discreteDynamicsWorld = new BulletSharp.DiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration); collisionWorld = discreteDynamicsWorld; } if (discreteDynamicsWorld != null) { solverInfo = discreteDynamicsWorld.SolverInfo; //we are required to keep this reference, or the GC will mess up dispatchInfo = discreteDynamicsWorld.DispatchInfo; solverInfo.SolverMode |= BulletSharp.SolverModes.CacheFriendly; //todo test if helps with performance or not if (flags.HasFlag(PhysicsEngineFlags.ContinuosCollisionDetection)) { CanCcd = true; solverInfo.SolverMode |= BulletSharp.SolverModes.Use2FrictionDirections | BulletSharp.SolverModes.RandomizeOrder; dispatchInfo.UseContinuous = true; } } }