1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/*************************************************************************
* *
* Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
* All rights reserved. Email: russ@q12.org Web: www.q12.org *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of EITHER: *
* (1) The GNU Lesser General Public License as published by the Free *
* Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. The text of the GNU Lesser *
* General Public License is included with this library in the *
* file LICENSE.TXT. *
* (2) The BSD-style license that is included with this library in *
* the file LICENSE-BSD.TXT. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
* LICENSE.TXT and LICENSE-BSD.TXT for more details. *
* *
*************************************************************************/
#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
#include "texturepath.h"
#ifdef _MSC_VER
#pragma warning(disable:4244 4305) // for VC++, no precision loss complaints
#endif
// select correct drawing functions
#ifdef dDOUBLE
#define dsDrawBox dsDrawBoxD
#define dsDrawSphere dsDrawSphereD
#define dsDrawCylinder dsDrawCylinderD
#define dsDrawCapsule dsDrawCapsuleD
#define dsDrawConvex dsDrawConvexD
#endif
bool write_world = false;
bool show_contacts = false;
dWorld * world;
dBody *top1, *top2;
dSpace *space;
dJointGroup contactgroup;
const dReal pinradius = 0.05f;
const dReal pinlength = 1.5f;
const dReal topradius = 1.0f;
const dReal toplength = 0.25f;
const dReal topmass = 1.0f;
#define MAX_CONTACTS 4
static void nearCallback (void *, dGeomID o1, dGeomID o2)
{
// for drawing the contact points
dMatrix3 RI;
dRSetIdentity (RI);
const dReal ss[3] = {0.02,0.02,0.02};
int i;
dBodyID b1 = dGeomGetBody(o1);
dBodyID b2 = dGeomGetBody(o2);
dContact contact[MAX_CONTACTS];
int numc = dCollide (o1,o2,MAX_CONTACTS,&contact[0].geom,
sizeof(dContact));
for (i=0; i<numc; i++) {
contact[i].surface.mode = dContactApprox1;
contact[i].surface.mu = 2;
dJointID c = dJointCreateContact (*world,contactgroup,contact+i);
dJointAttach (c,b1,b2);
if (show_contacts)
dsDrawBox (contact[i].geom.pos, RI, ss);
}
}
// start simulation - set viewpoint
static void start()
{
static float xyz[3] = {4.777f, -2.084f, 2.18f};
static float hpr[3] = {153.0f, -14.5f, 0.0f};
dsSetViewpoint (xyz,hpr);
printf ("Orange top approximates conservation of angular momentum\n");
printf ("Green top uses conservation of angular velocity\n");
printf ("---\n");
printf ("SPACE to reset\n");
printf ("A to tilt the tops.\n");
printf ("T to toggle showing the contact points.\n");
printf ("1 to save the current state to 'state.dif'.\n");
}
char locase (char c)
{
if (c >= 'A' && c <= 'Z') return c - ('a'-'A');
else return c;
}
// called when a key pressed
static void reset();
static void tilt();
static void command (int cmd)
{
cmd = locase (cmd);
if (cmd == ' ')
{
reset();
}
else if (cmd == 'a') {
tilt();
}
else if (cmd == 't') {
show_contacts = !show_contacts;
}
else if (cmd == '1') {
write_world = true;
}
}
// simulation loop
static void simLoop (int pause)
{
dsSetColor (0,0,2);
space->collide(0,&nearCallback);
if (!pause)
//world->quickStep(0.02);
world->step(0.02);
if (write_world) {
FILE *f = fopen ("state.dif","wt");
if (f) {
dWorldExportDIF (*world,f,"X");
fclose (f);
}
write_world = false;
}
// remove all contact joints
dJointGroupEmpty (contactgroup);
dsSetTexture (DS_WOOD);
dsSetColor (1,0.5f,0);
dsDrawCylinder(top1->getPosition(),
top1->getRotation(),
toplength, topradius);
dsDrawCapsule(top1->getPosition(),
top1->getRotation(),
pinlength, pinradius);
dsSetColor (0.5f,1,0);
dsDrawCylinder(top2->getPosition(),
top2->getRotation(),
toplength, topradius);
dsDrawCapsule(top2->getPosition(),
top2->getRotation(),
pinlength, pinradius);
}
static void reset()
{
dMatrix3 R;
dRSetIdentity(R);
top1->setRotation(R);
top2->setRotation(R);
top1->setPosition(0.8f, -2, 2);
top2->setPosition(0.8f, 2, 2);
top1->setAngularVel(1,0,7);
top2->setAngularVel(1,0,7);
top1->setLinearVel(0,0.2f,0);
top2->setLinearVel(0,0.2f,0);
}
static void tilt()
{
top1->addTorque(0, 10, 0);
top2->addTorque(0, 10, 0);
}
int main (int argc, char **argv)
{
// setup pointers to drawstuff callback functions
dsFunctions fn;
fn.version = DS_VERSION;
fn.start = &start;
fn.step = &simLoop;
fn.command = &command;
fn.stop = 0;
fn.path_to_textures = DRAWSTUFF_TEXTURE_PATH;
// create world
dInitODE();
world = new dWorld();
world->setGravity(0,0,-0.5f);
world->setCFM(1e-5f);
world->setLinearDamping(0.00001f);
world->setAngularDamping(0.0001f);
space = new dSimpleSpace(0);
dPlane *floor = new dPlane(*space, 0,0,1,0);
top1 = new dBody(*world);
top2 = new dBody(*world);
dMass m;
m.setCylinderTotal(1, 3, topradius, toplength);
top1->setMass(m);
top2->setMass(m);
dGeom *g1, *g2, *pin1, *pin2;
g1 = new dCylinder(*space, topradius, toplength);
g1->setBody(*top1);
g2 = new dCylinder(*space, topradius, toplength);
g2->setBody(*top2);
pin1 = new dCapsule(*space, pinradius, pinlength);
pin1->setBody(*top1);
pin2 = new dCapsule(*space, pinradius, pinlength);
pin2->setBody(*top2);
top2->setGyroscopicMode(false);
reset();
// run simulation
dsSimulationLoop (argc,argv,512,384,&fn);
delete g1;
delete g2;
delete pin1;
delete pin2;
delete floor;
contactgroup.empty();
delete top1;
delete top2;
delete space;
delete world;
dCloseODE();
}
|