/// <summary> /// Adds the object to the rendering system. /// </summary> /// <param name="o">Object to add.</param> /// <returns>Display object created from the object.</returns> public LineDisplayObjectBase Add(object o) { //Create the correct kind of display object for this line drawer. Type displayType; if (displayTypes.TryGetValue(o.GetType(), out displayType)) { #if !WINDOWS LineDisplayObjectBase objectAdded = (LineDisplayObjectBase)displayType.GetConstructor( new Type[] { o.GetType(), typeof(LineDrawer) }) .Invoke(new object[] { o, this }); #else var objectAdded = (LineDisplayObjectBase)Activator.CreateInstance(displayType, new[] { o, this }); #endif displayMapping.Add(o, objectAdded); return(objectAdded); } if (o is SolverGroup) { //Solver groups are special. If no special type-specific display object //has been registered for a solver group, add every child individually. foreach (SolverUpdateable item in (o as SolverGroup).SolverUpdateables) { LineDisplayObjectBase objectAdded = Add(item); if (objectAdded != null) { objectAdded.IsDrawing = item.IsActiveInSolver; //Match the initial activity of the item. } } return(null); } return(null); //Don't have this type registered; don't know what to do with it. }
protected override void LoadContent() { dataFont = Content.Load <SpriteFont>("DataFont"); tinyFont = Content.Load <SpriteFont>("TinyFont"); controlsMenu = Content.Load <Texture2D>("bepuphysicscontrols"); IsFixedTimeStep = false; LineDrawer = new BasicEffect(GraphicsDevice); UIDrawer = new SpriteBatch(GraphicsDevice); DataTextDrawer = new TextDrawer(UIDrawer, dataFont, Color.White); TinyTextDrawer = new TextDrawer(UIDrawer, tinyFont, Color.White); Mouse.SetPosition(200, 200); ModelDrawer.Clear(); ConstraintDrawer.Clear(); if (ServerSimulation != null) { ServerSimulation.CleanUp(); } Type SimulationType = typeof(WorldSimulator); Globals.world = (WorldSimulator)Activator.CreateInstance(SimulationType, new object[] { this }); ServerSimulation = Globals.world; foreach (Entity e in Globals.space.Entities) { if ((string)e.Tag != "noDisplayObject") { ModelDrawer.Add(e); } else { e.Tag = null; } } for (int i = 0; i < Globals.space.Solver.SolverUpdateables.Count; i++) { //Add the solver updateable and match up the activity setting. LineDisplayObjectBase objectAdded = ConstraintDrawer.Add(Globals.space.Solver.SolverUpdateables[i]); if (objectAdded != null) { objectAdded.IsDrawing = Globals.space.Solver.SolverUpdateables[i].IsActive; } } GC.Collect(); }
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, -9.81f, 0f); //If left unset, the default value is (0,0,0). //Create the tossable ball. kapow = new Sphere(new Vector3(11000, 0, 0), .6f, 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; }
/// <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(); }