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