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 | #include <ode/ode.h> |
---|
24 | #include <drawstuff/drawstuff.h> |
---|
25 | |
---|
26 | #ifdef _MSC_VER |
---|
27 | #pragma warning(disable:4244 4305) // for VC++, no precision loss complaints |
---|
28 | #endif |
---|
29 | |
---|
30 | // select correct drawing functions |
---|
31 | #ifdef dDOUBLE |
---|
32 | #define dsDrawBox dsDrawBoxD |
---|
33 | #endif |
---|
34 | |
---|
35 | |
---|
36 | // some constants |
---|
37 | #define SIDE (0.5f) // side length of a box |
---|
38 | #define MASS (1.0) // mass of a box |
---|
39 | |
---|
40 | |
---|
41 | // dynamics and collision objects |
---|
42 | static dWorldID world; |
---|
43 | static dBodyID body[2]; |
---|
44 | static dGeomID geom[2]; |
---|
45 | static dJointID lmotor[2]; |
---|
46 | static dJointID amotor[2]; |
---|
47 | static dSpaceID space; |
---|
48 | static dJointGroupID contactgroup; |
---|
49 | |
---|
50 | |
---|
51 | // start simulation - set viewpoint |
---|
52 | |
---|
53 | static void start() |
---|
54 | { |
---|
55 | static float xyz[3] = {1.0382f,-1.0811f,1.4700f}; |
---|
56 | static float hpr[3] = {135.0000f,-19.5000f,0.0000f}; |
---|
57 | dsSetViewpoint (xyz,hpr); |
---|
58 | printf ("Press 'q,a,z' to control one axis of lmotor connectiong two bodies. (q is +,a is 0, z is -)\n"); |
---|
59 | printf ("Press 'w,e,r' to control one axis of lmotor connectiong first body with world. (w is +,e is 0, r is -)\n"); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | // called when a key pressed |
---|
64 | |
---|
65 | static void command (int cmd) |
---|
66 | { |
---|
67 | if (cmd == 'q' || cmd == 'Q') { |
---|
68 | dJointSetLMotorParam(lmotor[0],dParamVel,0); |
---|
69 | dJointSetLMotorParam(lmotor[0],dParamVel2,0); |
---|
70 | dJointSetLMotorParam(lmotor[0],dParamVel3,0.1); |
---|
71 | } else if (cmd == 'a' || cmd == 'A') { |
---|
72 | dJointSetLMotorParam(lmotor[0],dParamVel,0); |
---|
73 | dJointSetLMotorParam(lmotor[0],dParamVel2,0); |
---|
74 | dJointSetLMotorParam(lmotor[0],dParamVel3,0); |
---|
75 | } else if (cmd == 'z' || cmd == 'Z') { |
---|
76 | dJointSetLMotorParam(lmotor[0],dParamVel,0); |
---|
77 | dJointSetLMotorParam(lmotor[0],dParamVel2,0); |
---|
78 | dJointSetLMotorParam(lmotor[0],dParamVel3,-0.1); |
---|
79 | } else if (cmd == 'w' || cmd == 'W') { |
---|
80 | dJointSetLMotorParam(lmotor[1],dParamVel,0.1); |
---|
81 | dJointSetLMotorParam(lmotor[1],dParamVel2,0); |
---|
82 | dJointSetLMotorParam(lmotor[1],dParamVel3,0); |
---|
83 | } else if (cmd == 'e' || cmd == 'E') { |
---|
84 | dJointSetLMotorParam(lmotor[1],dParamVel,0); |
---|
85 | dJointSetLMotorParam(lmotor[1],dParamVel2,0); |
---|
86 | dJointSetLMotorParam(lmotor[1],dParamVel3,0); |
---|
87 | } else if (cmd == 'r' || cmd == 'R') { |
---|
88 | dJointSetLMotorParam(lmotor[1],dParamVel,-0.1); |
---|
89 | dJointSetLMotorParam(lmotor[1],dParamVel2,0); |
---|
90 | dJointSetLMotorParam(lmotor[1],dParamVel3,0); |
---|
91 | } |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | static void nearCallback (void *data, dGeomID o1, dGeomID o2) |
---|
98 | { |
---|
99 | // exit without doing anything if the two bodies are connected by a joint |
---|
100 | dBodyID b1 = dGeomGetBody(o1); |
---|
101 | dBodyID b2 = dGeomGetBody(o2); |
---|
102 | |
---|
103 | dContact contact; |
---|
104 | contact.surface.mode = 0; |
---|
105 | contact.surface.mu = dInfinity; |
---|
106 | if (dCollide (o1,o2,1,&contact.geom,sizeof(dContactGeom))) { |
---|
107 | dJointID c = dJointCreateContact (world,contactgroup,&contact); |
---|
108 | dJointAttach (c,b1,b2); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // simulation loop |
---|
113 | |
---|
114 | static void simLoop (int pause) |
---|
115 | { |
---|
116 | if (!pause) { |
---|
117 | dSpaceCollide(space,0,&nearCallback); |
---|
118 | dWorldQuickStep (world,0.05); |
---|
119 | dJointGroupEmpty(contactgroup); |
---|
120 | } |
---|
121 | |
---|
122 | dReal sides1[3]; |
---|
123 | dGeomBoxGetLengths(geom[0], sides1); |
---|
124 | dReal sides2[3]; |
---|
125 | dGeomBoxGetLengths(geom[1], sides2); |
---|
126 | dsSetTexture (DS_WOOD); |
---|
127 | dsSetColor (1,1,0); |
---|
128 | dsDrawBox (dBodyGetPosition(body[0]),dBodyGetRotation(body[0]),sides1); |
---|
129 | dsSetColor (0,1,1); |
---|
130 | dsDrawBox (dBodyGetPosition(body[1]),dBodyGetRotation(body[1]),sides2); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | int main (int argc, char **argv) |
---|
135 | { |
---|
136 | // setup pointers to drawstuff callback functions |
---|
137 | dsFunctions fn; |
---|
138 | fn.version = DS_VERSION; |
---|
139 | fn.start = &start; |
---|
140 | fn.step = &simLoop; |
---|
141 | fn.command = &command; |
---|
142 | fn.stop = 0; |
---|
143 | fn.path_to_textures = "../../drawstuff/textures"; |
---|
144 | if(argc>=2) |
---|
145 | { |
---|
146 | fn.path_to_textures = argv[1]; |
---|
147 | } |
---|
148 | |
---|
149 | // create world |
---|
150 | dInitODE(); |
---|
151 | contactgroup = dJointGroupCreate(0); |
---|
152 | world = dWorldCreate(); |
---|
153 | space = dSimpleSpaceCreate(0); |
---|
154 | dMass m; |
---|
155 | dMassSetBox (&m,1,SIDE,SIDE,SIDE); |
---|
156 | dMassAdjust (&m,MASS); |
---|
157 | |
---|
158 | body[0] = dBodyCreate (world); |
---|
159 | dBodySetMass (body[0],&m); |
---|
160 | dBodySetPosition (body[0],0,0,1); |
---|
161 | geom[0] = dCreateBox(space,SIDE,SIDE,SIDE); |
---|
162 | body[1] = dBodyCreate (world); |
---|
163 | dBodySetMass (body[1],&m); |
---|
164 | dBodySetPosition (body[1],0,0,2); |
---|
165 | geom[1] = dCreateBox(space,SIDE,SIDE,SIDE); |
---|
166 | |
---|
167 | dGeomSetBody(geom[0],body[0]); |
---|
168 | dGeomSetBody(geom[1],body[1]); |
---|
169 | |
---|
170 | lmotor[0] = dJointCreateLMotor (world,0); |
---|
171 | dJointAttach (lmotor[0],body[0],body[1]); |
---|
172 | lmotor[1] = dJointCreateLMotor (world,0); |
---|
173 | dJointAttach (lmotor[1],body[0],0); |
---|
174 | amotor[0] = dJointCreateAMotor(world,0); |
---|
175 | dJointAttach(amotor[0], body[0],body[1]); |
---|
176 | amotor[1] = dJointCreateAMotor(world,0); |
---|
177 | dJointAttach(amotor[1], body[0], 0); |
---|
178 | |
---|
179 | for (int i=0; i<2; i++) { |
---|
180 | dJointSetAMotorNumAxes(amotor[i], 3); |
---|
181 | dJointSetAMotorAxis(amotor[i],0,1,1,0,0); |
---|
182 | dJointSetAMotorAxis(amotor[i],1,1,0,1,0); |
---|
183 | dJointSetAMotorAxis(amotor[i],2,1,0,0,1); |
---|
184 | dJointSetAMotorParam(amotor[i],dParamFMax,0.00001); |
---|
185 | dJointSetAMotorParam(amotor[i],dParamFMax2,0.00001); |
---|
186 | dJointSetAMotorParam(amotor[i],dParamFMax3,0.00001); |
---|
187 | |
---|
188 | dJointSetAMotorParam(amotor[i],dParamVel,0); |
---|
189 | dJointSetAMotorParam(amotor[i],dParamVel2,0); |
---|
190 | dJointSetAMotorParam(amotor[i],dParamVel3,0); |
---|
191 | |
---|
192 | dJointSetLMotorNumAxes(lmotor[i],3); |
---|
193 | dJointSetLMotorAxis(lmotor[i],0,1,1,0,0); |
---|
194 | dJointSetLMotorAxis(lmotor[i],1,1,0,1,0); |
---|
195 | dJointSetLMotorAxis(lmotor[i],2,1,0,0,1); |
---|
196 | |
---|
197 | dJointSetLMotorParam(lmotor[i],dParamFMax,0.0001); |
---|
198 | dJointSetLMotorParam(lmotor[i],dParamFMax2,0.0001); |
---|
199 | dJointSetLMotorParam(lmotor[i],dParamFMax3,0.0001); |
---|
200 | } |
---|
201 | |
---|
202 | // run simulation |
---|
203 | dsSimulationLoop (argc,argv,352,288,&fn); |
---|
204 | |
---|
205 | dJointGroupDestroy(contactgroup); |
---|
206 | dSpaceDestroy (space); |
---|
207 | dWorldDestroy (world); |
---|
208 | dCloseODE(); |
---|
209 | return 0; |
---|
210 | } |
---|