public void InitializeWorld() { mGameObjects = new List <NetGameObject>(); //mWorldMap = new WorldMap(); #if _USE_BEPU_PHYSICS space = new BEPUphysics.Space(); space.ForceUpdater.Gravity = new BEPUutilities.Vector3(0, -9.81f, 0); #elif _USE_BULLET_SHARP Broadphase = new BulletSharp.AxisSweep3(new BulletSharp.Math.Vector3(-1000, -1000, -1000), new BulletSharp.Math.Vector3(1000, 1000, 1000)); var conf = new BulletSharp.DefaultCollisionConfiguration(); var dispatcher = new BulletSharp.CollisionDispatcher(conf); world = new BulletSharp.MultiBodyDynamicsWorld(dispatcher, Broadphase, new BulletSharp.MultiBodyConstraintSolver(), conf); world.Gravity = new BulletSharp.Math.Vector3(0, -9.81f, 0); var box = new BulletSharp.BoxShape(50f, 1f, 50f); //box.Margin = 0f; floor = new BulletSharp.RigidBody(new BulletSharp.RigidBodyConstructionInfo(0f, new BulletSharp.DefaultMotionState(), box, new BulletSharp.Math.Vector3(0, -2f, 0))); floor.CollisionFlags = BulletSharp.CollisionFlags.KinematicObject; BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix(); worldTrans.M41 = 0f; worldTrans.M42 = -2f; worldTrans.M43 = 0; floor.WorldTransform = worldTrans; //floor.Friction = 0.5f; world.AddRigidBody(floor, BulletSharp.CollisionFilterGroups.DefaultFilter, //BulletSharp.CollisionFilterGroups.Everything); (BulletSharp.CollisionFilterGroups.DefaultFilter | BulletSharp.CollisionFilterGroups.StaticFilter | BulletSharp.CollisionFilterGroups.KinematicFilter | BulletSharp.CollisionFilterGroups.DebrisFilter | BulletSharp.CollisionFilterGroups.SensorTrigger | BulletSharp.CollisionFilterGroups.CharacterFilter ) ); #elif _USE_BEPU_PHYSICS_V2 characters = new BepuPhysics.CharacterControllers(BufferPool); Simulation = Simulation.Create(BufferPool, new BepuPhysics.CharacterNarrowphaseCallbacks(characters), new DemoPoseIntegratorCallbacks(new System.Numerics.Vector3(0, -10, 0))); Simulation.Statics.Add(new StaticDescription(new System.Numerics.Vector3(0, 0, 0), new CollidableDescription(Simulation.Shapes.Add(new Box(50, 1, 50)), 0.1f))); #endif }
/// <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; } } }
private void LoadPhysics() { var defaultCollisionConfiguration = new BulletSharp.DefaultCollisionConfiguration(); var collisionDispatcher = new BulletSharp.CollisionDispatcher(); var worldAabbMin = new Vector3(-1000, -1000, -1000); var worldAabbMax = new Vector3(1000, 1000, 1000); var broadphase = new BulletSharp.AxisSweep3(worldAabbMin, worldAabbMax); this.collisionWorld = new BulletSharp.CollisionWorld(collisionDispatcher, broadphase, defaultCollisionConfiguration); this.collisionWorld.DebugDrawer = new BulletDebugDrawer(this.viewscreenProgram); foreach (var collisionObject in mousePole.ContactObjects) { collisionWorld.AddCollisionObject(collisionObject); } }
/// <summary> /// Initializes the Physics engine using the specified flags. /// </summary> /// <param name="flags">The flags.</param> /// <exception cref="System.NotImplementedException">SoftBody processing is not yet available</exception> internal Simulation(PhysicsEngineFlags flags = PhysicsEngineFlags.None) { 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; } } BulletSharp.PersistentManifold.ContactProcessed += PersistentManifoldContactProcessed; BulletSharp.PersistentManifold.ContactDestroyed += PersistentManifoldContactDestroyed; }