yake::physics supports any number of physics plugins. The following snippet of code (based on typical Yake demos) loads two physics plugins:
loadPlugin("physicsODE"); loadPlugin("physicsNX");
Now we need to create the physics systems so that we can create the simulations:
IPhysicsSystem* pPhysicsSystemOde = create<IPhysicsSystem>("ode"); // ODE based IPhysicsSystem* pPhysicsSystemNx = create<IPhysicsSystem>("nx"); // Novodex based
Now we're ready to create worlds!
A physics::IWorld object encapsulates a simulation. It's also a factory for a variety of objects (actors, shapes, joints, motors, affectors …).
Let's create a world:
SharedPtr<IWorld> pWorld = pPhysicsSystem->createWorld();
You can create any number of worlds (aka simulations).
Typically you want to set the global gravity for your world.
pWorld->setGlobalGravity( Vector3(0,-9.81,0) );
NB: For this tutorial we assume Y is the up vector in the world. NB: It is possible to add local gravity zones using affectors.
Different physics systems ship with different solvers. Different solvers often have different settings. Let's enumerate the solvers, choose one, and set a property for it (the step size).
// get all possible solvers StringVector solvers = pWorld->getSupportedSolvers(); assert( !solvers.empty() ); // select the first one in the list pWorld->useSolver( solvers.front() ); // get all possible properties for the current solver PropertyNameList params = pWorld->getCurrentSolverParams(); // set stepping to 50 Hz if (contains(params,"stepsize")) pWorld->setCurrentSolverParam("stepsize", "0.02");
First, the code:
pWorld->step( 0.1 ); // pass elapsed time in seconds
Generally, world implementations are free to decide to run the simulation in one step, or a hundred. You don't know. But we set the solver's “stepsize” to 0.02 seconds so we can safely assume that the world will internally be stepped 5 times.
Now you're ready to fill the world with objects.