3. Hello Physics World !

This is the 3rd ODE (Open Dynamics Engine) tutorial.
ODE is simulation using this to the basic flow associated with the API. The sample program as the most simple physical simulation covered by falling objects. Programming is a textbook example is the first Hello World example to show the need. This is the physical simulation version of Hello World.
ODE simulations of a typical flow associated with the API enumeration.
- Simulation flow
- Drawing setting function Drawstuff
- Setup a cameras dsSetViewPoint ()
- Setup a cameras dsSetViewPoint ()
- Initialize ODE dInitODE ()
- Create a world for dynamics dWorldCreate ()
- Set gravity dWorldSetGravity ()
- Create a rigid body
- Set mass dBodySetMass ()
- Set position dBodySetPosition ()
- Set orientation dBodySetRotation ()
- Set mass dBodySetMass ()
- Simulation Loop
- Step a simulation world dWorldStep ()
- Write simulation codes
- Destroy the world dWorldDestroy ()
- Close ODE dCloseODE ()
- Drawing setting function Drawstuff
Then, with the source code to comment further below. , please read the main function.
// Sample3 by Kosei Demura
#include "ode/ode.h"
#include "drawstuff/drawstuff.h"
#ifdef dDOUBLE
#define dsDrawSphere dsDrawSphereD
#endif
static dWorldID world; // a physics world
dBodyID ball; // an ball
const dReal radius = 0.2, mass = 1.0; // radius(m)、weight(kg) of an ball
// Simulation loop
static void simLoop (int pause)
{
const dReal *pos,*R; // position, ratation matrix
dWorldStep(world,0.05); // Step a simulation world, time step is 0.05 [s]
dsSetColor(1.0,0.0,0.0); // Set color (red, green, blue) value is from 0 to 1.0
pos = dBodyGetPosition(ball); // Get a position
R = dBodyGetRotation(ball); // Get a ratation
dsDrawSphere(pos,R,radius); // Draw a sphere
}
// Start function void start()
{
// Set a camera
static float xyz[3] = {0.0,-3.0,1.0}; // View position (x, y, z [m])
static float hpr[3] = {90.0,0.0,0.0}; // View direction (head, pitch, roll[°])
dsSetViewpoint (xyz,hpr); // Set a view point
}
// main function
int main (int argc, char **argv)
{
dReal x0 = 0.0, y0 = 0.0, dReal z0 = 1.0; // initial position of an ball[m]
dMass m1; // mass parameter
// for drawstuff
dsFunctions fn; // drawstuff structure
fn.version = DS_VERSION; // the version of the drawstuff
fn.start = &start; // start function
fn.step = &simLoop; // step function
fn.command = NULL; // no command function for keyboard
fn.stop = NULL; // no stop function
fn.path_to_textures = "../../drawstuff/textures"; // path to the texture
dInitODE(); // Initialize ODE
world = dWorldCreate(); // Create a dynamic world
dWorldSetGravity(world,0,0,-0.001); // Set gravity(x, y, z)
// Create a ball
ball = dBodyCreate(world); // Crate a rigid body
dMassSetZero(&m1); // Initialize mass parameters
dMassSetSphereTotal(&m1,mass,radius); // Calcurate a mass parameter
dBodySetMass(ball,&m1); // Set a mass parameter
dBodySetPosition(ball, x0, y0, z0); // Set a position(x, y, z)
// Simulation loop
// argc, argv are argument of main function. Window size is 352 x 288 [pixel]
// fn is a structure of drawstuff
dsSimulationLoop (argc,argv,352,288,&fn);
dWorldDestroy (world); // Destroy a world
dCloseODE(); // Close ODE
return 0;
}
This is a red ball of free-fall program. ODE is simulation of the flow, first, dInitODE () to initialize ODE. Next, calculate the physical world (World) dWorldCreate () to create.Calculated by the physical object to make. ODE, a body that body (the body) and controversy. They dBodyCreate (world) and got it. objects, then its mass and position and attitude to set parameters. This program is the only sphere of mass and position to set the parameters.
Generated objects and settings are finished, the simulation process. This is dsSimulationLoop (), by calling simLoop function is running. simLoop function dWorldStep (world, 0.05) is a single step process simulation. Time advance to the second argument, this case is 0.05 seconds. dsDrawSphere () in the fall to see the ball.
At the end of the simulation, tidying up. dWorldDestroy (world) and destroy the world, dCloseODE () ODE in the final processing.
The function will begin with a lowercase d ODE’s API (application program interface) and, ds, starting with the function drawstuff API. ODE drawstuff came with the test program library for display.
You can download sample1-110105.zip from here, build and run! Visual C 2008 and the Code:: Blocks 8.02 for the project files are included.
This is the acceleration of gravity red ball drop slowly in the future, through what the ground lost. In fact, on the collision-detection program is not included.
Next time, we learn how to use drawstuff which is a simple 3 D graphics library based OpenGL attach to ODE.
API
- dBodyID dBodyCreate (dWorldID world)
Rigid and make that return as the ID number Returns - const dReal * dBodyGetPosition (dBodyID body)
Rigid body’s center of gravity position to obtain the return pointer - const dReal * dBodyGetRotation (dBodyID body)
The rotation matrix pointer to get the returns - void dBodySetMass (dBodyID body, const dMass * mass)
Rigid body mass to set the parameters for mass - void dBodySetPosition (dBodyID body, dReal x, dReal y, dReal z)
Rigid body’s center of gravity to the absolute coordinate system (x, y, z) to set up the position - void dBodySetRotation (dBodyID body, const dMatrix3 R)
Rigid body to rotate the position to represent the procession set up R - dGeomID dCreateBox (dSpaceID space, dReal lx, dReal ly, dReal lz)
The length of each side space (lx, ly, lz) to generate the rectangular solid geometry. - dGeomID dCreateCylinder (dSpaceID space, dReal r, dReal l)
Radius r, l length of the cylindrical geometry to generate the returns that ID number - dGeomID dCreatePlane (dSpaceID space, dReal a, dReal b, dReal c, dReal d)
space to ax by cz = d plane geometry to generate - void dGeomSetBody (dGeomID geom, dBodyID body)
Two attributes of the object geometry and rigid body associate geom - dSpaceID dHashSpaceCreate (0)
Space collision calculation to generate the returns that ID number - void dJointGroupEmpty (dJointGroupID)
Contact points are stored in the empty a joint group - void dJointAttach (dJointID joint, dBodyID body1, dBodyID dBody2)
Body1 joint rigid joints and to enlist body2 - dJointGroupID dJointGroupCreate (0)
Contact group to store a joint group to generate the returns that ID number - void dMassSetBoxTotal (dMass * mass, dReal m, dReal lx, dReal ly, dReal lz)
Size (lx, ly, lz), total quality m rectangular solid mass of parameters to calculate the mass - void dMassSetCylinderTotal (dMass * mass, dReal m, int dir, dReal r, dReal l)
Direction dir (1 = x, 2 = y, 3 = z), radius r, the length of l, m total mass of the cylinder to calculate the mass of mass parameter - void dMassSetZero (dMass * mass)
0 mass parameter mass elements to initialize - void dRFromAxisAndAngle (dMatrix3 R, dReal ax, dReal ay, dReal az, dReal angle) axial vector (ax, ay, az) around the angle [rad] in the rotation when the rotation matrix R seek
- void dSpaceCollide (dSpaceID space, void * data, dNearCallback * callback)
in the space of a potential collision geometry of a two out, calling the argument callback function. data callback function is to pass a pointer to the data - dWorldID dWorldCreate ()
World physics calculation produces the ID (identification) number to return Returns - void dWorldDestroy (dWorldID world)
The physical world to destroy the World calculation - void dWorldSetGravity (dWorldID world, dReal x, dReal y, dReal z)
World to world (x, y, z) [m/s2] to set the acceleration of gravity - void dWorldStep (dWorldID world, dReal step_size)
World simulation world in a single step forward. The second argument is integral step step_size time [s] - void dsDrawBoxD (const double pos [3], const double R [12], const double sides [3]) pos-rotated position to take a matrix R sides of the rectangular solid size to draw
- void dsSetViewPoint (float xyz [3], float hpr [3])
Xyz perspective and stares hpr [°] to set up - void dsSimulationLoop (int argc, char ** argv, int w, int h, dsfunction * fn)
Calling loop simulation. 1 to 2 of the second argument is the main function of the argument, the second third and fourth the size of the drawing window (next w, vertical h), fifth class argument is drawing functions