Exemple #1
0
        /// <summary>
        /// Manages the switch to a new physics engine simulation.
        /// </summary>
        /// <param name="sim">Index of the simulation to switch to.</param>
        public void SwitchSimulation(int sim)
        {
            currentSimulationIndex = sim;

            //Clear out any old rendering stuff.
            ModelDrawer.Clear();
            ConstraintDrawer.Clear();

            //Tell the previous simulation it's done.
            if (currentSimulation != null)
            {
                currentSimulation.CleanUp();
            }
            //Create the new demo.
            Type demoType = demoTypes[currentSimulationIndex - 1];

#if !WINDOWS
            currentSimulation = (Demo)demoType.GetConstructor(new[] { typeof(DemosGame) }).Invoke(new object[] { this });
#else
            currentSimulation = (Demo)Activator.CreateInstance(demoType, new object[] { this });
#endif
            #region DisplayObject creation

            foreach (Entity e in currentSimulation.Space.Entities)
            {
                if ((string)e.Tag != "noDisplayObject")
                {
                    ModelDrawer.Add(e);
                }
                else //Remove the now unnecessary tag.
                {
                    e.Tag = null;
                }
            }
            for (int i = 0; i < currentSimulation.Space.Solver.SolverUpdateables.Count; i++)
            {
                //Add the solver updateable and match up the activity setting.
                LineDisplayObjectBase objectAdded = ConstraintDrawer.Add(currentSimulation.Space.Solver.SolverUpdateables[i]);
                if (objectAdded != null)
                {
                    objectAdded.IsDrawing = currentSimulation.Space.Solver.SolverUpdateables[i].IsActive;
                }
            }

            #endregion

            GC.Collect();
        }
Exemple #2
0
        public StandardDemo(DemosGame game)
            : base(game)
        {
            freeCameraControlScheme = new FreeCameraControlScheme(10, game.Camera, game);

            //Creates the player character (C).
            character = new CharacterControllerInput(Space, game.Camera, game);

            //Creates the drivable vehicle (V).
            var wheelModel   = game.Content.Load <Model>("carWheel");
            var wheelTexture = game.Content.Load <Texture2D>("wheel");

            whitePixel = game.Content.Load <Texture2D>("whitePixel");
            vehicle    = new VehicleInput(new Vector3(10000, 0, 0), Space, game.Camera, game, game.ModelDrawer, wheelModel, wheelTexture);
            Space.ForceUpdater.Gravity = new Vector3(0, (Fix64)(-9.81m), 0); //If left unset, the default value is (0,0,0).

            //Create the tossable ball.
            kapow      = new Sphere(new Vector3(11000, 0, 0), (Fix64).6m, 20);
            kapowMaker = new Explosion(Vector3.Zero, 400, 15, Space);
            //Create the right-click grab spring.
            grabber                  = new MotorizedGrabSpring();
            grabberGraphic           = game.ConstraintDrawer.Add(grabber);
            grabberGraphic.IsDrawing = false;
            Space.Add(grabber);
            Space.Add(kapow);


            //IMPORTANT PERFORMANCE NOTE:
            //  BEPUphysics uses an iterative system to solve constraints.  You can tell it to do more or less iterations.
            //  Less iterations is faster; more iterations makes the result more accurate.
            //
            //  The amount of iterations needed for a simulation varies.  The "Wall" and "Pyramid" simulations are each fairly
            //  solver intensive, but as few as 4 iterations can be used with acceptable results.
            //  The "Jenga" simulation usually needs a few more iterations for stability; 7-9 is a good minimum.
            //
            //  The Dogbot demo shows how accuracy can smoothly increase with more iterations.
            //  With very few iterations (1-3), it has slightly jaggier movement, as if the parts used to construct it were a little cheap.
            //  As you give it a few more iterations, the motors and constraints get more and more robust.
            //
            //  Many simulations can work perfectly fine with very few iterations,
            //  and using a low number of iterations can substantially improve performance.
            //
            //  To change the number of iterations used, uncomment and change the following line (10 iterations is the default):

            //Space.Solver.IterationLimit = 10;

            rayCastFilter = RayCastFilter;
        }