aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ode-0.9/ode/demo/demo_basket.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/ode-0.9/ode/demo/demo_basket.cpp')
-rw-r--r--libraries/ode-0.9/ode/demo/demo_basket.cpp278
1 files changed, 278 insertions, 0 deletions
diff --git a/libraries/ode-0.9/ode/demo/demo_basket.cpp b/libraries/ode-0.9/ode/demo/demo_basket.cpp
new file mode 100644
index 0000000..35fb49a
--- /dev/null
+++ b/libraries/ode-0.9/ode/demo/demo_basket.cpp
@@ -0,0 +1,278 @@
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// Basket ball demo.
24// Serves as a test for the sphere vs trimesh collider
25// By Bram Stolk.
26// Press the spacebar to reset the position of the ball.
27
28#include <ode/config.h>
29#include <assert.h>
30#ifdef HAVE_UNISTD_H
31#include <unistd.h>
32#endif
33#include <ode/ode.h>
34#include <drawstuff/drawstuff.h>
35
36#include "basket_geom.h" // this is our world mesh
37
38#ifdef _MSC_VER
39#pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
40#endif
41
42// some constants
43
44#define RADIUS 0.14
45
46// dynamics and collision objects (chassis, 3 wheels, environment)
47
48static dWorldID world;
49static dSpaceID space;
50
51static dBodyID sphbody;
52static dGeomID sphgeom;
53
54static dJointGroupID contactgroup;
55static dGeomID world_mesh;
56
57
58// this is called by dSpaceCollide when two objects in space are
59// potentially colliding.
60
61static void nearCallback (void *data, dGeomID o1, dGeomID o2)
62{
63 assert(o1);
64 assert(o2);
65
66 if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
67 {
68 fprintf(stderr,"testing space %p %p\n", o1,o2);
69 // colliding a space with something
70 dSpaceCollide2(o1,o2,data,&nearCallback);
71 // Note we do not want to test intersections within a space,
72 // only between spaces.
73 return;
74 }
75
76// fprintf(stderr,"testing geoms %p %p\n", o1, o2);
77
78 const int N = 32;
79 dContact contact[N];
80 int n = dCollide (o1,o2,N,&(contact[0].geom),sizeof(dContact));
81 if (n > 0)
82 {
83 for (int i=0; i<n; i++)
84 {
85 // Paranoia <-- not working for some people, temporarily removed for 0.6
86 //dIASSERT(dVALIDVEC3(contact[i].geom.pos));
87 //dIASSERT(dVALIDVEC3(contact[i].geom.normal));
88 //dIASSERT(!dIsNan(contact[i].geom.depth));
89 contact[i].surface.slip1 = 0.7;
90 contact[i].surface.slip2 = 0.7;
91 contact[i].surface.mode = dContactSoftERP | dContactSoftCFM | dContactApprox1 | dContactSlip1 | dContactSlip2;
92 contact[i].surface.mu = 50.0; // was: dInfinity
93 contact[i].surface.soft_erp = 0.96;
94 contact[i].surface.soft_cfm = 0.04;
95 dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
96 dJointAttach (c,
97 dGeomGetBody(contact[i].geom.g1),
98 dGeomGetBody(contact[i].geom.g2));
99 }
100 }
101}
102
103
104// start simulation - set viewpoint
105
106static void start()
107{
108 static float xyz[3] = {-8,0,5};
109 static float hpr[3] = {0.0f,-29.5000f,0.0000f};
110 dsSetViewpoint (xyz,hpr);
111}
112
113
114
115static void reset_ball(void)
116{
117 float sx=0.0f, sy=3.40f, sz=7.05;
118
119#if defined(_MSC_VER) && defined(dDOUBLE)
120 sy -= 0.01; // Cheat, to make it score under win32/double
121#endif
122
123 dQuaternion q;
124 dQSetIdentity(q);
125 dBodySetPosition (sphbody, sx, sy, sz);
126 dBodySetQuaternion(sphbody, q);
127 dBodySetLinearVel (sphbody, 0,0,0);
128 dBodySetAngularVel (sphbody, 0,0,0);
129}
130
131
132// called when a key pressed
133
134static void command (int cmd)
135{
136 switch (cmd)
137 {
138 case ' ':
139 reset_ball();
140 break;
141 }
142}
143
144
145// simulation loop
146
147static void simLoop (int pause)
148{
149 double simstep = 0.001; // 1ms simulation steps
150 double dt = dsElapsedTime();
151
152 int nrofsteps = (int) ceilf(dt/simstep);
153// fprintf(stderr, "dt=%f, nr of steps = %d\n", dt, nrofsteps);
154
155 for (int i=0; i<nrofsteps && !pause; i++)
156 {
157 dSpaceCollide (space,0,&nearCallback);
158 dWorldQuickStep (world, simstep);
159 dJointGroupEmpty (contactgroup);
160 }
161
162 dsSetColor (1,1,1);
163 const dReal *SPos = dBodyGetPosition(sphbody);
164 const dReal *SRot = dBodyGetRotation(sphbody);
165 float spos[3] = {SPos[0], SPos[1], SPos[2]};
166 float srot[12] = { SRot[0], SRot[1], SRot[2], SRot[3], SRot[4], SRot[5], SRot[6], SRot[7], SRot[8], SRot[9], SRot[10], SRot[11] };
167 dsDrawSphere
168 (
169 spos,
170 srot,
171 RADIUS
172 );
173
174 // draw world trimesh
175 dsSetColor(0.4,0.7,0.9);
176 dsSetTexture (DS_NONE);
177
178 const dReal* Pos = dGeomGetPosition(world_mesh);
179 //dIASSERT(dVALIDVEC3(Pos));
180 float pos[3] = { Pos[0], Pos[1], Pos[2] };
181
182 const dReal* Rot = dGeomGetRotation(world_mesh);
183 //dIASSERT(dVALIDMAT3(Rot));
184 float rot[12] = { Rot[0], Rot[1], Rot[2], Rot[3], Rot[4], Rot[5], Rot[6], Rot[7], Rot[8], Rot[9], Rot[10], Rot[11] };
185
186 int numi = sizeof(world_indices) / sizeof(int);
187
188 for (int i=0; i<numi/3; i++)
189 {
190 int i0 = world_indices[i*3+0];
191 int i1 = world_indices[i*3+1];
192 int i2 = world_indices[i*3+2];
193 float *v0 = world_vertices+i0*3;
194 float *v1 = world_vertices+i1*3;
195 float *v2 = world_vertices+i2*3;
196 dsDrawTriangle(pos, rot, v0,v1,v2, true); // single precision draw
197 }
198}
199
200
201int main (int argc, char **argv)
202{
203 dMass m;
204 dMatrix3 R;
205
206 // setup pointers to drawstuff callback functions
207 dsFunctions fn;
208 fn.version = DS_VERSION;
209 fn.start = &start;
210 fn.step = &simLoop;
211 fn.command = &command;
212 fn.stop = 0;
213 fn.path_to_textures = "../../drawstuff/textures";
214 if(argc==2)
215 fn.path_to_textures = argv[1];
216
217 // create world
218 dInitODE();
219 world = dWorldCreate();
220 space = dHashSpaceCreate (0);
221
222 contactgroup = dJointGroupCreate (0);
223 dWorldSetGravity (world,0,0,-9.8);
224 dWorldSetQuickStepNumIterations (world, 64);
225
226 // Create a static world using a triangle mesh that we can collide with.
227 int numv = sizeof(world_vertices)/(3*sizeof(float));
228 int numi = sizeof(world_indices)/ sizeof(int);
229 printf("numv=%d, numi=%d\n", numv, numi);
230 dTriMeshDataID Data = dGeomTriMeshDataCreate();
231
232// fprintf(stderr,"Building Single Precision Mesh\n");
233
234 dGeomTriMeshDataBuildSingle
235 (
236 Data,
237 world_vertices,
238 3 * sizeof(float),
239 numv,
240 world_indices,
241 numi,
242 3 * sizeof(int)
243 );
244
245 world_mesh = dCreateTriMesh(space, Data, 0, 0, 0);
246 dGeomTriMeshEnableTC(world_mesh, dSphereClass, false);
247 dGeomTriMeshEnableTC(world_mesh, dBoxClass, false);
248 dGeomSetPosition(world_mesh, 0, 0, 0.5);
249 dRSetIdentity(R);
250 //dIASSERT(dVALIDMAT3(R));
251
252 dGeomSetRotation (world_mesh, R);
253
254 float sx=0.0, sy=3.40, sz=6.80;
255 sphbody = dBodyCreate (world);
256 dMassSetSphere (&m,1,RADIUS);
257 dBodySetMass (sphbody,&m);
258 sphgeom = dCreateSphere(0, RADIUS);
259 dGeomSetBody (sphgeom,sphbody);
260 reset_ball();
261 dSpaceAdd (space, sphgeom);
262
263 // run simulation
264 dsSimulationLoop (argc,argv,352,288,&fn);
265
266 // Causes segm violation? Why?
267 // (because dWorldDestroy() destroys body connected to geom; must call first!)
268 dGeomDestroy(sphgeom);
269 dGeomDestroy (world_mesh);
270
271 dJointGroupEmpty (contactgroup);
272 dJointGroupDestroy (contactgroup);
273 dSpaceDestroy (space);
274 dWorldDestroy (world);
275 dCloseODE();
276 return 0;
277}
278