]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - world.c
Rework R_CanSeeBox a bit to support an eyejitter feature, and make it available throu...
[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 LINK_TO_LIBODE
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 #ifndef LINK_TO_LIBODE
1474         const char* dllnames [] =
1475         {
1476 # if defined(WIN32)
1477                 "libode3.dll",
1478                 "libode2.dll",
1479                 "libode1.dll",
1480 # elif defined(MACOSX)
1481                 "libode.3.dylib",
1482                 "libode.2.dylib",
1483                 "libode.1.dylib",
1484 # else
1485                 "libode.so.3",
1486                 "libode.so.2",
1487                 "libode.so.1",
1488 # endif
1489                 NULL
1490         };
1491 #endif
1492
1493         Cvar_RegisterVariable(&physics_ode_quadtree_depth);
1494         Cvar_RegisterVariable(&physics_ode_contactsurfacelayer);
1495         Cvar_RegisterVariable(&physics_ode_worldstep_iterations);
1496         Cvar_RegisterVariable(&physics_ode_contact_mu);
1497         Cvar_RegisterVariable(&physics_ode_contact_erp);
1498         Cvar_RegisterVariable(&physics_ode_contact_cfm);
1499         Cvar_RegisterVariable(&physics_ode_contact_maxpoints);
1500         Cvar_RegisterVariable(&physics_ode_world_erp);
1501         Cvar_RegisterVariable(&physics_ode_world_cfm);
1502         Cvar_RegisterVariable(&physics_ode_world_damping);
1503         Cvar_RegisterVariable(&physics_ode_world_damping_linear);
1504         Cvar_RegisterVariable(&physics_ode_world_damping_linear_threshold);
1505         Cvar_RegisterVariable(&physics_ode_world_damping_angular);
1506         Cvar_RegisterVariable(&physics_ode_world_damping_angular_threshold);
1507         Cvar_RegisterVariable(&physics_ode_world_gravitymod);
1508         Cvar_RegisterVariable(&physics_ode_iterationsperframe);
1509         Cvar_RegisterVariable(&physics_ode_constantstep);
1510         Cvar_RegisterVariable(&physics_ode_movelimit);
1511         Cvar_RegisterVariable(&physics_ode_spinlimit);
1512         Cvar_RegisterVariable(&physics_ode_trick_fixnan);
1513         Cvar_RegisterVariable(&physics_ode_autodisable);
1514         Cvar_RegisterVariable(&physics_ode_autodisable_steps);
1515         Cvar_RegisterVariable(&physics_ode_autodisable_time);
1516         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_linear);
1517         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_angular);
1518         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_samples);
1519         Cvar_RegisterVariable(&physics_ode_printstats);
1520         Cvar_RegisterVariable(&physics_ode_allowconvex);
1521         Cvar_RegisterVariable(&physics_ode);
1522
1523 #ifndef LINK_TO_LIBODE
1524         // Load the DLL
1525         if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs))
1526 #endif
1527         {
1528                 dInitODE();
1529 //              dInitODE2(0);
1530 #ifndef LINK_TO_LIBODE
1531 # ifdef dSINGLE
1532                 if (!dCheckConfiguration("ODE_single_precision"))
1533 # else
1534                 if (!dCheckConfiguration("ODE_double_precision"))
1535 # endif
1536                 {
1537 # ifdef dSINGLE
1538                         Con_Printf("ODE library not compiled for single precision - incompatible!  Not using ODE physics.\n");
1539 # else
1540                         Con_Printf("ODE library not compiled for double precision - incompatible!  Not using ODE physics.\n");
1541 # endif
1542                         Sys_UnloadLibrary(&ode_dll);
1543                         ode_dll = NULL;
1544                 }
1545                 else
1546                 {
1547 # ifdef dSINGLE
1548                         Con_Printf("ODE library loaded with single precision.\n");
1549 # else
1550                         Con_Printf("ODE library loaded with double precision.\n");
1551 # endif
1552                         Con_Printf("ODE configuration list: %s\n", dGetConfiguration());
1553                 }
1554 #endif
1555         }
1556 #endif
1557 }
1558
1559 static void World_Physics_Shutdown(void)
1560 {
1561 #ifdef USEODE
1562 #ifndef LINK_TO_LIBODE
1563         if (ode_dll)
1564 #endif
1565         {
1566                 dCloseODE();
1567 #ifndef LINK_TO_LIBODE
1568                 Sys_UnloadLibrary(&ode_dll);
1569                 ode_dll = NULL;
1570 #endif
1571         }
1572 #endif
1573 }
1574
1575 #ifdef USEODE
1576 static void World_Physics_UpdateODE(world_t *world)
1577 {
1578         dWorldID odeworld;
1579
1580         odeworld = (dWorldID)world->physics.ode_world;
1581
1582         // ERP and CFM
1583         if (physics_ode_world_erp.value >= 0)
1584                 dWorldSetERP(odeworld, physics_ode_world_erp.value);
1585         if (physics_ode_world_cfm.value >= 0)
1586                 dWorldSetCFM(odeworld, physics_ode_world_cfm.value);
1587         // Damping
1588         if (physics_ode_world_damping.integer)
1589         {
1590                 dWorldSetLinearDamping(odeworld, (physics_ode_world_damping_linear.value >= 0) ? (physics_ode_world_damping_linear.value * physics_ode_world_damping.value) : 0);
1591                 dWorldSetLinearDampingThreshold(odeworld, (physics_ode_world_damping_linear_threshold.value >= 0) ? (physics_ode_world_damping_linear_threshold.value * physics_ode_world_damping.value) : 0);
1592                 dWorldSetAngularDamping(odeworld, (physics_ode_world_damping_angular.value >= 0) ? (physics_ode_world_damping_angular.value * physics_ode_world_damping.value) : 0);
1593                 dWorldSetAngularDampingThreshold(odeworld, (physics_ode_world_damping_angular_threshold.value >= 0) ? (physics_ode_world_damping_angular_threshold.value * physics_ode_world_damping.value) : 0);
1594         }
1595         else
1596         {
1597                 dWorldSetLinearDamping(odeworld, 0);
1598                 dWorldSetLinearDampingThreshold(odeworld, 0);
1599                 dWorldSetAngularDamping(odeworld, 0);
1600                 dWorldSetAngularDampingThreshold(odeworld, 0);
1601         }
1602         // Autodisable
1603         dWorldSetAutoDisableFlag(odeworld, (physics_ode_autodisable.integer) ? 1 : 0);
1604         if (physics_ode_autodisable.integer)
1605         {
1606                 dWorldSetAutoDisableSteps(odeworld, bound(1, physics_ode_autodisable_steps.integer, 100)); 
1607                 dWorldSetAutoDisableTime(odeworld, physics_ode_autodisable_time.value);
1608                 dWorldSetAutoDisableAverageSamplesCount(odeworld, bound(1, physics_ode_autodisable_threshold_samples.integer, 100));
1609                 dWorldSetAutoDisableLinearThreshold(odeworld, physics_ode_autodisable_threshold_linear.value); 
1610                 dWorldSetAutoDisableAngularThreshold(odeworld, physics_ode_autodisable_threshold_angular.value); 
1611         }
1612 }
1613
1614 static void World_Physics_EnableODE(world_t *world)
1615 {
1616         dVector3 center, extents;
1617         if (world->physics.ode)
1618                 return;
1619 #ifndef LINK_TO_LIBODE
1620         if (!ode_dll)
1621                 return;
1622 #endif
1623         world->physics.ode = true;
1624         VectorMAM(0.5f, world->mins, 0.5f, world->maxs, center);
1625         VectorSubtract(world->maxs, center, extents);
1626         world->physics.ode_world = dWorldCreate();
1627         world->physics.ode_space = dQuadTreeSpaceCreate(NULL, center, extents, bound(1, physics_ode_quadtree_depth.integer, 10));
1628         world->physics.ode_contactgroup = dJointGroupCreate(0);
1629
1630         World_Physics_UpdateODE(world);
1631 }
1632 #endif
1633
1634 static void World_Physics_Start(world_t *world)
1635 {
1636 #ifdef USEODE
1637         if (world->physics.ode)
1638                 return;
1639         World_Physics_EnableODE(world);
1640 #endif
1641 }
1642
1643 static void World_Physics_End(world_t *world)
1644 {
1645 #ifdef USEODE
1646         if (world->physics.ode)
1647         {
1648                 dWorldDestroy((dWorldID)world->physics.ode_world);
1649                 dSpaceDestroy((dSpaceID)world->physics.ode_space);
1650                 dJointGroupDestroy((dJointGroupID)world->physics.ode_contactgroup);
1651                 world->physics.ode = false;
1652         }
1653 #endif
1654 }
1655
1656 void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed)
1657 {
1658         ed->priv.server->ode_joint_type = 0;
1659 #ifdef USEODE
1660         if(ed->priv.server->ode_joint)
1661                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1662         ed->priv.server->ode_joint = NULL;
1663 #endif
1664 }
1665
1666 void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
1667 {
1668         edict_odefunc_t *f, *nf;
1669
1670         // entity is not physics controlled, free any physics data
1671         ed->priv.server->ode_physics = false;
1672 #ifdef USEODE
1673         if (ed->priv.server->ode_geom)
1674                 dGeomDestroy((dGeomID)ed->priv.server->ode_geom);
1675         ed->priv.server->ode_geom = NULL;
1676         if (ed->priv.server->ode_body)
1677         {
1678                 dJointID j;
1679                 dBodyID b1, b2;
1680                 prvm_edict_t *ed2;
1681                 while(dBodyGetNumJoints((dBodyID)ed->priv.server->ode_body))
1682                 {
1683                         j = dBodyGetJoint((dBodyID)ed->priv.server->ode_body, 0);
1684                         ed2 = (prvm_edict_t *) dJointGetData(j);
1685                         b1 = dJointGetBody(j, 0);
1686                         b2 = dJointGetBody(j, 1);
1687                         if(b1 == (dBodyID)ed->priv.server->ode_body)
1688                         {
1689                                 b1 = 0;
1690                                 ed2->priv.server->ode_joint_enemy = 0;
1691                         }
1692                         if(b2 == (dBodyID)ed->priv.server->ode_body)
1693                         {
1694                                 b2 = 0;
1695                                 ed2->priv.server->ode_joint_aiment = 0;
1696                         }
1697                         dJointAttach(j, b1, b2);
1698                 }
1699                 dBodyDestroy((dBodyID)ed->priv.server->ode_body);
1700         }
1701         ed->priv.server->ode_body = NULL;
1702 #endif
1703         if (ed->priv.server->ode_vertex3f)
1704                 Mem_Free(ed->priv.server->ode_vertex3f);
1705         ed->priv.server->ode_vertex3f = NULL;
1706         ed->priv.server->ode_numvertices = 0;
1707         if (ed->priv.server->ode_element3i)
1708                 Mem_Free(ed->priv.server->ode_element3i);
1709         ed->priv.server->ode_element3i = NULL;
1710         ed->priv.server->ode_numtriangles = 0;
1711         if(ed->priv.server->ode_massbuf)
1712                 Mem_Free(ed->priv.server->ode_massbuf);
1713         ed->priv.server->ode_massbuf = NULL;
1714         // clear functions stack
1715         for(f = ed->priv.server->ode_func; f; f = nf)
1716         {
1717                 nf = f->next;
1718                 Mem_Free(f);
1719         }
1720         ed->priv.server->ode_func = NULL;
1721 }
1722
1723 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f)
1724 {
1725 #ifdef USEODE
1726         dBodyID body = (dBodyID)ed->priv.server->ode_body;
1727
1728         switch(f->type)
1729         {
1730         case ODEFUNC_ENABLE:
1731                 dBodyEnable(body);
1732                 break;
1733         case ODEFUNC_DISABLE:
1734                 dBodyDisable(body);
1735                 break;
1736         case ODEFUNC_FORCE:
1737                 dBodyEnable(body);
1738                 dBodyAddForceAtPos(body, f->v1[0], f->v1[1], f->v1[2], f->v2[0], f->v2[1], f->v2[2]);
1739                 break;
1740         case ODEFUNC_TORQUE:
1741                 dBodyEnable(body);
1742                 dBodyAddTorque(body, f->v1[0], f->v1[1], f->v1[2]);
1743                 break;
1744         default:
1745                 break;
1746         }
1747 #endif
1748 }
1749
1750 #ifdef USEODE
1751 static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
1752 {
1753         prvm_prog_t *prog = world->prog;
1754         const dReal *avel;
1755         const dReal *o;
1756         const dReal *r; // for some reason dBodyGetRotation returns a [3][4] matrix
1757         const dReal *vel;
1758         dBodyID body = (dBodyID)ed->priv.server->ode_body;
1759         int movetype;
1760         matrix4x4_t bodymatrix;
1761         matrix4x4_t entitymatrix;
1762         vec3_t angles;
1763         vec3_t avelocity;
1764         vec3_t forward, left, up;
1765         vec3_t origin;
1766         vec3_t spinvelocity;
1767         vec3_t velocity;
1768         int jointtype;
1769         if (!body)
1770                 return;
1771         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1772         if (movetype != MOVETYPE_PHYSICS)
1773         {
1774                 jointtype = (int)PRVM_gameedictfloat(ed, jointtype);
1775                 switch(jointtype)
1776                 {
1777                         // TODO feed back data from physics
1778                         case JOINTTYPE_POINT:
1779                                 break;
1780                         case JOINTTYPE_HINGE:
1781                                 break;
1782                         case JOINTTYPE_SLIDER:
1783                                 break;
1784                         case JOINTTYPE_UNIVERSAL:
1785                                 break;
1786                         case JOINTTYPE_HINGE2:
1787                                 break;
1788                         case JOINTTYPE_FIXED:
1789                                 break;
1790                 }
1791                 return;
1792         }
1793         // store the physics engine data into the entity
1794         o = dBodyGetPosition(body);
1795         r = dBodyGetRotation(body);
1796         vel = dBodyGetLinearVel(body);
1797         avel = dBodyGetAngularVel(body);
1798         VectorCopy(o, origin);
1799         forward[0] = r[0];
1800         forward[1] = r[4];
1801         forward[2] = r[8];
1802         left[0] = r[1];
1803         left[1] = r[5];
1804         left[2] = r[9];
1805         up[0] = r[2];
1806         up[1] = r[6];
1807         up[2] = r[10];
1808         VectorCopy(vel, velocity);
1809         VectorCopy(avel, spinvelocity);
1810         Matrix4x4_FromVectors(&bodymatrix, forward, left, up, origin);
1811         Matrix4x4_Concat(&entitymatrix, &bodymatrix, &ed->priv.server->ode_offsetimatrix);
1812         Matrix4x4_ToVectors(&entitymatrix, forward, left, up, origin);
1813
1814         AnglesFromVectors(angles, forward, up, false);
1815         VectorSet(avelocity, RAD2DEG(spinvelocity[PITCH]), RAD2DEG(spinvelocity[ROLL]), RAD2DEG(spinvelocity[YAW]));
1816
1817         {
1818                 float pitchsign = 1;
1819                 if(prog == SVVM_prog) // FIXME some better way?
1820                 {
1821                         pitchsign = SV_GetPitchSign(prog, ed);
1822                 }
1823                 else if(prog == CLVM_prog)
1824                 {
1825                         pitchsign = CL_GetPitchSign(prog, ed);
1826                 }
1827                 angles[PITCH] *= pitchsign;
1828                 avelocity[PITCH] *= pitchsign;
1829         }
1830
1831         VectorCopy(origin, PRVM_gameedictvector(ed, origin));
1832         VectorCopy(velocity, PRVM_gameedictvector(ed, velocity));
1833         //VectorCopy(forward, PRVM_gameedictvector(ed, axis_forward));
1834         //VectorCopy(left, PRVM_gameedictvector(ed, axis_left));
1835         //VectorCopy(up, PRVM_gameedictvector(ed, axis_up));
1836         //VectorCopy(spinvelocity, PRVM_gameedictvector(ed, spinvelocity));
1837         VectorCopy(angles, PRVM_gameedictvector(ed, angles));
1838         VectorCopy(avelocity, PRVM_gameedictvector(ed, avelocity));
1839
1840         // values for BodyFromEntity to check if the qc modified anything later
1841         VectorCopy(origin, ed->priv.server->ode_origin);
1842         VectorCopy(velocity, ed->priv.server->ode_velocity);
1843         VectorCopy(angles, ed->priv.server->ode_angles);
1844         VectorCopy(avelocity, ed->priv.server->ode_avelocity);
1845         ed->priv.server->ode_gravity = dBodyGetGravityMode(body) != 0;
1846
1847         if(prog == SVVM_prog) // FIXME some better way?
1848         {
1849                 SV_LinkEdict(ed);
1850                 SV_LinkEdict_TouchAreaGrid(ed);
1851         }
1852 }
1853
1854 static void World_Physics_Frame_ForceFromEntity(world_t *world, prvm_edict_t *ed)
1855 {
1856         prvm_prog_t *prog = world->prog;
1857         int forcetype = 0, movetype = 0, enemy = 0;
1858         vec3_t movedir, origin;
1859
1860         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1861         forcetype = (int)PRVM_gameedictfloat(ed, forcetype);
1862         if (movetype == MOVETYPE_PHYSICS)
1863                 forcetype = FORCETYPE_NONE; // can't have both
1864         if (!forcetype)
1865                 return;
1866         enemy = PRVM_gameedictedict(ed, enemy);
1867         if (enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
1868                 return;
1869         VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
1870         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
1871         dBodyEnable((dBodyID)prog->edicts[enemy].priv.server->ode_body);
1872         switch(forcetype)
1873         {
1874                 case FORCETYPE_FORCE:
1875                         if (movedir[0] || movedir[1] || movedir[2])
1876                                 dBodyAddForce((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2]);
1877                         break;
1878                 case FORCETYPE_FORCEATPOS:
1879                         if (movedir[0] || movedir[1] || movedir[2])
1880                                 dBodyAddForceAtPos((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2], origin[0], origin[1], origin[2]);
1881                         break;
1882                 case FORCETYPE_TORQUE:
1883                         if (movedir[0] || movedir[1] || movedir[2])
1884                                 dBodyAddTorque((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2]);
1885                         break;
1886                 case FORCETYPE_NONE:
1887                 default:
1888                         // bad force
1889                         break;
1890         }
1891 }
1892
1893 static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed)
1894 {
1895         prvm_prog_t *prog = world->prog;
1896         dJointID j = 0;
1897         dBodyID b1 = 0;
1898         dBodyID b2 = 0;
1899         int movetype = 0;
1900         int jointtype = 0;
1901         int enemy = 0, aiment = 0;
1902         vec3_t origin, velocity, angles, forward, left, up, movedir;
1903         vec_t CFM, ERP, FMax, Stop, Vel;
1904
1905         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1906         jointtype = (int)PRVM_gameedictfloat(ed, jointtype);
1907         VectorClear(origin);
1908         VectorClear(velocity);
1909         VectorClear(angles);
1910         VectorClear(movedir);
1911         enemy = PRVM_gameedictedict(ed, enemy);
1912         aiment = PRVM_gameedictedict(ed, aiment);
1913         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
1914         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
1915         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
1916         VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
1917         if(movetype == MOVETYPE_PHYSICS)
1918                 jointtype = JOINTTYPE_NONE; // can't have both
1919         if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
1920                 enemy = 0;
1921         if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
1922                 aiment = 0;
1923         // see http://www.ode.org/old_list_archives/2006-January/017614.html
1924         // we want to set ERP? make it fps independent and work like a spring constant
1925         // note: if movedir[2] is 0, it becomes ERP = 1, CFM = 1.0 / (H * K)
1926         if(movedir[0] > 0 && movedir[1] > 0)
1927         {
1928                 float K = movedir[0];
1929                 float D = movedir[1];
1930                 float R = 2.0 * D * sqrt(K); // we assume D is premultiplied by sqrt(sprungMass)
1931                 CFM = 1.0 / (world->physics.ode_step * K + R); // always > 0
1932                 ERP = world->physics.ode_step * K * CFM;
1933                 Vel = 0;
1934                 FMax = 0;
1935                 Stop = movedir[2];
1936         }
1937         else if(movedir[1] < 0)
1938         {
1939                 CFM = 0;
1940                 ERP = 0;
1941                 Vel = movedir[0];
1942                 FMax = -movedir[1]; // TODO do we need to multiply with world.physics.ode_step?
1943                 Stop = movedir[2] > 0 ? movedir[2] : dInfinity;
1944         }
1945         else // movedir[0] > 0, movedir[1] == 0 or movedir[0] < 0, movedir[1] >= 0
1946         {
1947                 CFM = 0;
1948                 ERP = 0;
1949                 Vel = 0;
1950                 FMax = 0;
1951                 Stop = dInfinity;
1952         }
1953         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))
1954                 return; // nothing to do
1955         AngleVectorsFLU(angles, forward, left, up);
1956         switch(jointtype)
1957         {
1958                 case JOINTTYPE_POINT:
1959                         j = dJointCreateBall((dWorldID)world->physics.ode_world, 0);
1960                         break;
1961                 case JOINTTYPE_HINGE:
1962                         j = dJointCreateHinge((dWorldID)world->physics.ode_world, 0);
1963                         break;
1964                 case JOINTTYPE_SLIDER:
1965                         j = dJointCreateSlider((dWorldID)world->physics.ode_world, 0);
1966                         break;
1967                 case JOINTTYPE_UNIVERSAL:
1968                         j = dJointCreateUniversal((dWorldID)world->physics.ode_world, 0);
1969                         break;
1970                 case JOINTTYPE_HINGE2:
1971                         j = dJointCreateHinge2((dWorldID)world->physics.ode_world, 0);
1972                         break;
1973                 case JOINTTYPE_FIXED:
1974                         j = dJointCreateFixed((dWorldID)world->physics.ode_world, 0);
1975                         break;
1976                 case JOINTTYPE_NONE:
1977                 default:
1978                         // no joint
1979                         j = 0;
1980                         break;
1981         }
1982         if(ed->priv.server->ode_joint)
1983         {
1984                 //Con_Printf("deleted old joint %i\n", (int) (ed - prog->edicts));
1985                 dJointAttach((dJointID)ed->priv.server->ode_joint, 0, 0);
1986                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1987         }
1988         ed->priv.server->ode_joint = (void *) j;
1989         ed->priv.server->ode_joint_type = jointtype;
1990         ed->priv.server->ode_joint_enemy = enemy;
1991         ed->priv.server->ode_joint_aiment = aiment;
1992         VectorCopy(origin, ed->priv.server->ode_joint_origin);
1993         VectorCopy(velocity, ed->priv.server->ode_joint_velocity);
1994         VectorCopy(angles, ed->priv.server->ode_joint_angles);
1995         VectorCopy(movedir, ed->priv.server->ode_joint_movedir);
1996         if(j)
1997         {
1998                 //Con_Printf("made new joint %i\n", (int) (ed - prog->edicts));
1999                 dJointSetData(j, (void *) ed);
2000                 if(enemy)
2001                         b1 = (dBodyID)prog->edicts[enemy].priv.server->ode_body;
2002                 if(aiment)
2003                         b2 = (dBodyID)prog->edicts[aiment].priv.server->ode_body;
2004                 dJointAttach(j, b1, b2);
2005
2006                 switch(jointtype)
2007                 {
2008                         case JOINTTYPE_POINT:
2009                                 dJointSetBallAnchor(j, origin[0], origin[1], origin[2]);
2010                                 break;
2011                         case JOINTTYPE_HINGE:
2012                                 dJointSetHingeAnchor(j, origin[0], origin[1], origin[2]);
2013                                 dJointSetHingeAxis(j, forward[0], forward[1], forward[2]);
2014                                 dJointSetHingeParam(j, dParamFMax, FMax);
2015                                 dJointSetHingeParam(j, dParamHiStop, Stop);
2016                                 dJointSetHingeParam(j, dParamLoStop, -Stop);
2017                                 dJointSetHingeParam(j, dParamStopCFM, CFM);
2018                                 dJointSetHingeParam(j, dParamStopERP, ERP);
2019                                 dJointSetHingeParam(j, dParamVel, Vel);
2020                                 break;
2021                         case JOINTTYPE_SLIDER:
2022                                 dJointSetSliderAxis(j, forward[0], forward[1], forward[2]);
2023                                 dJointSetSliderParam(j, dParamFMax, FMax);
2024                                 dJointSetSliderParam(j, dParamHiStop, Stop);
2025                                 dJointSetSliderParam(j, dParamLoStop, -Stop);
2026                                 dJointSetSliderParam(j, dParamStopCFM, CFM);
2027                                 dJointSetSliderParam(j, dParamStopERP, ERP);
2028                                 dJointSetSliderParam(j, dParamVel, Vel);
2029                                 break;
2030                         case JOINTTYPE_UNIVERSAL:
2031                                 dJointSetUniversalAnchor(j, origin[0], origin[1], origin[2]);
2032                                 dJointSetUniversalAxis1(j, forward[0], forward[1], forward[2]);
2033                                 dJointSetUniversalAxis2(j, up[0], up[1], up[2]);
2034                                 dJointSetUniversalParam(j, dParamFMax, FMax);
2035                                 dJointSetUniversalParam(j, dParamHiStop, Stop);
2036                                 dJointSetUniversalParam(j, dParamLoStop, -Stop);
2037                                 dJointSetUniversalParam(j, dParamStopCFM, CFM);
2038                                 dJointSetUniversalParam(j, dParamStopERP, ERP);
2039                                 dJointSetUniversalParam(j, dParamVel, Vel);
2040                                 dJointSetUniversalParam(j, dParamFMax2, FMax);
2041                                 dJointSetUniversalParam(j, dParamHiStop2, Stop);
2042                                 dJointSetUniversalParam(j, dParamLoStop2, -Stop);
2043                                 dJointSetUniversalParam(j, dParamStopCFM2, CFM);
2044                                 dJointSetUniversalParam(j, dParamStopERP2, ERP);
2045                                 dJointSetUniversalParam(j, dParamVel2, Vel);
2046                                 break;
2047                         case JOINTTYPE_HINGE2:
2048                                 dJointSetHinge2Anchor(j, origin[0], origin[1], origin[2]);
2049                                 dJointSetHinge2Axis1(j, forward[0], forward[1], forward[2]);
2050                                 dJointSetHinge2Axis2(j, velocity[0], velocity[1], velocity[2]);
2051                                 dJointSetHinge2Param(j, dParamFMax, FMax);
2052                                 dJointSetHinge2Param(j, dParamHiStop, Stop);
2053                                 dJointSetHinge2Param(j, dParamLoStop, -Stop);
2054                                 dJointSetHinge2Param(j, dParamStopCFM, CFM);
2055                                 dJointSetHinge2Param(j, dParamStopERP, ERP);
2056                                 dJointSetHinge2Param(j, dParamVel, Vel);
2057                                 dJointSetHinge2Param(j, dParamFMax2, FMax);
2058                                 dJointSetHinge2Param(j, dParamHiStop2, Stop);
2059                                 dJointSetHinge2Param(j, dParamLoStop2, -Stop);
2060                                 dJointSetHinge2Param(j, dParamStopCFM2, CFM);
2061                                 dJointSetHinge2Param(j, dParamStopERP2, ERP);
2062                                 dJointSetHinge2Param(j, dParamVel2, Vel);
2063                                 break;
2064                         case JOINTTYPE_FIXED:
2065                                 break;
2066                         case 0:
2067                         default:
2068                                 Sys_Error("what? but above the joint was valid...\n");
2069                                 break;
2070                 }
2071 #undef SETPARAMS
2072
2073         }
2074 }
2075
2076 // test convex geometry data
2077 // planes for a cube, these should coincide with the 
2078 dReal test_convex_planes[] = 
2079 {
2080     1.0f ,0.0f ,0.0f ,2.25f,
2081     0.0f ,1.0f ,0.0f ,2.25f,
2082     0.0f ,0.0f ,1.0f ,2.25f,
2083     -1.0f,0.0f ,0.0f ,2.25f,
2084     0.0f ,-1.0f,0.0f ,2.25f,
2085     0.0f ,0.0f ,-1.0f,2.25f
2086 };
2087 const unsigned int test_convex_planecount = 6;
2088 // points for a cube
2089 dReal test_convex_points[] = 
2090 {
2091         2.25f,2.25f,2.25f,    // point 0
2092         -2.25f,2.25f,2.25f,   // point 1
2093     2.25f,-2.25f,2.25f,   // point 2
2094     -2.25f,-2.25f,2.25f,  // point 3
2095     2.25f,2.25f,-2.25f,   // point 4
2096     -2.25f,2.25f,-2.25f,  // point 5
2097     2.25f,-2.25f,-2.25f,  // point 6
2098     -2.25f,-2.25f,-2.25f, // point 7
2099 };
2100 const unsigned int test_convex_pointcount = 8;
2101 // polygons for a cube (6 squares), index 
2102 unsigned int test_convex_polygons[] = 
2103 {
2104         4,0,2,6,4, // positive X
2105     4,1,0,4,5, // positive Y
2106     4,0,1,3,2, // positive Z
2107     4,3,1,5,7, // negative X
2108     4,2,3,7,6, // negative Y
2109     4,5,4,6,7, // negative Z
2110 };
2111
2112 static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
2113 {
2114         prvm_prog_t *prog = world->prog;
2115         const float *iv;
2116         const int *ie;
2117         dBodyID body;
2118         dMass mass;
2119         const dReal *ovelocity, *ospinvelocity;
2120         void *dataID;
2121         dp_model_t *model;
2122         float *ov;
2123         int *oe;
2124         int axisindex;
2125         int modelindex = 0;
2126         int movetype = MOVETYPE_NONE;
2127         int numtriangles;
2128         int numvertices;
2129         int solid = SOLID_NOT, geomtype = 0;
2130         int triangleindex;
2131         int vertexindex;
2132         mempool_t *mempool;
2133         qboolean modified = false;
2134         vec3_t angles;
2135         vec3_t avelocity;
2136         vec3_t entmaxs;
2137         vec3_t entmins;
2138         vec3_t forward;
2139         vec3_t geomcenter;
2140         vec3_t geomsize;
2141         vec3_t left;
2142         vec3_t origin;
2143         vec3_t spinvelocity;
2144         vec3_t up;
2145         vec3_t velocity;
2146         vec_t f;
2147         vec_t length;
2148         vec_t massval = 1.0f;
2149         vec_t movelimit;
2150         vec_t radius;
2151         vec3_t scale;
2152         vec_t spinlimit;
2153         vec_t test;
2154         qboolean gravity;
2155         qboolean geom_modified = false;
2156         edict_odefunc_t *func, *nextf;
2157
2158         dReal *planes, *planesData, *pointsData;
2159         unsigned int *polygons, *polygonsData, polyvert;
2160         qboolean *mapped, *used, convex_compatible;
2161         int numplanes = 0, numpoints = 0, i;
2162
2163 #ifndef LINK_TO_LIBODE
2164         if (!ode_dll)
2165                 return;
2166 #endif
2167         VectorClear(entmins);
2168         VectorClear(entmaxs);
2169
2170         solid = (int)PRVM_gameedictfloat(ed, solid);
2171         geomtype = (int)PRVM_gameedictfloat(ed, geomtype);
2172         movetype = (int)PRVM_gameedictfloat(ed, movetype);
2173         // support scale and q3map/radiant's modelscale_vec
2174         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)
2175                 VectorCopy(PRVM_gameedictvector(ed, modelscale_vec), scale);
2176         else if (PRVM_gameedictfloat(ed, scale))
2177                 VectorSet(scale, PRVM_gameedictfloat(ed, scale), PRVM_gameedictfloat(ed, scale), PRVM_gameedictfloat(ed, scale));
2178         else
2179                 VectorSet(scale, 1.0f, 1.0f, 1.0f);
2180         modelindex = 0;
2181         if (PRVM_gameedictfloat(ed, mass))
2182                 massval = PRVM_gameedictfloat(ed, mass);
2183         if (movetype != MOVETYPE_PHYSICS)
2184                 massval = 1.0f;
2185         mempool = prog->progs_mempool;
2186         model = NULL;
2187         if (!geomtype)
2188         {
2189                 // VorteX: keep support for deprecated solid fields to not break mods
2190                 if (solid == SOLID_PHYSICS_TRIMESH || solid == SOLID_BSP)
2191                         geomtype = GEOMTYPE_TRIMESH;
2192                 else if (solid == SOLID_NOT || solid == SOLID_TRIGGER)
2193                         geomtype = GEOMTYPE_NONE;
2194                 else if (solid == SOLID_PHYSICS_SPHERE)
2195                         geomtype = GEOMTYPE_SPHERE;
2196                 else if (solid == SOLID_PHYSICS_CAPSULE)
2197                         geomtype = GEOMTYPE_CAPSULE;
2198                 else if (solid == SOLID_PHYSICS_CYLINDER)
2199                         geomtype = GEOMTYPE_CYLINDER;
2200                 else if (solid == SOLID_PHYSICS_BOX)
2201                         geomtype = GEOMTYPE_BOX;
2202                 else
2203                         geomtype = GEOMTYPE_BOX;
2204         }
2205         if (geomtype == GEOMTYPE_TRIMESH)
2206         {
2207                 modelindex = (int)PRVM_gameedictfloat(ed, modelindex);
2208                 if (world == &sv.world)
2209                         model = SV_GetModelByIndex(modelindex);
2210                 else if (world == &cl.world)
2211                         model = CL_GetModelByIndex(modelindex);
2212                 else
2213                         model = NULL;
2214                 if (model)
2215                 {
2216                         entmins[0] = model->normalmins[0] * scale[0];
2217                         entmins[1] = model->normalmins[1] * scale[1];
2218                         entmins[2] = model->normalmins[2] * scale[2];
2219                         entmaxs[0] = model->normalmaxs[0] * scale[0];
2220                         entmaxs[1] = model->normalmaxs[1] * scale[1];
2221                         entmaxs[2] = model->normalmaxs[2] * scale[2];
2222                         geom_modified = !VectorCompare(ed->priv.server->ode_scale, scale) || ed->priv.server->ode_modelindex != modelindex;
2223                 }
2224                 else
2225                 {
2226                         Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2227                         geomtype = GEOMTYPE_BOX;
2228                         VectorCopy(PRVM_gameedictvector(ed, mins), entmins);
2229                         VectorCopy(PRVM_gameedictvector(ed, maxs), entmaxs);
2230                         modelindex = 0;
2231                         geom_modified = !VectorCompare(ed->priv.server->ode_mins, entmins) || !VectorCompare(ed->priv.server->ode_maxs, entmaxs);
2232                 }
2233         }
2234         else if (geomtype && geomtype != GEOMTYPE_NONE)
2235         {
2236                 VectorCopy(PRVM_gameedictvector(ed, mins), entmins);
2237                 VectorCopy(PRVM_gameedictvector(ed, maxs), entmaxs);
2238                 geom_modified = !VectorCompare(ed->priv.server->ode_mins, entmins) || !VectorCompare(ed->priv.server->ode_maxs, entmaxs);
2239         }
2240         else
2241         {
2242                 // geometry type not set, falling back
2243                 if (ed->priv.server->ode_physics)
2244                         World_Physics_RemoveFromEntity(world, ed);
2245                 return;
2246         }
2247
2248         VectorSubtract(entmaxs, entmins, geomsize);
2249         if (VectorLength2(geomsize) == 0)
2250         {
2251                 // we don't allow point-size physics objects...
2252                 if (ed->priv.server->ode_physics)
2253                         World_Physics_RemoveFromEntity(world, ed);
2254                 return;
2255         }
2256
2257         // get friction
2258         ed->priv.server->ode_friction = PRVM_gameedictfloat(ed, friction) ? PRVM_gameedictfloat(ed, friction) : 1.0f;
2259
2260         // check if we need to create or replace the geom
2261         if (!ed->priv.server->ode_physics || ed->priv.server->ode_mass != massval || geom_modified)
2262         {
2263                 modified = true;
2264                 World_Physics_RemoveFromEntity(world, ed);
2265                 ed->priv.server->ode_physics = true;
2266                 VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2267                 if (PRVM_gameedictvector(ed, massofs))
2268                         VectorCopy(geomcenter, PRVM_gameedictvector(ed, massofs));
2269
2270                 // check geomsize
2271                 if (geomsize[0] * geomsize[1] * geomsize[2] == 0)
2272                 {
2273                         if (movetype == MOVETYPE_PHYSICS)
2274                                 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)));
2275                         VectorSet(geomsize, 1.0f, 1.0f, 1.0f);
2276                 }
2277
2278                 // greate geom
2279                 switch(geomtype)
2280                 {
2281                 case GEOMTYPE_TRIMESH:
2282                         // add an optimized mesh to the model containing only the SUPERCONTENTS_SOLID surfaces
2283                         if (!model->brush.collisionmesh)
2284                                 Mod_CreateCollisionMesh(model);
2285                         if (!model->brush.collisionmesh)
2286                         {
2287                                 Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2288                                 goto treatasbox;
2289                         }
2290
2291                         // check if trimesh can be defined with convex
2292                         convex_compatible = false;
2293                         for (i = 0;i < model->nummodelsurfaces;i++)
2294                         {
2295                                 if (!strcmp(((msurface_t *)(model->data_surfaces + model->firstmodelsurface + i))->texture->name, "collisionconvex"))
2296                                 {
2297                                         convex_compatible = true;
2298                                         break;
2299                                 }
2300                         }
2301
2302                         // ODE requires persistent mesh storage, so we need to copy out
2303                         // the data from the model because renderer restarts could free it
2304                         // during the game, additionally we need to flip the triangles...
2305                         // note: ODE does preprocessing of the mesh for culling, removing
2306                         // concave edges, etc., so this is not a lightweight operation
2307                         ed->priv.server->ode_numvertices = numvertices = model->brush.collisionmesh->numverts;
2308                         ed->priv.server->ode_vertex3f = (float *)Mem_Alloc(mempool, numvertices * sizeof(float[3]));
2309
2310                         // VorteX: rebuild geomsize based on entity's collision mesh, honor scale
2311                         VectorSet(entmins, 0, 0, 0);
2312                         VectorSet(entmaxs, 0, 0, 0);
2313                         for (vertexindex = 0, ov = ed->priv.server->ode_vertex3f, iv = model->brush.collisionmesh->vertex3f;vertexindex < numvertices;vertexindex++, ov += 3, iv += 3)
2314                         {
2315                                 ov[0] = iv[0] * scale[0];
2316                                 ov[1] = iv[1] * scale[1];
2317                                 ov[2] = iv[2] * scale[2];
2318                                 entmins[0] = min(entmins[0], ov[0]);
2319                                 entmins[1] = min(entmins[1], ov[1]);
2320                                 entmins[2] = min(entmins[2], ov[2]);
2321                                 entmaxs[0] = max(entmaxs[0], ov[0]);
2322                                 entmaxs[1] = max(entmaxs[1], ov[1]);
2323                                 entmaxs[2] = max(entmaxs[2], ov[2]);
2324                         }
2325                         if (!PRVM_gameedictvector(ed, massofs))
2326                                 VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2327                         for (vertexindex = 0, ov = ed->priv.server->ode_vertex3f, iv = model->brush.collisionmesh->vertex3f;vertexindex < numvertices;vertexindex++, ov += 3, iv += 3)
2328                         {
2329                                 ov[0] = ov[0] - geomcenter[0];
2330                                 ov[1] = ov[1] - geomcenter[1];
2331                                 ov[2] = ov[2] - geomcenter[2];
2332                         }
2333                         VectorSubtract(entmaxs, entmins, geomsize);
2334                         if (VectorLength2(geomsize) == 0)
2335                         {
2336                                 if (movetype == MOVETYPE_PHYSICS)
2337                                         Con_Printf("entity %i collision mesh has null geomsize\n", PRVM_NUM_FOR_EDICT(ed));
2338                                 VectorSet(geomsize, 1.0f, 1.0f, 1.0f);
2339                         }
2340                         ed->priv.server->ode_numtriangles = numtriangles = model->brush.collisionmesh->numtriangles;
2341                         ed->priv.server->ode_element3i = (int *)Mem_Alloc(mempool, numtriangles * sizeof(int[3]));
2342                         //memcpy(ed->priv.server->ode_element3i, model->brush.collisionmesh->element3i, ed->priv.server->ode_numtriangles * sizeof(int[3]));
2343                         for (triangleindex = 0, oe = ed->priv.server->ode_element3i, ie = model->brush.collisionmesh->element3i;triangleindex < numtriangles;triangleindex++, oe += 3, ie += 3)
2344                         {
2345                                 oe[0] = ie[2];
2346                                 oe[1] = ie[1];
2347                                 oe[2] = ie[0];
2348                         }
2349                         // create geom
2350                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2351                         if (!convex_compatible || !physics_ode_allowconvex.integer)
2352                         {
2353                                 // trimesh
2354                                 dataID = dGeomTriMeshDataCreate();
2355                                 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]));
2356                                 ed->priv.server->ode_geom = (void *)dCreateTriMesh((dSpaceID)world->physics.ode_space, (dTriMeshDataID)dataID, NULL, NULL, NULL);
2357                                 dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2358                         }
2359                         else
2360                         {
2361                                 // VorteX: this code is unfinished in two ways
2362                                 // - no duplicate vertex merging are done
2363                                 // - triangles that shares same edge and havee sam plane are not merget into poly
2364                                 // so, currently it only works for geosphere meshes with no UV
2365
2366                                 Con_Printf("Build convex hull for model %s...\n", model->name);
2367                                 // build convex geometry from trimesh data
2368                                 // this ensures that trimesh's triangles can form correct convex geometry
2369                                 // not many of error checking is performed
2370                                 // ODE's conve hull data consist of:
2371                                 //    planes  : an array of planes in the form: normal X, normal Y, normal Z, distance
2372                                 //    points  : an array of points X,Y,Z
2373                                 //    polygons: an array of indices to the points of each  polygon,it should be the number of vertices
2374                                 //              followed by that amount of indices to "points" in counter clockwise order
2375                                 polygonsData = polygons = (unsigned int *)Mem_Alloc(mempool, numtriangles*sizeof(int)*4);
2376                                 planesData = planes = (dReal *)Mem_Alloc(mempool, numtriangles*sizeof(dReal)*4);
2377                                 mapped = (qboolean *)Mem_Alloc(mempool, numvertices*sizeof(qboolean));
2378                                 used = (qboolean *)Mem_Alloc(mempool, numtriangles*sizeof(qboolean));
2379                                 memset(mapped, 0, numvertices*sizeof(qboolean));
2380                                 memset(used, 0, numtriangles*sizeof(qboolean));
2381                                 numplanes = numpoints = polyvert = 0;
2382                                 // build convex hull
2383                                 // todo: merge duplicated verts here
2384                                 Con_Printf("Building...\n");
2385                                 iv = ed->priv.server->ode_vertex3f;
2386                                 for (triangleindex = 0; triangleindex < numtriangles; triangleindex++)
2387                                 {
2388                                         // already formed a polygon?
2389                                         if (used[triangleindex])
2390                                                 continue; 
2391                                         // init polygon
2392                                         // switch clockwise->counterclockwise
2393                                         ie = &model->brush.collisionmesh->element3i[triangleindex*3];
2394                                         used[triangleindex] = true;
2395                                         TriangleNormal(&iv[ie[0]*3], &iv[ie[1]*3], &iv[ie[2]*3], planes);
2396                                         VectorNormalize(planes);
2397                                         polygons[0] = 3;
2398                                         polygons[3] = (unsigned int)ie[0]; mapped[polygons[3]] = true;
2399                                         polygons[2] = (unsigned int)ie[1]; mapped[polygons[2]] = true;
2400                                         polygons[1] = (unsigned int)ie[2]; mapped[polygons[1]] = true;
2401
2402                                         // now find and include concave triangles
2403                                         for (i = triangleindex; i < numtriangles; i++)
2404                                         {
2405                                                 if (used[i])
2406                                                         continue;
2407                                                 // should share at least 2 vertexes
2408                                                 for (polyvert = 1; polyvert <= polygons[0]; polyvert++)
2409                                                 {
2410                                                         // todo: merge in triangles that shares an edge and have same plane here
2411                                                 }
2412                                         }
2413
2414                                         // add polygon to overall stats
2415                                         planes[3] = DotProduct(&iv[polygons[1]*3], planes);
2416                                         polygons += (polygons[0]+1);
2417                                         planes += 4;
2418                                         numplanes++;
2419                                 }
2420                                 Mem_Free(used);
2421                                 // save points
2422                                 for (vertexindex = 0, numpoints = 0; vertexindex < numvertices; vertexindex++)
2423                                         if (mapped[vertexindex])
2424                                                 numpoints++;
2425                                 pointsData = (dReal *)Mem_Alloc(mempool, numpoints*sizeof(dReal)*3 + numplanes*sizeof(dReal)*4); // planes is appended
2426                                 for (vertexindex = 0, numpoints = 0; vertexindex < numvertices; vertexindex++)
2427                                 {
2428                                         if (mapped[vertexindex])
2429                                         {
2430                                                 VectorCopy(&iv[vertexindex*3], &pointsData[numpoints*3]);
2431                                                 numpoints++;
2432                                         }
2433                                 }
2434                                 Mem_Free(mapped);
2435                                 Con_Printf("Points: \n");
2436                                 for (i = 0; i < (int)numpoints; i++)
2437                                         Con_Printf("%3i: %3.1f %3.1f %3.1f\n", i, pointsData[i*3], pointsData[i*3+1], pointsData[i*3+2]);
2438                                 // save planes
2439                                 planes = planesData;
2440                                 planesData = pointsData + numpoints*3;
2441                                 memcpy(planesData, planes, numplanes*sizeof(dReal)*4);
2442                                 Mem_Free(planes);
2443                                 Con_Printf("planes...\n");
2444                                 for (i = 0; i < numplanes; i++)
2445                                         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]);
2446                                 // save polygons
2447                                 polyvert = polygons - polygonsData;
2448                                 polygons = polygonsData;
2449                                 polygonsData = (unsigned int *)Mem_Alloc(mempool, polyvert*sizeof(int));
2450                                 memcpy(polygonsData, polygons, polyvert*sizeof(int));
2451                                 Mem_Free(polygons);
2452                                 Con_Printf("Polygons: \n");
2453                                 polygons = polygonsData;
2454                                 for (i = 0; i < numplanes; i++)
2455                                 {
2456                                         Con_Printf("%3i : %i ", i, polygons[0]);
2457                                         for (triangleindex = 1; triangleindex <= (int)polygons[0]; triangleindex++)
2458                                                 Con_Printf("%3i ", polygons[triangleindex]);
2459                                         polygons += (polygons[0]+1);
2460                                         Con_Printf("\n");
2461                                 }
2462                                 Mem_Free(ed->priv.server->ode_element3i);
2463                                 ed->priv.server->ode_element3i = (int *)polygonsData;
2464                                 Mem_Free(ed->priv.server->ode_vertex3f);
2465                                 ed->priv.server->ode_vertex3f = (float *)pointsData;
2466                                 // check for properly build polygons by calculating the determinant of the 3x3 matrix composed of the first 3 points in the polygon
2467                                 // this code is picked from ODE Source
2468                                 Con_Printf("Check...\n");
2469                                 polygons = polygonsData;
2470                                 for (i = 0; i < numplanes; i++)
2471                                 {
2472                                         if((pointsData[(polygons[1]*3)+0]*pointsData[(polygons[2]*3)+1]*pointsData[(polygons[3]*3)+2] +
2473                                                 pointsData[(polygons[1]*3)+1]*pointsData[(polygons[2]*3)+2]*pointsData[(polygons[3]*3)+0] +
2474                                                 pointsData[(polygons[1]*3)+2]*pointsData[(polygons[2]*3)+0]*pointsData[(polygons[3]*3)+1] -
2475                                                 pointsData[(polygons[1]*3)+2]*pointsData[(polygons[2]*3)+1]*pointsData[(polygons[3]*3)+0] -
2476                                                 pointsData[(polygons[1]*3)+1]*pointsData[(polygons[2]*3)+0]*pointsData[(polygons[3]*3)+2] -
2477                                                 pointsData[(polygons[1]*3)+0]*pointsData[(polygons[2]*3)+2]*pointsData[(polygons[3]*3)+1]) < 0)
2478                                                 Con_Printf("WARNING: Polygon %d is not defined counterclockwise\n", i);
2479                                         if (planesData[(i*4)+3] < 0)
2480                                                 Con_Printf("WARNING: Plane %d does not contain the origin\n", i);
2481                                         polygons += (*polygons + 1);
2482                                 }
2483                                 // create geom
2484                                 Con_Printf("Create geom...\n");
2485                                 ed->priv.server->ode_geom = (void *)dCreateConvex((dSpaceID)world->physics.ode_space, planesData, numplanes, pointsData, numpoints, polygonsData);
2486                                 dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2487                                 Con_Printf("Done!\n");
2488                         }
2489                         break;
2490                 case GEOMTYPE_BOX:
2491 treatasbox:
2492                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2493                         ed->priv.server->ode_geom = (void *)dCreateBox((dSpaceID)world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]);
2494                         dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2495                         break;
2496                 case GEOMTYPE_SPHERE:
2497                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2498                         ed->priv.server->ode_geom = (void *)dCreateSphere((dSpaceID)world->physics.ode_space, geomsize[0] * 0.5f);
2499                         dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f);
2500                         break;
2501                 case GEOMTYPE_CAPSULE:
2502                         axisindex = 0;
2503                         if (geomsize[axisindex] < geomsize[1])
2504                                 axisindex = 1;
2505                         if (geomsize[axisindex] < geomsize[2])
2506                                 axisindex = 2;
2507                         // the qc gives us 3 axis radius, the longest axis is the capsule
2508                         // axis, since ODE doesn't like this idea we have to create a
2509                         // capsule which uses the standard orientation, and apply a
2510                         // transform to it
2511                         if (axisindex == 0)
2512                         {
2513                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2514                                 radius = min(geomsize[1], geomsize[2]) * 0.5f;
2515                         }
2516                         else if (axisindex == 1)
2517                         {
2518                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2519                                 radius = min(geomsize[0], geomsize[2]) * 0.5f;
2520                         }
2521                         else
2522                         {
2523                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2524                                 radius = min(geomsize[0], geomsize[1]) * 0.5f;
2525                         }
2526                         length = geomsize[axisindex] - radius*2;
2527                         // because we want to support more than one axisindex, we have to
2528                         // create a transform, and turn on its cleanup setting (which will
2529                         // cause the child to be destroyed when it is destroyed)
2530                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2531                         dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length);
2532                         break;
2533                 case GEOMTYPE_CAPSULE_X:
2534                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2535                         radius = min(geomsize[1], geomsize[2]) * 0.5f;
2536                         length = geomsize[0] - radius*2;
2537                         // check if length is not enough, reduce radius then
2538                         if (length <= 0)
2539                         {
2540                                 radius -= (1 - length)*0.5;
2541                                 length = 1;
2542                         }
2543                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2544                         dMassSetCapsuleTotal(&mass, massval, 1, radius, length);
2545                         break;
2546                 case GEOMTYPE_CAPSULE_Y:
2547                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2548                         radius = min(geomsize[0], geomsize[2]) * 0.5f;
2549                         length = geomsize[1] - radius*2;
2550                         // check if length is not enough, reduce radius then
2551                         if (length <= 0)
2552                         {
2553                                 radius -= (1 - length)*0.5;
2554                                 length = 1;
2555                         }
2556                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2557                         dMassSetCapsuleTotal(&mass, massval, 2, radius, length);
2558                         break;
2559                 case GEOMTYPE_CAPSULE_Z:
2560                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2561                         radius = min(geomsize[1], geomsize[0]) * 0.5f;
2562                         length = geomsize[2] - radius*2;
2563                         // check if length is not enough, reduce radius then
2564                         if (length <= 0)
2565                         {
2566                                 radius -= (1 - length)*0.5;
2567                                 length = 1;
2568                         }
2569                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2570                         dMassSetCapsuleTotal(&mass, massval, 3, radius, length);
2571                         break;
2572                 case GEOMTYPE_CYLINDER:
2573                         axisindex = 0;
2574                         if (geomsize[axisindex] < geomsize[1])
2575                                 axisindex = 1;
2576                         if (geomsize[axisindex] < geomsize[2])
2577                                 axisindex = 2;
2578                         // the qc gives us 3 axis radius, the longest axis is the capsule
2579                         // axis, since ODE doesn't like this idea we have to create a
2580                         // capsule which uses the standard orientation, and apply a
2581                         // transform to it
2582                         if (axisindex == 0)
2583                         {
2584                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2585                                 radius = min(geomsize[1], geomsize[2]) * 0.5f;
2586                         }
2587                         else if (axisindex == 1)
2588                         {
2589                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2590                                 radius = min(geomsize[0], geomsize[2]) * 0.5f;
2591                         }
2592                         else
2593                         {
2594                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2595                                 radius = min(geomsize[0], geomsize[1]) * 0.5f;
2596                         }
2597                         length = geomsize[axisindex];
2598                         // check if length is not enough, reduce radius then
2599                         if (length <= 0)
2600                         {
2601                                 radius -= (1 - length)*0.5;
2602                                 length = 1;
2603                         }
2604                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2605                         dMassSetCylinderTotal(&mass, massval, axisindex+1, radius, length);
2606                         break;
2607                 case GEOMTYPE_CYLINDER_X:
2608                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2609                         radius = min(geomsize[1], geomsize[2]) * 0.5f;
2610                         length = geomsize[0];
2611                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2612                         dMassSetCylinderTotal(&mass, massval, 1, radius, length);
2613                         break;
2614                 case GEOMTYPE_CYLINDER_Y:
2615                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2616                         radius = min(geomsize[0], geomsize[2]) * 0.5f;
2617                         length = geomsize[1];
2618                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2619                         dMassSetCylinderTotal(&mass, massval, 2, radius, length);
2620                         break;
2621                 case GEOMTYPE_CYLINDER_Z:
2622                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2623                         radius = min(geomsize[0], geomsize[1]) * 0.5f;
2624                         length = geomsize[2];
2625                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2626                         dMassSetCylinderTotal(&mass, massval, 3, radius, length);
2627                         break;
2628                 default:
2629                         Sys_Error("World_Physics_BodyFromEntity: unrecognized geomtype value %i was accepted by filter\n", solid);
2630                         // this goto only exists to prevent warnings from the compiler
2631                         // about uninitialized variables (mass), while allowing it to
2632                         // catch legitimate uninitialized variable warnings
2633                         goto treatasbox;
2634                 }
2635                 ed->priv.server->ode_mass = massval;
2636                 ed->priv.server->ode_modelindex = modelindex;
2637                 VectorCopy(entmins, ed->priv.server->ode_mins);
2638                 VectorCopy(entmaxs, ed->priv.server->ode_maxs);
2639                 VectorCopy(scale, ed->priv.server->ode_scale);
2640                 ed->priv.server->ode_movelimit = min(geomsize[0], min(geomsize[1], geomsize[2]));
2641                 Matrix4x4_Invert_Simple(&ed->priv.server->ode_offsetimatrix, &ed->priv.server->ode_offsetmatrix);
2642                 ed->priv.server->ode_massbuf = Mem_Alloc(mempool, sizeof(mass));
2643                 memcpy(ed->priv.server->ode_massbuf, &mass, sizeof(dMass));
2644         }
2645
2646         if (ed->priv.server->ode_geom)
2647                 dGeomSetData((dGeomID)ed->priv.server->ode_geom, (void*)ed);
2648         if (movetype == MOVETYPE_PHYSICS && ed->priv.server->ode_geom)
2649         {
2650                 // entity is dynamic
2651                 if (ed->priv.server->ode_body == NULL)
2652                 {
2653                         ed->priv.server->ode_body = (void *)(body = dBodyCreate((dWorldID)world->physics.ode_world));
2654                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2655                         dBodySetData(body, (void*)ed);
2656                         dBodySetMass(body, (dMass *) ed->priv.server->ode_massbuf);
2657                         modified = true;
2658                 }
2659         }
2660         else
2661         {
2662                 // entity is deactivated
2663                 if (ed->priv.server->ode_body != NULL)
2664                 {
2665                         if(ed->priv.server->ode_geom)
2666                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2667                         dBodyDestroy((dBodyID) ed->priv.server->ode_body);
2668                         ed->priv.server->ode_body = NULL;
2669                         modified = true;
2670                 }
2671         }
2672
2673         // get current data from entity
2674         VectorClear(origin);
2675         VectorClear(velocity);
2676         //VectorClear(forward);
2677         //VectorClear(left);
2678         //VectorClear(up);
2679         //VectorClear(spinvelocity);
2680         VectorClear(angles);
2681         VectorClear(avelocity);
2682         gravity = true;
2683         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
2684         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
2685         //VectorCopy(PRVM_gameedictvector(ed, axis_forward), forward);
2686         //VectorCopy(PRVM_gameedictvector(ed, axis_left), left);
2687         //VectorCopy(PRVM_gameedictvector(ed, axis_up), up);
2688         //VectorCopy(PRVM_gameedictvector(ed, spinvelocity), spinvelocity);
2689         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
2690         VectorCopy(PRVM_gameedictvector(ed, avelocity), avelocity);
2691         if (PRVM_gameedictfloat(ed, gravity) != 0.0f && PRVM_gameedictfloat(ed, gravity) < 0.5f) gravity = false;
2692         if (ed == prog->edicts)
2693                 gravity = false;
2694
2695         // compatibility for legacy entities
2696         //if (!VectorLength2(forward) || solid == SOLID_BSP)
2697         {
2698                 float pitchsign = 1;
2699                 vec3_t qangles, qavelocity;
2700                 VectorCopy(angles, qangles);
2701                 VectorCopy(avelocity, qavelocity);
2702
2703                 if(prog == SVVM_prog) // FIXME some better way?
2704                 {
2705                         pitchsign = SV_GetPitchSign(prog, ed);
2706                 }
2707                 else if(prog == CLVM_prog)
2708                 {
2709                         pitchsign = CL_GetPitchSign(prog, ed);
2710                 }
2711                 qangles[PITCH] *= pitchsign;
2712                 qavelocity[PITCH] *= pitchsign;
2713
2714                 AngleVectorsFLU(qangles, forward, left, up);
2715                 // convert single-axis rotations in avelocity to spinvelocity
2716                 // FIXME: untested math - check signs
2717                 VectorSet(spinvelocity, DEG2RAD(qavelocity[PITCH]), DEG2RAD(qavelocity[ROLL]), DEG2RAD(qavelocity[YAW]));
2718         }
2719
2720         // compatibility for legacy entities
2721         switch (solid)
2722         {
2723         case SOLID_BBOX:
2724         case SOLID_SLIDEBOX:
2725         case SOLID_CORPSE:
2726                 VectorSet(forward, 1, 0, 0);
2727                 VectorSet(left, 0, 1, 0);
2728                 VectorSet(up, 0, 0, 1);
2729                 VectorSet(spinvelocity, 0, 0, 0);
2730                 break;
2731         }
2732
2733
2734         // we must prevent NANs...
2735         if (physics_ode_trick_fixnan.integer)
2736         {
2737                 test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity);
2738                 if (VEC_IS_NAN(test))
2739                 {
2740                         modified = true;
2741                         //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]);
2742                         if (physics_ode_trick_fixnan.integer >= 2)
2743                                 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]);
2744                         test = VectorLength2(origin);
2745                         if (VEC_IS_NAN(test))
2746                                 VectorClear(origin);
2747                         test = VectorLength2(forward) * VectorLength2(left) * VectorLength2(up);
2748                         if (VEC_IS_NAN(test))
2749                         {
2750                                 VectorSet(angles, 0, 0, 0);
2751                                 VectorSet(forward, 1, 0, 0);
2752                                 VectorSet(left, 0, 1, 0);
2753                                 VectorSet(up, 0, 0, 1);
2754                         }
2755                         test = VectorLength2(velocity);
2756                         if (VEC_IS_NAN(test))
2757                                 VectorClear(velocity);
2758                         test = VectorLength2(spinvelocity);
2759                         if (VEC_IS_NAN(test))
2760                         {
2761                                 VectorClear(avelocity);
2762                                 VectorClear(spinvelocity);
2763                         }
2764                 }
2765         }
2766
2767         // check if the qc edited any position data
2768         if (!VectorCompare(origin, ed->priv.server->ode_origin)
2769          || !VectorCompare(velocity, ed->priv.server->ode_velocity)
2770          || !VectorCompare(angles, ed->priv.server->ode_angles)
2771          || !VectorCompare(avelocity, ed->priv.server->ode_avelocity)
2772          || gravity != ed->priv.server->ode_gravity)
2773                 modified = true;
2774
2775         // store the qc values into the physics engine
2776         body = (dBodyID)ed->priv.server->ode_body;
2777         if (modified && ed->priv.server->ode_geom)
2778         {
2779                 dVector3 r[3];
2780                 matrix4x4_t entitymatrix;
2781                 matrix4x4_t bodymatrix;
2782
2783 #if 0
2784                 Con_Printf("entity %i got changed by QC\n", (int) (ed - prog->edicts));
2785                 if(!VectorCompare(origin, ed->priv.server->ode_origin))
2786                         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]);
2787                 if(!VectorCompare(velocity, ed->priv.server->ode_velocity))
2788                         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]);
2789                 if(!VectorCompare(angles, ed->priv.server->ode_angles))
2790                         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]);
2791                 if(!VectorCompare(avelocity, ed->priv.server->ode_avelocity))
2792                         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]);
2793                 if(gravity != ed->priv.server->ode_gravity)
2794                         Con_Printf("  gravity: %i -> %i\n", ed->priv.server->ode_gravity, gravity);
2795 #endif
2796                 // values for BodyFromEntity to check if the qc modified anything later
2797                 VectorCopy(origin, ed->priv.server->ode_origin);
2798                 VectorCopy(velocity, ed->priv.server->ode_velocity);
2799                 VectorCopy(angles, ed->priv.server->ode_angles);
2800                 VectorCopy(avelocity, ed->priv.server->ode_avelocity);
2801                 ed->priv.server->ode_gravity = gravity;
2802
2803                 Matrix4x4_FromVectors(&entitymatrix, forward, left, up, origin);
2804                 Matrix4x4_Concat(&bodymatrix, &entitymatrix, &ed->priv.server->ode_offsetmatrix);
2805                 Matrix4x4_ToVectors(&bodymatrix, forward, left, up, origin);
2806                 r[0][0] = forward[0];
2807                 r[1][0] = forward[1];
2808                 r[2][0] = forward[2];
2809                 r[0][1] = left[0];
2810                 r[1][1] = left[1];
2811                 r[2][1] = left[2];
2812                 r[0][2] = up[0];
2813                 r[1][2] = up[1];
2814                 r[2][2] = up[2];
2815                 if (body)
2816                 {
2817                         if (movetype == MOVETYPE_PHYSICS)
2818                         {
2819                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2820                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2821                                 dBodySetRotation(body, r[0]);
2822                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2823                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2824                                 dBodySetGravityMode(body, gravity);
2825                         }
2826                         else
2827                         {
2828                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2829                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2830                                 dBodySetRotation(body, r[0]);
2831                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2832                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2833                                 dBodySetGravityMode(body, gravity);
2834                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2835                         }
2836                 }
2837                 else
2838                 {
2839                         // no body... then let's adjust the parameters of the geom directly
2840                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0); // just in case we previously HAD a body (which should never happen)
2841                         dGeomSetPosition((dGeomID)ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
2842                         dGeomSetRotation((dGeomID)ed->priv.server->ode_geom, r[0]);
2843                 }
2844         }
2845
2846         if (body)
2847         {
2848
2849                 // limit movement speed to prevent missed collisions at high speed
2850                 ovelocity = dBodyGetLinearVel(body);
2851                 ospinvelocity = dBodyGetAngularVel(body);
2852                 movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit;
2853                 test = VectorLength2(ovelocity);
2854                 if (test > movelimit*movelimit)
2855                 {
2856                         // scale down linear velocity to the movelimit
2857                         // scale down angular velocity the same amount for consistency
2858                         f = movelimit / sqrt(test);
2859                         VectorScale(ovelocity, f, velocity);
2860                         VectorScale(ospinvelocity, f, spinvelocity);
2861                         dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2862                         dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2863                 }
2864
2865                 // make sure the angular velocity is not exploding
2866                 spinlimit = physics_ode_spinlimit.value;
2867                 test = VectorLength2(ospinvelocity);
2868                 if (test > spinlimit)
2869                 {
2870                         dBodySetAngularVel(body, 0, 0, 0);
2871                 }
2872
2873                 // apply functions and clear stack
2874                 for(func = ed->priv.server->ode_func; func; func = nextf)
2875                 {
2876                         nextf = func->next;
2877                         World_Physics_ApplyCmd(ed, func);
2878                         Mem_Free(func);
2879                 }
2880                 ed->priv.server->ode_func = NULL;
2881         }
2882 }
2883
2884 #define MAX_CONTACTS 32
2885 static void nearCallback (void *data, dGeomID o1, dGeomID o2)
2886 {
2887         world_t *world = (world_t *)data;
2888         prvm_prog_t *prog = world->prog;
2889         dContact contact[MAX_CONTACTS]; // max contacts per collision pair
2890         int b1enabled = 0, b2enabled = 0;
2891         dBodyID b1, b2;
2892         dJointID c;
2893         int i;
2894         int numcontacts;
2895         float bouncefactor1 = 0.0f;
2896         float bouncestop1 = 60.0f / 800.0f;
2897         float bouncefactor2 = 0.0f;
2898         float bouncestop2 = 60.0f / 800.0f;
2899         float erp;
2900         dVector3 grav;
2901         prvm_edict_t *ed1, *ed2;
2902
2903         if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
2904         {
2905                 // colliding a space with something
2906                 dSpaceCollide2(o1, o2, data, &nearCallback);
2907                 // Note we do not want to test intersections within a space,
2908                 // only between spaces.
2909                 //if (dGeomIsSpace(o1)) dSpaceCollide(o1, data, &nearCallback);
2910                 //if (dGeomIsSpace(o2)) dSpaceCollide(o2, data, &nearCallback);
2911                 return;
2912         }
2913
2914         b1 = dGeomGetBody(o1);
2915         if (b1)
2916                 b1enabled = dBodyIsEnabled(b1);
2917         b2 = dGeomGetBody(o2);
2918         if (b2)
2919                 b2enabled = dBodyIsEnabled(b2);
2920
2921         // at least one object has to be using MOVETYPE_PHYSICS and should be enabled or we just don't care
2922         if (!b1enabled && !b2enabled)
2923                 return;
2924         
2925         // exit without doing anything if the two bodies are connected by a joint
2926         if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact))
2927                 return;
2928
2929         ed1 = (prvm_edict_t *) dGeomGetData(o1);
2930         if(ed1 && ed1->priv.server->free)
2931                 ed1 = NULL;
2932         if(ed1)
2933         {
2934                 bouncefactor1 = PRVM_gameedictfloat(ed1, bouncefactor);
2935                 bouncestop1 = PRVM_gameedictfloat(ed1, bouncestop);
2936                 if (!bouncestop1)
2937                         bouncestop1 = 60.0f / 800.0f;
2938         }
2939
2940         ed2 = (prvm_edict_t *) dGeomGetData(o2);
2941         if(ed2 && ed2->priv.server->free)
2942                 ed2 = NULL;
2943         if(ed2)
2944         {
2945                 bouncefactor2 = PRVM_gameedictfloat(ed2, bouncefactor);
2946                 bouncestop2 = PRVM_gameedictfloat(ed2, bouncestop);
2947                 if (!bouncestop2)
2948                         bouncestop2 = 60.0f / 800.0f;
2949         }
2950
2951         if(prog == SVVM_prog)
2952         {
2953                 if(ed1 && PRVM_serveredictfunction(ed1, touch))
2954                 {
2955                         SV_LinkEdict_TouchAreaGrid_Call(ed1, ed2 ? ed2 : prog->edicts);
2956                 }
2957                 if(ed2 && PRVM_serveredictfunction(ed2, touch))
2958                 {
2959                         SV_LinkEdict_TouchAreaGrid_Call(ed2, ed1 ? ed1 : prog->edicts);
2960                 }
2961         }
2962
2963         // merge bounce factors and bounce stop
2964         if(bouncefactor2 > 0)
2965         {
2966                 if(bouncefactor1 > 0)
2967                 {
2968                         // TODO possibly better logic to merge bounce factor data?
2969                         if(bouncestop2 < bouncestop1)
2970                                 bouncestop1 = bouncestop2;
2971                         if(bouncefactor2 > bouncefactor1)
2972                                 bouncefactor1 = bouncefactor2;
2973                 }
2974                 else
2975                 {
2976                         bouncestop1 = bouncestop2;
2977                         bouncefactor1 = bouncefactor2;
2978                 }
2979         }
2980         dWorldGetGravity((dWorldID)world->physics.ode_world, grav);
2981         bouncestop1 *= fabs(grav[2]);
2982
2983         // get erp
2984         // select object that moves faster ang get it's erp
2985         erp = (VectorLength2(PRVM_gameedictvector(ed1, velocity)) > VectorLength2(PRVM_gameedictvector(ed2, velocity))) ? PRVM_gameedictfloat(ed1, erp) : PRVM_gameedictfloat(ed2, erp);
2986
2987         // get max contact points for this collision
2988         numcontacts = (int)PRVM_gameedictfloat(ed1, maxcontacts);
2989         if (!numcontacts)
2990                 numcontacts = physics_ode_contact_maxpoints.integer;
2991         if (PRVM_gameedictfloat(ed2, maxcontacts))
2992                 numcontacts = max(numcontacts, (int)PRVM_gameedictfloat(ed2, maxcontacts));
2993         else
2994                 numcontacts = max(numcontacts, physics_ode_contact_maxpoints.integer);
2995
2996         // generate contact points between the two non-space geoms
2997         numcontacts = dCollide(o1, o2, min(MAX_CONTACTS, numcontacts), &(contact[0].geom), sizeof(contact[0]));
2998         // add these contact points to the simulation
2999         for (i = 0;i < numcontacts;i++)
3000         {
3001                 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);
3002                 contact[i].surface.mu = physics_ode_contact_mu.value * ed1->priv.server->ode_friction * ed2->priv.server->ode_friction;
3003                 contact[i].surface.soft_erp = physics_ode_contact_erp.value + erp;
3004                 contact[i].surface.soft_cfm = physics_ode_contact_cfm.value;
3005                 contact[i].surface.bounce = bouncefactor1;
3006                 contact[i].surface.bounce_vel = bouncestop1;
3007                 c = dJointCreateContact((dWorldID)world->physics.ode_world, (dJointGroupID)world->physics.ode_contactgroup, contact + i);
3008                 dJointAttach(c, b1, b2);
3009         }
3010 }
3011 #endif
3012
3013 void World_Physics_Frame(world_t *world, double frametime, double gravity)
3014 {
3015 #ifdef USEODE
3016         prvm_prog_t *prog = world->prog;
3017         double tdelta, tdelta2, tdelta3, simulationtime, collisiontime;
3018
3019         tdelta = Sys_DirtyTime();
3020         if (world->physics.ode && physics_ode.integer)
3021         {
3022                 int i;
3023                 prvm_edict_t *ed;
3024
3025                 if (!physics_ode_constantstep.value)
3026                 {
3027                         world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000);
3028                         world->physics.ode_step = frametime / world->physics.ode_iterations;
3029                 }
3030                 else
3031                 {
3032                         world->physics.ode_time += frametime;
3033                         // step size
3034                         if (physics_ode_constantstep.value > 0 && physics_ode_constantstep.value < 1)
3035                                 world->physics.ode_step = physics_ode_constantstep.value;
3036                         else
3037                                 world->physics.ode_step = sys_ticrate.value;
3038                         if (world->physics.ode_time > 0.2f)
3039                                 world->physics.ode_time = world->physics.ode_step;
3040                         // set number of iterations to process
3041                         world->physics.ode_iterations = 0;
3042                         while(world->physics.ode_time >= world->physics.ode_step)
3043                         {
3044                                 world->physics.ode_iterations++;
3045                                 world->physics.ode_time -= world->physics.ode_step;
3046                         }
3047                 }       
3048                 world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step;
3049                 World_Physics_UpdateODE(world);
3050
3051                 // copy physics properties from entities to physics engine
3052                 if (prog)
3053                 {
3054                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
3055                                 if (!prog->edicts[i].priv.required->free)
3056                                         World_Physics_Frame_BodyFromEntity(world, ed);
3057                         // oh, and it must be called after all bodies were created
3058                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
3059                                 if (!prog->edicts[i].priv.required->free)
3060                                         World_Physics_Frame_JointFromEntity(world, ed);
3061                 }
3062
3063                 tdelta2 = Sys_DirtyTime();
3064                 collisiontime = 0;
3065                 for (i = 0;i < world->physics.ode_iterations;i++)
3066                 {
3067                         // set the gravity
3068                         dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity * physics_ode_world_gravitymod.value);
3069                         // set the tolerance for closeness of objects
3070                         dWorldSetContactSurfaceLayer((dWorldID)world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value));
3071                         // run collisions for the current world state, creating JointGroup
3072                         tdelta3 = Sys_DirtyTime();
3073                         dSpaceCollide((dSpaceID)world->physics.ode_space, (void *)world, nearCallback);
3074                         collisiontime += (Sys_DirtyTime() - tdelta3)*10000;
3075                         // apply forces
3076                         if (prog)
3077                         {
3078                                 int j;
3079                                 for (j = 0, ed = prog->edicts + j;j < prog->num_edicts;j++, ed++)
3080                                         if (!prog->edicts[j].priv.required->free)
3081                                                 World_Physics_Frame_ForceFromEntity(world, ed);
3082                         }
3083                         // run physics (move objects, calculate new velocities)
3084                         // be sure not to pass 0 as step time because that causes an ODE error
3085                         dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldstep_iterations.integer, 200));
3086                         if (world->physics.ode_step > 0)
3087                                 dWorldQuickStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
3088                         // clear the JointGroup now that we're done with it
3089                         dJointGroupEmpty((dJointGroupID)world->physics.ode_contactgroup);
3090                 }
3091                 simulationtime = (Sys_DirtyTime() - tdelta2)*10000;
3092
3093                 // copy physics properties from physics engine to entities and do some stats
3094                 if (prog)
3095                 {
3096                         for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
3097                                 if (!prog->edicts[i].priv.required->free)
3098                                         World_Physics_Frame_BodyToEntity(world, ed);
3099
3100                         // print stats
3101                         if (physics_ode_printstats.integer)
3102                         {
3103                                 dBodyID body;
3104
3105                                 world->physics.ode_numobjects = 0;
3106                                 world->physics.ode_activeovjects = 0;
3107                                 for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
3108                                 {
3109                                         if (prog->edicts[i].priv.required->free)
3110                                                 continue;
3111                                         body = (dBodyID)prog->edicts[i].priv.server->ode_body;
3112                                         if (!body)
3113                                                 continue;
3114                                         world->physics.ode_numobjects++;
3115                                         if (dBodyIsEnabled(body))
3116                                                 world->physics.ode_activeovjects++;
3117                                 }
3118                                 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));
3119                         }
3120                 }
3121         }
3122 #endif
3123 }