Tutorial 1: The Simulator

 

 

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 OPAL, you need to create a Simulator.

 

To create a Simulator, do the following:

 

 

opal::Simulator* sim = opal::createSimulator();

 

 

 

 

 

This allocates and returns a pointer to a Simulator.  You will want to maintain a pointer to the 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:

 

 

opal::Vec3r g(0, -9.81, 0);

sim->setGravity(g);

sim->setStepSize(0.02);

 

 

 

 

 

 

 

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 OPAL 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:

 

 

void mySimulationLoop()

{

    while(1)

    {

        opal::real dt = appGetElapsedTime();

        updateUserInput();

        sim->simulate(dt);

        redraw();

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

The “simulate” function internally breaks the dt into smaller pieces (the size of the step size) and iteratively simulates everything using this constant step size.  “Leftover” dt (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 dt intelligently.

 

When you are finished, be sure to destroy the Simulator:

 

 

sim->destroy();

 

 

 

 

 

This will deallocate the Simulator and everything contained within it.

 

OPAL is Copyright © 2004-2005 Alan Fischer, Andres Reinot, and Tyler Streeter