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