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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
Benoit CHAPEROT 2003-2004 www.jstarlab.com
Support for terrain and cones, collision and drawing.
Terrains can be with z up (dTerrainZ) or y up (dTerrainY).
Terrains are defined by a height field.
Terrains are now placeable.
Terrains can now be finite or infinite (repeat themselve in the x and (y or z) directions).
Terrains can potentially collide with everything that collides with planes and rays;
see the switch statement.
Cones currently collides only with terrain and planes and rays.
Cones, with high radius to height ratios are perfect to simulate vehicle wheels on terrains.
There was an error in the depths returned by dCollideTerrain.
Plus now contacts are not sorted according to their depths.
Contact sorting is now supposed to be done externally.
Not all dCollide functions seem to sort contacts according to depth.
Requesting a high number of contacts, sorting them and then considering only the most significant contacts
is a good way I think to improve stability.
* Cones Collisions with spheres, boxes, ccylinder and trimesh are now roughly approximated using sphere collisions.
You will need to complete the following operations (with ODE 0.039):
*** add to folder ode\src:
dCone.cpp
dTerrainY.cpp
dTerrainZ.cpp
collision_std_internal.h
On linux => edit each .cpp file and comment out #include "windows.h" & #include "ode\ode.h"
*** add to drawstuff\src\drawstuff.cpp:
static void drawCone(float l, float r)
{
int i;
float tmp,ny,nz,a,ca,sa;
const int n = 24; // number of sides to the cone (divisible by 4)
a = float(M_PI*2.0)/float(n);
sa = (float) sin(a);
ca = (float) cos(a);
// draw top
glShadeModel (GL_FLAT);
ny=1; nz=0; // normal vector = (0,ny,nz)
glBegin (GL_TRIANGLE_FAN);
glNormal3d (0,0,1);
glVertex3d (0,0,l);
for (i=0; i<=n; i++) {
if (i==1 || i==n/2+1)
setColor (color[0]*0.75f,color[1]*0.75f,color[2]*0.75f,color[3]);
glNormal3d (ny*r,nz*r,0);
glVertex3d (ny*r,nz*r,0);
if (i==1 || i==n/2+1)
setColor (color[0],color[1],color[2],color[3]);
// rotate ny,nz
tmp = ca*ny - sa*nz;
nz = sa*ny + ca*nz;
ny = tmp;
}
glEnd();
// draw bottom
ny=1; nz=0; // normal vector = (0,ny,nz)
glBegin (GL_TRIANGLE_FAN);
glNormal3d (0,0,-1);
glVertex3d (0,0,0);
for (i=0; i<=n; i++) {
if (i==1 || i==n/2+1)
setColor (color[0]*0.75f,color[1]*0.75f,color[2]*0.75f,color[3]);
glNormal3d (0,0,-1);
glVertex3d (ny*r,nz*r,0);
if (i==1 || i==n/2+1)
setColor (color[0],color[1],color[2],color[3]);
// rotate ny,nz
tmp = ca*ny + sa*nz;
nz = -sa*ny + ca*nz;
ny = tmp;
}
glEnd();
}
void dsDrawCone (const float pos[3], const float R[12], float length, float radius)
{
if (current_state != 2) dsError ("drawing function called outside simulation loop");
setupDrawingMode();
glShadeModel (GL_SMOOTH);
setTransform (pos,R);
drawCone (length,radius);
glPopMatrix();
if (use_shadows) {
setShadowDrawingMode();
setShadowTransform();
setTransform (pos,R);
drawCone (length,radius);
glPopMatrix();
glPopMatrix();
glDepthRange (0,1);
}
}
void dsDrawConeD (const double pos[3], const double R[12], float length, float radius)
{
int i;
float pos2[3],R2[12];
for (i=0; i<3; i++) pos2[i]=(float)pos[i];
for (i=0; i<12; i++) R2[i]=(float)R[i];
dsDrawCone(pos2,R2,length,radius);
}
static float GetHeight(int x,int y,int nNumNodesPerSide,float *pHeights)
{
int nNumNodesPerSideMask = nNumNodesPerSide - 1;
return pHeights[ (((unsigned int)(y) & nNumNodesPerSideMask) * nNumNodesPerSide)
+ ((unsigned int)(x) & nNumNodesPerSideMask)];
}
void dsDrawTerrainY(int x,int z,float vLength,float vNodeLength,int nNumNodesPerSide,float *pHeights,const float *pR,const float *ppos)
{
float A[3],B[3],C[3],D[3];
float R[12];
float pos[3];
if (pR)
memcpy(R,pR,sizeof(R));
else
{
memset(R,0,sizeof(R));
R[0] = 1.f;
R[5] = 1.f;
R[10] = 1.f;
}
if (ppos)
memcpy(pos,ppos,sizeof(pos));
else
memset(pos,0,sizeof(pos));
float vx,vz;
vx = vLength * x;
vz = vLength * z;
int i;
for (i=0;i<nNumNodesPerSide;i++)
{
for (int j=0;j<nNumNodesPerSide;j++)
{
A[0] = i * vNodeLength + vx;
A[2] = j * vNodeLength + vz;
A[1] = GetHeight(i,j,nNumNodesPerSide,pHeights);
B[0] = (i+1) * vNodeLength + vx;
B[2] = j * vNodeLength + vz;
B[1] = GetHeight(i+1,j,nNumNodesPerSide,pHeights);
C[0] = i * vNodeLength + vx;
C[2] = (j+1) * vNodeLength + vz;
C[1] = GetHeight(i,j+1,nNumNodesPerSide,pHeights);
D[0] = (i+1) * vNodeLength + vx;
D[2] = (j+1) * vNodeLength + vz;
D[1] = GetHeight(i+1,j+1,nNumNodesPerSide,pHeights);
dsDrawTriangle(pos,R,C,B,A,1);
dsDrawTriangle(pos,R,D,B,C,1);
}
}
}
void dsDrawTerrainZ(int x,int z,float vLength,float vNodeLength,int nNumNodesPerSide,float *pHeights,const float *pR,const float *ppos)
{
float A[3],B[3],C[3],D[3];
float R[12];
float pos[3];
if (pR)
memcpy(R,pR,sizeof(R));
else
{
memset(R,0,sizeof(R));
R[0] = 1.f;
R[5] = 1.f;
R[10] = 1.f;
}
if (ppos)
memcpy(pos,ppos,sizeof(pos));
else
memset(pos,0,sizeof(pos));
float vx,vz;
vx = vLength * x;
vz = vLength * z;
int i;
for (i=0;i<nNumNodesPerSide;i++)
{
for (int j=0;j<nNumNodesPerSide;j++)
{
A[0] = i * vNodeLength + vx;
A[1] = j * vNodeLength + vz;
A[2] = GetHeight(i,j,nNumNodesPerSide,pHeights);
B[0] = (i+1) * vNodeLength + vx;
B[1] = j * vNodeLength + vz;
B[2] = GetHeight(i+1,j,nNumNodesPerSide,pHeights);
C[0] = i * vNodeLength + vx;
C[1] = (j+1) * vNodeLength + vz;
C[2] = GetHeight(i,j+1,nNumNodesPerSide,pHeights);
D[0] = (i+1) * vNodeLength + vx;
D[1] = (j+1) * vNodeLength + vz;
D[2] = GetHeight(i+1,j+1,nNumNodesPerSide,pHeights);
dsDrawTriangle(pos,R,C,A,B,1);
dsDrawTriangle(pos,R,D,C,B,1);
}
}
}
*** add to include\drawstuff\drawstuff.h:
void dsDrawCone (const float pos[3], const float R[12], float length, float radius);
void dsDrawConeD (const double pos[3], const double R[12], float length, float radius);
void dsDrawTerrainY(int x,int y,float vLength,float vNodeLength,int nNumNodesPerSide,float *pHeights,const float *pR,const float *ppos);
void dsDrawTerrainZ(int x,int y,float vLength,float vNodeLength,int nNumNodesPerSide,float *pHeights,const float *pR,const float *ppos);
*** add in include\ode\collision.h line 77:
/* class numbers - each geometry object needs a unique number */
enum {
dSphereClass = 0,
dBoxClass,
dCCylinderClass,
dCylinderClass,
dPlaneClass,
dRayClass,
dGeomTransformClass,
dTriMeshClass,
dTerrainYClass, //here
dTerrainZClass, //here
dConeClass, //here
dFirstSpaceClass,
dSimpleSpaceClass = dFirstSpaceClass,
dHashSpaceClass,
dQuadTreeSpaceClass,
dLastSpaceClass = dQuadTreeSpaceClass,
dFirstUserClass,
dLastUserClass = dFirstUserClass + dMaxUserClasses - 1,
dGeomNumClasses
};
dGeomID dCreateTerrainY (dSpaceID space, dReal *pHeights,dReal vLength,int nNumNodesPerSide, int bFinite, int bPlaceable);
dReal dGeomTerrainYPointDepth (dGeomID g, dReal x, dReal y, dReal z);
dGeomID dCreateTerrainZ (dSpaceID space, dReal *pHeights,dReal vLength,int nNumNodesPerSide, int bFinite, int bPlaceable);
dReal dGeomTerrainZPointDepth (dGeomID g, dReal x, dReal y, dReal z);
dGeomID dCreateCone(dSpaceID space, dReal radius, dReal length);
void dGeomConeSetParams (dGeomID cone, dReal radius, dReal length);
void dGeomConeGetParams (dGeomID cone, dReal *radius, dReal *length);
dReal dGeomConePointDepth(dGeomID g, dReal x, dReal y, dReal z);
*** add in include\ode\odemath.h:
#define dOP(a,op,b,c) \
(a)[0] = ((b)[0]) op ((c)[0]); \
(a)[1] = ((b)[1]) op ((c)[1]); \
(a)[2] = ((b)[2]) op ((c)[2]);
#define dOPC(a,op,b,c) \
(a)[0] = ((b)[0]) op (c); \
(a)[1] = ((b)[1]) op (c); \
(a)[2] = ((b)[2]) op (c);
#define dOPE(a,op,b) \
(a)[0] op ((b)[0]); \
(a)[1] op ((b)[1]); \
(a)[2] op ((b)[2]);
#define dOPEC(a,op,c) \
(a)[0] op (c); \
(a)[1] op (c); \
(a)[2] op (c);
#define dLENGTH(a) \
(dSqrt( ((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]) ));
#define dLENGTHSQUARED(a) \
(((a)[0])*((a)[0]) + ((a)[1])*((a)[1]) + ((a)[2])*((a)[2]));
*** add in ode\src\collision_kernel.cpp function 'static void initColliders()' next to other 'setCollider' calls:
setCollider (dTerrainYClass,dSphereClass,&dCollideTerrainY);
setCollider (dTerrainYClass,dBoxClass,&dCollideTerrainY);
setCollider (dTerrainYClass,dCCylinderClass,&dCollideTerrainY);
setCollider (dTerrainYClass,dRayClass,&dCollideTerrainY);
setCollider (dTerrainYClass,dConeClass,&dCollideTerrainY);
setCollider (dTerrainZClass,dSphereClass,&dCollideTerrainZ);
setCollider (dTerrainZClass,dBoxClass,&dCollideTerrainZ);
setCollider (dTerrainZClass,dCCylinderClass,&dCollideTerrainZ);
setCollider (dTerrainZClass,dRayClass,&dCollideTerrainZ);
setCollider (dTerrainZClass,dConeClass,&dCollideTerrainZ);
setCollider (dRayClass,dConeClass,&dCollideRayCone);
setCollider (dConeClass,dPlaneClass,&dCollideConePlane);
setCollider (dConeClass,dSphereClass,&dCollideConeSphere);
setCollider (dConeClass,dBoxClass,&dCollideConeBox);
setCollider (dCCylinderClass,dConeClass,&dCollideCCylinderCone);
setCollider (dTriMeshClass,dConeClass,&dCollideTriMeshCone);
*** add in ode\src\collision_std.h:
int dCollideTerrainY(dxGeom *o1, dxGeom *o2, int flags,dContactGeom *contact, int skip);
int dCollideTerrainZ(dxGeom *o1, dxGeom *o2, int flags,dContactGeom *contact, int skip);
int dCollideConePlane (dxGeom *o1, dxGeom *o2, int flags,dContactGeom *contact, int skip);
int dCollideRayCone (dxGeom *o1, dxGeom *o2, int flags,dContactGeom *contact, int skip);
int dCollideConeSphere(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip);
int dCollideConeBox(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip);
int dCollideCCylinderCone(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip);
int dCollideTriMeshCone(dxGeom *o1, dxGeom *o2, int flags, dContactGeom *contact, int skip);
*** add dCone.cpp, dTerrainY.cpp and dTerrainZ.cpp to the the ODE_SRC variable in the makefile
On Linux => add dCone.cpp, dTerrainY.cpp and dTerrainZ.cpp to the the libode_a_SOURCES variable in the ode/src/Makefile.am file.
*** now you can now test using file test_boxstackb.cpp (to add in folder ode\test).
|