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