Simulators are factories that create, maintain, and destroy most other objects. They encapsulate all collision detection and physics simulation. Before doing really anything else with XPA, you need to create a Simulator.
To create a Simulator, do the following:
Simulator.CreateMethod = XnaDev.Physics.Ode.OdeSimulator.CreateOdeSimulator;
sim = Simulator.Create();
This creates an ODE Simulator. You will want to maintain a Simulator until you are totally done with the simulation. Note that it is possible to use multiple Simulators concurrently, each representing completely different simulations.
Simulators have a few changeable parameters. Some of the important ones are gravity and simulation step size:
sim.Gravity = new Vector3(0, -9, 0);
sim.StepSize = 0.02f;
A step size of 0.02 means that the simulation will be updated at a rate of 50 Hz. Smaller step sizes yield more accurate simulations that take longer to compute. If you are using XPA for some interactive real-time simulation, you will probably be redrawing
objects in your simulation regularly. Before you redraw, tell the Simulator to simulate things ahead for the amount of time that has elapsed since the previous frame. Here is an example of a typical simulation loop:
protected override void Update()
// The time since Update was called last
float elapsed = (float)ElapsedTime.TotalSeconds;
// TODO: Add your game logic here
The “Simulate” function internally breaks the elapsed into smaller pieces (the size of the step size) and iteratively simulates everything using this constant step size. “Leftover” elapsed (smaller than the step size) is stored until the next call to simulate.
This scheme ensures consistent results across different speeds of computers; you simply tell the Simulator how much time to simulate and it internally handles the elapsed intelligently.
When you are finished, be sure to destroy the Simulator:
This will deallocate the Simulator and everything contained within it.