Add a TriMesh to TriMesh collision demo.
[ode.git] / ode / demo / demo_step.cpp
blob841b1777cbf7f478cbdd6b34faea69b1293e46a0
1 /*************************************************************************
2 * *
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of EITHER: *
8 * (1) The GNU Lesser General Public License as published by the Free *
9 * Software Foundation; either version 2.1 of the License, or (at *
10 * your option) any later version. The text of the GNU Lesser *
11 * General Public License is included with this library in the *
12 * file LICENSE.TXT. *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
15 * *
16 * This library is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
19 * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
20 * *
21 *************************************************************************/
23 // test the step function by comparing the output of the fast and the slow
24 // version, for various systems. currently you have to define COMPARE_METHODS
25 // in step.cpp for this to work properly.
27 // @@@ report MAX error
29 #include <time.h>
30 #include <ode/ode.h>
31 #include <drawstuff/drawstuff.h>
32 #include "texturepath.h"
34 #ifdef _MSC_VER
35 #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
36 #endif
38 // select correct drawing functions
40 #ifdef dDOUBLE
41 #define dsDrawBox dsDrawBoxD
42 #define dsDrawSphere dsDrawSphereD
43 #define dsDrawCylinder dsDrawCylinderD
44 #define dsDrawCapsule dsDrawCapsuleD
45 #endif
48 // some constants
50 #define NUM 10 // number of bodies
51 #define NUMJ 9 // number of joints
52 #define SIDE (0.2) // side length of a box
53 #define MASS (1.0) // mass of a box
54 #define RADIUS (0.1732f) // sphere radius
58 // dynamics and collision objects
60 static dWorldID world=0;
61 static dBodyID body[NUM];
62 static dJointID joint[NUMJ];
65 // create the test system
67 void createTest()
69 int i,j;
70 if (world) dWorldDestroy (world);
72 world = dWorldCreate();
74 // create random bodies
75 for (i=0; i<NUM; i++) {
76 // create bodies at random position and orientation
77 body[i] = dBodyCreate (world);
78 dBodySetPosition (body[i],dRandReal()*2-1,dRandReal()*2-1,
79 dRandReal()*2+RADIUS);
80 dReal q[4];
81 for (j=0; j<4; j++) q[j] = dRandReal()*2-1;
82 dBodySetQuaternion (body[i],q);
84 // set random velocity
85 dBodySetLinearVel (body[i], dRandReal()*2-1,dRandReal()*2-1,
86 dRandReal()*2-1);
87 dBodySetAngularVel (body[i], dRandReal()*2-1,dRandReal()*2-1,
88 dRandReal()*2-1);
90 // set random mass (random diagonal mass rotated by a random amount)
91 dMass m;
92 dMatrix3 R;
93 dMassSetBox (&m,1,dRandReal()+0.1,dRandReal()+0.1,dRandReal()+0.1);
94 dMassAdjust (&m,dRandReal()+1);
95 for (j=0; j<4; j++) q[j] = dRandReal()*2-1;
96 dQtoR (q,R);
97 dMassRotate (&m,R);
98 dBodySetMass (body[i],&m);
101 // create ball-n-socket joints at random positions, linking random bodies
102 // (but make sure not to link the same pair of bodies twice)
103 char linked[NUM*NUM];
104 for (i=0; i<NUM*NUM; i++) linked[i] = 0;
105 for (i=0; i<NUMJ; i++) {
106 int b1,b2;
107 do {
108 b1 = dRandInt (NUM);
109 b2 = dRandInt (NUM);
110 } while (linked[b1*NUM + b2] || b1==b2);
111 linked[b1*NUM + b2] = 1;
112 linked[b2*NUM + b1] = 1;
113 joint[i] = dJointCreateBall (world,0);
114 dJointAttach (joint[i],body[b1],body[b2]);
115 dJointSetBallAnchor (joint[i],dRandReal()*2-1,
116 dRandReal()*2-1,dRandReal()*2+RADIUS);
119 for (i=0; i<NUM; i++) {
120 // move bodies a bit to get some joint error
121 const dReal *pos = dBodyGetPosition (body[i]);
122 dBodySetPosition (body[i],pos[0]+dRandReal()*0.2-0.1,
123 pos[1]+dRandReal()*0.2-0.1,pos[2]+dRandReal()*0.2-0.1);
128 // start simulation - set viewpoint
130 static void start()
132 dAllocateODEDataForThread(dAllocateMaskAll);
134 float xyz[3] = {2.6117f,-1.4433f,2.3700f};
135 float hpr[3] = {151.5000f,-25.5000f,0.0000f};
136 dsSetViewpoint (xyz,hpr);
140 // simulation loop
142 static void simLoop (int pause)
144 if (!pause) {
145 // add random forces and torques to all bodies
146 int i;
147 const dReal scale1 = 5;
148 const dReal scale2 = 5;
149 for (i=0; i<NUM; i++) {
150 dBodyAddForce (body[i],
151 scale1*(dRandReal()*2-1),
152 scale1*(dRandReal()*2-1),
153 scale1*(dRandReal()*2-1));
154 dBodyAddTorque (body[i],
155 scale2*(dRandReal()*2-1),
156 scale2*(dRandReal()*2-1),
157 scale2*(dRandReal()*2-1));
160 dWorldStep (world,0.05);
161 createTest();
164 // float sides[3] = {SIDE,SIDE,SIDE};
165 dsSetColor (1,1,0);
166 for (int i=0; i<NUM; i++)
167 dsDrawSphere (dBodyGetPosition(body[i]), dBodyGetRotation(body[i]),RADIUS);
171 int main (int argc, char **argv)
173 // setup pointers to drawstuff callback functions
174 dsFunctions fn;
175 fn.version = DS_VERSION;
176 fn.start = &start;
177 fn.step = &simLoop;
178 fn.command = 0;
179 fn.stop = 0;
180 fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
182 dInitODE2(0);
183 dRandSetSeed (time(0));
184 createTest();
186 // run simulation
187 dsSimulationLoop (argc, argv, DS_SIMULATION_DEFAULT_WIDTH, DS_SIMULATION_DEFAULT_HEIGHT, &fn);
189 dWorldDestroy (world);
190 dCloseODE();
191 return 0;