diff options
Diffstat (limited to 'libraries/ode-0.9/ode/demo/demo_step.cpp')
-rw-r--r-- | libraries/ode-0.9/ode/demo/demo_step.cpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/libraries/ode-0.9/ode/demo/demo_step.cpp b/libraries/ode-0.9/ode/demo/demo_step.cpp new file mode 100644 index 0000000..df2385c --- /dev/null +++ b/libraries/ode-0.9/ode/demo/demo_step.cpp | |||
@@ -0,0 +1,193 @@ | |||
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 | *************************************************************************/ | ||
22 | |||
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. | ||
26 | // | ||
27 | // @@@ report MAX error | ||
28 | |||
29 | #include <time.h> | ||
30 | #include <ode/ode.h> | ||
31 | #include <drawstuff/drawstuff.h> | ||
32 | |||
33 | #ifdef _MSC_VER | ||
34 | #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints | ||
35 | #endif | ||
36 | |||
37 | // select correct drawing functions | ||
38 | |||
39 | #ifdef dDOUBLE | ||
40 | #define dsDrawBox dsDrawBoxD | ||
41 | #define dsDrawSphere dsDrawSphereD | ||
42 | #define dsDrawCylinder dsDrawCylinderD | ||
43 | #define dsDrawCapsule dsDrawCapsuleD | ||
44 | #endif | ||
45 | |||
46 | |||
47 | // some constants | ||
48 | |||
49 | #define NUM 10 // number of bodies | ||
50 | #define NUMJ 9 // number of joints | ||
51 | #define SIDE (0.2) // side length of a box | ||
52 | #define MASS (1.0) // mass of a box | ||
53 | #define RADIUS (0.1732f) // sphere radius | ||
54 | |||
55 | |||
56 | |||
57 | // dynamics and collision objects | ||
58 | |||
59 | static dWorldID world=0; | ||
60 | static dBodyID body[NUM]; | ||
61 | static dJointID joint[NUMJ]; | ||
62 | |||
63 | |||
64 | // create the test system | ||
65 | |||
66 | void createTest() | ||
67 | { | ||
68 | int i,j; | ||
69 | if (world) dWorldDestroy (world); | ||
70 | |||
71 | world = dWorldCreate(); | ||
72 | |||
73 | // create random bodies | ||
74 | for (i=0; i<NUM; i++) { | ||
75 | // create bodies at random position and orientation | ||
76 | body[i] = dBodyCreate (world); | ||
77 | dBodySetPosition (body[i],dRandReal()*2-1,dRandReal()*2-1, | ||
78 | dRandReal()*2+RADIUS); | ||
79 | dReal q[4]; | ||
80 | for (j=0; j<4; j++) q[j] = dRandReal()*2-1; | ||
81 | dBodySetQuaternion (body[i],q); | ||
82 | |||
83 | // set random velocity | ||
84 | dBodySetLinearVel (body[i], dRandReal()*2-1,dRandReal()*2-1, | ||
85 | dRandReal()*2-1); | ||
86 | dBodySetAngularVel (body[i], dRandReal()*2-1,dRandReal()*2-1, | ||
87 | dRandReal()*2-1); | ||
88 | |||
89 | // set random mass (random diagonal mass rotated by a random amount) | ||
90 | dMass m; | ||
91 | dMatrix3 R; | ||
92 | dMassSetBox (&m,1,dRandReal()+0.1,dRandReal()+0.1,dRandReal()+0.1); | ||
93 | dMassAdjust (&m,dRandReal()+1); | ||
94 | for (j=0; j<4; j++) q[j] = dRandReal()*2-1; | ||
95 | dQtoR (q,R); | ||
96 | dMassRotate (&m,R); | ||
97 | dBodySetMass (body[i],&m); | ||
98 | } | ||
99 | |||
100 | // create ball-n-socket joints at random positions, linking random bodies | ||
101 | // (but make sure not to link the same pair of bodies twice) | ||
102 | char linked[NUM*NUM]; | ||
103 | for (i=0; i<NUM*NUM; i++) linked[i] = 0; | ||
104 | for (i=0; i<NUMJ; i++) { | ||
105 | int b1,b2; | ||
106 | do { | ||
107 | b1 = dRandInt (NUM); | ||
108 | b2 = dRandInt (NUM); | ||
109 | } while (linked[b1*NUM + b2] || b1==b2); | ||
110 | linked[b1*NUM + b2] = 1; | ||
111 | linked[b2*NUM + b1] = 1; | ||
112 | joint[i] = dJointCreateBall (world,0); | ||
113 | dJointAttach (joint[i],body[b1],body[b2]); | ||
114 | dJointSetBallAnchor (joint[i],dRandReal()*2-1, | ||
115 | dRandReal()*2-1,dRandReal()*2+RADIUS); | ||
116 | } | ||
117 | |||
118 | for (i=0; i<NUM; i++) { | ||
119 | // move bodies a bit to get some joint error | ||
120 | const dReal *pos = dBodyGetPosition (body[i]); | ||
121 | dBodySetPosition (body[i],pos[0]+dRandReal()*0.2-0.1, | ||
122 | pos[1]+dRandReal()*0.2-0.1,pos[2]+dRandReal()*0.2-0.1); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | |||
127 | // start simulation - set viewpoint | ||
128 | |||
129 | static void start() | ||
130 | { | ||
131 | static float xyz[3] = {2.6117f,-1.4433f,2.3700f}; | ||
132 | static float hpr[3] = {151.5000f,-30.5000f,0.0000f}; | ||
133 | dsSetViewpoint (xyz,hpr); | ||
134 | } | ||
135 | |||
136 | |||
137 | // simulation loop | ||
138 | |||
139 | static void simLoop (int pause) | ||
140 | { | ||
141 | if (!pause) { | ||
142 | // add random forces and torques to all bodies | ||
143 | int i; | ||
144 | const dReal scale1 = 5; | ||
145 | const dReal scale2 = 5; | ||
146 | for (i=0; i<NUM; i++) { | ||
147 | dBodyAddForce (body[i], | ||
148 | scale1*(dRandReal()*2-1), | ||
149 | scale1*(dRandReal()*2-1), | ||
150 | scale1*(dRandReal()*2-1)); | ||
151 | dBodyAddTorque (body[i], | ||
152 | scale2*(dRandReal()*2-1), | ||
153 | scale2*(dRandReal()*2-1), | ||
154 | scale2*(dRandReal()*2-1)); | ||
155 | } | ||
156 | |||
157 | dWorldStep (world,0.05); | ||
158 | createTest(); | ||
159 | } | ||
160 | |||
161 | // float sides[3] = {SIDE,SIDE,SIDE}; | ||
162 | dsSetColor (1,1,0); | ||
163 | for (int i=0; i<NUM; i++) | ||
164 | dsDrawSphere (dBodyGetPosition(body[i]), dBodyGetRotation(body[i]),RADIUS); | ||
165 | } | ||
166 | |||
167 | |||
168 | int main (int argc, char **argv) | ||
169 | { | ||
170 | // setup pointers to drawstuff callback functions | ||
171 | dsFunctions fn; | ||
172 | fn.version = DS_VERSION; | ||
173 | fn.start = &start; | ||
174 | fn.step = &simLoop; | ||
175 | fn.command = 0; | ||
176 | fn.stop = 0; | ||
177 | fn.path_to_textures = "../../drawstuff/textures"; | ||
178 | if(argc==2) | ||
179 | { | ||
180 | fn.path_to_textures = argv[1]; | ||
181 | } | ||
182 | |||
183 | dInitODE(); | ||
184 | dRandSetSeed (time(0)); | ||
185 | createTest(); | ||
186 | |||
187 | // run simulation | ||
188 | dsSimulationLoop (argc,argv,352,288,&fn); | ||
189 | |||
190 | dWorldDestroy (world); | ||
191 | dCloseODE(); | ||
192 | return 0; | ||
193 | } | ||