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