1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
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 *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
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. *
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
31 #include <drawstuff/drawstuff.h>
32 #include "texturepath.h"
35 #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
38 // select correct drawing functions
41 #define dsDrawBox dsDrawBoxD
42 #define dsDrawSphere dsDrawSphereD
43 #define dsDrawCylinder dsDrawCylinderD
44 #define dsDrawCapsule dsDrawCapsuleD
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
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
);
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,
87 dBodySetAngularVel (body
[i
], dRandReal()*2-1,dRandReal()*2-1,
90 // set random mass (random diagonal mass rotated by a random amount)
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;
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
++) {
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
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
);
142 static void simLoop (int pause
)
145 // add random forces and torques to all bodies
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);
164 // float sides[3] = {SIDE,SIDE,SIDE};
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
175 fn
.version
= DS_VERSION
;
180 fn
.path_to_textures
= DRAWSTUFF_TEXTURE_PATH
;
183 dRandSetSeed (time(0));
187 dsSimulationLoop (argc
, argv
, DS_SIMULATION_DEFAULT_WIDTH
, DS_SIMULATION_DEFAULT_HEIGHT
, &fn
);
189 dWorldDestroy (world
);