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