]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - world.c
fix Collision_ClipTrace_Line_Sphere calculation of impactdist (had a
[xonotic/darkplaces.git] / world.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // world.c -- world query functions
21
22 #include "quakedef.h"
23 #include "clvm_cmds.h"
24 #include "cl_collision.h"
25
26 /*
27
28 entities never clip against themselves, or their owner
29
30 line of sight checks trace->inopen and trace->inwater, but bullets don't
31
32 */
33
34 static void World_Physics_Init(void);
35 void World_Init(void)
36 {
37         Collision_Init();
38         World_Physics_Init();
39 }
40
41 static void World_Physics_Shutdown(void);
42 void World_Shutdown(void)
43 {
44         World_Physics_Shutdown();
45 }
46
47 static void World_Physics_Start(world_t *world);
48 void World_Start(world_t *world)
49 {
50         World_Physics_Start(world);
51 }
52
53 static void World_Physics_End(world_t *world);
54 void World_End(world_t *world)
55 {
56         World_Physics_End(world);
57 }
58
59 //============================================================================
60
61 /// World_ClearLink is used for new headnodes
62 void World_ClearLink (link_t *l)
63 {
64         l->entitynumber = 0;
65         l->prev = l->next = l;
66 }
67
68 void World_RemoveLink (link_t *l)
69 {
70         l->next->prev = l->prev;
71         l->prev->next = l->next;
72 }
73
74 void World_InsertLinkBefore (link_t *l, link_t *before, int entitynumber)
75 {
76         l->entitynumber = entitynumber;
77         l->next = before;
78         l->prev = before->prev;
79         l->prev->next = l;
80         l->next->prev = l;
81 }
82
83 /*
84 ===============================================================================
85
86 ENTITY AREA CHECKING
87
88 ===============================================================================
89 */
90
91 void World_PrintAreaStats(world_t *world, const char *worldname)
92 {
93         Con_Printf("%s areagrid check stats: %d calls %d nodes (%f per call) %d entities (%f per call)\n", worldname, world->areagrid_stats_calls, world->areagrid_stats_nodechecks, (double) world->areagrid_stats_nodechecks / (double) world->areagrid_stats_calls, world->areagrid_stats_entitychecks, (double) world->areagrid_stats_entitychecks / (double) world->areagrid_stats_calls);
94         world->areagrid_stats_calls = 0;
95         world->areagrid_stats_nodechecks = 0;
96         world->areagrid_stats_entitychecks = 0;
97 }
98
99 /*
100 ===============
101 World_SetSize
102
103 ===============
104 */
105 void World_SetSize(world_t *world, const char *filename, const vec3_t mins, const vec3_t maxs, prvm_prog_t *prog)
106 {
107         int i;
108
109         strlcpy(world->filename, filename, sizeof(world->filename));
110         VectorCopy(mins, world->mins);
111         VectorCopy(maxs, world->maxs);
112         world->prog = prog;
113
114         // the areagrid_marknumber is not allowed to be 0
115         if (world->areagrid_marknumber < 1)
116                 world->areagrid_marknumber = 1;
117         // choose either the world box size, or a larger box to ensure the grid isn't too fine
118         world->areagrid_size[0] = max(world->maxs[0] - world->mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
119         world->areagrid_size[1] = max(world->maxs[1] - world->mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
120         world->areagrid_size[2] = max(world->maxs[2] - world->mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
121         // figure out the corners of such a box, centered at the center of the world box
122         world->areagrid_mins[0] = (world->mins[0] + world->maxs[0] - world->areagrid_size[0]) * 0.5f;
123         world->areagrid_mins[1] = (world->mins[1] + world->maxs[1] - world->areagrid_size[1]) * 0.5f;
124         world->areagrid_mins[2] = (world->mins[2] + world->maxs[2] - world->areagrid_size[2]) * 0.5f;
125         world->areagrid_maxs[0] = (world->mins[0] + world->maxs[0] + world->areagrid_size[0]) * 0.5f;
126         world->areagrid_maxs[1] = (world->mins[1] + world->maxs[1] + world->areagrid_size[1]) * 0.5f;
127         world->areagrid_maxs[2] = (world->mins[2] + world->maxs[2] + world->areagrid_size[2]) * 0.5f;
128         // now calculate the actual useful info from that
129         VectorNegate(world->areagrid_mins, world->areagrid_bias);
130         world->areagrid_scale[0] = AREA_GRID / world->areagrid_size[0];
131         world->areagrid_scale[1] = AREA_GRID / world->areagrid_size[1];
132         world->areagrid_scale[2] = AREA_GRID / world->areagrid_size[2];
133         World_ClearLink(&world->areagrid_outside);
134         for (i = 0;i < AREA_GRIDNODES;i++)
135                 World_ClearLink(&world->areagrid[i]);
136         if (developer_extra.integer)
137                 Con_DPrintf("areagrid settings: divisions %ix%ix1 : box %f %f %f : %f %f %f size %f %f %f grid %f %f %f (mingrid %f)\n", AREA_GRID, AREA_GRID, world->areagrid_mins[0], world->areagrid_mins[1], world->areagrid_mins[2], world->areagrid_maxs[0], world->areagrid_maxs[1], world->areagrid_maxs[2], world->areagrid_size[0], world->areagrid_size[1], world->areagrid_size[2], 1.0f / world->areagrid_scale[0], 1.0f / world->areagrid_scale[1], 1.0f / world->areagrid_scale[2], sv_areagrid_mingridsize.value);
138 }
139
140 /*
141 ===============
142 World_UnlinkAll
143
144 ===============
145 */
146 void World_UnlinkAll(world_t *world)
147 {
148         prvm_prog_t *prog = world->prog;
149         int i;
150         link_t *grid;
151         // unlink all entities one by one
152         grid = &world->areagrid_outside;
153         while (grid->next != grid)
154                 World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
155         for (i = 0, grid = world->areagrid;i < AREA_GRIDNODES;i++, grid++)
156                 while (grid->next != grid)
157                         World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
158 }
159
160 /*
161 ===============
162
163 ===============
164 */
165 void World_UnlinkEdict(prvm_edict_t *ent)
166 {
167         int i;
168         for (i = 0;i < ENTITYGRIDAREAS;i++)
169         {
170                 if (ent->priv.server->areagrid[i].prev)
171                 {
172                         World_RemoveLink (&ent->priv.server->areagrid[i]);
173                         ent->priv.server->areagrid[i].prev = ent->priv.server->areagrid[i].next = NULL;
174                 }
175         }
176 }
177
178 int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t requestmaxs, int maxlist, prvm_edict_t **list)
179 {
180         prvm_prog_t *prog = world->prog;
181         int numlist;
182         link_t *grid;
183         link_t *l;
184         prvm_edict_t *ent;
185         vec3_t paddedmins, paddedmaxs;
186         int igrid[3], igridmins[3], igridmaxs[3];
187
188         VectorSet(paddedmins, requestmins[0] - 1.0f, requestmins[1] - 1.0f, requestmins[2] - 1.0f);
189         VectorSet(paddedmaxs, requestmaxs[0] + 1.0f, requestmaxs[1] + 1.0f, requestmaxs[2] + 1.0f);
190
191         // FIXME: if areagrid_marknumber wraps, all entities need their
192         // ent->priv.server->areagridmarknumber reset
193         world->areagrid_stats_calls++;
194         world->areagrid_marknumber++;
195         igridmins[0] = (int) floor((paddedmins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
196         igridmins[1] = (int) floor((paddedmins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
197         //igridmins[2] = (int) ((paddedmins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
198         igridmaxs[0] = (int) floor((paddedmaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
199         igridmaxs[1] = (int) floor((paddedmaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
200         //igridmaxs[2] = (int) ((paddedmaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
201         igridmins[0] = max(0, igridmins[0]);
202         igridmins[1] = max(0, igridmins[1]);
203         //igridmins[2] = max(0, igridmins[2]);
204         igridmaxs[0] = min(AREA_GRID, igridmaxs[0]);
205         igridmaxs[1] = min(AREA_GRID, igridmaxs[1]);
206         //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]);
207
208         // paranoid debugging
209         //VectorSet(igridmins, 0, 0, 0);VectorSet(igridmaxs, AREA_GRID, AREA_GRID, AREA_GRID);
210
211         numlist = 0;
212         // add entities not linked into areagrid because they are too big or
213         // outside the grid bounds
214         if (world->areagrid_outside.next != &world->areagrid_outside)
215         {
216                 grid = &world->areagrid_outside;
217                 for (l = grid->next;l != grid;l = l->next)
218                 {
219                         ent = PRVM_EDICT_NUM(l->entitynumber);
220                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
221                         {
222                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
223                                 if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
224                                 {
225                                         if (numlist < maxlist)
226                                                 list[numlist] = ent;
227                                         numlist++;
228                                 }
229                                 world->areagrid_stats_entitychecks++;
230                         }
231                 }
232         }
233         // add grid linked entities
234         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
235         {
236                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
237                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
238                 {
239                         if (grid->next != grid)
240                         {
241                                 for (l = grid->next;l != grid;l = l->next)
242                                 {
243                                         ent = PRVM_EDICT_NUM(l->entitynumber);
244                                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
245                                         {
246                                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
247                                                 if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
248                                                 {
249                                                         if (numlist < maxlist)
250                                                                 list[numlist] = ent;
251                                                         numlist++;
252                                                 }
253                                                 //Con_Printf("%d %f %f %f %f %f %f : %d : %f %f %f %f %f %f\n", BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs), ent->priv.server->areamins[0], ent->priv.server->areamins[1], ent->priv.server->areamins[2], ent->priv.server->areamaxs[0], ent->priv.server->areamaxs[1], ent->priv.server->areamaxs[2], PRVM_NUM_FOR_EDICT(ent), mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
254                                         }
255                                         world->areagrid_stats_entitychecks++;
256                                 }
257                         }
258                 }
259         }
260         return numlist;
261 }
262
263 static void World_LinkEdict_AreaGrid(world_t *world, prvm_edict_t *ent)
264 {
265         prvm_prog_t *prog = world->prog;
266         link_t *grid;
267         int igrid[3], igridmins[3], igridmaxs[3], gridnum, entitynumber = PRVM_NUM_FOR_EDICT(ent);
268
269         if (entitynumber <= 0 || entitynumber >= prog->max_edicts || PRVM_EDICT_NUM(entitynumber) != ent)
270         {
271                 Con_Printf ("World_LinkEdict_AreaGrid: invalid edict %p (edicts is %p, edict compared to prog->edicts is %i)\n", (void *)ent, (void *)prog->edicts, entitynumber);
272                 return;
273         }
274
275         igridmins[0] = (int) floor((ent->priv.server->areamins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
276         igridmins[1] = (int) floor((ent->priv.server->areamins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
277         //igridmins[2] = (int) floor((ent->priv.server->areamins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
278         igridmaxs[0] = (int) floor((ent->priv.server->areamaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
279         igridmaxs[1] = (int) floor((ent->priv.server->areamaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
280         //igridmaxs[2] = (int) floor((ent->priv.server->areamaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
281         if (igridmins[0] < 0 || igridmaxs[0] > AREA_GRID || igridmins[1] < 0 || igridmaxs[1] > AREA_GRID || ((igridmaxs[0] - igridmins[0]) * (igridmaxs[1] - igridmins[1])) > ENTITYGRIDAREAS)
282         {
283                 // wow, something outside the grid, store it as such
284                 World_InsertLinkBefore (&ent->priv.server->areagrid[0], &world->areagrid_outside, entitynumber);
285                 return;
286         }
287
288         gridnum = 0;
289         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
290         {
291                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
292                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++, gridnum++)
293                         World_InsertLinkBefore (&ent->priv.server->areagrid[gridnum], grid, entitynumber);
294         }
295 }
296
297 /*
298 ===============
299 World_LinkEdict
300
301 ===============
302 */
303 void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const vec3_t maxs)
304 {
305         prvm_prog_t *prog = world->prog;
306         // unlink from old position first
307         if (ent->priv.server->areagrid[0].prev)
308                 World_UnlinkEdict(ent);
309
310         // don't add the world
311         if (ent == prog->edicts)
312                 return;
313
314         // don't add free entities
315         if (ent->priv.server->free)
316                 return;
317
318         VectorCopy(mins, ent->priv.server->areamins);
319         VectorCopy(maxs, ent->priv.server->areamaxs);
320         World_LinkEdict_AreaGrid(world, ent);
321 }
322
323
324
325
326 //============================================================================
327 // physics engine support
328 //============================================================================
329
330 #ifndef ODE_STATIC
331 # define ODE_DYNAMIC 1
332 #endif
333
334 #if defined(ODE_STATIC) || defined(ODE_DYNAMIC)
335 #define USEODE 1
336 #endif
337
338 // recent ODE trunk has dWorldStepFast1 removed
339 //#define ODE_USE_STEPFAST
340
341 #ifdef USEODE
342 cvar_t physics_ode_quadtree_depth = {0, "physics_ode_quadtree_depth","5", "desired subdivision level of quadtree culling space"};
343 cvar_t physics_ode_contactsurfacelayer = {0, "physics_ode_contactsurfacelayer","1", "allows objects to overlap this many units to reduce jitter"};
344 cvar_t physics_ode_worldstep = {0, "physics_ode_worldstep","2", "step function to use, 0 - dWorldStep, 1 - dWorldStepFast1, 2 - dWorldQuickStep"};
345 cvar_t physics_ode_worldstep_iterations = {0, "physics_ode_worldstep_iterations", "20", "parameter to dWorldQuickStep and dWorldStepFast1"};
346 cvar_t physics_ode_contact_mu = {0, "physics_ode_contact_mu", "1", "contact solver mu parameter - friction pyramid approximation 1 (see ODE User Guide)"};
347 cvar_t physics_ode_contact_erp = {0, "physics_ode_contact_erp", "0.96", "contact solver erp parameter - Error Restitution Percent (see ODE User Guide)"};
348 cvar_t physics_ode_contact_cfm = {0, "physics_ode_contact_cfm", "0", "contact solver cfm parameter - Constraint Force Mixing (see ODE User Guide)"};
349 cvar_t physics_ode_world_erp = {0, "physics_ode_world_erp", "-1", "world solver erp parameter - Error Restitution Percent (see ODE User Guide); use defaults when set to -1"};
350 cvar_t physics_ode_world_cfm = {0, "physics_ode_world_cfm", "-1", "world solver cfm parameter - Constraint Force Mixing (see ODE User Guide); not touched when -1"};
351 cvar_t physics_ode_world_damping = {0, "physics_ode_world_damping", "1", "enabled damping scale (see ODE User Guide), this scales all damping values, be aware that behavior depends of step type"};
352 cvar_t physics_ode_world_damping_linear = {0, "physics_ode_world_damping_linear", "0.005", "world linear damping scale (see ODE User Guide); use defaults when set to -1"};
353 cvar_t physics_ode_world_damping_linear_threshold = {0, "physics_ode_world_damping_linear_threshold", "0.01", "world linear damping threshold (see ODE User Guide); use defaults when set to -1"};
354 cvar_t physics_ode_world_damping_angular = {0, "physics_ode_world_damping_angular", "0.005", "world angular damping scale (see ODE User Guide); use defaults when set to -1"};
355 cvar_t physics_ode_world_damping_angular_threshold = {0, "physics_ode_world_damping_angular_threshold", "0.01", "world angular damping threshold (see ODE User Guide); use defaults when set to -1"};
356 cvar_t physics_ode_world_gravitymod = {0, "physics_ode_world_gravitymod", "1", "multiplies gravity got from sv_gravity, this may be needed to tweak if strong damping is used"};
357 cvar_t physics_ode_iterationsperframe = {0, "physics_ode_iterationsperframe", "1", "divisor for time step, runs multiple physics steps per frame"};
358 cvar_t physics_ode_constantstep = {0, "physics_ode_constantstep", "1", "use constant step (sys_ticrate value) instead of variable step which tends to increase stability"};
359 cvar_t physics_ode_autodisable = {0, "physics_ode_autodisable", "1", "automatic disabling of objects which dont move for long period of time, makes object stacking a lot faster"};
360 cvar_t physics_ode_autodisable_steps = {0, "physics_ode_autodisable_steps", "10", "how many steps object should be dormant to be autodisabled"};
361 cvar_t physics_ode_autodisable_time = {0, "physics_ode_autodisable_time", "0", "how many seconds object should be dormant to be autodisabled"};
362 cvar_t physics_ode_autodisable_threshold_linear = {0, "physics_ode_autodisable_threshold_linear", "0.2", "body will be disabled if it's linear move below this value"};
363 cvar_t physics_ode_autodisable_threshold_angular = {0, "physics_ode_autodisable_threshold_angular", "0.3", "body will be disabled if it's angular move below this value"};
364 cvar_t physics_ode_autodisable_threshold_samples = {0, "physics_ode_autodisable_threshold_samples", "5", "average threshold with this number of samples"};
365 cvar_t physics_ode_movelimit = {0, "physics_ode_movelimit", "0.5", "clamp velocity if a single move would exceed this percentage of object thickness, to prevent flying through walls, be aware that behavior depends of step type"};
366 cvar_t physics_ode_spinlimit = {0, "physics_ode_spinlimit", "10000", "reset spin velocity if it gets too large"};
367 cvar_t physics_ode_trick_fixnan = {0, "physics_ode_trick_fixnan", "1", "engine trick that checks and fixes NaN velocity/origin/angles on objects, a value of 2 makes console prints on each fix"};
368 cvar_t physics_ode_printstats = {0, "physics_ode_printstats", "0", "print ODE stats each frame"};
369 cvar_t physics_ode = {0, "physics_ode", "0", "run ODE physics (VERY experimental and potentially buggy)"};
370
371 // LordHavoc: this large chunk of definitions comes from the ODE library
372 // include files.
373
374 #ifdef ODE_STATIC
375 #include "ode/ode.h"
376 #else
377 #ifdef WINAPI
378 // ODE does not use WINAPI
379 #define ODE_API
380 #else
381 #define ODE_API
382 #endif
383
384 // note: dynamic builds of ODE tend to be double precision, this is not used
385 // for static builds
386 typedef double dReal;
387
388 typedef dReal dVector3[4];
389 typedef dReal dVector4[4];
390 typedef dReal dMatrix3[4*3];
391 typedef dReal dMatrix4[4*4];
392 typedef dReal dMatrix6[8*6];
393 typedef dReal dQuaternion[4];
394
395 struct dxWorld;         /* dynamics world */
396 struct dxSpace;         /* collision space */
397 struct dxBody;          /* rigid body (dynamics object) */
398 struct dxGeom;          /* geometry (collision object) */
399 struct dxJoint;
400 struct dxJointNode;
401 struct dxJointGroup;
402 struct dxTriMeshData;
403
404 #define dInfinity 3.402823466e+38f
405
406 typedef struct dxWorld *dWorldID;
407 typedef struct dxSpace *dSpaceID;
408 typedef struct dxBody *dBodyID;
409 typedef struct dxGeom *dGeomID;
410 typedef struct dxJoint *dJointID;
411 typedef struct dxJointGroup *dJointGroupID;
412 typedef struct dxTriMeshData *dTriMeshDataID;
413
414 typedef struct dJointFeedback
415 {
416         dVector3 f1;            /* force applied to body 1 */
417         dVector3 t1;            /* torque applied to body 1 */
418         dVector3 f2;            /* force applied to body 2 */
419         dVector3 t2;            /* torque applied to body 2 */
420 }
421 dJointFeedback;
422
423 typedef enum dJointType
424 {
425         dJointTypeNone = 0,
426         dJointTypeBall,
427         dJointTypeHinge,
428         dJointTypeSlider,
429         dJointTypeContact,
430         dJointTypeUniversal,
431         dJointTypeHinge2,
432         dJointTypeFixed,
433         dJointTypeNull,
434         dJointTypeAMotor,
435         dJointTypeLMotor,
436         dJointTypePlane2D,
437         dJointTypePR,
438         dJointTypePU,
439         dJointTypePiston
440 }
441 dJointType;
442
443 #define D_ALL_PARAM_NAMES(start) \
444   /* parameters for limits and motors */ \
445   dParamLoStop = start, \
446   dParamHiStop, \
447   dParamVel, \
448   dParamFMax, \
449   dParamFudgeFactor, \
450   dParamBounce, \
451   dParamCFM, \
452   dParamStopERP, \
453   dParamStopCFM, \
454   /* parameters for suspension */ \
455   dParamSuspensionERP, \
456   dParamSuspensionCFM, \
457   dParamERP, \
458
459 #define D_ALL_PARAM_NAMES_X(start,x) \
460   /* parameters for limits and motors */ \
461   dParamLoStop ## x = start, \
462   dParamHiStop ## x, \
463   dParamVel ## x, \
464   dParamFMax ## x, \
465   dParamFudgeFactor ## x, \
466   dParamBounce ## x, \
467   dParamCFM ## x, \
468   dParamStopERP ## x, \
469   dParamStopCFM ## x, \
470   /* parameters for suspension */ \
471   dParamSuspensionERP ## x, \
472   dParamSuspensionCFM ## x, \
473   dParamERP ## x,
474
475 enum {
476   D_ALL_PARAM_NAMES(0)
477   D_ALL_PARAM_NAMES_X(0x100,2)
478   D_ALL_PARAM_NAMES_X(0x200,3)
479
480   /* add a multiple of this constant to the basic parameter numbers to get
481    * the parameters for the second, third etc axes.
482    */
483   dParamGroup=0x100
484 };
485
486 typedef struct dMass
487 {
488         dReal mass;
489         dVector3 c;
490         dMatrix3 I;
491 }
492 dMass;
493
494 enum
495 {
496         dContactMu2                     = 0x001,
497         dContactFDir1           = 0x002,
498         dContactBounce          = 0x004,
499         dContactSoftERP         = 0x008,
500         dContactSoftCFM         = 0x010,
501         dContactMotion1         = 0x020,
502         dContactMotion2         = 0x040,
503         dContactMotionN         = 0x080,
504         dContactSlip1           = 0x100,
505         dContactSlip2           = 0x200,
506         
507         dContactApprox0         = 0x0000,
508         dContactApprox1_1       = 0x1000,
509         dContactApprox1_2       = 0x2000,
510         dContactApprox1         = 0x3000
511 };
512
513 typedef struct dSurfaceParameters
514 {
515         /* must always be defined */
516         int mode;
517         dReal mu;
518
519         /* only defined if the corresponding flag is set in mode */
520         dReal mu2;
521         dReal bounce;
522         dReal bounce_vel;
523         dReal soft_erp;
524         dReal soft_cfm;
525         dReal motion1,motion2,motionN;
526         dReal slip1,slip2;
527 } dSurfaceParameters;
528
529 typedef struct dContactGeom
530 {
531         dVector3 pos;          ///< contact position
532         dVector3 normal;       ///< normal vector
533         dReal depth;           ///< penetration depth
534         dGeomID g1,g2;         ///< the colliding geoms
535         int side1,side2;       ///< (to be documented)
536 }
537 dContactGeom;
538
539 typedef struct dContact
540 {
541         dSurfaceParameters surface;
542         dContactGeom geom;
543         dVector3 fdir1;
544 }
545 dContact;
546
547 typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2);
548
549 // SAP
550 // Order XZY or ZXY usually works best, if your Y is up.
551 #define dSAP_AXES_XYZ  ((0)|(1<<2)|(2<<4))
552 #define dSAP_AXES_XZY  ((0)|(2<<2)|(1<<4))
553 #define dSAP_AXES_YXZ  ((1)|(0<<2)|(2<<4))
554 #define dSAP_AXES_YZX  ((1)|(2<<2)|(0<<4))
555 #define dSAP_AXES_ZXY  ((2)|(0<<2)|(1<<4))
556 #define dSAP_AXES_ZYX  ((2)|(1<<2)|(0<<4))
557
558 //const char*     (ODE_API *dGetConfiguration)(void);
559 int             (ODE_API *dCheckConfiguration)( const char* token );
560 int             (ODE_API *dInitODE)(void);
561 //int             (ODE_API *dInitODE2)(unsigned int uiInitFlags);
562 //int             (ODE_API *dAllocateODEDataForThread)(unsigned int uiAllocateFlags);
563 //void            (ODE_API *dCleanupODEAllDataForThread)(void);
564 void            (ODE_API *dCloseODE)(void);
565
566 //int             (ODE_API *dMassCheck)(const dMass *m);
567 //void            (ODE_API *dMassSetZero)(dMass *);
568 //void            (ODE_API *dMassSetParameters)(dMass *, dReal themass, dReal cgx, dReal cgy, dReal cgz, dReal I11, dReal I22, dReal I33, dReal I12, dReal I13, dReal I23);
569 //void            (ODE_API *dMassSetSphere)(dMass *, dReal density, dReal radius);
570 void            (ODE_API *dMassSetSphereTotal)(dMass *, dReal total_mass, dReal radius);
571 //void            (ODE_API *dMassSetCapsule)(dMass *, dReal density, int direction, dReal radius, dReal length);
572 void            (ODE_API *dMassSetCapsuleTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length);
573 //void            (ODE_API *dMassSetCylinder)(dMass *, dReal density, int direction, dReal radius, dReal length);
574 void            (ODE_API *dMassSetCylinderTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length);
575 //void            (ODE_API *dMassSetBox)(dMass *, dReal density, dReal lx, dReal ly, dReal lz);
576 void            (ODE_API *dMassSetBoxTotal)(dMass *, dReal total_mass, dReal lx, dReal ly, dReal lz);
577 //void            (ODE_API *dMassSetTrimesh)(dMass *, dReal density, dGeomID g);
578 //void            (ODE_API *dMassSetTrimeshTotal)(dMass *m, dReal total_mass, dGeomID g);
579 //void            (ODE_API *dMassAdjust)(dMass *, dReal newmass);
580 //void            (ODE_API *dMassTranslate)(dMass *, dReal x, dReal y, dReal z);
581 //void            (ODE_API *dMassRotate)(dMass *, const dMatrix3 R);
582 //void            (ODE_API *dMassAdd)(dMass *a, const dMass *b);
583 //
584 dWorldID        (ODE_API *dWorldCreate)(void);
585 void            (ODE_API *dWorldDestroy)(dWorldID world);
586 void            (ODE_API *dWorldSetGravity)(dWorldID, dReal x, dReal y, dReal z);
587 void            (ODE_API *dWorldGetGravity)(dWorldID, dVector3 gravity);
588 void            (ODE_API *dWorldSetERP)(dWorldID, dReal erp);
589 //dReal           (ODE_API *dWorldGetERP)(dWorldID);
590 void            (ODE_API *dWorldSetCFM)(dWorldID, dReal cfm);
591 //dReal           (ODE_API *dWorldGetCFM)(dWorldID);
592 void            (ODE_API *dWorldStep)(dWorldID, dReal stepsize);
593 //void            (ODE_API *dWorldImpulseToForce)(dWorldID, dReal stepsize, dReal ix, dReal iy, dReal iz, dVector3 force);
594 void            (ODE_API *dWorldQuickStep)(dWorldID w, dReal stepsize);
595 void            (ODE_API *dWorldSetQuickStepNumIterations)(dWorldID, int num);
596 //int             (ODE_API *dWorldGetQuickStepNumIterations)(dWorldID);
597 //void            (ODE_API *dWorldSetQuickStepW)(dWorldID, dReal over_relaxation);
598 //dReal           (ODE_API *dWorldGetQuickStepW)(dWorldID);
599 //void            (ODE_API *dWorldSetContactMaxCorrectingVel)(dWorldID, dReal vel);
600 //dReal           (ODE_API *dWorldGetContactMaxCorrectingVel)(dWorldID);
601 void            (ODE_API *dWorldSetContactSurfaceLayer)(dWorldID, dReal depth);
602 //dReal           (ODE_API *dWorldGetContactSurfaceLayer)(dWorldID);
603 #ifdef ODE_USE_STEPFAST
604 void            (ODE_API *dWorldStepFast1)(dWorldID, dReal stepsize, int maxiterations);
605 #endif
606 //void            (ODE_API *dWorldSetAutoEnableDepthSF1)(dWorldID, int autoEnableDepth);
607 //int             (ODE_API *dWorldGetAutoEnableDepthSF1)(dWorldID);
608 //dReal           (ODE_API *dWorldGetAutoDisableLinearThreshold)(dWorldID);
609 void            (ODE_API *dWorldSetAutoDisableLinearThreshold)(dWorldID, dReal linear_threshold);
610 //dReal           (ODE_API *dWorldGetAutoDisableAngularThreshold)(dWorldID);
611 void            (ODE_API *dWorldSetAutoDisableAngularThreshold)(dWorldID, dReal angular_threshold);
612 //dReal           (ODE_API *dWorldGetAutoDisableLinearAverageThreshold)(dWorldID);
613 //void            (ODE_API *dWorldSetAutoDisableLinearAverageThreshold)(dWorldID, dReal linear_average_threshold);
614 //dReal           (ODE_API *dWorldGetAutoDisableAngularAverageThreshold)(dWorldID);
615 //void            (ODE_API *dWorldSetAutoDisableAngularAverageThreshold)(dWorldID, dReal angular_average_threshold);
616 //int             (ODE_API *dWorldGetAutoDisableAverageSamplesCount)(dWorldID);
617 void            (ODE_API *dWorldSetAutoDisableAverageSamplesCount)(dWorldID, unsigned int average_samples_count );
618 //int             (ODE_API *dWorldGetAutoDisableSteps)(dWorldID);
619 void            (ODE_API *dWorldSetAutoDisableSteps)(dWorldID, int steps);
620 //dReal           (ODE_API *dWorldGetAutoDisableTime)(dWorldID);
621 void            (ODE_API *dWorldSetAutoDisableTime)(dWorldID, dReal time);
622 //int             (ODE_API *dWorldGetAutoDisableFlag)(dWorldID);
623 void            (ODE_API *dWorldSetAutoDisableFlag)(dWorldID, int do_auto_disable);
624 //dReal           (ODE_API *dWorldGetLinearDampingThreshold)(dWorldID w);
625 void            (ODE_API *dWorldSetLinearDampingThreshold)(dWorldID w, dReal threshold);
626 //dReal           (ODE_API *dWorldGetAngularDampingThreshold)(dWorldID w);
627 void            (ODE_API *dWorldSetAngularDampingThreshold)(dWorldID w, dReal threshold);
628 //dReal           (ODE_API *dWorldGetLinearDamping)(dWorldID w);
629 void            (ODE_API *dWorldSetLinearDamping)(dWorldID w, dReal scale);
630 //dReal           (ODE_API *dWorldGetAngularDamping)(dWorldID w);
631 void            (ODE_API *dWorldSetAngularDamping)(dWorldID w, dReal scale);
632 //void            (ODE_API *dWorldSetDamping)(dWorldID w, dReal linear_scale, dReal angular_scale);
633 //dReal           (ODE_API *dWorldGetMaxAngularSpeed)(dWorldID w);
634 //void            (ODE_API *dWorldSetMaxAngularSpeed)(dWorldID w, dReal max_speed);
635 //dReal           (ODE_API *dBodyGetAutoDisableLinearThreshold)(dBodyID);
636 //void            (ODE_API *dBodySetAutoDisableLinearThreshold)(dBodyID, dReal linear_average_threshold);
637 //dReal           (ODE_API *dBodyGetAutoDisableAngularThreshold)(dBodyID);
638 //void            (ODE_API *dBodySetAutoDisableAngularThreshold)(dBodyID, dReal angular_average_threshold);
639 //int             (ODE_API *dBodyGetAutoDisableAverageSamplesCount)(dBodyID);
640 //void            (ODE_API *dBodySetAutoDisableAverageSamplesCount)(dBodyID, unsigned int average_samples_count);
641 //int             (ODE_API *dBodyGetAutoDisableSteps)(dBodyID);
642 //void            (ODE_API *dBodySetAutoDisableSteps)(dBodyID, int steps);
643 //dReal           (ODE_API *dBodyGetAutoDisableTime)(dBodyID);
644 //void            (ODE_API *dBodySetAutoDisableTime)(dBodyID, dReal time);
645 //int             (ODE_API *dBodyGetAutoDisableFlag)(dBodyID);
646 //void            (ODE_API *dBodySetAutoDisableFlag)(dBodyID, int do_auto_disable);
647 //void            (ODE_API *dBodySetAutoDisableDefaults)(dBodyID);
648 //dWorldID        (ODE_API *dBodyGetWorld)(dBodyID);
649 dBodyID         (ODE_API *dBodyCreate)(dWorldID);
650 void            (ODE_API *dBodyDestroy)(dBodyID);
651 void            (ODE_API *dBodySetData)(dBodyID, void *data);
652 void *          (ODE_API *dBodyGetData)(dBodyID);
653 void            (ODE_API *dBodySetPosition)(dBodyID, dReal x, dReal y, dReal z);
654 void            (ODE_API *dBodySetRotation)(dBodyID, const dMatrix3 R);
655 //void            (ODE_API *dBodySetQuaternion)(dBodyID, const dQuaternion q);
656 void            (ODE_API *dBodySetLinearVel)(dBodyID, dReal x, dReal y, dReal z);
657 void            (ODE_API *dBodySetAngularVel)(dBodyID, dReal x, dReal y, dReal z);
658 const dReal *   (ODE_API *dBodyGetPosition)(dBodyID);
659 //void            (ODE_API *dBodyCopyPosition)(dBodyID body, dVector3 pos);
660 const dReal *   (ODE_API *dBodyGetRotation)(dBodyID);
661 //void            (ODE_API *dBodyCopyRotation)(dBodyID, dMatrix3 R);
662 //const dReal *   (ODE_API *dBodyGetQuaternion)(dBodyID);
663 //void            (ODE_API *dBodyCopyQuaternion)(dBodyID body, dQuaternion quat);
664 const dReal *   (ODE_API *dBodyGetLinearVel)(dBodyID);
665 const dReal *   (ODE_API *dBodyGetAngularVel)(dBodyID);
666 void            (ODE_API *dBodySetMass)(dBodyID, const dMass *mass);
667 //void            (ODE_API *dBodyGetMass)(dBodyID, dMass *mass);
668 //void            (ODE_API *dBodyAddForce)(dBodyID, dReal fx, dReal fy, dReal fz);
669 //void            (ODE_API *dBodyAddTorque)(dBodyID, dReal fx, dReal fy, dReal fz);
670 //void            (ODE_API *dBodyAddRelForce)(dBodyID, dReal fx, dReal fy, dReal fz);
671 void            (ODE_API *dBodyAddRelTorque)(dBodyID, dReal fx, dReal fy, dReal fz);
672 //void            (ODE_API *dBodyAddForceAtPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz);
673 void            (ODE_API *dBodyAddForceAtRelPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz);
674 //void            (ODE_API *dBodyAddRelForceAtPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz);
675 //void            (ODE_API *dBodyAddRelForceAtRelPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz);
676 //const dReal *   (ODE_API *dBodyGetForce)(dBodyID);
677 //const dReal *   (ODE_API *dBodyGetTorque)(dBodyID);
678 //void            (ODE_API *dBodySetForce)(dBodyID b, dReal x, dReal y, dReal z);
679 //void            (ODE_API *dBodySetTorque)(dBodyID b, dReal x, dReal y, dReal z);
680 //void            (ODE_API *dBodyGetRelPointPos)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
681 //void            (ODE_API *dBodyGetRelPointVel)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
682 //void            (ODE_API *dBodyGetPointVel)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
683 //void            (ODE_API *dBodyGetPosRelPoint)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
684 //void            (ODE_API *dBodyVectorToWorld)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
685 //void            (ODE_API *dBodyVectorFromWorld)(dBodyID, dReal px, dReal py, dReal pz, dVector3 result);
686 //void            (ODE_API *dBodySetFiniteRotationMode)(dBodyID, int mode);
687 //void            (ODE_API *dBodySetFiniteRotationAxis)(dBodyID, dReal x, dReal y, dReal z);
688 //int             (ODE_API *dBodyGetFiniteRotationMode)(dBodyID);
689 //void            (ODE_API *dBodyGetFiniteRotationAxis)(dBodyID, dVector3 result);
690 int             (ODE_API *dBodyGetNumJoints)(dBodyID b);
691 dJointID        (ODE_API *dBodyGetJoint)(dBodyID, int index);
692 //void            (ODE_API *dBodySetDynamic)(dBodyID);
693 //void            (ODE_API *dBodySetKinematic)(dBodyID);
694 //int             (ODE_API *dBodyIsKinematic)(dBodyID);
695 void            (ODE_API *dBodyEnable)(dBodyID);
696 void            (ODE_API *dBodyDisable)(dBodyID);
697 int             (ODE_API *dBodyIsEnabled)(dBodyID);
698 void            (ODE_API *dBodySetGravityMode)(dBodyID b, int mode);
699 int             (ODE_API *dBodyGetGravityMode)(dBodyID b);
700 //void            (*dBodySetMovedCallback)(dBodyID b, void(ODE_API *callback)(dBodyID));
701 //dGeomID         (ODE_API *dBodyGetFirstGeom)(dBodyID b);
702 //dGeomID         (ODE_API *dBodyGetNextGeom)(dGeomID g);
703 //void            (ODE_API *dBodySetDampingDefaults)(dBodyID b);
704 //dReal           (ODE_API *dBodyGetLinearDamping)(dBodyID b);
705 //void            (ODE_API *dBodySetLinearDamping)(dBodyID b, dReal scale);
706 //dReal           (ODE_API *dBodyGetAngularDamping)(dBodyID b);
707 //void            (ODE_API *dBodySetAngularDamping)(dBodyID b, dReal scale);
708 //void            (ODE_API *dBodySetDamping)(dBodyID b, dReal linear_scale, dReal angular_scale);
709 //dReal           (ODE_API *dBodyGetLinearDampingThreshold)(dBodyID b);
710 //void            (ODE_API *dBodySetLinearDampingThreshold)(dBodyID b, dReal threshold);
711 //dReal           (ODE_API *dBodyGetAngularDampingThreshold)(dBodyID b);
712 //void            (ODE_API *dBodySetAngularDampingThreshold)(dBodyID b, dReal threshold);
713 //dReal           (ODE_API *dBodyGetMaxAngularSpeed)(dBodyID b);
714 //void            (ODE_API *dBodySetMaxAngularSpeed)(dBodyID b, dReal max_speed);
715 //int             (ODE_API *dBodyGetGyroscopicMode)(dBodyID b);
716 //void            (ODE_API *dBodySetGyroscopicMode)(dBodyID b, int enabled);
717 dJointID        (ODE_API *dJointCreateBall)(dWorldID, dJointGroupID);
718 dJointID        (ODE_API *dJointCreateHinge)(dWorldID, dJointGroupID);
719 dJointID        (ODE_API *dJointCreateSlider)(dWorldID, dJointGroupID);
720 dJointID        (ODE_API *dJointCreateContact)(dWorldID, dJointGroupID, const dContact *);
721 dJointID        (ODE_API *dJointCreateHinge2)(dWorldID, dJointGroupID);
722 dJointID        (ODE_API *dJointCreateUniversal)(dWorldID, dJointGroupID);
723 //dJointID        (ODE_API *dJointCreatePR)(dWorldID, dJointGroupID);
724 //dJointID        (ODE_API *dJointCreatePU)(dWorldID, dJointGroupID);
725 //dJointID        (ODE_API *dJointCreatePiston)(dWorldID, dJointGroupID);
726 dJointID        (ODE_API *dJointCreateFixed)(dWorldID, dJointGroupID);
727 //dJointID        (ODE_API *dJointCreateNull)(dWorldID, dJointGroupID);
728 //dJointID        (ODE_API *dJointCreateAMotor)(dWorldID, dJointGroupID);
729 //dJointID        (ODE_API *dJointCreateLMotor)(dWorldID, dJointGroupID);
730 //dJointID        (ODE_API *dJointCreatePlane2D)(dWorldID, dJointGroupID);
731 void            (ODE_API *dJointDestroy)(dJointID);
732 dJointGroupID   (ODE_API *dJointGroupCreate)(int max_size);
733 void            (ODE_API *dJointGroupDestroy)(dJointGroupID);
734 void            (ODE_API *dJointGroupEmpty)(dJointGroupID);
735 //int             (ODE_API *dJointGetNumBodies)(dJointID);
736 void            (ODE_API *dJointAttach)(dJointID, dBodyID body1, dBodyID body2);
737 //void            (ODE_API *dJointEnable)(dJointID);
738 //void            (ODE_API *dJointDisable)(dJointID);
739 //int             (ODE_API *dJointIsEnabled)(dJointID);
740 void            (ODE_API *dJointSetData)(dJointID, void *data);
741 void *          (ODE_API *dJointGetData)(dJointID);
742 //dJointType      (ODE_API *dJointGetType)(dJointID);
743 dBodyID         (ODE_API *dJointGetBody)(dJointID, int index);
744 //void            (ODE_API *dJointSetFeedback)(dJointID, dJointFeedback *);
745 //dJointFeedback *(ODE_API *dJointGetFeedback)(dJointID);
746 void            (ODE_API *dJointSetBallAnchor)(dJointID, dReal x, dReal y, dReal z);
747 //void            (ODE_API *dJointSetBallAnchor2)(dJointID, dReal x, dReal y, dReal z);
748 void            (ODE_API *dJointSetBallParam)(dJointID, int parameter, dReal value);
749 void            (ODE_API *dJointSetHingeAnchor)(dJointID, dReal x, dReal y, dReal z);
750 //void            (ODE_API *dJointSetHingeAnchorDelta)(dJointID, dReal x, dReal y, dReal z, dReal ax, dReal ay, dReal az);
751 void            (ODE_API *dJointSetHingeAxis)(dJointID, dReal x, dReal y, dReal z);
752 //void            (ODE_API *dJointSetHingeAxisOffset)(dJointID j, dReal x, dReal y, dReal z, dReal angle);
753 void            (ODE_API *dJointSetHingeParam)(dJointID, int parameter, dReal value);
754 //void            (ODE_API *dJointAddHingeTorque)(dJointID joint, dReal torque);
755 void            (ODE_API *dJointSetSliderAxis)(dJointID, dReal x, dReal y, dReal z);
756 //void            (ODE_API *dJointSetSliderAxisDelta)(dJointID, dReal x, dReal y, dReal z, dReal ax, dReal ay, dReal az);
757 void            (ODE_API *dJointSetSliderParam)(dJointID, int parameter, dReal value);
758 //void            (ODE_API *dJointAddSliderForce)(dJointID joint, dReal force);
759 void            (ODE_API *dJointSetHinge2Anchor)(dJointID, dReal x, dReal y, dReal z);
760 void            (ODE_API *dJointSetHinge2Axis1)(dJointID, dReal x, dReal y, dReal z);
761 void            (ODE_API *dJointSetHinge2Axis2)(dJointID, dReal x, dReal y, dReal z);
762 void            (ODE_API *dJointSetHinge2Param)(dJointID, int parameter, dReal value);
763 //void            (ODE_API *dJointAddHinge2Torques)(dJointID joint, dReal torque1, dReal torque2);
764 void            (ODE_API *dJointSetUniversalAnchor)(dJointID, dReal x, dReal y, dReal z);
765 void            (ODE_API *dJointSetUniversalAxis1)(dJointID, dReal x, dReal y, dReal z);
766 //void            (ODE_API *dJointSetUniversalAxis1Offset)(dJointID, dReal x, dReal y, dReal z, dReal offset1, dReal offset2);
767 void            (ODE_API *dJointSetUniversalAxis2)(dJointID, dReal x, dReal y, dReal z);
768 //void            (ODE_API *dJointSetUniversalAxis2Offset)(dJointID, dReal x, dReal y, dReal z, dReal offset1, dReal offset2);
769 void            (ODE_API *dJointSetUniversalParam)(dJointID, int parameter, dReal value);
770 //void            (ODE_API *dJointAddUniversalTorques)(dJointID joint, dReal torque1, dReal torque2);
771 //void            (ODE_API *dJointSetPRAnchor)(dJointID, dReal x, dReal y, dReal z);
772 //void            (ODE_API *dJointSetPRAxis1)(dJointID, dReal x, dReal y, dReal z);
773 //void            (ODE_API *dJointSetPRAxis2)(dJointID, dReal x, dReal y, dReal z);
774 //void            (ODE_API *dJointSetPRParam)(dJointID, int parameter, dReal value);
775 //void            (ODE_API *dJointAddPRTorque)(dJointID j, dReal torque);
776 //void            (ODE_API *dJointSetPUAnchor)(dJointID, dReal x, dReal y, dReal z);
777 //void            (ODE_API *dJointSetPUAnchorOffset)(dJointID, dReal x, dReal y, dReal z, dReal dx, dReal dy, dReal dz);
778 //void            (ODE_API *dJointSetPUAxis1)(dJointID, dReal x, dReal y, dReal z);
779 //void            (ODE_API *dJointSetPUAxis2)(dJointID, dReal x, dReal y, dReal z);
780 //void            (ODE_API *dJointSetPUAxis3)(dJointID, dReal x, dReal y, dReal z);
781 //void            (ODE_API *dJointSetPUAxisP)(dJointID id, dReal x, dReal y, dReal z);
782 //void            (ODE_API *dJointSetPUParam)(dJointID, int parameter, dReal value);
783 //void            (ODE_API *dJointAddPUTorque)(dJointID j, dReal torque);
784 //void            (ODE_API *dJointSetPistonAnchor)(dJointID, dReal x, dReal y, dReal z);
785 //void            (ODE_API *dJointSetPistonAnchorOffset)(dJointID j, dReal x, dReal y, dReal z, dReal dx, dReal dy, dReal dz);
786 //void            (ODE_API *dJointSetPistonParam)(dJointID, int parameter, dReal value);
787 //void            (ODE_API *dJointAddPistonForce)(dJointID joint, dReal force);
788 //void            (ODE_API *dJointSetFixed)(dJointID);
789 //void            (ODE_API *dJointSetFixedParam)(dJointID, int parameter, dReal value);
790 //void            (ODE_API *dJointSetAMotorNumAxes)(dJointID, int num);
791 //void            (ODE_API *dJointSetAMotorAxis)(dJointID, int anum, int rel, dReal x, dReal y, dReal z);
792 //void            (ODE_API *dJointSetAMotorAngle)(dJointID, int anum, dReal angle);
793 //void            (ODE_API *dJointSetAMotorParam)(dJointID, int parameter, dReal value);
794 //void            (ODE_API *dJointSetAMotorMode)(dJointID, int mode);
795 //void            (ODE_API *dJointAddAMotorTorques)(dJointID, dReal torque1, dReal torque2, dReal torque3);
796 //void            (ODE_API *dJointSetLMotorNumAxes)(dJointID, int num);
797 //void            (ODE_API *dJointSetLMotorAxis)(dJointID, int anum, int rel, dReal x, dReal y, dReal z);
798 //void            (ODE_API *dJointSetLMotorParam)(dJointID, int parameter, dReal value);
799 //void            (ODE_API *dJointSetPlane2DXParam)(dJointID, int parameter, dReal value);
800 //void            (ODE_API *dJointSetPlane2DYParam)(dJointID, int parameter, dReal value);
801 //void            (ODE_API *dJointSetPlane2DAngleParam)(dJointID, int parameter, dReal value);
802 //void            (ODE_API *dJointGetBallAnchor)(dJointID, dVector3 result);
803 //void            (ODE_API *dJointGetBallAnchor2)(dJointID, dVector3 result);
804 //dReal           (ODE_API *dJointGetBallParam)(dJointID, int parameter);
805 //void            (ODE_API *dJointGetHingeAnchor)(dJointID, dVector3 result);
806 //void            (ODE_API *dJointGetHingeAnchor2)(dJointID, dVector3 result);
807 //void            (ODE_API *dJointGetHingeAxis)(dJointID, dVector3 result);
808 //dReal           (ODE_API *dJointGetHingeParam)(dJointID, int parameter);
809 //dReal           (ODE_API *dJointGetHingeAngle)(dJointID);
810 //dReal           (ODE_API *dJointGetHingeAngleRate)(dJointID);
811 //dReal           (ODE_API *dJointGetSliderPosition)(dJointID);
812 //dReal           (ODE_API *dJointGetSliderPositionRate)(dJointID);
813 //void            (ODE_API *dJointGetSliderAxis)(dJointID, dVector3 result);
814 //dReal           (ODE_API *dJointGetSliderParam)(dJointID, int parameter);
815 //void            (ODE_API *dJointGetHinge2Anchor)(dJointID, dVector3 result);
816 //void            (ODE_API *dJointGetHinge2Anchor2)(dJointID, dVector3 result);
817 //void            (ODE_API *dJointGetHinge2Axis1)(dJointID, dVector3 result);
818 //void            (ODE_API *dJointGetHinge2Axis2)(dJointID, dVector3 result);
819 //dReal           (ODE_API *dJointGetHinge2Param)(dJointID, int parameter);
820 //dReal           (ODE_API *dJointGetHinge2Angle1)(dJointID);
821 //dReal           (ODE_API *dJointGetHinge2Angle1Rate)(dJointID);
822 //dReal           (ODE_API *dJointGetHinge2Angle2Rate)(dJointID);
823 //void            (ODE_API *dJointGetUniversalAnchor)(dJointID, dVector3 result);
824 //void            (ODE_API *dJointGetUniversalAnchor2)(dJointID, dVector3 result);
825 //void            (ODE_API *dJointGetUniversalAxis1)(dJointID, dVector3 result);
826 //void            (ODE_API *dJointGetUniversalAxis2)(dJointID, dVector3 result);
827 //dReal           (ODE_API *dJointGetUniversalParam)(dJointID, int parameter);
828 //void            (ODE_API *dJointGetUniversalAngles)(dJointID, dReal *angle1, dReal *angle2);
829 //dReal           (ODE_API *dJointGetUniversalAngle1)(dJointID);
830 //dReal           (ODE_API *dJointGetUniversalAngle2)(dJointID);
831 //dReal           (ODE_API *dJointGetUniversalAngle1Rate)(dJointID);
832 //dReal           (ODE_API *dJointGetUniversalAngle2Rate)(dJointID);
833 //void            (ODE_API *dJointGetPRAnchor)(dJointID, dVector3 result);
834 //dReal           (ODE_API *dJointGetPRPosition)(dJointID);
835 //dReal           (ODE_API *dJointGetPRPositionRate)(dJointID);
836 //dReal           (ODE_API *dJointGetPRAngle)(dJointID);
837 //dReal           (ODE_API *dJointGetPRAngleRate)(dJointID);
838 //void            (ODE_API *dJointGetPRAxis1)(dJointID, dVector3 result);
839 //void            (ODE_API *dJointGetPRAxis2)(dJointID, dVector3 result);
840 //dReal           (ODE_API *dJointGetPRParam)(dJointID, int parameter);
841 //void            (ODE_API *dJointGetPUAnchor)(dJointID, dVector3 result);
842 //dReal           (ODE_API *dJointGetPUPosition)(dJointID);
843 //dReal           (ODE_API *dJointGetPUPositionRate)(dJointID);
844 //void            (ODE_API *dJointGetPUAxis1)(dJointID, dVector3 result);
845 //void            (ODE_API *dJointGetPUAxis2)(dJointID, dVector3 result);
846 //void            (ODE_API *dJointGetPUAxis3)(dJointID, dVector3 result);
847 //void            (ODE_API *dJointGetPUAxisP)(dJointID id, dVector3 result);
848 //void            (ODE_API *dJointGetPUAngles)(dJointID, dReal *angle1, dReal *angle2);
849 //dReal           (ODE_API *dJointGetPUAngle1)(dJointID);
850 //dReal           (ODE_API *dJointGetPUAngle1Rate)(dJointID);
851 //dReal           (ODE_API *dJointGetPUAngle2)(dJointID);
852 //dReal           (ODE_API *dJointGetPUAngle2Rate)(dJointID);
853 //dReal           (ODE_API *dJointGetPUParam)(dJointID, int parameter);
854 //dReal           (ODE_API *dJointGetPistonPosition)(dJointID);
855 //dReal           (ODE_API *dJointGetPistonPositionRate)(dJointID);
856 //dReal           (ODE_API *dJointGetPistonAngle)(dJointID);
857 //dReal           (ODE_API *dJointGetPistonAngleRate)(dJointID);
858 //void            (ODE_API *dJointGetPistonAnchor)(dJointID, dVector3 result);
859 //void            (ODE_API *dJointGetPistonAnchor2)(dJointID, dVector3 result);
860 //void            (ODE_API *dJointGetPistonAxis)(dJointID, dVector3 result);
861 //dReal           (ODE_API *dJointGetPistonParam)(dJointID, int parameter);
862 //int             (ODE_API *dJointGetAMotorNumAxes)(dJointID);
863 //void            (ODE_API *dJointGetAMotorAxis)(dJointID, int anum, dVector3 result);
864 //int             (ODE_API *dJointGetAMotorAxisRel)(dJointID, int anum);
865 //dReal           (ODE_API *dJointGetAMotorAngle)(dJointID, int anum);
866 //dReal           (ODE_API *dJointGetAMotorAngleRate)(dJointID, int anum);
867 //dReal           (ODE_API *dJointGetAMotorParam)(dJointID, int parameter);
868 //int             (ODE_API *dJointGetAMotorMode)(dJointID);
869 //int             (ODE_API *dJointGetLMotorNumAxes)(dJointID);
870 //void            (ODE_API *dJointGetLMotorAxis)(dJointID, int anum, dVector3 result);
871 //dReal           (ODE_API *dJointGetLMotorParam)(dJointID, int parameter);
872 //dReal           (ODE_API *dJointGetFixedParam)(dJointID, int parameter);
873 //dJointID        (ODE_API *dConnectingJoint)(dBodyID, dBodyID);
874 //int             (ODE_API *dConnectingJointList)(dBodyID, dBodyID, dJointID*);
875 int             (ODE_API *dAreConnected)(dBodyID, dBodyID);
876 int             (ODE_API *dAreConnectedExcluding)(dBodyID body1, dBodyID body2, int joint_type);
877 //
878 dSpaceID        (ODE_API *dSimpleSpaceCreate)(dSpaceID space);
879 dSpaceID        (ODE_API *dHashSpaceCreate)(dSpaceID space);
880 dSpaceID        (ODE_API *dQuadTreeSpaceCreate)(dSpaceID space, const dVector3 Center, const dVector3 Extents, int Depth);
881 //dSpaceID        (ODE_API *dSweepAndPruneSpaceCreate)( dSpaceID space, int axisorder );
882 void            (ODE_API *dSpaceDestroy)(dSpaceID);
883 //void            (ODE_API *dHashSpaceSetLevels)(dSpaceID space, int minlevel, int maxlevel);
884 //void            (ODE_API *dHashSpaceGetLevels)(dSpaceID space, int *minlevel, int *maxlevel);
885 //void            (ODE_API *dSpaceSetCleanup)(dSpaceID space, int mode);
886 //int             (ODE_API *dSpaceGetCleanup)(dSpaceID space);
887 //void            (ODE_API *dSpaceSetSublevel)(dSpaceID space, int sublevel);
888 //int             (ODE_API *dSpaceGetSublevel)(dSpaceID space);
889 //void            (ODE_API *dSpaceSetManualCleanup)(dSpaceID space, int mode);
890 //int             (ODE_API *dSpaceGetManualCleanup)(dSpaceID space);
891 //void            (ODE_API *dSpaceAdd)(dSpaceID, dGeomID);
892 //void            (ODE_API *dSpaceRemove)(dSpaceID, dGeomID);
893 //int             (ODE_API *dSpaceQuery)(dSpaceID, dGeomID);
894 //void            (ODE_API *dSpaceClean)(dSpaceID);
895 //int             (ODE_API *dSpaceGetNumGeoms)(dSpaceID);
896 //dGeomID         (ODE_API *dSpaceGetGeom)(dSpaceID, int i);
897 //int             (ODE_API *dSpaceGetClass)(dSpaceID space);
898 //
899 void            (ODE_API *dGeomDestroy)(dGeomID geom);
900 void            (ODE_API *dGeomSetData)(dGeomID geom, void* data);
901 void *          (ODE_API *dGeomGetData)(dGeomID geom);
902 void            (ODE_API *dGeomSetBody)(dGeomID geom, dBodyID body);
903 dBodyID         (ODE_API *dGeomGetBody)(dGeomID geom);
904 void            (ODE_API *dGeomSetPosition)(dGeomID geom, dReal x, dReal y, dReal z);
905 void            (ODE_API *dGeomSetRotation)(dGeomID geom, const dMatrix3 R);
906 //void            (ODE_API *dGeomSetQuaternion)(dGeomID geom, const dQuaternion Q);
907 //const dReal *   (ODE_API *dGeomGetPosition)(dGeomID geom);
908 //void            (ODE_API *dGeomCopyPosition)(dGeomID geom, dVector3 pos);
909 //const dReal *   (ODE_API *dGeomGetRotation)(dGeomID geom);
910 //void            (ODE_API *dGeomCopyRotation)(dGeomID geom, dMatrix3 R);
911 //void            (ODE_API *dGeomGetQuaternion)(dGeomID geom, dQuaternion result);
912 //void            (ODE_API *dGeomGetAABB)(dGeomID geom, dReal aabb[6]);
913 int             (ODE_API *dGeomIsSpace)(dGeomID geom);
914 //dSpaceID        (ODE_API *dGeomGetSpace)(dGeomID);
915 //int             (ODE_API *dGeomGetClass)(dGeomID geom);
916 //void            (ODE_API *dGeomSetCategoryBits)(dGeomID geom, unsigned long bits);
917 //void            (ODE_API *dGeomSetCollideBits)(dGeomID geom, unsigned long bits);
918 //unsigned long   (ODE_API *dGeomGetCategoryBits)(dGeomID);
919 //unsigned long   (ODE_API *dGeomGetCollideBits)(dGeomID);
920 //void            (ODE_API *dGeomEnable)(dGeomID geom);
921 //void            (ODE_API *dGeomDisable)(dGeomID geom);
922 //int             (ODE_API *dGeomIsEnabled)(dGeomID geom);
923 //void            (ODE_API *dGeomSetOffsetPosition)(dGeomID geom, dReal x, dReal y, dReal z);
924 //void            (ODE_API *dGeomSetOffsetRotation)(dGeomID geom, const dMatrix3 R);
925 //void            (ODE_API *dGeomSetOffsetQuaternion)(dGeomID geom, const dQuaternion Q);
926 //void            (ODE_API *dGeomSetOffsetWorldPosition)(dGeomID geom, dReal x, dReal y, dReal z);
927 //void            (ODE_API *dGeomSetOffsetWorldRotation)(dGeomID geom, const dMatrix3 R);
928 //void            (ODE_API *dGeomSetOffsetWorldQuaternion)(dGeomID geom, const dQuaternion);
929 //void            (ODE_API *dGeomClearOffset)(dGeomID geom);
930 //int             (ODE_API *dGeomIsOffset)(dGeomID geom);
931 //const dReal *   (ODE_API *dGeomGetOffsetPosition)(dGeomID geom);
932 //void            (ODE_API *dGeomCopyOffsetPosition)(dGeomID geom, dVector3 pos);
933 //const dReal *   (ODE_API *dGeomGetOffsetRotation)(dGeomID geom);
934 //void            (ODE_API *dGeomCopyOffsetRotation)(dGeomID geom, dMatrix3 R);
935 //void            (ODE_API *dGeomGetOffsetQuaternion)(dGeomID geom, dQuaternion result);
936 int             (ODE_API *dCollide)(dGeomID o1, dGeomID o2, int flags, dContactGeom *contact, int skip);
937 //
938 void            (ODE_API *dSpaceCollide)(dSpaceID space, void *data, dNearCallback *callback);
939 void            (ODE_API *dSpaceCollide2)(dGeomID space1, dGeomID space2, void *data, dNearCallback *callback);
940 //
941 dGeomID         (ODE_API *dCreateSphere)(dSpaceID space, dReal radius);
942 //void            (ODE_API *dGeomSphereSetRadius)(dGeomID sphere, dReal radius);
943 //dReal           (ODE_API *dGeomSphereGetRadius)(dGeomID sphere);
944 //dReal           (ODE_API *dGeomSpherePointDepth)(dGeomID sphere, dReal x, dReal y, dReal z);
945 //
946 //dGeomID         (ODE_API *dCreateConvex)(dSpaceID space, dReal *_planes, unsigned int _planecount, dReal *_points, unsigned int _pointcount,unsigned int *_polygons);
947 //void            (ODE_API *dGeomSetConvex)(dGeomID g, dReal *_planes, unsigned int _count, dReal *_points, unsigned int _pointcount,unsigned int *_polygons);
948 //
949 dGeomID         (ODE_API *dCreateBox)(dSpaceID space, dReal lx, dReal ly, dReal lz);
950 //void            (ODE_API *dGeomBoxSetLengths)(dGeomID box, dReal lx, dReal ly, dReal lz);
951 //void            (ODE_API *dGeomBoxGetLengths)(dGeomID box, dVector3 result);
952 //dReal           (ODE_API *dGeomBoxPointDepth)(dGeomID box, dReal x, dReal y, dReal z);
953 //dReal           (ODE_API *dGeomBoxPointDepth)(dGeomID box, dReal x, dReal y, dReal z);
954 //
955 //dGeomID         (ODE_API *dCreatePlane)(dSpaceID space, dReal a, dReal b, dReal c, dReal d);
956 //void            (ODE_API *dGeomPlaneSetParams)(dGeomID plane, dReal a, dReal b, dReal c, dReal d);
957 //void            (ODE_API *dGeomPlaneGetParams)(dGeomID plane, dVector4 result);
958 //dReal           (ODE_API *dGeomPlanePointDepth)(dGeomID plane, dReal x, dReal y, dReal z);
959 //
960 dGeomID         (ODE_API *dCreateCapsule)(dSpaceID space, dReal radius, dReal length);
961 //void            (ODE_API *dGeomCapsuleSetParams)(dGeomID ccylinder, dReal radius, dReal length);
962 //void            (ODE_API *dGeomCapsuleGetParams)(dGeomID ccylinder, dReal *radius, dReal *length);
963 //dReal           (ODE_API *dGeomCapsulePointDepth)(dGeomID ccylinder, dReal x, dReal y, dReal z);
964 //
965 dGeomID         (ODE_API *dCreateCylinder)(dSpaceID space, dReal radius, dReal length);
966 //void            (ODE_API *dGeomCylinderSetParams)(dGeomID cylinder, dReal radius, dReal length);
967 //void            (ODE_API *dGeomCylinderGetParams)(dGeomID cylinder, dReal *radius, dReal *length);
968 //
969 //dGeomID         (ODE_API *dCreateRay)(dSpaceID space, dReal length);
970 //void            (ODE_API *dGeomRaySetLength)(dGeomID ray, dReal length);
971 //dReal           (ODE_API *dGeomRayGetLength)(dGeomID ray);
972 //void            (ODE_API *dGeomRaySet)(dGeomID ray, dReal px, dReal py, dReal pz, dReal dx, dReal dy, dReal dz);
973 //void            (ODE_API *dGeomRayGet)(dGeomID ray, dVector3 start, dVector3 dir);
974 //
975 dGeomID         (ODE_API *dCreateGeomTransform)(dSpaceID space);
976 void            (ODE_API *dGeomTransformSetGeom)(dGeomID g, dGeomID obj);
977 //dGeomID         (ODE_API *dGeomTransformGetGeom)(dGeomID g);
978 void            (ODE_API *dGeomTransformSetCleanup)(dGeomID g, int mode);
979 //int             (ODE_API *dGeomTransformGetCleanup)(dGeomID g);
980 //void            (ODE_API *dGeomTransformSetInfo)(dGeomID g, int mode);
981 //int             (ODE_API *dGeomTransformGetInfo)(dGeomID g);
982
983 enum { TRIMESH_FACE_NORMALS };
984 typedef int dTriCallback(dGeomID TriMesh, dGeomID RefObject, int TriangleIndex);
985 typedef void dTriArrayCallback(dGeomID TriMesh, dGeomID RefObject, const int* TriIndices, int TriCount);
986 typedef int dTriRayCallback(dGeomID TriMesh, dGeomID Ray, int TriangleIndex, dReal u, dReal v);
987 typedef int dTriTriMergeCallback(dGeomID TriMesh, int FirstTriangleIndex, int SecondTriangleIndex);
988
989 dTriMeshDataID  (ODE_API *dGeomTriMeshDataCreate)(void);
990 void            (ODE_API *dGeomTriMeshDataDestroy)(dTriMeshDataID g);
991 //void            (ODE_API *dGeomTriMeshDataSet)(dTriMeshDataID g, int data_id, void* in_data);
992 //void*           (ODE_API *dGeomTriMeshDataGet)(dTriMeshDataID g, int data_id);
993 //void            (*dGeomTriMeshSetLastTransform)( (ODE_API *dGeomID g, dMatrix4 last_trans );
994 //dReal*          (*dGeomTriMeshGetLastTransform)( (ODE_API *dGeomID g );
995 void            (ODE_API *dGeomTriMeshDataBuildSingle)(dTriMeshDataID g, const void* Vertices, int VertexStride, int VertexCount,  const void* Indices, int IndexCount, int TriStride);
996 //void            (ODE_API *dGeomTriMeshDataBuildSingle1)(dTriMeshDataID g, const void* Vertices, int VertexStride, int VertexCount,  const void* Indices, int IndexCount, int TriStride, const void* Normals);
997 //void            (ODE_API *dGeomTriMeshDataBuildDouble)(dTriMeshDataID g,  const void* Vertices,  int VertexStride, int VertexCount,  const void* Indices, int IndexCount, int TriStride);
998 //void            (ODE_API *dGeomTriMeshDataBuildDouble1)(dTriMeshDataID g,  const void* Vertices,  int VertexStride, int VertexCount,  const void* Indices, int IndexCount, int TriStride, const void* Normals);
999 //void            (ODE_API *dGeomTriMeshDataBuildSimple)(dTriMeshDataID g, const dReal* Vertices, int VertexCount, const dTriIndex* Indices, int IndexCount);
1000 //void            (ODE_API *dGeomTriMeshDataBuildSimple1)(dTriMeshDataID g, const dReal* Vertices, int VertexCount, const dTriIndex* Indices, int IndexCount, const int* Normals);
1001 //void            (ODE_API *dGeomTriMeshDataPreprocess)(dTriMeshDataID g);
1002 //void            (ODE_API *dGeomTriMeshDataGetBuffer)(dTriMeshDataID g, unsigned char** buf, int* bufLen);
1003 //void            (ODE_API *dGeomTriMeshDataSetBuffer)(dTriMeshDataID g, unsigned char* buf);
1004 //void            (ODE_API *dGeomTriMeshSetCallback)(dGeomID g, dTriCallback* Callback);
1005 //dTriCallback*   (ODE_API *dGeomTriMeshGetCallback)(dGeomID g);
1006 //void            (ODE_API *dGeomTriMeshSetArrayCallback)(dGeomID g, dTriArrayCallback* ArrayCallback);
1007 //dTriArrayCallback* (ODE_API *dGeomTriMeshGetArrayCallback)(dGeomID g);
1008 //void            (ODE_API *dGeomTriMeshSetRayCallback)(dGeomID g, dTriRayCallback* Callback);
1009 //dTriRayCallback* (ODE_API *dGeomTriMeshGetRayCallback)(dGeomID g);
1010 //void            (ODE_API *dGeomTriMeshSetTriMergeCallback)(dGeomID g, dTriTriMergeCallback* Callback);
1011 //dTriTriMergeCallback* (ODE_API *dGeomTriMeshGetTriMergeCallback)(dGeomID g);
1012 dGeomID         (ODE_API *dCreateTriMesh)(dSpaceID space, dTriMeshDataID Data, dTriCallback* Callback, dTriArrayCallback* ArrayCallback, dTriRayCallback* RayCallback);
1013 //void            (ODE_API *dGeomTriMeshSetData)(dGeomID g, dTriMeshDataID Data);
1014 //dTriMeshDataID  (ODE_API *dGeomTriMeshGetData)(dGeomID g);
1015 //void            (ODE_API *dGeomTriMeshEnableTC)(dGeomID g, int geomClass, int enable);
1016 //int             (ODE_API *dGeomTriMeshIsTCEnabled)(dGeomID g, int geomClass);
1017 //void            (ODE_API *dGeomTriMeshClearTCCache)(dGeomID g);
1018 //dTriMeshDataID  (ODE_API *dGeomTriMeshGetTriMeshDataID)(dGeomID g);
1019 //void            (ODE_API *dGeomTriMeshGetTriangle)(dGeomID g, int Index, dVector3* v0, dVector3* v1, dVector3* v2);
1020 //void            (ODE_API *dGeomTriMeshGetPoint)(dGeomID g, int Index, dReal u, dReal v, dVector3 Out);
1021 //int             (ODE_API *dGeomTriMeshGetTriangleCount )(dGeomID g);
1022 //void            (ODE_API *dGeomTriMeshDataUpdate)(dTriMeshDataID g);
1023
1024 static dllfunction_t odefuncs[] =
1025 {
1026 //      {"dGetConfiguration",                                                   (void **) &dGetConfiguration},
1027         {"dCheckConfiguration",                                                 (void **) &dCheckConfiguration},
1028         {"dInitODE",                                                                    (void **) &dInitODE},
1029 //      {"dInitODE2",                                                                   (void **) &dInitODE2},
1030 //      {"dAllocateODEDataForThread",                                   (void **) &dAllocateODEDataForThread},
1031 //      {"dCleanupODEAllDataForThread",                                 (void **) &dCleanupODEAllDataForThread},
1032         {"dCloseODE",                                                                   (void **) &dCloseODE},
1033 //      {"dMassCheck",                                                                  (void **) &dMassCheck},
1034 //      {"dMassSetZero",                                                                (void **) &dMassSetZero},
1035 //      {"dMassSetParameters",                                                  (void **) &dMassSetParameters},
1036 //      {"dMassSetSphere",                                                              (void **) &dMassSetSphere},
1037         {"dMassSetSphereTotal",                                                 (void **) &dMassSetSphereTotal},
1038 //      {"dMassSetCapsule",                                                             (void **) &dMassSetCapsule},
1039         {"dMassSetCapsuleTotal",                                                (void **) &dMassSetCapsuleTotal},
1040 //      {"dMassSetCylinder",                                                    (void **) &dMassSetCylinder},
1041         {"dMassSetCylinderTotal",                                               (void **) &dMassSetCylinderTotal},
1042 //      {"dMassSetBox",                                                                 (void **) &dMassSetBox},
1043         {"dMassSetBoxTotal",                                                    (void **) &dMassSetBoxTotal},
1044 //      {"dMassSetTrimesh",                                                             (void **) &dMassSetTrimesh},
1045 //      {"dMassSetTrimeshTotal",                                                (void **) &dMassSetTrimeshTotal},
1046 //      {"dMassAdjust",                                                                 (void **) &dMassAdjust},
1047 //      {"dMassTranslate",                                                              (void **) &dMassTranslate},
1048 //      {"dMassRotate",                                                                 (void **) &dMassRotate},
1049 //      {"dMassAdd",                                                                    (void **) &dMassAdd},
1050
1051         {"dWorldCreate",                                                                (void **) &dWorldCreate},
1052         {"dWorldDestroy",                                                               (void **) &dWorldDestroy},
1053         {"dWorldSetGravity",                                                    (void **) &dWorldSetGravity},
1054         {"dWorldGetGravity",                                                    (void **) &dWorldGetGravity},
1055         {"dWorldSetERP",                                                                (void **) &dWorldSetERP},
1056 //      {"dWorldGetERP",                                                                (void **) &dWorldGetERP},
1057         {"dWorldSetCFM",                                                                (void **) &dWorldSetCFM},
1058 //      {"dWorldGetCFM",                                                                (void **) &dWorldGetCFM},
1059         {"dWorldStep",                                                                  (void **) &dWorldStep},
1060 //      {"dWorldImpulseToForce",                                                (void **) &dWorldImpulseToForce},
1061         {"dWorldQuickStep",                                                             (void **) &dWorldQuickStep},
1062         {"dWorldSetQuickStepNumIterations",                             (void **) &dWorldSetQuickStepNumIterations},
1063 //      {"dWorldGetQuickStepNumIterations",                             (void **) &dWorldGetQuickStepNumIterations},
1064 //      {"dWorldSetQuickStepW",                                                 (void **) &dWorldSetQuickStepW},
1065 //      {"dWorldGetQuickStepW",                                                 (void **) &dWorldGetQuickStepW},
1066 //      {"dWorldSetContactMaxCorrectingVel",                    (void **) &dWorldSetContactMaxCorrectingVel},
1067 //      {"dWorldGetContactMaxCorrectingVel",                    (void **) &dWorldGetContactMaxCorrectingVel},
1068         {"dWorldSetContactSurfaceLayer",                                (void **) &dWorldSetContactSurfaceLayer},
1069 //      {"dWorldGetContactSurfaceLayer",                                (void **) &dWorldGetContactSurfaceLayer},
1070 #ifdef ODE_USE_STEPFAST
1071         {"dWorldStepFast1",                                                             (void **) &dWorldStepFast1},
1072 #endif
1073 //      {"dWorldSetAutoEnableDepthSF1",                                 (void **) &dWorldSetAutoEnableDepthSF1},
1074 //      {"dWorldGetAutoEnableDepthSF1",                                 (void **) &dWorldGetAutoEnableDepthSF1},
1075 //      {"dWorldGetAutoDisableLinearThreshold",                 (void **) &dWorldGetAutoDisableLinearThreshold},
1076         {"dWorldSetAutoDisableLinearThreshold",                 (void **) &dWorldSetAutoDisableLinearThreshold},
1077 //      {"dWorldGetAutoDisableAngularThreshold",                (void **) &dWorldGetAutoDisableAngularThreshold},
1078         {"dWorldSetAutoDisableAngularThreshold",                (void **) &dWorldSetAutoDisableAngularThreshold},
1079 //      {"dWorldGetAutoDisableLinearAverageThreshold",  (void **) &dWorldGetAutoDisableLinearAverageThreshold},
1080 //      {"dWorldSetAutoDisableLinearAverageThreshold",  (void **) &dWorldSetAutoDisableLinearAverageThreshold},
1081 //      {"dWorldGetAutoDisableAngularAverageThreshold", (void **) &dWorldGetAutoDisableAngularAverageThreshold},
1082 //      {"dWorldSetAutoDisableAngularAverageThreshold", (void **) &dWorldSetAutoDisableAngularAverageThreshold},
1083 //      {"dWorldGetAutoDisableAverageSamplesCount",             (void **) &dWorldGetAutoDisableAverageSamplesCount},
1084         {"dWorldSetAutoDisableAverageSamplesCount",             (void **) &dWorldSetAutoDisableAverageSamplesCount},
1085 //      {"dWorldGetAutoDisableSteps",                                   (void **) &dWorldGetAutoDisableSteps},
1086         {"dWorldSetAutoDisableSteps",                                   (void **) &dWorldSetAutoDisableSteps},
1087 //      {"dWorldGetAutoDisableTime",                                    (void **) &dWorldGetAutoDisableTime},
1088         {"dWorldSetAutoDisableTime",                                    (void **) &dWorldSetAutoDisableTime},
1089 //      {"dWorldGetAutoDisableFlag",                                    (void **) &dWorldGetAutoDisableFlag},
1090         {"dWorldSetAutoDisableFlag",                                    (void **) &dWorldSetAutoDisableFlag},
1091 //      {"dWorldGetLinearDampingThreshold",                             (void **) &dWorldGetLinearDampingThreshold},
1092         {"dWorldSetLinearDampingThreshold",                             (void **) &dWorldSetLinearDampingThreshold},
1093 //      {"dWorldGetAngularDampingThreshold",                    (void **) &dWorldGetAngularDampingThreshold},
1094         {"dWorldSetAngularDampingThreshold",                    (void **) &dWorldSetAngularDampingThreshold},
1095 //      {"dWorldGetLinearDamping",                                              (void **) &dWorldGetLinearDamping},
1096         {"dWorldSetLinearDamping",                                              (void **) &dWorldSetLinearDamping},
1097 //      {"dWorldGetAngularDamping",                                             (void **) &dWorldGetAngularDamping},
1098         {"dWorldSetAngularDamping",                                             (void **) &dWorldSetAngularDamping},
1099 //      {"dWorldSetDamping",                                                    (void **) &dWorldSetDamping},
1100 //      {"dWorldGetMaxAngularSpeed",                                    (void **) &dWorldGetMaxAngularSpeed},
1101 //      {"dWorldSetMaxAngularSpeed",                                    (void **) &dWorldSetMaxAngularSpeed},
1102 //      {"dBodyGetAutoDisableLinearThreshold",                  (void **) &dBodyGetAutoDisableLinearThreshold},
1103 //      {"dBodySetAutoDisableLinearThreshold",                  (void **) &dBodySetAutoDisableLinearThreshold},
1104 //      {"dBodyGetAutoDisableAngularThreshold",                 (void **) &dBodyGetAutoDisableAngularThreshold},
1105 //      {"dBodySetAutoDisableAngularThreshold",                 (void **) &dBodySetAutoDisableAngularThreshold},
1106 //      {"dBodyGetAutoDisableAverageSamplesCount",              (void **) &dBodyGetAutoDisableAverageSamplesCount},
1107 //      {"dBodySetAutoDisableAverageSamplesCount",              (void **) &dBodySetAutoDisableAverageSamplesCount},
1108 //      {"dBodyGetAutoDisableSteps",                                    (void **) &dBodyGetAutoDisableSteps},
1109 //      {"dBodySetAutoDisableSteps",                                    (void **) &dBodySetAutoDisableSteps},
1110 //      {"dBodyGetAutoDisableTime",                                             (void **) &dBodyGetAutoDisableTime},
1111 //      {"dBodySetAutoDisableTime",                                             (void **) &dBodySetAutoDisableTime},
1112 //      {"dBodyGetAutoDisableFlag",                                             (void **) &dBodyGetAutoDisableFlag},
1113 //      {"dBodySetAutoDisableFlag",                                             (void **) &dBodySetAutoDisableFlag},
1114 //      {"dBodySetAutoDisableDefaults",                                 (void **) &dBodySetAutoDisableDefaults},
1115 //      {"dBodyGetWorld",                                                               (void **) &dBodyGetWorld},
1116         {"dBodyCreate",                                                                 (void **) &dBodyCreate},
1117         {"dBodyDestroy",                                                                (void **) &dBodyDestroy},
1118         {"dBodySetData",                                                                (void **) &dBodySetData},
1119         {"dBodyGetData",                                                                (void **) &dBodyGetData},
1120         {"dBodySetPosition",                                                    (void **) &dBodySetPosition},
1121         {"dBodySetRotation",                                                    (void **) &dBodySetRotation},
1122 //      {"dBodySetQuaternion",                                                  (void **) &dBodySetQuaternion},
1123         {"dBodySetLinearVel",                                                   (void **) &dBodySetLinearVel},
1124         {"dBodySetAngularVel",                                                  (void **) &dBodySetAngularVel},
1125         {"dBodyGetPosition",                                                    (void **) &dBodyGetPosition},
1126 //      {"dBodyCopyPosition",                                                   (void **) &dBodyCopyPosition},
1127         {"dBodyGetRotation",                                                    (void **) &dBodyGetRotation},
1128 //      {"dBodyCopyRotation",                                                   (void **) &dBodyCopyRotation},
1129 //      {"dBodyGetQuaternion",                                                  (void **) &dBodyGetQuaternion},
1130 //      {"dBodyCopyQuaternion",                                                 (void **) &dBodyCopyQuaternion},
1131         {"dBodyGetLinearVel",                                                   (void **) &dBodyGetLinearVel},
1132         {"dBodyGetAngularVel",                                                  (void **) &dBodyGetAngularVel},
1133         {"dBodySetMass",                                                                (void **) &dBodySetMass},
1134 //      {"dBodyGetMass",                                                                (void **) &dBodyGetMass},
1135 //      {"dBodyAddForce",                                                               (void **) &dBodyAddForce},
1136 //      {"dBodyAddTorque",                                                              (void **) &dBodyAddTorque},
1137 //      {"dBodyAddRelForce",                                                    (void **) &dBodyAddRelForce},
1138         {"dBodyAddRelTorque",                                                   (void **) &dBodyAddRelTorque},
1139 //      {"dBodyAddForceAtPos",                                                  (void **) &dBodyAddForceAtPos},
1140         {"dBodyAddForceAtRelPos",                                               (void **) &dBodyAddForceAtRelPos},
1141 //      {"dBodyAddRelForceAtPos",                                               (void **) &dBodyAddRelForceAtPos},
1142 //      {"dBodyAddRelForceAtRelPos",                                    (void **) &dBodyAddRelForceAtRelPos},
1143 //      {"dBodyGetForce",                                                               (void **) &dBodyGetForce},
1144 //      {"dBodyGetTorque",                                                              (void **) &dBodyGetTorque},
1145 //      {"dBodySetForce",                                                               (void **) &dBodySetForce},
1146 //      {"dBodySetTorque",                                                              (void **) &dBodySetTorque},
1147 //      {"dBodyGetRelPointPos",                                                 (void **) &dBodyGetRelPointPos},
1148 //      {"dBodyGetRelPointVel",                                                 (void **) &dBodyGetRelPointVel},
1149 //      {"dBodyGetPointVel",                                                    (void **) &dBodyGetPointVel},
1150 //      {"dBodyGetPosRelPoint",                                                 (void **) &dBodyGetPosRelPoint},
1151 //      {"dBodyVectorToWorld",                                                  (void **) &dBodyVectorToWorld},
1152 //      {"dBodyVectorFromWorld",                                                (void **) &dBodyVectorFromWorld},
1153 //      {"dBodySetFiniteRotationMode",                                  (void **) &dBodySetFiniteRotationMode},
1154 //      {"dBodySetFiniteRotationAxis",                                  (void **) &dBodySetFiniteRotationAxis},
1155 //      {"dBodyGetFiniteRotationMode",                                  (void **) &dBodyGetFiniteRotationMode},
1156 //      {"dBodyGetFiniteRotationAxis",                                  (void **) &dBodyGetFiniteRotationAxis},
1157         {"dBodyGetNumJoints",                                                   (void **) &dBodyGetNumJoints},
1158         {"dBodyGetJoint",                                                               (void **) &dBodyGetJoint},
1159 //      {"dBodySetDynamic",                                                             (void **) &dBodySetDynamic},
1160 //      {"dBodySetKinematic",                                                   (void **) &dBodySetKinematic},
1161 //      {"dBodyIsKinematic",                                                    (void **) &dBodyIsKinematic},
1162         {"dBodyEnable",                                                                 (void **) &dBodyEnable},
1163         {"dBodyDisable",                                                                (void **) &dBodyDisable},
1164         {"dBodyIsEnabled",                                                              (void **) &dBodyIsEnabled},
1165         {"dBodySetGravityMode",                                                 (void **) &dBodySetGravityMode},
1166         {"dBodyGetGravityMode",                                                 (void **) &dBodyGetGravityMode},
1167 //      {"dBodySetMovedCallback",                                               (void **) &dBodySetMovedCallback},
1168 //      {"dBodyGetFirstGeom",                                                   (void **) &dBodyGetFirstGeom},
1169 //      {"dBodyGetNextGeom",                                                    (void **) &dBodyGetNextGeom},
1170 //      {"dBodySetDampingDefaults",                                             (void **) &dBodySetDampingDefaults},
1171 //      {"dBodyGetLinearDamping",                                               (void **) &dBodyGetLinearDamping},
1172 //      {"dBodySetLinearDamping",                                               (void **) &dBodySetLinearDamping},
1173 //      {"dBodyGetAngularDamping",                                              (void **) &dBodyGetAngularDamping},
1174 //      {"dBodySetAngularDamping",                                              (void **) &dBodySetAngularDamping},
1175 //      {"dBodySetDamping",                                                             (void **) &dBodySetDamping},
1176 //      {"dBodyGetLinearDampingThreshold",                              (void **) &dBodyGetLinearDampingThreshold},
1177 //      {"dBodySetLinearDampingThreshold",                              (void **) &dBodySetLinearDampingThreshold},
1178 //      {"dBodyGetAngularDampingThreshold",                             (void **) &dBodyGetAngularDampingThreshold},
1179 //      {"dBodySetAngularDampingThreshold",                             (void **) &dBodySetAngularDampingThreshold},
1180 //      {"dBodyGetMaxAngularSpeed",                                             (void **) &dBodyGetMaxAngularSpeed},
1181 //      {"dBodySetMaxAngularSpeed",                                             (void **) &dBodySetMaxAngularSpeed},
1182 //      {"dBodyGetGyroscopicMode",                                              (void **) &dBodyGetGyroscopicMode},
1183 //      {"dBodySetGyroscopicMode",                                              (void **) &dBodySetGyroscopicMode},
1184         {"dJointCreateBall",                                                    (void **) &dJointCreateBall},
1185         {"dJointCreateHinge",                                                   (void **) &dJointCreateHinge},
1186         {"dJointCreateSlider",                                                  (void **) &dJointCreateSlider},
1187         {"dJointCreateContact",                                                 (void **) &dJointCreateContact},
1188         {"dJointCreateHinge2",                                                  (void **) &dJointCreateHinge2},
1189         {"dJointCreateUniversal",                                               (void **) &dJointCreateUniversal},
1190 //      {"dJointCreatePR",                                                              (void **) &dJointCreatePR},
1191 //      {"dJointCreatePU",                                                              (void **) &dJointCreatePU},
1192 //      {"dJointCreatePiston",                                                  (void **) &dJointCreatePiston},
1193         {"dJointCreateFixed",                                                   (void **) &dJointCreateFixed},
1194 //      {"dJointCreateNull",                                                    (void **) &dJointCreateNull},
1195 //      {"dJointCreateAMotor",                                                  (void **) &dJointCreateAMotor},
1196 //      {"dJointCreateLMotor",                                                  (void **) &dJointCreateLMotor},
1197 //      {"dJointCreatePlane2D",                                                 (void **) &dJointCreatePlane2D},
1198         {"dJointDestroy",                                                               (void **) &dJointDestroy},
1199         {"dJointGroupCreate",                                                   (void **) &dJointGroupCreate},
1200         {"dJointGroupDestroy",                                                  (void **) &dJointGroupDestroy},
1201         {"dJointGroupEmpty",                                                    (void **) &dJointGroupEmpty},
1202 //      {"dJointGetNumBodies",                                                  (void **) &dJointGetNumBodies},
1203         {"dJointAttach",                                                                (void **) &dJointAttach},
1204 //      {"dJointEnable",                                                                (void **) &dJointEnable},
1205 //      {"dJointDisable",                                                               (void **) &dJointDisable},
1206 //      {"dJointIsEnabled",                                                             (void **) &dJointIsEnabled},
1207         {"dJointSetData",                                                               (void **) &dJointSetData},
1208         {"dJointGetData",                                                               (void **) &dJointGetData},
1209 //      {"dJointGetType",                                                               (void **) &dJointGetType},
1210         {"dJointGetBody",                                                               (void **) &dJointGetBody},
1211 //      {"dJointSetFeedback",                                                   (void **) &dJointSetFeedback},
1212 //      {"dJointGetFeedback",                                                   (void **) &dJointGetFeedback},
1213         {"dJointSetBallAnchor",                                                 (void **) &dJointSetBallAnchor},
1214 //      {"dJointSetBallAnchor2",                                                (void **) &dJointSetBallAnchor2},
1215         {"dJointSetBallParam",                                                  (void **) &dJointSetBallParam},
1216         {"dJointSetHingeAnchor",                                                (void **) &dJointSetHingeAnchor},
1217 //      {"dJointSetHingeAnchorDelta",                                   (void **) &dJointSetHingeAnchorDelta},
1218         {"dJointSetHingeAxis",                                                  (void **) &dJointSetHingeAxis},
1219 //      {"dJointSetHingeAxisOffset",                                    (void **) &dJointSetHingeAxisOffset},
1220         {"dJointSetHingeParam",                                                 (void **) &dJointSetHingeParam},
1221 //      {"dJointAddHingeTorque",                                                (void **) &dJointAddHingeTorque},
1222         {"dJointSetSliderAxis",                                                 (void **) &dJointSetSliderAxis},
1223 //      {"dJointSetSliderAxisDelta",                                    (void **) &dJointSetSliderAxisDelta},
1224         {"dJointSetSliderParam",                                                (void **) &dJointSetSliderParam},
1225 //      {"dJointAddSliderForce",                                                (void **) &dJointAddSliderForce},
1226         {"dJointSetHinge2Anchor",                                               (void **) &dJointSetHinge2Anchor},
1227         {"dJointSetHinge2Axis1",                                                (void **) &dJointSetHinge2Axis1},
1228         {"dJointSetHinge2Axis2",                                                (void **) &dJointSetHinge2Axis2},
1229         {"dJointSetHinge2Param",                                                (void **) &dJointSetHinge2Param},
1230 //      {"dJointAddHinge2Torques",                                              (void **) &dJointAddHinge2Torques},
1231         {"dJointSetUniversalAnchor",                                    (void **) &dJointSetUniversalAnchor},
1232         {"dJointSetUniversalAxis1",                                             (void **) &dJointSetUniversalAxis1},
1233 //      {"dJointSetUniversalAxis1Offset",                               (void **) &dJointSetUniversalAxis1Offset},
1234         {"dJointSetUniversalAxis2",                                             (void **) &dJointSetUniversalAxis2},
1235 //      {"dJointSetUniversalAxis2Offset",                               (void **) &dJointSetUniversalAxis2Offset},
1236         {"dJointSetUniversalParam",                                             (void **) &dJointSetUniversalParam},
1237 //      {"dJointAddUniversalTorques",                                   (void **) &dJointAddUniversalTorques},
1238 //      {"dJointSetPRAnchor",                                                   (void **) &dJointSetPRAnchor},
1239 //      {"dJointSetPRAxis1",                                                    (void **) &dJointSetPRAxis1},
1240 //      {"dJointSetPRAxis2",                                                    (void **) &dJointSetPRAxis2},
1241 //      {"dJointSetPRParam",                                                    (void **) &dJointSetPRParam},
1242 //      {"dJointAddPRTorque",                                                   (void **) &dJointAddPRTorque},
1243 //      {"dJointSetPUAnchor",                                                   (void **) &dJointSetPUAnchor},
1244 //      {"dJointSetPUAnchorOffset",                                             (void **) &dJointSetPUAnchorOffset},
1245 //      {"dJointSetPUAxis1",                                                    (void **) &dJointSetPUAxis1},
1246 //      {"dJointSetPUAxis2",                                                    (void **) &dJointSetPUAxis2},
1247 //      {"dJointSetPUAxis3",                                                    (void **) &dJointSetPUAxis3},
1248 //      {"dJointSetPUAxisP",                                                    (void **) &dJointSetPUAxisP},
1249 //      {"dJointSetPUParam",                                                    (void **) &dJointSetPUParam},
1250 //      {"dJointAddPUTorque",                                                   (void **) &dJointAddPUTorque},
1251 //      {"dJointSetPistonAnchor",                                               (void **) &dJointSetPistonAnchor},
1252 //      {"dJointSetPistonAnchorOffset",                                 (void **) &dJointSetPistonAnchorOffset},
1253 //      {"dJointSetPistonParam",                                                (void **) &dJointSetPistonParam},
1254 //      {"dJointAddPistonForce",                                                (void **) &dJointAddPistonForce},
1255 //      {"dJointSetFixed",                                                              (void **) &dJointSetFixed},
1256 //      {"dJointSetFixedParam",                                                 (void **) &dJointSetFixedParam},
1257 //      {"dJointSetAMotorNumAxes",                                              (void **) &dJointSetAMotorNumAxes},
1258 //      {"dJointSetAMotorAxis",                                                 (void **) &dJointSetAMotorAxis},
1259 //      {"dJointSetAMotorAngle",                                                (void **) &dJointSetAMotorAngle},
1260 //      {"dJointSetAMotorParam",                                                (void **) &dJointSetAMotorParam},
1261 //      {"dJointSetAMotorMode",                                                 (void **) &dJointSetAMotorMode},
1262 //      {"dJointAddAMotorTorques",                                              (void **) &dJointAddAMotorTorques},
1263 //      {"dJointSetLMotorNumAxes",                                              (void **) &dJointSetLMotorNumAxes},
1264 //      {"dJointSetLMotorAxis",                                                 (void **) &dJointSetLMotorAxis},
1265 //      {"dJointSetLMotorParam",                                                (void **) &dJointSetLMotorParam},
1266 //      {"dJointSetPlane2DXParam",                                              (void **) &dJointSetPlane2DXParam},
1267 //      {"dJointSetPlane2DYParam",                                              (void **) &dJointSetPlane2DYParam},
1268 //      {"dJointSetPlane2DAngleParam",                                  (void **) &dJointSetPlane2DAngleParam},
1269 //      {"dJointGetBallAnchor",                                                 (void **) &dJointGetBallAnchor},
1270 //      {"dJointGetBallAnchor2",                                                (void **) &dJointGetBallAnchor2},
1271 //      {"dJointGetBallParam",                                                  (void **) &dJointGetBallParam},
1272 //      {"dJointGetHingeAnchor",                                                (void **) &dJointGetHingeAnchor},
1273 //      {"dJointGetHingeAnchor2",                                               (void **) &dJointGetHingeAnchor2},
1274 //      {"dJointGetHingeAxis",                                                  (void **) &dJointGetHingeAxis},
1275 //      {"dJointGetHingeParam",                                                 (void **) &dJointGetHingeParam},
1276 //      {"dJointGetHingeAngle",                                                 (void **) &dJointGetHingeAngle},
1277 //      {"dJointGetHingeAngleRate",                                             (void **) &dJointGetHingeAngleRate},
1278 //      {"dJointGetSliderPosition",                                             (void **) &dJointGetSliderPosition},
1279 //      {"dJointGetSliderPositionRate",                                 (void **) &dJointGetSliderPositionRate},
1280 //      {"dJointGetSliderAxis",                                                 (void **) &dJointGetSliderAxis},
1281 //      {"dJointGetSliderParam",                                                (void **) &dJointGetSliderParam},
1282 //      {"dJointGetHinge2Anchor",                                               (void **) &dJointGetHinge2Anchor},
1283 //      {"dJointGetHinge2Anchor2",                                              (void **) &dJointGetHinge2Anchor2},
1284 //      {"dJointGetHinge2Axis1",                                                (void **) &dJointGetHinge2Axis1},
1285 //      {"dJointGetHinge2Axis2",                                                (void **) &dJointGetHinge2Axis2},
1286 //      {"dJointGetHinge2Param",                                                (void **) &dJointGetHinge2Param},
1287 //      {"dJointGetHinge2Angle1",                                               (void **) &dJointGetHinge2Angle1},
1288 //      {"dJointGetHinge2Angle1Rate",                                   (void **) &dJointGetHinge2Angle1Rate},
1289 //      {"dJointGetHinge2Angle2Rate",                                   (void **) &dJointGetHinge2Angle2Rate},
1290 //      {"dJointGetUniversalAnchor",                                    (void **) &dJointGetUniversalAnchor},
1291 //      {"dJointGetUniversalAnchor2",                                   (void **) &dJointGetUniversalAnchor2},
1292 //      {"dJointGetUniversalAxis1",                                             (void **) &dJointGetUniversalAxis1},
1293 //      {"dJointGetUniversalAxis2",                                             (void **) &dJointGetUniversalAxis2},
1294 //      {"dJointGetUniversalParam",                                             (void **) &dJointGetUniversalParam},
1295 //      {"dJointGetUniversalAngles",                                    (void **) &dJointGetUniversalAngles},
1296 //      {"dJointGetUniversalAngle1",                                    (void **) &dJointGetUniversalAngle1},
1297 //      {"dJointGetUniversalAngle2",                                    (void **) &dJointGetUniversalAngle2},
1298 //      {"dJointGetUniversalAngle1Rate",                                (void **) &dJointGetUniversalAngle1Rate},
1299 //      {"dJointGetUniversalAngle2Rate",                                (void **) &dJointGetUniversalAngle2Rate},
1300 //      {"dJointGetPRAnchor",                                                   (void **) &dJointGetPRAnchor},
1301 //      {"dJointGetPRPosition",                                                 (void **) &dJointGetPRPosition},
1302 //      {"dJointGetPRPositionRate",                                             (void **) &dJointGetPRPositionRate},
1303 //      {"dJointGetPRAngle",                                                    (void **) &dJointGetPRAngle},
1304 //      {"dJointGetPRAngleRate",                                                (void **) &dJointGetPRAngleRate},
1305 //      {"dJointGetPRAxis1",                                                    (void **) &dJointGetPRAxis1},
1306 //      {"dJointGetPRAxis2",                                                    (void **) &dJointGetPRAxis2},
1307 //      {"dJointGetPRParam",                                                    (void **) &dJointGetPRParam},
1308 //      {"dJointGetPUAnchor",                                                   (void **) &dJointGetPUAnchor},
1309 //      {"dJointGetPUPosition",                                                 (void **) &dJointGetPUPosition},
1310 //      {"dJointGetPUPositionRate",                                             (void **) &dJointGetPUPositionRate},
1311 //      {"dJointGetPUAxis1",                                                    (void **) &dJointGetPUAxis1},
1312 //      {"dJointGetPUAxis2",                                                    (void **) &dJointGetPUAxis2},
1313 //      {"dJointGetPUAxis3",                                                    (void **) &dJointGetPUAxis3},
1314 //      {"dJointGetPUAxisP",                                                    (void **) &dJointGetPUAxisP},
1315 //      {"dJointGetPUAngles",                                                   (void **) &dJointGetPUAngles},
1316 //      {"dJointGetPUAngle1",                                                   (void **) &dJointGetPUAngle1},
1317 //      {"dJointGetPUAngle1Rate",                                               (void **) &dJointGetPUAngle1Rate},
1318 //      {"dJointGetPUAngle2",                                                   (void **) &dJointGetPUAngle2},
1319 //      {"dJointGetPUAngle2Rate",                                               (void **) &dJointGetPUAngle2Rate},
1320 //      {"dJointGetPUParam",                                                    (void **) &dJointGetPUParam},
1321 //      {"dJointGetPistonPosition",                                             (void **) &dJointGetPistonPosition},
1322 //      {"dJointGetPistonPositionRate",                                 (void **) &dJointGetPistonPositionRate},
1323 //      {"dJointGetPistonAngle",                                                (void **) &dJointGetPistonAngle},
1324 //      {"dJointGetPistonAngleRate",                                    (void **) &dJointGetPistonAngleRate},
1325 //      {"dJointGetPistonAnchor",                                               (void **) &dJointGetPistonAnchor},
1326 //      {"dJointGetPistonAnchor2",                                              (void **) &dJointGetPistonAnchor2},
1327 //      {"dJointGetPistonAxis",                                                 (void **) &dJointGetPistonAxis},
1328 //      {"dJointGetPistonParam",                                                (void **) &dJointGetPistonParam},
1329 //      {"dJointGetAMotorNumAxes",                                              (void **) &dJointGetAMotorNumAxes},
1330 //      {"dJointGetAMotorAxis",                                                 (void **) &dJointGetAMotorAxis},
1331 //      {"dJointGetAMotorAxisRel",                                              (void **) &dJointGetAMotorAxisRel},
1332 //      {"dJointGetAMotorAngle",                                                (void **) &dJointGetAMotorAngle},
1333 //      {"dJointGetAMotorAngleRate",                                    (void **) &dJointGetAMotorAngleRate},
1334 //      {"dJointGetAMotorParam",                                                (void **) &dJointGetAMotorParam},
1335 //      {"dJointGetAMotorMode",                                                 (void **) &dJointGetAMotorMode},
1336 //      {"dJointGetLMotorNumAxes",                                              (void **) &dJointGetLMotorNumAxes},
1337 //      {"dJointGetLMotorAxis",                                                 (void **) &dJointGetLMotorAxis},
1338 //      {"dJointGetLMotorParam",                                                (void **) &dJointGetLMotorParam},
1339 //      {"dJointGetFixedParam",                                                 (void **) &dJointGetFixedParam},
1340 //      {"dConnectingJoint",                                                    (void **) &dConnectingJoint},
1341 //      {"dConnectingJointList",                                                (void **) &dConnectingJointList},
1342         {"dAreConnected",                                                               (void **) &dAreConnected},
1343         {"dAreConnectedExcluding",                                              (void **) &dAreConnectedExcluding},
1344         {"dSimpleSpaceCreate",                                                  (void **) &dSimpleSpaceCreate},
1345         {"dHashSpaceCreate",                                                    (void **) &dHashSpaceCreate},
1346         {"dQuadTreeSpaceCreate",                                                (void **) &dQuadTreeSpaceCreate},
1347 //      {"dSweepAndPruneSpaceCreate",                                   (void **) &dSweepAndPruneSpaceCreate},
1348         {"dSpaceDestroy",                                                               (void **) &dSpaceDestroy},
1349 //      {"dHashSpaceSetLevels",                                                 (void **) &dHashSpaceSetLevels},
1350 //      {"dHashSpaceGetLevels",                                                 (void **) &dHashSpaceGetLevels},
1351 //      {"dSpaceSetCleanup",                                                    (void **) &dSpaceSetCleanup},
1352 //      {"dSpaceGetCleanup",                                                    (void **) &dSpaceGetCleanup},
1353 //      {"dSpaceSetSublevel",                                                   (void **) &dSpaceSetSublevel},
1354 //      {"dSpaceGetSublevel",                                                   (void **) &dSpaceGetSublevel},
1355 //      {"dSpaceSetManualCleanup",                                              (void **) &dSpaceSetManualCleanup},
1356 //      {"dSpaceGetManualCleanup",                                              (void **) &dSpaceGetManualCleanup},
1357 //      {"dSpaceAdd",                                                                   (void **) &dSpaceAdd},
1358 //      {"dSpaceRemove",                                                                (void **) &dSpaceRemove},
1359 //      {"dSpaceQuery",                                                                 (void **) &dSpaceQuery},
1360 //      {"dSpaceClean",                                                                 (void **) &dSpaceClean},
1361 //      {"dSpaceGetNumGeoms",                                                   (void **) &dSpaceGetNumGeoms},
1362 //      {"dSpaceGetGeom",                                                               (void **) &dSpaceGetGeom},
1363 //      {"dSpaceGetClass",                                                              (void **) &dSpaceGetClass},
1364         {"dGeomDestroy",                                                                (void **) &dGeomDestroy},
1365         {"dGeomSetData",                                                                (void **) &dGeomSetData},
1366         {"dGeomGetData",                                                                (void **) &dGeomGetData},
1367         {"dGeomSetBody",                                                                (void **) &dGeomSetBody},
1368         {"dGeomGetBody",                                                                (void **) &dGeomGetBody},
1369         {"dGeomSetPosition",                                                    (void **) &dGeomSetPosition},
1370         {"dGeomSetRotation",                                                    (void **) &dGeomSetRotation},
1371 //      {"dGeomSetQuaternion",                                                  (void **) &dGeomSetQuaternion},
1372 //      {"dGeomGetPosition",                                                    (void **) &dGeomGetPosition},
1373 //      {"dGeomCopyPosition",                                                   (void **) &dGeomCopyPosition},
1374 //      {"dGeomGetRotation",                                                    (void **) &dGeomGetRotation},
1375 //      {"dGeomCopyRotation",                                                   (void **) &dGeomCopyRotation},
1376 //      {"dGeomGetQuaternion",                                                  (void **) &dGeomGetQuaternion},
1377 //      {"dGeomGetAABB",                                                                (void **) &dGeomGetAABB},
1378         {"dGeomIsSpace",                                                                (void **) &dGeomIsSpace},
1379 //      {"dGeomGetSpace",                                                               (void **) &dGeomGetSpace},
1380 //      {"dGeomGetClass",                                                               (void **) &dGeomGetClass},
1381 //      {"dGeomSetCategoryBits",                                                (void **) &dGeomSetCategoryBits},
1382 //      {"dGeomSetCollideBits",                                                 (void **) &dGeomSetCollideBits},
1383 //      {"dGeomGetCategoryBits",                                                (void **) &dGeomGetCategoryBits},
1384 //      {"dGeomGetCollideBits",                                                 (void **) &dGeomGetCollideBits},
1385 //      {"dGeomEnable",                                                                 (void **) &dGeomEnable},
1386 //      {"dGeomDisable",                                                                (void **) &dGeomDisable},
1387 //      {"dGeomIsEnabled",                                                              (void **) &dGeomIsEnabled},
1388 //      {"dGeomSetOffsetPosition",                                              (void **) &dGeomSetOffsetPosition},
1389 //      {"dGeomSetOffsetRotation",                                              (void **) &dGeomSetOffsetRotation},
1390 //      {"dGeomSetOffsetQuaternion",                                    (void **) &dGeomSetOffsetQuaternion},
1391 //      {"dGeomSetOffsetWorldPosition",                                 (void **) &dGeomSetOffsetWorldPosition},
1392 //      {"dGeomSetOffsetWorldRotation",                                 (void **) &dGeomSetOffsetWorldRotation},
1393 //      {"dGeomSetOffsetWorldQuaternion",                               (void **) &dGeomSetOffsetWorldQuaternion},
1394 //      {"dGeomClearOffset",                                                    (void **) &dGeomClearOffset},
1395 //      {"dGeomIsOffset",                                                               (void **) &dGeomIsOffset},
1396 //      {"dGeomGetOffsetPosition",                                              (void **) &dGeomGetOffsetPosition},
1397 //      {"dGeomCopyOffsetPosition",                                             (void **) &dGeomCopyOffsetPosition},
1398 //      {"dGeomGetOffsetRotation",                                              (void **) &dGeomGetOffsetRotation},
1399 //      {"dGeomCopyOffsetRotation",                                             (void **) &dGeomCopyOffsetRotation},
1400 //      {"dGeomGetOffsetQuaternion",                                    (void **) &dGeomGetOffsetQuaternion},
1401         {"dCollide",                                                                    (void **) &dCollide},
1402         {"dSpaceCollide",                                                               (void **) &dSpaceCollide},
1403         {"dSpaceCollide2",                                                              (void **) &dSpaceCollide2},
1404         {"dCreateSphere",                                                               (void **) &dCreateSphere},
1405 //      {"dGeomSphereSetRadius",                                                (void **) &dGeomSphereSetRadius},
1406 //      {"dGeomSphereGetRadius",                                                (void **) &dGeomSphereGetRadius},
1407 //      {"dGeomSpherePointDepth",                                               (void **) &dGeomSpherePointDepth},
1408 //      {"dCreateConvex",                                                               (void **) &dCreateConvex},
1409 //      {"dGeomSetConvex",                                                              (void **) &dGeomSetConvex},
1410         {"dCreateBox",                                                                  (void **) &dCreateBox},
1411 //      {"dGeomBoxSetLengths",                                                  (void **) &dGeomBoxSetLengths},
1412 //      {"dGeomBoxGetLengths",                                                  (void **) &dGeomBoxGetLengths},
1413 //      {"dGeomBoxPointDepth",                                                  (void **) &dGeomBoxPointDepth},
1414 //      {"dGeomBoxPointDepth",                                                  (void **) &dGeomBoxPointDepth},
1415 //      {"dCreatePlane",                                                                (void **) &dCreatePlane},
1416 //      {"dGeomPlaneSetParams",                                                 (void **) &dGeomPlaneSetParams},
1417 //      {"dGeomPlaneGetParams",                                                 (void **) &dGeomPlaneGetParams},
1418 //      {"dGeomPlanePointDepth",                                                (void **) &dGeomPlanePointDepth},
1419         {"dCreateCapsule",                                                              (void **) &dCreateCapsule},
1420 //      {"dGeomCapsuleSetParams",                                               (void **) &dGeomCapsuleSetParams},
1421 //      {"dGeomCapsuleGetParams",                                               (void **) &dGeomCapsuleGetParams},
1422 //      {"dGeomCapsulePointDepth",                                              (void **) &dGeomCapsulePointDepth},
1423         {"dCreateCylinder",                                                             (void **) &dCreateCylinder},
1424 //      {"dGeomCylinderSetParams",                                              (void **) &dGeomCylinderSetParams},
1425 //      {"dGeomCylinderGetParams",                                              (void **) &dGeomCylinderGetParams},
1426 //      {"dCreateRay",                                                                  (void **) &dCreateRay},
1427 //      {"dGeomRaySetLength",                                                   (void **) &dGeomRaySetLength},
1428 //      {"dGeomRayGetLength",                                                   (void **) &dGeomRayGetLength},
1429 //      {"dGeomRaySet",                                                                 (void **) &dGeomRaySet},
1430 //      {"dGeomRayGet",                                                                 (void **) &dGeomRayGet},
1431         {"dCreateGeomTransform",                                                (void **) &dCreateGeomTransform},
1432         {"dGeomTransformSetGeom",                                               (void **) &dGeomTransformSetGeom},
1433 //      {"dGeomTransformGetGeom",                                               (void **) &dGeomTransformGetGeom},
1434         {"dGeomTransformSetCleanup",                                    (void **) &dGeomTransformSetCleanup},
1435 //      {"dGeomTransformGetCleanup",                                    (void **) &dGeomTransformGetCleanup},
1436 //      {"dGeomTransformSetInfo",                                               (void **) &dGeomTransformSetInfo},
1437 //      {"dGeomTransformGetInfo",                                               (void **) &dGeomTransformGetInfo},
1438         {"dGeomTriMeshDataCreate",                      (void **) &dGeomTriMeshDataCreate},
1439         {"dGeomTriMeshDataDestroy",                     (void **) &dGeomTriMeshDataDestroy},
1440 //      {"dGeomTriMeshDataSet",                         (void **) &dGeomTriMeshDataSet},
1441 //      {"dGeomTriMeshDataGet",                         (void **) &dGeomTriMeshDataGet},
1442 //      {"dGeomTriMeshSetLastTransform",                (void **) &dGeomTriMeshSetLastTransform},
1443 //      {"dGeomTriMeshGetLastTransform",                (void **) &dGeomTriMeshGetLastTransform},
1444         {"dGeomTriMeshDataBuildSingle",                 (void **) &dGeomTriMeshDataBuildSingle},
1445 //      {"dGeomTriMeshDataBuildSingle1",                (void **) &dGeomTriMeshDataBuildSingle1},
1446 //      {"dGeomTriMeshDataBuildDouble",                 (void **) &dGeomTriMeshDataBuildDouble},
1447 //      {"dGeomTriMeshDataBuildDouble1",                (void **) &dGeomTriMeshDataBuildDouble1},
1448 //      {"dGeomTriMeshDataBuildSimple",                 (void **) &dGeomTriMeshDataBuildSimple},
1449 //      {"dGeomTriMeshDataBuildSimple1",                (void **) &dGeomTriMeshDataBuildSimple1},
1450 //      {"dGeomTriMeshDataPreprocess",                  (void **) &dGeomTriMeshDataPreprocess},
1451 //      {"dGeomTriMeshDataGetBuffer",                   (void **) &dGeomTriMeshDataGetBuffer},
1452 //      {"dGeomTriMeshDataSetBuffer",                   (void **) &dGeomTriMeshDataSetBuffer},
1453 //      {"dGeomTriMeshSetCallback",                     (void **) &dGeomTriMeshSetCallback},
1454 //      {"dGeomTriMeshGetCallback",                     (void **) &dGeomTriMeshGetCallback},
1455 //      {"dGeomTriMeshSetArrayCallback",                (void **) &dGeomTriMeshSetArrayCallback},
1456 //      {"dGeomTriMeshGetArrayCallback",                (void **) &dGeomTriMeshGetArrayCallback},
1457 //      {"dGeomTriMeshSetRayCallback",                  (void **) &dGeomTriMeshSetRayCallback},
1458 //      {"dGeomTriMeshGetRayCallback",                  (void **) &dGeomTriMeshGetRayCallback},
1459 //      {"dGeomTriMeshSetTriMergeCallback",             (void **) &dGeomTriMeshSetTriMergeCallback},
1460 //      {"dGeomTriMeshGetTriMergeCallback",             (void **) &dGeomTriMeshGetTriMergeCallback},
1461         {"dCreateTriMesh",                              (void **) &dCreateTriMesh},
1462 //      {"dGeomTriMeshSetData",                         (void **) &dGeomTriMeshSetData},
1463 //      {"dGeomTriMeshGetData",                         (void **) &dGeomTriMeshGetData},
1464 //      {"dGeomTriMeshEnableTC",                        (void **) &dGeomTriMeshEnableTC},
1465 //      {"dGeomTriMeshIsTCEnabled",                     (void **) &dGeomTriMeshIsTCEnabled},
1466 //      {"dGeomTriMeshClearTCCache",                    (void **) &dGeomTriMeshClearTCCache},
1467 //      {"dGeomTriMeshGetTriMeshDataID",                (void **) &dGeomTriMeshGetTriMeshDataID},
1468 //      {"dGeomTriMeshGetTriangle",                     (void **) &dGeomTriMeshGetTriangle},
1469 //      {"dGeomTriMeshGetPoint",                        (void **) &dGeomTriMeshGetPoint},
1470 //      {"dGeomTriMeshGetTriangleCount",                (void **) &dGeomTriMeshGetTriangleCount},
1471 //      {"dGeomTriMeshDataUpdate",                      (void **) &dGeomTriMeshDataUpdate},
1472         {NULL, NULL}
1473 };
1474
1475 // Handle for ODE DLL
1476 dllhandle_t ode_dll = NULL;
1477 #endif
1478 #endif
1479
1480 static void World_Physics_Init(void)
1481 {
1482 #ifdef USEODE
1483 #ifdef ODE_DYNAMIC
1484         const char* dllnames [] =
1485         {
1486 # if defined(WIN32)
1487                 "libode1.dll",
1488 # elif defined(MACOSX)
1489                 "libode.1.dylib",
1490 # else
1491                 "libode.so.1",
1492 # endif
1493                 NULL
1494         };
1495 #endif
1496
1497         Cvar_RegisterVariable(&physics_ode_quadtree_depth);
1498         Cvar_RegisterVariable(&physics_ode_contactsurfacelayer);
1499         Cvar_RegisterVariable(&physics_ode_worldstep);
1500         Cvar_RegisterVariable(&physics_ode_worldstep_iterations);
1501         Cvar_RegisterVariable(&physics_ode_contact_mu);
1502         Cvar_RegisterVariable(&physics_ode_contact_erp);
1503         Cvar_RegisterVariable(&physics_ode_contact_cfm);
1504         Cvar_RegisterVariable(&physics_ode_world_erp);
1505         Cvar_RegisterVariable(&physics_ode_world_cfm);
1506         Cvar_RegisterVariable(&physics_ode_world_damping);
1507         Cvar_RegisterVariable(&physics_ode_world_damping_linear);
1508         Cvar_RegisterVariable(&physics_ode_world_damping_linear_threshold);
1509         Cvar_RegisterVariable(&physics_ode_world_damping_angular);
1510         Cvar_RegisterVariable(&physics_ode_world_damping_angular_threshold);
1511         Cvar_RegisterVariable(&physics_ode_world_gravitymod);
1512         Cvar_RegisterVariable(&physics_ode_iterationsperframe);
1513         Cvar_RegisterVariable(&physics_ode_constantstep);
1514         Cvar_RegisterVariable(&physics_ode_movelimit);
1515         Cvar_RegisterVariable(&physics_ode_spinlimit);
1516         Cvar_RegisterVariable(&physics_ode_trick_fixnan);
1517         Cvar_RegisterVariable(&physics_ode_autodisable);
1518         Cvar_RegisterVariable(&physics_ode_autodisable_steps);
1519         Cvar_RegisterVariable(&physics_ode_autodisable_time);
1520         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_linear);
1521         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_angular);
1522         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_samples);
1523         Cvar_RegisterVariable(&physics_ode_printstats);
1524         Cvar_RegisterVariable(&physics_ode);
1525
1526 #ifdef ODE_DYNAMIC
1527         // Load the DLL
1528         if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs))
1529 #endif
1530         {
1531                 dInitODE();
1532 //              dInitODE2(0);
1533 #ifdef ODE_DYNAMIC
1534 # ifdef dSINGLE
1535                 if (!dCheckConfiguration("ODE_single_precision"))
1536 # else
1537                 if (!dCheckConfiguration("ODE_double_precision"))
1538 # endif
1539                 {
1540 # ifdef dSINGLE
1541                         Con_Printf("ODE library not compiled for single precision - incompatible!  Not using ODE physics.\n");
1542 # else
1543                         Con_Printf("ODE library not compiled for double precision - incompatible!  Not using ODE physics.\n");
1544 # endif
1545                         Sys_UnloadLibrary(&ode_dll);
1546                         ode_dll = NULL;
1547                 }
1548                 else
1549                 {
1550 # ifdef dSINGLE
1551                         Con_Printf("ODE library loaded with single precision.\n");
1552 # else
1553                         Con_Printf("ODE library loaded with double precision.\n");
1554 # endif
1555                 }
1556 #endif
1557         }
1558 #endif
1559 }
1560
1561 static void World_Physics_Shutdown(void)
1562 {
1563 #ifdef USEODE
1564 #ifdef ODE_DYNAMIC
1565         if (ode_dll)
1566 #endif
1567         {
1568                 dCloseODE();
1569 #ifdef ODE_DYNAMIC
1570                 Sys_UnloadLibrary(&ode_dll);
1571                 ode_dll = NULL;
1572 #endif
1573         }
1574 #endif
1575 }
1576
1577 #ifdef USEODE
1578 static void World_Physics_UpdateODE(world_t *world)
1579 {
1580         dWorldID odeworld;
1581
1582         odeworld = (dWorldID)world->physics.ode_world;
1583
1584         // ERP and CFM
1585         if (physics_ode_world_erp.value >= 0)
1586                 dWorldSetERP(odeworld, physics_ode_world_erp.value);
1587         if (physics_ode_world_cfm.value >= 0)
1588                 dWorldSetCFM(odeworld, physics_ode_world_cfm.value);
1589         // Damping
1590         if (physics_ode_world_damping.integer)
1591         {
1592                 dWorldSetLinearDamping(odeworld, (physics_ode_world_damping_linear.value >= 0) ? (physics_ode_world_damping_linear.value * physics_ode_world_damping.value) : 0);
1593                 dWorldSetLinearDampingThreshold(odeworld, (physics_ode_world_damping_linear_threshold.value >= 0) ? (physics_ode_world_damping_linear_threshold.value * physics_ode_world_damping.value) : 0);
1594                 dWorldSetAngularDamping(odeworld, (physics_ode_world_damping_angular.value >= 0) ? (physics_ode_world_damping_angular.value * physics_ode_world_damping.value) : 0);
1595                 dWorldSetAngularDampingThreshold(odeworld, (physics_ode_world_damping_angular_threshold.value >= 0) ? (physics_ode_world_damping_angular_threshold.value * physics_ode_world_damping.value) : 0);
1596         }
1597         else
1598         {
1599                 dWorldSetLinearDamping(odeworld, 0);
1600                 dWorldSetLinearDampingThreshold(odeworld, 0);
1601                 dWorldSetAngularDamping(odeworld, 0);
1602                 dWorldSetAngularDampingThreshold(odeworld, 0);
1603         }
1604         // Autodisable
1605         dWorldSetAutoDisableFlag(odeworld, (physics_ode_autodisable.integer) ? 1 : 0);
1606         if (physics_ode_autodisable.integer)
1607         {
1608                 dWorldSetAutoDisableSteps(odeworld, bound(1, physics_ode_autodisable_steps.integer, 100)); 
1609                 dWorldSetAutoDisableTime(odeworld, physics_ode_autodisable_time.value);
1610                 dWorldSetAutoDisableAverageSamplesCount(odeworld, bound(1, physics_ode_autodisable_threshold_samples.integer, 100));
1611                 dWorldSetAutoDisableLinearThreshold(odeworld, physics_ode_autodisable_threshold_linear.value); 
1612                 dWorldSetAutoDisableAngularThreshold(odeworld, physics_ode_autodisable_threshold_angular.value); 
1613         }
1614 }
1615
1616 static void World_Physics_EnableODE(world_t *world)
1617 {
1618         dVector3 center, extents;
1619         if (world->physics.ode)
1620                 return;
1621 #ifdef ODE_DYNAMIC
1622         if (!ode_dll)
1623                 return;
1624 #endif
1625         world->physics.ode = true;
1626         VectorMAM(0.5f, world->mins, 0.5f, world->maxs, center);
1627         VectorSubtract(world->maxs, center, extents);
1628         world->physics.ode_world = dWorldCreate();
1629         world->physics.ode_space = dQuadTreeSpaceCreate(NULL, center, extents, bound(1, physics_ode_quadtree_depth.integer, 10));
1630         world->physics.ode_contactgroup = dJointGroupCreate(0);
1631
1632         World_Physics_UpdateODE(world);
1633 }
1634 #endif
1635
1636 static void World_Physics_Start(world_t *world)
1637 {
1638 #ifdef USEODE
1639         if (world->physics.ode)
1640                 return;
1641         World_Physics_EnableODE(world);
1642 #endif
1643 }
1644
1645 static void World_Physics_End(world_t *world)
1646 {
1647 #ifdef USEODE
1648         if (world->physics.ode)
1649         {
1650                 dWorldDestroy((dWorldID)world->physics.ode_world);
1651                 dSpaceDestroy((dSpaceID)world->physics.ode_space);
1652                 dJointGroupDestroy((dJointGroupID)world->physics.ode_contactgroup);
1653                 world->physics.ode = false;
1654         }
1655 #endif
1656 }
1657
1658 void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed)
1659 {
1660         ed->priv.server->ode_joint_type = 0;
1661 #ifdef USEODE
1662         if(ed->priv.server->ode_joint)
1663                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1664         ed->priv.server->ode_joint = NULL;
1665 #endif
1666 }
1667
1668 void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
1669 {
1670         edict_odefunc_t *f, *nf;
1671
1672         // entity is not physics controlled, free any physics data
1673         ed->priv.server->ode_physics = false;
1674 #ifdef USEODE
1675         if (ed->priv.server->ode_geom)
1676                 dGeomDestroy((dGeomID)ed->priv.server->ode_geom);
1677         ed->priv.server->ode_geom = NULL;
1678         if (ed->priv.server->ode_body)
1679         {
1680                 dJointID j;
1681                 dBodyID b1, b2;
1682                 prvm_edict_t *ed2;
1683                 while(dBodyGetNumJoints((dBodyID)ed->priv.server->ode_body))
1684                 {
1685                         j = dBodyGetJoint((dBodyID)ed->priv.server->ode_body, 0);
1686                         ed2 = (prvm_edict_t *) dJointGetData(j);
1687                         b1 = dJointGetBody(j, 0);
1688                         b2 = dJointGetBody(j, 1);
1689                         if(b1 == (dBodyID)ed->priv.server->ode_body)
1690                         {
1691                                 b1 = 0;
1692                                 ed2->priv.server->ode_joint_enemy = 0;
1693                         }
1694                         if(b2 == (dBodyID)ed->priv.server->ode_body)
1695                         {
1696                                 b2 = 0;
1697                                 ed2->priv.server->ode_joint_aiment = 0;
1698                         }
1699                         dJointAttach(j, b1, b2);
1700                 }
1701                 dBodyDestroy((dBodyID)ed->priv.server->ode_body);
1702         }
1703         ed->priv.server->ode_body = NULL;
1704 #endif
1705         if (ed->priv.server->ode_vertex3f)
1706                 Mem_Free(ed->priv.server->ode_vertex3f);
1707         ed->priv.server->ode_vertex3f = NULL;
1708         ed->priv.server->ode_numvertices = 0;
1709         if (ed->priv.server->ode_element3i)
1710                 Mem_Free(ed->priv.server->ode_element3i);
1711         ed->priv.server->ode_element3i = NULL;
1712         ed->priv.server->ode_numtriangles = 0;
1713         if(ed->priv.server->ode_massbuf)
1714                 Mem_Free(ed->priv.server->ode_massbuf);
1715         ed->priv.server->ode_massbuf = NULL;
1716         // clear functions stack
1717         for(f = ed->priv.server->ode_func; f; f = nf)
1718         {
1719                 nf = f->next;
1720                 Mem_Free(f);
1721         }
1722         ed->priv.server->ode_func = NULL;
1723 }
1724
1725 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f)
1726 {
1727         dBodyID body = (dBodyID)ed->priv.server->ode_body;
1728
1729 #ifdef USEODE
1730         switch(f->type)
1731         {
1732         case ODEFUNC_ENABLE:
1733                 dBodyEnable(body);
1734                 break;
1735         case ODEFUNC_DISABLE:
1736                 dBodyDisable(body);
1737                 break;
1738         case ODEFUNC_RELFORCEATPOS:
1739                 dBodyEnable(body);
1740                 dBodyAddForceAtRelPos(body, f->v1[0], f->v1[1], f->v1[2], f->v2[0], f->v2[1], f->v2[2]);
1741                 break;
1742         case ODEFUNC_RELTORQUE:
1743                 dBodyEnable(body);
1744                 dBodyAddRelTorque(body, f->v1[0], f->v1[1], f->v1[2]);
1745                 break;
1746         default:
1747                 break;
1748         }
1749 #endif
1750 }
1751
1752 #ifdef USEODE
1753 static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
1754 {
1755         prvm_prog_t *prog = world->prog;
1756         const dReal *avel;
1757         const dReal *o;
1758         const dReal *r; // for some reason dBodyGetRotation returns a [3][4] matrix
1759         const dReal *vel;
1760         dBodyID body = (dBodyID)ed->priv.server->ode_body;
1761         int movetype;
1762         matrix4x4_t bodymatrix;
1763         matrix4x4_t entitymatrix;
1764         vec3_t angles;
1765         vec3_t avelocity;
1766         vec3_t forward, left, up;
1767         vec3_t origin;
1768         vec3_t spinvelocity;
1769         vec3_t velocity;
1770         int jointtype;
1771         if (!body)
1772                 return;
1773         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1774         if (movetype != MOVETYPE_PHYSICS)
1775         {
1776                 jointtype = (int)PRVM_gameedictfloat(ed, jointtype);
1777                 switch(jointtype)
1778                 {
1779                         // TODO feed back data from physics
1780                         case JOINTTYPE_POINT:
1781                                 break;
1782                         case JOINTTYPE_HINGE:
1783                                 break;
1784                         case JOINTTYPE_SLIDER:
1785                                 break;
1786                         case JOINTTYPE_UNIVERSAL:
1787                                 break;
1788                         case JOINTTYPE_HINGE2:
1789                                 break;
1790                         case JOINTTYPE_FIXED:
1791                                 break;
1792                 }
1793                 return;
1794         }
1795         // store the physics engine data into the entity
1796         o = dBodyGetPosition(body);
1797         r = dBodyGetRotation(body);
1798         vel = dBodyGetLinearVel(body);
1799         avel = dBodyGetAngularVel(body);
1800         VectorCopy(o, origin);
1801         forward[0] = r[0];
1802         forward[1] = r[4];
1803         forward[2] = r[8];
1804         left[0] = r[1];
1805         left[1] = r[5];
1806         left[2] = r[9];
1807         up[0] = r[2];
1808         up[1] = r[6];
1809         up[2] = r[10];
1810         VectorCopy(vel, velocity);
1811         VectorCopy(avel, spinvelocity);
1812         Matrix4x4_FromVectors(&bodymatrix, forward, left, up, origin);
1813         Matrix4x4_Concat(&entitymatrix, &bodymatrix, &ed->priv.server->ode_offsetimatrix);
1814         Matrix4x4_ToVectors(&entitymatrix, forward, left, up, origin);
1815
1816         AnglesFromVectors(angles, forward, up, false);
1817         VectorSet(avelocity, RAD2DEG(spinvelocity[PITCH]), RAD2DEG(spinvelocity[ROLL]), RAD2DEG(spinvelocity[YAW]));
1818
1819         {
1820                 float pitchsign = 1;
1821                 if(prog == SVVM_prog) // FIXME some better way?
1822                 {
1823                         pitchsign = SV_GetPitchSign(prog, ed);
1824                 }
1825                 else if(prog == CLVM_prog)
1826                 {
1827                         pitchsign = CL_GetPitchSign(prog, ed);
1828                 }
1829                 angles[PITCH] *= pitchsign;
1830                 avelocity[PITCH] *= pitchsign;
1831         }
1832
1833         VectorCopy(origin, PRVM_gameedictvector(ed, origin));
1834         VectorCopy(velocity, PRVM_gameedictvector(ed, velocity));
1835         //VectorCopy(forward, PRVM_gameedictvector(ed, axis_forward));
1836         //VectorCopy(left, PRVM_gameedictvector(ed, axis_left));
1837         //VectorCopy(up, PRVM_gameedictvector(ed, axis_up));
1838         //VectorCopy(spinvelocity, PRVM_gameedictvector(ed, spinvelocity));
1839         VectorCopy(angles, PRVM_gameedictvector(ed, angles));
1840         VectorCopy(avelocity, PRVM_gameedictvector(ed, avelocity));
1841
1842         // values for BodyFromEntity to check if the qc modified anything later
1843         VectorCopy(origin, ed->priv.server->ode_origin);
1844         VectorCopy(velocity, ed->priv.server->ode_velocity);
1845         VectorCopy(angles, ed->priv.server->ode_angles);
1846         VectorCopy(avelocity, ed->priv.server->ode_avelocity);
1847         ed->priv.server->ode_gravity = dBodyGetGravityMode(body) != 0;
1848
1849         if(prog == SVVM_prog) // FIXME some better way?
1850         {
1851                 SV_LinkEdict(ed);
1852                 SV_LinkEdict_TouchAreaGrid(ed);
1853         }
1854 }
1855
1856 static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed)
1857 {
1858         prvm_prog_t *prog = world->prog;
1859         dJointID j = 0;
1860         dBodyID b1 = 0;
1861         dBodyID b2 = 0;
1862         int movetype = 0;
1863         int jointtype = 0;
1864         int enemy = 0, aiment = 0;
1865         vec3_t origin, velocity, angles, forward, left, up, movedir;
1866         vec_t CFM, ERP, FMax, Stop, Vel;
1867         VectorClear(origin);
1868         VectorClear(velocity);
1869         VectorClear(angles);
1870         VectorClear(movedir);
1871         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1872         jointtype = (int)PRVM_gameedictfloat(ed, jointtype);
1873         enemy = PRVM_gameedictedict(ed, enemy);
1874         aiment = PRVM_gameedictedict(ed, aiment);
1875         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
1876         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
1877         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
1878         VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
1879         if(movetype == MOVETYPE_PHYSICS)
1880                 jointtype = 0; // can't have both
1881         if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
1882                 enemy = 0;
1883         if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
1884                 aiment = 0;
1885         // see http://www.ode.org/old_list_archives/2006-January/017614.html
1886         // we want to set ERP? make it fps independent and work like a spring constant
1887         // note: if movedir[2] is 0, it becomes ERP = 1, CFM = 1.0 / (H * K)
1888         if(movedir[0] > 0 && movedir[1] > 0)
1889         {
1890                 float K = movedir[0];
1891                 float D = movedir[1];
1892                 float R = 2.0 * D * sqrt(K); // we assume D is premultiplied by sqrt(sprungMass)
1893                 CFM = 1.0 / (world->physics.ode_step * K + R); // always > 0
1894                 ERP = world->physics.ode_step * K * CFM;
1895                 Vel = 0;
1896                 FMax = 0;
1897                 Stop = movedir[2];
1898         }
1899         else if(movedir[1] < 0)
1900         {
1901                 CFM = 0;
1902                 ERP = 0;
1903                 Vel = movedir[0];
1904                 FMax = -movedir[1]; // TODO do we need to multiply with world.physics.ode_step?
1905                 Stop = movedir[2] > 0 ? movedir[2] : dInfinity;
1906         }
1907         else // movedir[0] > 0, movedir[1] == 0 or movedir[0] < 0, movedir[1] >= 0
1908         {
1909                 CFM = 0;
1910                 ERP = 0;
1911                 Vel = 0;
1912                 FMax = 0;
1913                 Stop = dInfinity;
1914         }
1915         if(jointtype == ed->priv.server->ode_joint_type && VectorCompare(origin, ed->priv.server->ode_joint_origin) && VectorCompare(velocity, ed->priv.server->ode_joint_velocity) && VectorCompare(angles, ed->priv.server->ode_joint_angles) && enemy == ed->priv.server->ode_joint_enemy && aiment == ed->priv.server->ode_joint_aiment && VectorCompare(movedir, ed->priv.server->ode_joint_movedir))
1916                 return; // nothing to do
1917         AngleVectorsFLU(angles, forward, left, up);
1918         switch(jointtype)
1919         {
1920                 case JOINTTYPE_POINT:
1921                         j = dJointCreateBall((dWorldID)world->physics.ode_world, 0);
1922                         break;
1923                 case JOINTTYPE_HINGE:
1924                         j = dJointCreateHinge((dWorldID)world->physics.ode_world, 0);
1925                         break;
1926                 case JOINTTYPE_SLIDER:
1927                         j = dJointCreateSlider((dWorldID)world->physics.ode_world, 0);
1928                         break;
1929                 case JOINTTYPE_UNIVERSAL:
1930                         j = dJointCreateUniversal((dWorldID)world->physics.ode_world, 0);
1931                         break;
1932                 case JOINTTYPE_HINGE2:
1933                         j = dJointCreateHinge2((dWorldID)world->physics.ode_world, 0);
1934                         break;
1935                 case JOINTTYPE_FIXED:
1936                         j = dJointCreateFixed((dWorldID)world->physics.ode_world, 0);
1937                         break;
1938                 case 0:
1939                 default:
1940                         // no joint
1941                         j = 0;
1942                         break;
1943         }
1944         if(ed->priv.server->ode_joint)
1945         {
1946                 //Con_Printf("deleted old joint %i\n", (int) (ed - prog->edicts));
1947                 dJointAttach((dJointID)ed->priv.server->ode_joint, 0, 0);
1948                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1949         }
1950         ed->priv.server->ode_joint = (void *) j;
1951         ed->priv.server->ode_joint_type = jointtype;
1952         ed->priv.server->ode_joint_enemy = enemy;
1953         ed->priv.server->ode_joint_aiment = aiment;
1954         VectorCopy(origin, ed->priv.server->ode_joint_origin);
1955         VectorCopy(velocity, ed->priv.server->ode_joint_velocity);
1956         VectorCopy(angles, ed->priv.server->ode_joint_angles);
1957         VectorCopy(movedir, ed->priv.server->ode_joint_movedir);
1958         if(j)
1959         {
1960                 //Con_Printf("made new joint %i\n", (int) (ed - prog->edicts));
1961                 dJointSetData(j, (void *) ed);
1962                 if(enemy)
1963                         b1 = (dBodyID)prog->edicts[enemy].priv.server->ode_body;
1964                 if(aiment)
1965                         b2 = (dBodyID)prog->edicts[aiment].priv.server->ode_body;
1966                 dJointAttach(j, b1, b2);
1967
1968                 switch(jointtype)
1969                 {
1970                         case JOINTTYPE_POINT:
1971                                 dJointSetBallAnchor(j, origin[0], origin[1], origin[2]);
1972                                 break;
1973                         case JOINTTYPE_HINGE:
1974                                 dJointSetHingeAnchor(j, origin[0], origin[1], origin[2]);
1975                                 dJointSetHingeAxis(j, forward[0], forward[1], forward[2]);
1976                                 dJointSetHingeParam(j, dParamFMax, FMax);
1977                                 dJointSetHingeParam(j, dParamHiStop, Stop);
1978                                 dJointSetHingeParam(j, dParamLoStop, -Stop);
1979                                 dJointSetHingeParam(j, dParamStopCFM, CFM);
1980                                 dJointSetHingeParam(j, dParamStopERP, ERP);
1981                                 dJointSetHingeParam(j, dParamVel, Vel);
1982                                 break;
1983                         case JOINTTYPE_SLIDER:
1984                                 dJointSetSliderAxis(j, forward[0], forward[1], forward[2]);
1985                                 dJointSetSliderParam(j, dParamFMax, FMax);
1986                                 dJointSetSliderParam(j, dParamHiStop, Stop);
1987                                 dJointSetSliderParam(j, dParamLoStop, -Stop);
1988                                 dJointSetSliderParam(j, dParamStopCFM, CFM);
1989                                 dJointSetSliderParam(j, dParamStopERP, ERP);
1990                                 dJointSetSliderParam(j, dParamVel, Vel);
1991                                 break;
1992                         case JOINTTYPE_UNIVERSAL:
1993                                 dJointSetUniversalAnchor(j, origin[0], origin[1], origin[2]);
1994                                 dJointSetUniversalAxis1(j, forward[0], forward[1], forward[2]);
1995                                 dJointSetUniversalAxis2(j, up[0], up[1], up[2]);
1996                                 dJointSetUniversalParam(j, dParamFMax, FMax);
1997                                 dJointSetUniversalParam(j, dParamHiStop, Stop);
1998                                 dJointSetUniversalParam(j, dParamLoStop, -Stop);
1999                                 dJointSetUniversalParam(j, dParamStopCFM, CFM);
2000                                 dJointSetUniversalParam(j, dParamStopERP, ERP);
2001                                 dJointSetUniversalParam(j, dParamVel, Vel);
2002                                 dJointSetUniversalParam(j, dParamFMax2, FMax);
2003                                 dJointSetUniversalParam(j, dParamHiStop2, Stop);
2004                                 dJointSetUniversalParam(j, dParamLoStop2, -Stop);
2005                                 dJointSetUniversalParam(j, dParamStopCFM2, CFM);
2006                                 dJointSetUniversalParam(j, dParamStopERP2, ERP);
2007                                 dJointSetUniversalParam(j, dParamVel2, Vel);
2008                                 break;
2009                         case JOINTTYPE_HINGE2:
2010                                 dJointSetHinge2Anchor(j, origin[0], origin[1], origin[2]);
2011                                 dJointSetHinge2Axis1(j, forward[0], forward[1], forward[2]);
2012                                 dJointSetHinge2Axis2(j, velocity[0], velocity[1], velocity[2]);
2013                                 dJointSetHinge2Param(j, dParamFMax, FMax);
2014                                 dJointSetHinge2Param(j, dParamHiStop, Stop);
2015                                 dJointSetHinge2Param(j, dParamLoStop, -Stop);
2016                                 dJointSetHinge2Param(j, dParamStopCFM, CFM);
2017                                 dJointSetHinge2Param(j, dParamStopERP, ERP);
2018                                 dJointSetHinge2Param(j, dParamVel, Vel);
2019                                 dJointSetHinge2Param(j, dParamFMax2, FMax);
2020                                 dJointSetHinge2Param(j, dParamHiStop2, Stop);
2021                                 dJointSetHinge2Param(j, dParamLoStop2, -Stop);
2022                                 dJointSetHinge2Param(j, dParamStopCFM2, CFM);
2023                                 dJointSetHinge2Param(j, dParamStopERP2, ERP);
2024                                 dJointSetHinge2Param(j, dParamVel2, Vel);
2025                                 break;
2026                         case JOINTTYPE_FIXED:
2027                                 break;
2028                         case 0:
2029                         default:
2030                                 Sys_Error("what? but above the joint was valid...\n");
2031                                 break;
2032                 }
2033 #undef SETPARAMS
2034
2035         }
2036 }
2037
2038 static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
2039 {
2040         prvm_prog_t *prog = world->prog;
2041         const float *iv;
2042         const int *ie;
2043         dBodyID body = (dBodyID)ed->priv.server->ode_body;
2044         dMass mass;
2045         dReal test;
2046         const dReal *ovelocity, *ospinvelocity;
2047         void *dataID;
2048         dVector3 capsulerot[3];
2049         dp_model_t *model;
2050         float *ov;
2051         int *oe;
2052         int axisindex;
2053         int modelindex = 0;
2054         int movetype = MOVETYPE_NONE;
2055         int numtriangles;
2056         int numvertices;
2057         int solid = SOLID_NOT;
2058         int triangleindex;
2059         int vertexindex;
2060         mempool_t *mempool;
2061         qboolean modified = false;
2062         vec3_t angles;
2063         vec3_t avelocity;
2064         vec3_t entmaxs;
2065         vec3_t entmins;
2066         vec3_t forward;
2067         vec3_t geomcenter;
2068         vec3_t geomsize;
2069         vec3_t left;
2070         vec3_t origin;
2071         vec3_t spinvelocity;
2072         vec3_t up;
2073         vec3_t velocity;
2074         vec_t f;
2075         vec_t length;
2076         vec_t massval = 1.0f;
2077         vec_t movelimit;
2078         vec_t radius;
2079         vec_t scale = 1.0f;
2080         vec_t spinlimit;
2081         qboolean gravity;
2082         edict_odefunc_t *func, *nextf;
2083
2084 #ifdef ODE_DYNAMIC
2085         if (!ode_dll)
2086                 return;
2087 #endif
2088         VectorClear(entmins);
2089         VectorClear(entmaxs);
2090         solid = (int)PRVM_gameedictfloat(ed, solid);
2091         movetype = (int)PRVM_gameedictfloat(ed, movetype);
2092         scale = PRVM_gameedictfloat(ed, scale);if (!scale) scale = 1.0f;
2093         modelindex = 0;
2094         mempool = prog->progs_mempool;
2095         model = NULL;
2096         switch(solid)
2097         {
2098         case SOLID_BSP:
2099         case SOLID_PHYSICS_TRIMESH:
2100                 modelindex = (int)PRVM_gameedictfloat(ed, modelindex);
2101                 if (world == &sv.world)
2102                         model = SV_GetModelByIndex(modelindex);
2103                 else if (world == &cl.world)
2104                         model = CL_GetModelByIndex(modelindex);
2105                 else
2106                         model = NULL;
2107                 if (model)
2108                 {
2109                         VectorScale(model->normalmins, scale, entmins);
2110                         VectorScale(model->normalmaxs, scale, entmaxs);
2111                         massval = PRVM_gameedictfloat(ed, mass);
2112                 }
2113                 else
2114                 {
2115                         modelindex = 0;
2116                         massval = 1.0f;
2117                 }
2118                 break;
2119         case SOLID_BBOX:
2120         //case SOLID_SLIDEBOX:
2121         case SOLID_CORPSE:
2122         case SOLID_PHYSICS_BOX:
2123         case SOLID_PHYSICS_SPHERE:
2124         case SOLID_PHYSICS_CAPSULE:
2125                 VectorCopy(PRVM_gameedictvector(ed, mins), entmins);
2126                 VectorCopy(PRVM_gameedictvector(ed, maxs), entmaxs);
2127                 massval = PRVM_gameedictfloat(ed, mass);
2128                 break;
2129         default:
2130                 if (ed->priv.server->ode_physics)
2131                         World_Physics_RemoveFromEntity(world, ed);
2132                 return;
2133         }
2134
2135         VectorSubtract(entmaxs, entmins, geomsize);
2136         if (VectorLength2(geomsize) == 0)
2137         {
2138                 // we don't allow point-size physics objects...
2139                 if (ed->priv.server->ode_physics)
2140                         World_Physics_RemoveFromEntity(world, ed);
2141                 return;
2142         }
2143
2144         if (movetype != MOVETYPE_PHYSICS)
2145                 massval = 1.0f;
2146
2147         // get friction from entity
2148         if (PRVM_gameedictfloat(ed, friction))
2149                 ed->priv.server->ode_friction = PRVM_gameedictfloat(ed, friction);
2150         else
2151                 ed->priv.server->ode_friction = 1.0;
2152                 
2153         // check if we need to create or replace the geom
2154         if (!ed->priv.server->ode_physics
2155          || !VectorCompare(ed->priv.server->ode_mins, entmins)
2156          || !VectorCompare(ed->priv.server->ode_maxs, entmaxs)
2157          || ed->priv.server->ode_mass != massval
2158          || ed->priv.server->ode_modelindex != modelindex)
2159         {
2160                 modified = true;
2161                 World_Physics_RemoveFromEntity(world, ed);
2162                 ed->priv.server->ode_physics = true;
2163                 VectorCopy(entmins, ed->priv.server->ode_mins);
2164                 VectorCopy(entmaxs, ed->priv.server->ode_maxs);
2165                 ed->priv.server->ode_mass = massval;
2166                 ed->priv.server->ode_modelindex = modelindex;
2167                 VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2168                 if (PRVM_gameedictvector(ed, massofs))
2169                         VectorCopy(geomcenter, PRVM_gameedictvector(ed, massofs));
2170                 else
2171                         VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2172                 ed->priv.server->ode_movelimit = min(geomsize[0], min(geomsize[1], geomsize[2]));
2173                 if (massval * geomsize[0] * geomsize[1] * geomsize[2] == 0)
2174                 {
2175                         if (movetype == MOVETYPE_PHYSICS)
2176                                 Con_Printf("entity %i (classname %s) .mass * .size_x * .size_y * .size_z == 0\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2177                         massval = 1.0f;
2178                         VectorSet(geomsize, 1.0f, 1.0f, 1.0f);
2179                 }
2180
2181                 switch(solid)
2182                 {
2183                 case SOLID_BSP:
2184                 case SOLID_PHYSICS_TRIMESH:
2185                         ed->priv.server->ode_offsetmatrix = identitymatrix;
2186                         if (!model)
2187                         {
2188                                 Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2189                                 goto treatasbox;
2190                         }
2191                         // add an optimized mesh to the model containing only the SUPERCONTENTS_SOLID surfaces
2192                         if (!model->brush.collisionmesh)
2193                                 Mod_CreateCollisionMesh(model);
2194                         if (!model->brush.collisionmesh || !model->brush.collisionmesh->numtriangles)
2195                         {
2196                                 Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2197                                 goto treatasbox;
2198                         }
2199                         // ODE requires persistent mesh storage, so we need to copy out
2200                         // the data from the model because renderer restarts could free it
2201                         // during the game, additionally we need to flip the triangles...
2202                         // note: ODE does preprocessing of the mesh for culling, removing
2203                         // concave edges, etc., so this is not a lightweight operation
2204                         ed->priv.server->ode_numvertices = numvertices = model->brush.collisionmesh->numverts;
2205                         ed->priv.server->ode_vertex3f = (float *)Mem_Alloc(mempool, numvertices * sizeof(float[3]));
2206                         for (vertexindex = 0, ov = ed->priv.server->ode_vertex3f, iv = model->brush.collisionmesh->vertex3f;vertexindex < numvertices;vertexindex++, ov += 3, iv += 3)
2207                         {
2208                                 ov[0] = iv[0] - geomcenter[0];
2209                                 ov[1] = iv[1] - geomcenter[1];
2210                                 ov[2] = iv[2] - geomcenter[2];
2211                         }
2212                         ed->priv.server->ode_numtriangles = numtriangles = model->brush.collisionmesh->numtriangles;
2213                         ed->priv.server->ode_element3i = (int *)Mem_Alloc(mempool, numtriangles * sizeof(int[3]));
2214                         //memcpy(ed->priv.server->ode_element3i, model->brush.collisionmesh->element3i, ed->priv.server->ode_numtriangles * sizeof(int[3]));
2215                         for (triangleindex = 0, oe = ed->priv.server->ode_element3i, ie = model->brush.collisionmesh->element3i;triangleindex < numtriangles;triangleindex++, oe += 3, ie += 3)
2216                         {
2217                                 oe[0] = ie[2];
2218                                 oe[1] = ie[1];
2219                                 oe[2] = ie[0];
2220                         }
2221                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2222                         // now create the geom
2223                         dataID = dGeomTriMeshDataCreate();
2224                         dGeomTriMeshDataBuildSingle((dTriMeshDataID)dataID, (void*)ed->priv.server->ode_vertex3f, sizeof(float[3]), ed->priv.server->ode_numvertices, ed->priv.server->ode_element3i, ed->priv.server->ode_numtriangles*3, sizeof(int[3]));
2225                         ed->priv.server->ode_geom = (void *)dCreateTriMesh((dSpaceID)world->physics.ode_space, (dTriMeshDataID)dataID, NULL, NULL, NULL);
2226                         dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2227                         break;
2228                 case SOLID_BBOX:
2229                 case SOLID_SLIDEBOX:
2230                 case SOLID_CORPSE:
2231                 case SOLID_PHYSICS_BOX:
2232 treatasbox:
2233                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2234                         ed->priv.server->ode_geom = (void *)dCreateBox((dSpaceID)world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]);
2235                         dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2236                         break;
2237                 case SOLID_PHYSICS_SPHERE:
2238                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2239                         ed->priv.server->ode_geom = (void *)dCreateSphere((dSpaceID)world->physics.ode_space, geomsize[0] * 0.5f);
2240                         dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f);
2241                         break;
2242                 case SOLID_PHYSICS_CAPSULE:
2243                 case SOLID_PHYSICS_CYLINDER:
2244                         axisindex = 0;
2245                         if (geomsize[axisindex] < geomsize[1])
2246                                 axisindex = 1;
2247                         if (geomsize[axisindex] < geomsize[2])
2248                                 axisindex = 2;
2249                         // the qc gives us 3 axis radius, the longest axis is the capsule
2250                         // axis, since ODE doesn't like this idea we have to create a
2251                         // capsule which uses the standard orientation, and apply a
2252                         // transform to it
2253                         memset(capsulerot, 0, sizeof(capsulerot));
2254                         if (axisindex == 0)
2255                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2256                         else if (axisindex == 1)
2257                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2258                         else
2259                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2260                         radius = geomsize[!axisindex] * 0.5f; // any other axis is the radius
2261                         length = geomsize[axisindex] - radius*2;
2262                         // because we want to support more than one axisindex, we have to
2263                         // create a transform, and turn on its cleanup setting (which will
2264                         // cause the child to be destroyed when it is destroyed)
2265                         if (solid == SOLID_PHYSICS_CAPSULE)
2266                         {
2267                                 ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2268                                 dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length);
2269                         }
2270                         else
2271                         {
2272                                 ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2273                                 dMassSetCylinderTotal(&mass, massval, axisindex+1, radius, length);
2274                         }
2275                         break;
2276                 default:
2277                         Sys_Error("World_Physics_BodyFromEntity: unrecognized solid value %i was accepted by filter\n", solid);
2278                         // this goto only exists to prevent warnings from the compiler
2279                         // about uninitialized variables (mass), while allowing it to
2280                         // catch legitimate uninitialized variable warnings
2281                         goto treatasbox;
2282                 }
2283                 Matrix4x4_Invert_Simple(&ed->priv.server->ode_offsetimatrix, &ed->priv.server->ode_offsetmatrix);
2284                 ed->priv.server->ode_massbuf = Mem_Alloc(mempool, sizeof(mass));
2285                 memcpy(ed->priv.server->ode_massbuf, &mass, sizeof(dMass));
2286         }
2287
2288         if (ed->priv.server->ode_geom)
2289                 dGeomSetData((dGeomID)ed->priv.server->ode_geom, (void*)ed);
2290         if (movetype == MOVETYPE_PHYSICS && ed->priv.server->ode_geom)
2291         {
2292                 if (ed->priv.server->ode_body == NULL)
2293                 {
2294                         ed->priv.server->ode_body = (void *)(body = dBodyCreate((dWorldID)world->physics.ode_world));
2295                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2296                         dBodySetData(body, (void*)ed);
2297                         dBodySetMass(body, (dMass *) ed->priv.server->ode_massbuf);
2298                         modified = true;
2299                 }
2300         }
2301         else
2302         {
2303                 if (ed->priv.server->ode_body != NULL)
2304                 {
2305                         if(ed->priv.server->ode_geom)
2306                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2307                         dBodyDestroy((dBodyID) ed->priv.server->ode_body);
2308                         ed->priv.server->ode_body = NULL;
2309                         modified = true;
2310                 }
2311         }
2312
2313         // get current data from entity
2314         VectorClear(origin);
2315         VectorClear(velocity);
2316         //VectorClear(forward);
2317         //VectorClear(left);
2318         //VectorClear(up);
2319         //VectorClear(spinvelocity);
2320         VectorClear(angles);
2321         VectorClear(avelocity);
2322         gravity = true;
2323         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
2324         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
2325         //VectorCopy(PRVM_gameedictvector(ed, axis_forward), forward);
2326         //VectorCopy(PRVM_gameedictvector(ed, axis_left), left);
2327         //VectorCopy(PRVM_gameedictvector(ed, axis_up), up);
2328         //VectorCopy(PRVM_gameedictvector(ed, spinvelocity), spinvelocity);
2329         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
2330         VectorCopy(PRVM_gameedictvector(ed, avelocity), avelocity);
2331         if (PRVM_gameedictfloat(ed, gravity) != 0.0f && PRVM_gameedictfloat(ed, gravity) < 0.5f) gravity = false;
2332         if (ed == prog->edicts)
2333                 gravity = false;
2334
2335         // compatibility for legacy entities
2336         //if (!VectorLength2(forward) || solid == SOLID_BSP)
2337         {
2338                 float pitchsign = 1;
2339                 vec3_t qangles, qavelocity;
2340                 VectorCopy(angles, qangles);
2341                 VectorCopy(avelocity, qavelocity);
2342
2343                 if(prog == SVVM_prog) // FIXME some better way?
2344                 {
2345                         pitchsign = SV_GetPitchSign(prog, ed);
2346                 }
2347                 else if(prog == CLVM_prog)
2348                 {
2349                         pitchsign = CL_GetPitchSign(prog, ed);
2350                 }
2351                 qangles[PITCH] *= pitchsign;
2352                 qavelocity[PITCH] *= pitchsign;
2353
2354                 AngleVectorsFLU(qangles, forward, left, up);
2355                 // convert single-axis rotations in avelocity to spinvelocity
2356                 // FIXME: untested math - check signs
2357                 VectorSet(spinvelocity, DEG2RAD(qavelocity[PITCH]), DEG2RAD(qavelocity[ROLL]), DEG2RAD(qavelocity[YAW]));
2358         }
2359
2360         // compatibility for legacy entities
2361         switch (solid)
2362         {
2363         case SOLID_BBOX:
2364         case SOLID_SLIDEBOX:
2365         case SOLID_CORPSE:
2366                 VectorSet(forward, 1, 0, 0);
2367                 VectorSet(left, 0, 1, 0);
2368                 VectorSet(up, 0, 0, 1);
2369                 VectorSet(spinvelocity, 0, 0, 0);
2370                 break;
2371         }
2372
2373
2374         // we must prevent NANs...
2375         if (physics_ode_trick_fixnan.integer)
2376         {
2377                 test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity);
2378                 if (IS_NAN(test))
2379                 {
2380                         modified = true;
2381                         //Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .velocity = '%f %f %f' .axis_forward = '%f %f %f' .axis_left = '%f %f %f' .axis_up = %f %f %f' .spinvelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], forward[0], forward[1], forward[2], left[0], left[1], left[2], up[0], up[1], up[2], spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2382                         if (physics_ode_trick_fixnan.integer >= 2)
2383                                 Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .velocity = '%f %f %f' .angles = '%f %f %f' .avelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], angles[0], angles[1], angles[2], avelocity[0], avelocity[1], avelocity[2]);
2384                         test = VectorLength2(origin);
2385                         if (IS_NAN(test))
2386                                 VectorClear(origin);
2387                         test = VectorLength2(forward) * VectorLength2(left) * VectorLength2(up);
2388                         if (IS_NAN(test))
2389                         {
2390                                 VectorSet(angles, 0, 0, 0);
2391                                 VectorSet(forward, 1, 0, 0);
2392                                 VectorSet(left, 0, 1, 0);
2393                                 VectorSet(up, 0, 0, 1);
2394                         }
2395                         test = VectorLength2(velocity);
2396                         if (IS_NAN(test))
2397                                 VectorClear(velocity);
2398                         test = VectorLength2(spinvelocity);
2399                         if (IS_NAN(test))
2400                         {
2401                                 VectorClear(avelocity);
2402                                 VectorClear(spinvelocity);
2403                         }
2404                 }
2405         }
2406
2407         // check if the qc edited any position data
2408         if (!VectorCompare(origin, ed->priv.server->ode_origin)
2409          || !VectorCompare(velocity, ed->priv.server->ode_velocity)
2410          || !VectorCompare(angles, ed->priv.server->ode_angles)
2411          || !VectorCompare(avelocity, ed->priv.server->ode_avelocity)
2412          || gravity != ed->priv.server->ode_gravity)
2413                 modified = true;
2414
2415         // store the qc values into the physics engine
2416         body = (dBodyID)ed->priv.server->ode_body;
2417         if (modified && ed->priv.server->ode_geom)
2418         {
2419                 dVector3 r[3];
2420                 matrix4x4_t entitymatrix;
2421                 matrix4x4_t bodymatrix;
2422
2423 #if 0
2424                 Con_Printf("entity %i got changed by QC\n", (int) (ed - prog->edicts));
2425                 if(!VectorCompare(origin, ed->priv.server->ode_origin))
2426                         Con_Printf("  origin: %f %f %f -> %f %f %f\n", ed->priv.server->ode_origin[0], ed->priv.server->ode_origin[1], ed->priv.server->ode_origin[2], origin[0], origin[1], origin[2]);
2427                 if(!VectorCompare(velocity, ed->priv.server->ode_velocity))
2428                         Con_Printf("  velocity: %f %f %f -> %f %f %f\n", ed->priv.server->ode_velocity[0], ed->priv.server->ode_velocity[1], ed->priv.server->ode_velocity[2], velocity[0], velocity[1], velocity[2]);
2429                 if(!VectorCompare(angles, ed->priv.server->ode_angles))
2430                         Con_Printf("  angles: %f %f %f -> %f %f %f\n", ed->priv.server->ode_angles[0], ed->priv.server->ode_angles[1], ed->priv.server->ode_angles[2], angles[0], angles[1], angles[2]);
2431                 if(!VectorCompare(avelocity, ed->priv.server->ode_avelocity))
2432                         Con_Printf("  avelocity: %f %f %f -> %f %f %f\n", ed->priv.server->ode_avelocity[0], ed->priv.server->ode_avelocity[1], ed->priv.server->ode_avelocity[2], avelocity[0], avelocity[1], avelocity[2]);
2433                 if(gravity != ed->priv.server->ode_gravity)
2434                         Con_Printf("  gravity: %i -> %i\n", ed->priv.server->ode_gravity, gravity);
2435 #endif
2436                 // values for BodyFromEntity to check if the qc modified anything later
2437                 VectorCopy(origin, ed->priv.server->ode_origin);
2438                 VectorCopy(velocity, ed->priv.server->ode_velocity);
2439                 VectorCopy(angles, ed->priv.server->ode_angles);
2440                 VectorCopy(avelocity, ed->priv.server->ode_avelocity);
2441                 ed->priv.server->ode_gravity = gravity;
2442
2443                 Matrix4x4_FromVectors(&entitymatrix, forward, left, up, origin);
2444                 Matrix4x4_Concat(&bodymatrix, &entitymatrix, &ed->priv.server->ode_offsetmatrix);
2445                 Matrix4x4_ToVectors(&bodymatrix, forward, left, up, origin);
2446                 r[0][0] = forward[0];
2447                 r[1][0] = forward[1];
2448                 r[2][0] = forward[2];
2449                 r[0][1] = left[0];
2450                 r[1][1] = left[1];
2451                 r[2][1] = left[2];
2452                 r[0][2] = up[0];
2453                 r[1][2] = up[1];
2454                 r[2][2] = up[2];
2455                 if(body)
2456                 {
2457                         if(movetype == MOVETYPE_PHYSICS)
2458                         {
2459                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2460                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2461                                 dBodySetRotation(body, r[0]);
2462                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2463                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2464                                 dBodySetGravityMode(body, gravity);
2465                         }
2466                         else
2467                         {
2468                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2469                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2470                                 dBodySetRotation(body, r[0]);
2471                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2472                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2473                                 dBodySetGravityMode(body, gravity);
2474                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2475                         }
2476                 }
2477                 else
2478                 {
2479                         // no body... then let's adjust the parameters of the geom directly
2480                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0); // just in case we previously HAD a body (which should never happen)
2481                         dGeomSetPosition((dGeomID)ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
2482                         dGeomSetRotation((dGeomID)ed->priv.server->ode_geom, r[0]);
2483                 }
2484         }
2485
2486         if(body)
2487         {
2488
2489                 // limit movement speed to prevent missed collisions at high speed
2490                 ovelocity = dBodyGetLinearVel(body);
2491                 ospinvelocity = dBodyGetAngularVel(body);
2492                 movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit;
2493                 test = VectorLength2(ovelocity);
2494                 if (test > movelimit*movelimit)
2495                 {
2496                         // scale down linear velocity to the movelimit
2497                         // scale down angular velocity the same amount for consistency
2498                         f = movelimit / sqrt(test);
2499                         VectorScale(ovelocity, f, velocity);
2500                         VectorScale(ospinvelocity, f, spinvelocity);
2501                         dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2502                         dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2503                 }
2504
2505                 // make sure the angular velocity is not exploding
2506                 spinlimit = physics_ode_spinlimit.value;
2507                 test = VectorLength2(ospinvelocity);
2508                 if (test > spinlimit)
2509                 {
2510                         dBodySetAngularVel(body, 0, 0, 0);
2511                 }
2512
2513                 // apply functions and clear stack
2514                 for(func = ed->priv.server->ode_func; func; func = nextf)
2515                 {
2516                         nextf = func->next;
2517                         World_Physics_ApplyCmd(ed, func);
2518                         Mem_Free(func);
2519                 }
2520                 ed->priv.server->ode_func = NULL;
2521         }
2522 }
2523
2524 #define MAX_CONTACTS 16
2525 static void nearCallback (void *data, dGeomID o1, dGeomID o2)
2526 {
2527         world_t *world = (world_t *)data;
2528         prvm_prog_t *prog = world->prog;
2529         dContact contact[MAX_CONTACTS]; // max contacts per collision pair
2530         dBodyID b1;
2531         dBodyID b2;
2532         dJointID c;
2533         int i;
2534         int numcontacts;
2535         float bouncefactor1 = 0.0f;
2536         float bouncestop1 = 60.0f / 800.0f;
2537         float bouncefactor2 = 0.0f;
2538         float bouncestop2 = 60.0f / 800.0f;
2539         dVector3 grav;
2540         prvm_edict_t *ed1, *ed2;
2541
2542         if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
2543         {
2544                 // colliding a space with something
2545                 dSpaceCollide2(o1, o2, data, &nearCallback);
2546                 // Note we do not want to test intersections within a space,
2547                 // only between spaces.
2548                 //if (dGeomIsSpace(o1)) dSpaceCollide(o1, data, &nearCallback);
2549                 //if (dGeomIsSpace(o2)) dSpaceCollide(o2, data, &nearCallback);
2550                 return;
2551         }
2552
2553         b1 = dGeomGetBody(o1);
2554         b2 = dGeomGetBody(o2);
2555
2556         // at least one object has to be using MOVETYPE_PHYSICS or we just don't care
2557         if (!b1 && !b2)
2558                 return;
2559
2560         // exit without doing anything if the two bodies are connected by a joint
2561         if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact))
2562                 return;
2563
2564         ed1 = (prvm_edict_t *) dGeomGetData(o1);
2565         if(ed1 && ed1->priv.server->free)
2566                 ed1 = NULL;
2567         if(ed1)
2568         {
2569                 bouncefactor1 = PRVM_gameedictfloat(ed1, bouncefactor);
2570                 bouncestop1 = PRVM_gameedictfloat(ed1, bouncestop);
2571                 if (!bouncestop1)
2572                         bouncestop1 = 60.0f / 800.0f;
2573         }
2574
2575         ed2 = (prvm_edict_t *) dGeomGetData(o2);
2576         if(ed2 && ed2->priv.server->free)
2577                 ed2 = NULL;
2578         if(ed2)
2579         {
2580                 bouncefactor2 = PRVM_gameedictfloat(ed2, bouncefactor);
2581                 bouncestop2 = PRVM_gameedictfloat(ed2, bouncestop);
2582                 if (!bouncestop2)
2583                         bouncestop2 = 60.0f / 800.0f;
2584         }
2585
2586         if(prog == SVVM_prog)
2587         {
2588                 if(ed1 && PRVM_serveredictfunction(ed1, touch))
2589                 {
2590                         SV_LinkEdict_TouchAreaGrid_Call(ed1, ed2 ? ed2 : prog->edicts);
2591                 }
2592                 if(ed2 && PRVM_serveredictfunction(ed2, touch))
2593                 {
2594                         SV_LinkEdict_TouchAreaGrid_Call(ed2, ed1 ? ed1 : prog->edicts);
2595                 }
2596         }
2597
2598         // merge bounce factors and bounce stop
2599         if(bouncefactor2 > 0)
2600         {
2601                 if(bouncefactor1 > 0)
2602                 {
2603                         // TODO possibly better logic to merge bounce factor data?
2604                         if(bouncestop2 < bouncestop1)
2605                                 bouncestop1 = bouncestop2;
2606                         if(bouncefactor2 > bouncefactor1)
2607                                 bouncefactor1 = bouncefactor2;
2608                 }
2609                 else
2610                 {
2611                         bouncestop1 = bouncestop2;
2612                         bouncefactor1 = bouncefactor2;
2613                 }
2614         }
2615         dWorldGetGravity((dWorldID)world->physics.ode_world, grav);
2616         bouncestop1 *= fabs(grav[2]);
2617
2618         // generate contact points between the two non-space geoms
2619         numcontacts = dCollide(o1, o2, MAX_CONTACTS, &(contact[0].geom), sizeof(contact[0]));
2620         // add these contact points to the simulation
2621         for (i = 0;i < numcontacts;i++)
2622         {
2623                 contact[i].surface.mode = (physics_ode_contact_mu.value != -1 ? dContactApprox1 : 0) | (physics_ode_contact_erp.value != -1 ? dContactSoftERP : 0) | (physics_ode_contact_cfm.value != -1 ? dContactSoftCFM : 0) | (bouncefactor1 > 0 ? dContactBounce : 0);
2624                 contact[i].surface.mu = physics_ode_contact_mu.value * ed1->priv.server->ode_friction * ed2->priv.server->ode_friction;
2625                 contact[i].surface.soft_erp = physics_ode_contact_erp.value;
2626                 contact[i].surface.soft_cfm = physics_ode_contact_cfm.value;
2627                 contact[i].surface.bounce = bouncefactor1;
2628                 contact[i].surface.bounce_vel = bouncestop1;
2629                 c = dJointCreateContact((dWorldID)world->physics.ode_world, (dJointGroupID)world->physics.ode_contactgroup, contact + i);
2630                 dJointAttach(c, b1, b2);
2631         }
2632 }
2633 #endif
2634
2635 void World_Physics_Frame(world_t *world, double frametime, double gravity)
2636 {
2637         prvm_prog_t *prog = world->prog;
2638         double tdelta, tdelta2, tdelta3, simulationtime, collisiontime;
2639
2640         tdelta = Sys_DirtyTime();
2641 #ifdef USEODE
2642         if (world->physics.ode && physics_ode.integer)
2643         {
2644                 int i;
2645                 prvm_edict_t *ed;
2646
2647                 world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000);
2648                 if (physics_ode_constantstep.integer)
2649                         world->physics.ode_step = sys_ticrate.value / world->physics.ode_iterations;
2650                 else
2651                         world->physics.ode_step = frametime / world->physics.ode_iterations;
2652                 world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step;
2653                 World_Physics_UpdateODE(world);
2654
2655                 // copy physics properties from entities to physics engine
2656                 if (prog)
2657                 {
2658                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2659                                 if (!prog->edicts[i].priv.required->free)
2660                                         World_Physics_Frame_BodyFromEntity(world, ed);
2661                         // oh, and it must be called after all bodies were created
2662                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2663                                 if (!prog->edicts[i].priv.required->free)
2664                                         World_Physics_Frame_JointFromEntity(world, ed);
2665                 }
2666
2667                 tdelta2 = Sys_DirtyTime();
2668                 collisiontime = 0;
2669                 for (i = 0;i < world->physics.ode_iterations;i++)
2670                 {
2671                         // set the gravity
2672                         dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity * physics_ode_world_gravitymod.value);
2673                         // set the tolerance for closeness of objects
2674                         dWorldSetContactSurfaceLayer((dWorldID)world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value));
2675
2676                         // run collisions for the current world state, creating JointGroup
2677                         tdelta3 = Sys_DirtyTime();
2678                         dSpaceCollide((dSpaceID)world->physics.ode_space, (void *)world, nearCallback);
2679                         collisiontime += (Sys_DirtyTime() - tdelta3)*10000;
2680
2681                         // run physics (move objects, calculate new velocities)
2682                         if (physics_ode_worldstep.integer == 2)
2683                         {
2684                                 dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldstep_iterations.integer, 200));
2685                                 dWorldQuickStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
2686                         }
2687 #ifdef ODE_USE_STEPFAST
2688                         else if (physics_ode_worldstep.integer == 1)
2689                                 dWorldStepFast1((dWorldID)world->physics.ode_world, world->physics.ode_step, bound(1, physics_ode_worldstep_iterations.integer, 200));
2690 #endif
2691                         else
2692                                 dWorldStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
2693
2694                         // clear the JointGroup now that we're done with it
2695                         dJointGroupEmpty((dJointGroupID)world->physics.ode_contactgroup);
2696                 }
2697                 simulationtime = (Sys_DirtyTime() - tdelta2)*10000;
2698
2699                 // copy physics properties from physics engine to entities and do some stats
2700                 if (prog)
2701                 {
2702                         for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2703                                 if (!prog->edicts[i].priv.required->free)
2704                                         World_Physics_Frame_BodyToEntity(world, ed);
2705
2706                         // print stats
2707                         if (physics_ode_printstats.integer)
2708                         {
2709                                 dBodyID body;
2710
2711                                 world->physics.ode_numobjects = 0;
2712                                 world->physics.ode_activeovjects = 0;
2713                                 for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2714                                 {
2715                                         if (prog->edicts[i].priv.required->free)
2716                                                 continue;
2717                                         body = (dBodyID)prog->edicts[i].priv.server->ode_body;
2718                                         if (!body)
2719                                                 continue;
2720                                         world->physics.ode_numobjects++;
2721                                         if (dBodyIsEnabled(body))
2722                                                 world->physics.ode_activeovjects++;
2723                                 }
2724                                 Con_Printf("ODE Stats(%s): %3.01f (%3.01f collision) %3.01f total : %i objects %i active %i disabled\n", prog->name, simulationtime, collisiontime, (Sys_DirtyTime() - tdelta)*10000, world->physics.ode_numobjects, world->physics.ode_activeovjects, (world->physics.ode_numobjects - world->physics.ode_activeovjects));
2725                         }
2726                 }
2727         }
2728 #endif
2729 }