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 | /* exercise the C++ interface */ |
---|
24 | |
---|
25 | #include <ode/ode.h> |
---|
26 | #include <drawstuff/drawstuff.h> |
---|
27 | |
---|
28 | #ifdef _MSC_VER |
---|
29 | #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints |
---|
30 | #endif |
---|
31 | |
---|
32 | // select correct drawing functions |
---|
33 | |
---|
34 | #ifdef dDOUBLE |
---|
35 | #define dsDrawBox dsDrawBoxD |
---|
36 | #define dsDrawSphere dsDrawSphereD |
---|
37 | #define dsDrawCylinder dsDrawCylinderD |
---|
38 | #define dsDrawCapsule dsDrawCapsuleD |
---|
39 | #endif |
---|
40 | |
---|
41 | |
---|
42 | // some constants |
---|
43 | |
---|
44 | #define NUM 10 // number of boxes |
---|
45 | #define SIDE (0.2) // side length of a box |
---|
46 | #define MASS (1.0) // mass of a box |
---|
47 | #define RADIUS (0.1732f) // sphere radius |
---|
48 | |
---|
49 | |
---|
50 | // dynamics and collision objects |
---|
51 | |
---|
52 | static dWorld world; |
---|
53 | static dSimpleSpace space (0); |
---|
54 | static dBody body[NUM]; |
---|
55 | static dBallJoint joint[NUM-1]; |
---|
56 | static dJointGroup contactgroup; |
---|
57 | static dBox box[NUM]; |
---|
58 | |
---|
59 | |
---|
60 | // this is called by space.collide when two objects in space are |
---|
61 | // potentially colliding. |
---|
62 | |
---|
63 | static void nearCallback (void *data, dGeomID o1, dGeomID o2) |
---|
64 | { |
---|
65 | // exit without doing anything if the two bodies are connected by a joint |
---|
66 | dBodyID b1 = dGeomGetBody(o1); |
---|
67 | dBodyID b2 = dGeomGetBody(o2); |
---|
68 | if (b1 && b2 && dAreConnected (b1,b2)) return; |
---|
69 | |
---|
70 | // @@@ it's still more convenient to use the C interface here. |
---|
71 | |
---|
72 | dContact contact; |
---|
73 | contact.surface.mode = 0; |
---|
74 | contact.surface.mu = dInfinity; |
---|
75 | if (dCollide (o1,o2,1,&contact.geom,sizeof(dContactGeom))) { |
---|
76 | dJointID c = dJointCreateContact (world.id(),contactgroup.id(),&contact); |
---|
77 | dJointAttach (c,b1,b2); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | // start simulation - set viewpoint |
---|
83 | |
---|
84 | static void start() |
---|
85 | { |
---|
86 | static float xyz[3] = {2.1640f,-1.3079f,1.7600f}; |
---|
87 | static float hpr[3] = {125.5000f,-17.0000f,0.0000f}; |
---|
88 | dsSetViewpoint (xyz,hpr); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | // simulation loop |
---|
93 | |
---|
94 | static void simLoop (int pause) |
---|
95 | { |
---|
96 | if (!pause) { |
---|
97 | static double angle = 0; |
---|
98 | angle += 0.05; |
---|
99 | body[NUM-1].addForce (0,0,1.5*(sin(angle)+1.0)); |
---|
100 | |
---|
101 | space.collide (0,&nearCallback); |
---|
102 | world.step (0.05); |
---|
103 | |
---|
104 | // remove all contact joints |
---|
105 | contactgroup.empty(); |
---|
106 | } |
---|
107 | |
---|
108 | dReal sides[3] = {SIDE,SIDE,SIDE}; |
---|
109 | dsSetColor (1,1,0); |
---|
110 | dsSetTexture (DS_WOOD); |
---|
111 | for (int i=0; i<NUM; i++) |
---|
112 | dsDrawBox (body[i].getPosition(),body[i].getRotation(),sides); |
---|
113 | } |
---|
114 | |
---|
115 | |
---|
116 | int main (int argc, char **argv) |
---|
117 | { |
---|
118 | // setup pointers to drawstuff callback functions |
---|
119 | dsFunctions fn; |
---|
120 | fn.version = DS_VERSION; |
---|
121 | fn.start = &start; |
---|
122 | fn.step = &simLoop; |
---|
123 | fn.command = 0; |
---|
124 | fn.stop = 0; |
---|
125 | fn.path_to_textures = "../../drawstuff/textures"; |
---|
126 | if(argc==2) |
---|
127 | { |
---|
128 | fn.path_to_textures = argv[1]; |
---|
129 | } |
---|
130 | |
---|
131 | // create world |
---|
132 | dInitODE(); |
---|
133 | |
---|
134 | int i; |
---|
135 | contactgroup.create (0); |
---|
136 | world.setGravity (0,0,-0.5); |
---|
137 | dWorldSetCFM (world.id(),1e-5); |
---|
138 | dPlane plane (space,0,0,1,0); |
---|
139 | |
---|
140 | for (i=0; i<NUM; i++) { |
---|
141 | body[i].create (world); |
---|
142 | dReal k = i*SIDE; |
---|
143 | body[i].setPosition (k,k,k+0.4); |
---|
144 | dMass m; |
---|
145 | m.setBox (1,SIDE,SIDE,SIDE); |
---|
146 | m.adjust (MASS); |
---|
147 | body[i].setMass (&m); |
---|
148 | body[i].setData ((void*)(size_t)i); |
---|
149 | |
---|
150 | box[i].create (space,SIDE,SIDE,SIDE); |
---|
151 | box[i].setBody (body[i]); |
---|
152 | } |
---|
153 | for (i=0; i<(NUM-1); i++) { |
---|
154 | joint[i].create (world); |
---|
155 | joint[i].attach (body[i],body[i+1]); |
---|
156 | dReal k = (i+0.5)*SIDE; |
---|
157 | joint[i].setAnchor (k,k,k+0.4); |
---|
158 | } |
---|
159 | |
---|
160 | // run simulation |
---|
161 | dsSimulationLoop (argc,argv,352,288,&fn); |
---|
162 | |
---|
163 | dCloseODE(); |
---|
164 | return 0; |
---|
165 | } |
---|