public App(int width, int height) { WorldSize = new Vector3(200, 200, 200); lastTime = 0; SceneRoot = new GameObject("SceneRoot", this); Shaders = new Dictionary <string, Shader>(); Materials = new Dictionary <string, Material>(); destroyed = new List <DestructableObject>(); DefaultBlendFactorSrc = BlendingFactorSrc.SrcAlpha; DefaultBlendFactorDest = BlendingFactorDest.OneMinusSrcAlpha; Window = new GameWindow(width, height, new GraphicsMode(32, 24, 0, 0)); Window.Load += OnLoadInternal; Window.Resize += OnResizeInternal; Window.Closing += Window_Closing; Window.UpdateFrame += OnUpdateInternal; Window.RenderFrame += OnRenderInternal; SceneRenderer = new SceneRenderer(this); SetupGL(); var cameraContainer = CreateGameObject("Camera"); ActiveCamera = cameraContainer.AddComponent <Camera>(); stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); time = new System.Diagnostics.Stopwatch(); time.Start(); Shaders.Add("unlit", new Shader("Shaders/unlit.glsl", "vertex", null, "fragment")); Shaders.Add("screen", new Shader("Shaders/screen.glsl", "vertex", null, "fragment")); Shaders.Add("screenCA", new Shader("Shaders/chromaticAbberation.glsl", "vertex", null, "fragment")); Shaders.Add("text", new Shader("Shaders/text.glsl", "vertex", null, "fragment")); Shaders.Add("gol", new Shader("Shaders/gol.glsl", "vertex", null, "fragment")); Shaders.Add("lit", new Shader("Shaders/lit.glsl", "vertex", null, "fragment")); Materials.Add("unlit", new Material(Shaders["unlit"], RenderMode.Opaque)); Materials.Add("lit", new Material(Shaders["lit"], RenderMode.Opaque)); GameObjectFactory = new GameObjectFactory(this); PrimitiveFactory = new PrimitiveFactory(); //var cs = new Jitter.Collision.CollisionSystemPersistentSAP(); //PhysicsWorld = new Jitter.World(cs); //PhysicsWorld.Gravity = new Jitter.LinearMath.JVector(0, -9.81f, 0); var collisionConfig = new BulletSharp.DefaultCollisionConfiguration(); PhysicsWorld = new BulletSharp.DiscreteDynamicsWorld( new BulletSharp.CollisionDispatcher(collisionConfig), new BulletSharp.DbvtBroadphase(), new BulletSharp.SequentialImpulseConstraintSolver(), collisionConfig); Ray.Init(this); Window.Run(60.0); }
/// <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; } } }
/// <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; }