]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - world.c
::stable-branch::reset=d2daf00cd44c7a3be817bfddbf221ff6029f8efb
[xonotic/darkplaces.git] / world.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // world.c -- world query functions
21
22 #include "quakedef.h"
23 #include "clvm_cmds.h"
24 #include "cl_collision.h"
25
26 /*
27
28 entities never clip against themselves, or their owner
29
30 line of sight checks trace->inopen and trace->inwater, but bullets don't
31
32 */
33
34 static void World_Physics_Init(void);
35 void World_Init(void)
36 {
37         Collision_Init();
38         World_Physics_Init();
39 }
40
41 static void World_Physics_Shutdown(void);
42 void World_Shutdown(void)
43 {
44         World_Physics_Shutdown();
45 }
46
47 static void World_Physics_Start(world_t *world);
48 void World_Start(world_t *world)
49 {
50         World_Physics_Start(world);
51 }
52
53 static void World_Physics_End(world_t *world);
54 void World_End(world_t *world)
55 {
56         World_Physics_End(world);
57 }
58
59 //============================================================================
60
61 /// World_ClearLink is used for new headnodes
62 void World_ClearLink (link_t *l)
63 {
64         l->entitynumber = 0;
65         l->prev = l->next = l;
66 }
67
68 void World_RemoveLink (link_t *l)
69 {
70         l->next->prev = l->prev;
71         l->prev->next = l->next;
72 }
73
74 void World_InsertLinkBefore (link_t *l, link_t *before, int entitynumber)
75 {
76         l->entitynumber = entitynumber;
77         l->next = before;
78         l->prev = before->prev;
79         l->prev->next = l;
80         l->next->prev = l;
81 }
82
83 /*
84 ===============================================================================
85
86 ENTITY AREA CHECKING
87
88 ===============================================================================
89 */
90
91 void World_PrintAreaStats(world_t *world, const char *worldname)
92 {
93         Con_Printf("%s areagrid check stats: %d calls %d nodes (%f per call) %d entities (%f per call)\n", worldname, world->areagrid_stats_calls, world->areagrid_stats_nodechecks, (double) world->areagrid_stats_nodechecks / (double) world->areagrid_stats_calls, world->areagrid_stats_entitychecks, (double) world->areagrid_stats_entitychecks / (double) world->areagrid_stats_calls);
94         world->areagrid_stats_calls = 0;
95         world->areagrid_stats_nodechecks = 0;
96         world->areagrid_stats_entitychecks = 0;
97 }
98
99 /*
100 ===============
101 World_SetSize
102
103 ===============
104 */
105 void World_SetSize(world_t *world, const char *filename, const vec3_t mins, const vec3_t maxs, prvm_prog_t *prog)
106 {
107         int i;
108
109         strlcpy(world->filename, filename, sizeof(world->filename));
110         VectorCopy(mins, world->mins);
111         VectorCopy(maxs, world->maxs);
112         world->prog = prog;
113
114         // the areagrid_marknumber is not allowed to be 0
115         if (world->areagrid_marknumber < 1)
116                 world->areagrid_marknumber = 1;
117         // choose either the world box size, or a larger box to ensure the grid isn't too fine
118         world->areagrid_size[0] = max(world->maxs[0] - world->mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
119         world->areagrid_size[1] = max(world->maxs[1] - world->mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
120         world->areagrid_size[2] = max(world->maxs[2] - world->mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
121         // figure out the corners of such a box, centered at the center of the world box
122         world->areagrid_mins[0] = (world->mins[0] + world->maxs[0] - world->areagrid_size[0]) * 0.5f;
123         world->areagrid_mins[1] = (world->mins[1] + world->maxs[1] - world->areagrid_size[1]) * 0.5f;
124         world->areagrid_mins[2] = (world->mins[2] + world->maxs[2] - world->areagrid_size[2]) * 0.5f;
125         world->areagrid_maxs[0] = (world->mins[0] + world->maxs[0] + world->areagrid_size[0]) * 0.5f;
126         world->areagrid_maxs[1] = (world->mins[1] + world->maxs[1] + world->areagrid_size[1]) * 0.5f;
127         world->areagrid_maxs[2] = (world->mins[2] + world->maxs[2] + world->areagrid_size[2]) * 0.5f;
128         // now calculate the actual useful info from that
129         VectorNegate(world->areagrid_mins, world->areagrid_bias);
130         world->areagrid_scale[0] = AREA_GRID / world->areagrid_size[0];
131         world->areagrid_scale[1] = AREA_GRID / world->areagrid_size[1];
132         world->areagrid_scale[2] = AREA_GRID / world->areagrid_size[2];
133         World_ClearLink(&world->areagrid_outside);
134         for (i = 0;i < AREA_GRIDNODES;i++)
135                 World_ClearLink(&world->areagrid[i]);
136         if (developer_extra.integer)
137                 Con_DPrintf("areagrid settings: divisions %ix%ix1 : box %f %f %f : %f %f %f size %f %f %f grid %f %f %f (mingrid %f)\n", AREA_GRID, AREA_GRID, world->areagrid_mins[0], world->areagrid_mins[1], world->areagrid_mins[2], world->areagrid_maxs[0], world->areagrid_maxs[1], world->areagrid_maxs[2], world->areagrid_size[0], world->areagrid_size[1], world->areagrid_size[2], 1.0f / world->areagrid_scale[0], 1.0f / world->areagrid_scale[1], 1.0f / world->areagrid_scale[2], sv_areagrid_mingridsize.value);
138 }
139
140 /*
141 ===============
142 World_UnlinkAll
143
144 ===============
145 */
146 void World_UnlinkAll(world_t *world)
147 {
148         prvm_prog_t *prog = world->prog;
149         int i;
150         link_t *grid;
151         // unlink all entities one by one
152         grid = &world->areagrid_outside;
153         while (grid->next != grid)
154                 World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
155         for (i = 0, grid = world->areagrid;i < AREA_GRIDNODES;i++, grid++)
156                 while (grid->next != grid)
157                         World_UnlinkEdict(PRVM_EDICT_NUM(grid->next->entitynumber));
158 }
159
160 /*
161 ===============
162
163 ===============
164 */
165 void World_UnlinkEdict(prvm_edict_t *ent)
166 {
167         int i;
168         for (i = 0;i < ENTITYGRIDAREAS;i++)
169         {
170                 if (ent->priv.server->areagrid[i].prev)
171                 {
172                         World_RemoveLink (&ent->priv.server->areagrid[i]);
173                         ent->priv.server->areagrid[i].prev = ent->priv.server->areagrid[i].next = NULL;
174                 }
175         }
176 }
177
178 int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t requestmaxs, int maxlist, prvm_edict_t **list)
179 {
180         prvm_prog_t *prog = world->prog;
181         int numlist;
182         link_t *grid;
183         link_t *l;
184         prvm_edict_t *ent;
185         vec3_t paddedmins, paddedmaxs;
186         int igrid[3], igridmins[3], igridmaxs[3];
187
188         VectorSet(paddedmins, requestmins[0] - 1.0f, requestmins[1] - 1.0f, requestmins[2] - 1.0f);
189         VectorSet(paddedmaxs, requestmaxs[0] + 1.0f, requestmaxs[1] + 1.0f, requestmaxs[2] + 1.0f);
190
191         // FIXME: if areagrid_marknumber wraps, all entities need their
192         // ent->priv.server->areagridmarknumber reset
193         world->areagrid_stats_calls++;
194         world->areagrid_marknumber++;
195         igridmins[0] = (int) floor((paddedmins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
196         igridmins[1] = (int) floor((paddedmins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
197         //igridmins[2] = (int) ((paddedmins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
198         igridmaxs[0] = (int) floor((paddedmaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
199         igridmaxs[1] = (int) floor((paddedmaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
200         //igridmaxs[2] = (int) ((paddedmaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
201         igridmins[0] = max(0, igridmins[0]);
202         igridmins[1] = max(0, igridmins[1]);
203         //igridmins[2] = max(0, igridmins[2]);
204         igridmaxs[0] = min(AREA_GRID, igridmaxs[0]);
205         igridmaxs[1] = min(AREA_GRID, igridmaxs[1]);
206         //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]);
207
208         // paranoid debugging
209         //VectorSet(igridmins, 0, 0, 0);VectorSet(igridmaxs, AREA_GRID, AREA_GRID, AREA_GRID);
210
211         numlist = 0;
212         // add entities not linked into areagrid because they are too big or
213         // outside the grid bounds
214         if (world->areagrid_outside.next)
215         {
216                 grid = &world->areagrid_outside;
217                 for (l = grid->next;l != grid;l = l->next)
218                 {
219                         ent = PRVM_EDICT_NUM(l->entitynumber);
220                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
221                         {
222                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
223                                 if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
224                                 {
225                                         if (numlist < maxlist)
226                                                 list[numlist] = ent;
227                                         numlist++;
228                                 }
229                                 world->areagrid_stats_entitychecks++;
230                         }
231                 }
232         }
233         // add grid linked entities
234         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
235         {
236                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
237                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++)
238                 {
239                         if (grid->next)
240                         {
241                                 for (l = grid->next;l != grid;l = l->next)
242                                 {
243                                         ent = PRVM_EDICT_NUM(l->entitynumber);
244                                         if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber)
245                                         {
246                                                 ent->priv.server->areagridmarknumber = world->areagrid_marknumber;
247                                                 if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs))
248                                                 {
249                                                         if (numlist < maxlist)
250                                                                 list[numlist] = ent;
251                                                         numlist++;
252                                                 }
253                                                 //Con_Printf("%d %f %f %f %f %f %f : %d : %f %f %f %f %f %f\n", BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs), ent->priv.server->areamins[0], ent->priv.server->areamins[1], ent->priv.server->areamins[2], ent->priv.server->areamaxs[0], ent->priv.server->areamaxs[1], ent->priv.server->areamaxs[2], PRVM_NUM_FOR_EDICT(ent), mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
254                                         }
255                                         world->areagrid_stats_entitychecks++;
256                                 }
257                         }
258                 }
259         }
260         return numlist;
261 }
262
263 static void World_LinkEdict_AreaGrid(world_t *world, prvm_edict_t *ent)
264 {
265         prvm_prog_t *prog = world->prog;
266         link_t *grid;
267         int igrid[3], igridmins[3], igridmaxs[3], gridnum, entitynumber = PRVM_NUM_FOR_EDICT(ent);
268
269         if (entitynumber <= 0 || entitynumber >= prog->max_edicts || PRVM_EDICT_NUM(entitynumber) != ent)
270         {
271                 Con_Printf ("World_LinkEdict_AreaGrid: invalid edict %p (edicts is %p, edict compared to prog->edicts is %i)\n", (void *)ent, (void *)prog->edicts, entitynumber);
272                 return;
273         }
274
275         igridmins[0] = (int) floor((ent->priv.server->areamins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]);
276         igridmins[1] = (int) floor((ent->priv.server->areamins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]);
277         //igridmins[2] = (int) floor((ent->priv.server->areamins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]);
278         igridmaxs[0] = (int) floor((ent->priv.server->areamaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1;
279         igridmaxs[1] = (int) floor((ent->priv.server->areamaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1;
280         //igridmaxs[2] = (int) floor((ent->priv.server->areamaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1;
281         if (igridmins[0] < 0 || igridmaxs[0] > AREA_GRID || igridmins[1] < 0 || igridmaxs[1] > AREA_GRID || ((igridmaxs[0] - igridmins[0]) * (igridmaxs[1] - igridmins[1])) > ENTITYGRIDAREAS)
282         {
283                 // wow, something outside the grid, store it as such
284                 World_InsertLinkBefore (&ent->priv.server->areagrid[0], &world->areagrid_outside, entitynumber);
285                 return;
286         }
287
288         gridnum = 0;
289         for (igrid[1] = igridmins[1];igrid[1] < igridmaxs[1];igrid[1]++)
290         {
291                 grid = world->areagrid + igrid[1] * AREA_GRID + igridmins[0];
292                 for (igrid[0] = igridmins[0];igrid[0] < igridmaxs[0];igrid[0]++, grid++, gridnum++)
293                         World_InsertLinkBefore (&ent->priv.server->areagrid[gridnum], grid, entitynumber);
294         }
295 }
296
297 /*
298 ===============
299 World_LinkEdict
300
301 ===============
302 */
303 void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const vec3_t maxs)
304 {
305         prvm_prog_t *prog = world->prog;
306         // unlink from old position first
307         if (ent->priv.server->areagrid[0].prev)
308                 World_UnlinkEdict(ent);
309
310         // don't add the world
311         if (ent == prog->edicts)
312                 return;
313
314         // don't add free entities
315         if (ent->priv.server->free)
316                 return;
317
318         VectorCopy(mins, ent->priv.server->areamins);
319         VectorCopy(maxs, ent->priv.server->areamaxs);
320         World_LinkEdict_AreaGrid(world, ent);
321 }
322
323
324
325
326 //============================================================================
327 // physics engine support
328 //============================================================================
329
330 #ifndef ODE_STATIC
331 # define ODE_DYNAMIC 1
332 #endif
333
334 #if defined(ODE_STATIC) || defined(ODE_DYNAMIC)
335 #define USEODE 1
336 #endif
337
338 #ifdef USEODE
339 cvar_t physics_ode_quadtree_depth = {0, "physics_ode_quadtree_depth","5", "desired subdivision level of quadtree culling space"};
340 cvar_t physics_ode_contactsurfacelayer = {0, "physics_ode_contactsurfacelayer","1", "allows objects to overlap this many units to reduce jitter"};
341 cvar_t physics_ode_worldstep_iterations = {0, "physics_ode_worldstep_iterations", "20", "parameter to dWorldQuickStep"};
342 cvar_t physics_ode_contact_mu = {0, "physics_ode_contact_mu", "1", "contact solver mu parameter - friction pyramid approximation 1 (see ODE User Guide)"};
343 cvar_t physics_ode_contact_erp = {0, "physics_ode_contact_erp", "0.96", "contact solver erp parameter - Error Restitution Percent (see ODE User Guide)"};
344 cvar_t physics_ode_contact_cfm = {0, "physics_ode_contact_cfm", "0", "contact solver cfm parameter - Constraint Force Mixing (see ODE User Guide)"};
345 cvar_t physics_ode_contact_maxpoints = {0, "physics_ode_contact_maxpoints", "16", "maximal number of contact points between 2 objects, higher = stable (and slower), can be up to 32"};
346 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"};
347 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"};
348 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"};
349 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"};
350 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"};
351 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"};
352 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"};
353 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"};
354 cvar_t physics_ode_iterationsperframe = {0, "physics_ode_iterationsperframe", "1", "divisor for time step, runs multiple physics steps per frame"};
355 cvar_t physics_ode_constantstep = {0, "physics_ode_constantstep", "0", "use constant step instead of variable step which tends to increase stability, if set to 1 uses sys_ticrate, instead uses it's own value"};
356 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"};
357 cvar_t physics_ode_autodisable_steps = {0, "physics_ode_autodisable_steps", "10", "how many steps object should be dormant to be autodisabled"};
358 cvar_t physics_ode_autodisable_time = {0, "physics_ode_autodisable_time", "0", "how many seconds object should be dormant to be autodisabled"};
359 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"};
360 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"};
361 cvar_t physics_ode_autodisable_threshold_samples = {0, "physics_ode_autodisable_threshold_samples", "5", "average threshold with this number of samples"};
362 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"};
363 cvar_t physics_ode_spinlimit = {0, "physics_ode_spinlimit", "10000", "reset spin velocity if it gets too large"};
364 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"};
365 cvar_t physics_ode_printstats = {0, "physics_ode_printstats", "0", "print ODE stats each frame"};
366 cvar_t physics_ode = {0, "physics_ode", "0", "run ODE physics (VERY experimental and potentially buggy)"};
367
368 // LordHavoc: this large chunk of definitions comes from the ODE library
369 // include files.
370
371 #ifdef ODE_STATIC
372 #include "ode/ode.h"
373 #else
374 #ifdef WINAPI
375 // ODE does not use WINAPI
376 #define ODE_API
377 #else
378 #define ODE_API
379 #endif
380
381 // note: dynamic builds of ODE tend to be double precision, this is not used
382 // for static builds
383 typedef double dReal;
384
385 typedef dReal dVector3[4];
386 typedef dReal dVector4[4];
387 typedef dReal dMatrix3[4*3];
388 typedef dReal dMatrix4[4*4];
389 typedef dReal dMatrix6[8*6];
390 typedef dReal dQuaternion[4];
391
392 struct dxWorld;         /* dynamics world */
393 struct dxSpace;         /* collision space */
394 struct dxBody;          /* rigid body (dynamics object) */
395 struct dxGeom;          /* geometry (collision object) */
396 struct dxJoint;
397 struct dxJointNode;
398 struct dxJointGroup;
399 struct dxTriMeshData;
400
401 #define dInfinity 3.402823466e+38f
402
403 typedef struct dxWorld *dWorldID;
404 typedef struct dxSpace *dSpaceID;
405 typedef struct dxBody *dBodyID;
406 typedef struct dxGeom *dGeomID;
407 typedef struct dxJoint *dJointID;
408 typedef struct dxJointGroup *dJointGroupID;
409 typedef struct dxTriMeshData *dTriMeshDataID;
410
411 typedef struct dJointFeedback
412 {
413         dVector3 f1;            /* force applied to body 1 */
414         dVector3 t1;            /* torque applied to body 1 */
415         dVector3 f2;            /* force applied to body 2 */
416         dVector3 t2;            /* torque applied to body 2 */
417 }
418 dJointFeedback;
419
420 typedef enum dJointType
421 {
422         dJointTypeNone = 0,
423         dJointTypeBall,
424         dJointTypeHinge,
425         dJointTypeSlider,
426         dJointTypeContact,
427         dJointTypeUniversal,
428         dJointTypeHinge2,
429         dJointTypeFixed,
430         dJointTypeNull,
431         dJointTypeAMotor,
432         dJointTypeLMotor,
433         dJointTypePlane2D,
434         dJointTypePR,
435         dJointTypePU,
436         dJointTypePiston
437 }
438 dJointType;
439
440 #define D_ALL_PARAM_NAMES(start) \
441   /* parameters for limits and motors */ \
442   dParamLoStop = start, \
443   dParamHiStop, \
444   dParamVel, \
445   dParamFMax, \
446   dParamFudgeFactor, \
447   dParamBounce, \
448   dParamCFM, \
449   dParamStopERP, \
450   dParamStopCFM, \
451   /* parameters for suspension */ \
452   dParamSuspensionERP, \
453   dParamSuspensionCFM, \
454   dParamERP, \
455
456 #define D_ALL_PARAM_NAMES_X(start,x) \
457   /* parameters for limits and motors */ \
458   dParamLoStop ## x = start, \
459   dParamHiStop ## x, \
460   dParamVel ## x, \
461   dParamFMax ## x, \
462   dParamFudgeFactor ## x, \
463   dParamBounce ## x, \
464   dParamCFM ## x, \
465   dParamStopERP ## x, \
466   dParamStopCFM ## x, \
467   /* parameters for suspension */ \
468   dParamSuspensionERP ## x, \
469   dParamSuspensionCFM ## x, \
470   dParamERP ## x,
471
472 enum {
473   D_ALL_PARAM_NAMES(0)
474   D_ALL_PARAM_NAMES_X(0x100,2)
475   D_ALL_PARAM_NAMES_X(0x200,3)
476
477   /* add a multiple of this constant to the basic parameter numbers to get
478    * the parameters for the second, third etc axes.
479    */
480   dParamGroup=0x100
481 };
482
483 typedef struct dMass
484 {
485         dReal mass;
486         dVector3 c;
487         dMatrix3 I;
488 }
489 dMass;
490
491 enum
492 {
493         dContactMu2                     = 0x001,
494         dContactFDir1           = 0x002,
495         dContactBounce          = 0x004,
496         dContactSoftERP         = 0x008,
497         dContactSoftCFM         = 0x010,
498         dContactMotion1         = 0x020,
499         dContactMotion2         = 0x040,
500         dContactMotionN         = 0x080,
501         dContactSlip1           = 0x100,
502         dContactSlip2           = 0x200,
503         
504         dContactApprox0         = 0x0000,
505         dContactApprox1_1       = 0x1000,
506         dContactApprox1_2       = 0x2000,
507         dContactApprox1         = 0x3000
508 };
509
510 typedef struct dSurfaceParameters
511 {
512         /* must always be defined */
513         int mode;
514         dReal mu;
515
516         /* only defined if the corresponding flag is set in mode */
517         dReal mu2;
518         dReal bounce;
519         dReal bounce_vel;
520         dReal soft_erp;
521         dReal soft_cfm;
522         dReal motion1,motion2,motionN;
523         dReal slip1,slip2;
524 } dSurfaceParameters;
525
526 typedef struct dContactGeom
527 {
528         dVector3 pos;          ///< contact position
529         dVector3 normal;       ///< normal vector
530         dReal depth;           ///< penetration depth
531         dGeomID g1,g2;         ///< the colliding geoms
532         int side1,side2;       ///< (to be documented)
533 }
534 dContactGeom;
535
536 typedef struct dContact
537 {
538         dSurfaceParameters surface;
539         dContactGeom geom;
540         dVector3 fdir1;
541 }
542 dContact;
543
544 typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2);
545
546 // SAP
547 // Order XZY or ZXY usually works best, if your Y is up.
548 #define dSAP_AXES_XYZ  ((0)|(1<<2)|(2<<4))
549 #define dSAP_AXES_XZY  ((0)|(2<<2)|(1<<4))
550 #define dSAP_AXES_YXZ  ((1)|(0<<2)|(2<<4))
551 #define dSAP_AXES_YZX  ((1)|(2<<2)|(0<<4))
552 #define dSAP_AXES_ZXY  ((2)|(0<<2)|(1<<4))
553 #define dSAP_AXES_ZYX  ((2)|(1<<2)|(0<<4))
554
555 const char*     (ODE_API *dGetConfiguration)(void);
556 int             (ODE_API *dCheckConfiguration)( const char* token );
557 int             (ODE_API *dInitODE)(void);
558 //int             (ODE_API *dInitODE2)(unsigned int uiInitFlags);
559 //int             (ODE_API *dAllocateODEDataForThread)(unsigned int uiAllocateFlags);
560 //void            (ODE_API *dCleanupODEAllDataForThread)(void);
561 void            (ODE_API *dCloseODE)(void);
562
563 //int             (ODE_API *dMassCheck)(const dMass *m);
564 //void            (ODE_API *dMassSetZero)(dMass *);
565 //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);
566 //void            (ODE_API *dMassSetSphere)(dMass *, dReal density, dReal radius);
567 void            (ODE_API *dMassSetSphereTotal)(dMass *, dReal total_mass, dReal radius);
568 //void            (ODE_API *dMassSetCapsule)(dMass *, dReal density, int direction, dReal radius, dReal length);
569 void            (ODE_API *dMassSetCapsuleTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length);
570 //void            (ODE_API *dMassSetCylinder)(dMass *, dReal density, int direction, dReal radius, dReal length);
571 void            (ODE_API *dMassSetCylinderTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length);
572 //void            (ODE_API *dMassSetBox)(dMass *, dReal density, dReal lx, dReal ly, dReal lz);
573 void            (ODE_API *dMassSetBoxTotal)(dMass *, dReal total_mass, dReal lx, dReal ly, dReal lz);
574 //void            (ODE_API *dMassSetTrimesh)(dMass *, dReal density, dGeomID g);
575 //void            (ODE_API *dMassSetTrimeshTotal)(dMass *m, dReal total_mass, dGeomID g);
576 //void            (ODE_API *dMassAdjust)(dMass *, dReal newmass);
577 //void            (ODE_API *dMassTranslate)(dMass *, dReal x, dReal y, dReal z);
578 //void            (ODE_API *dMassRotate)(dMass *, const dMatrix3 R);
579 //void            (ODE_API *dMassAdd)(dMass *a, const dMass *b);
580 //
581 dWorldID        (ODE_API *dWorldCreate)(void);
582 void            (ODE_API *dWorldDestroy)(dWorldID world);
583 void            (ODE_API *dWorldSetGravity)(dWorldID, dReal x, dReal y, dReal z);
584 void            (ODE_API *dWorldGetGravity)(dWorldID, dVector3 gravity);
585 void            (ODE_API *dWorldSetERP)(dWorldID, dReal erp);
586 //dReal           (ODE_API *dWorldGetERP)(dWorldID);
587 void            (ODE_API *dWorldSetCFM)(dWorldID, dReal cfm);
588 //dReal           (ODE_API *dWorldGetCFM)(dWorldID);
589 //void            (ODE_API *dWorldStep)(dWorldID, dReal stepsize);
590 //void            (ODE_API *dWorldImpulseToForce)(dWorldID, dReal stepsize, dReal ix, dReal iy, dReal iz, dVector3 force);
591 void            (ODE_API *dWorldQuickStep)(dWorldID w, dReal stepsize);
592 void            (ODE_API *dWorldSetQuickStepNumIterations)(dWorldID, int num);
593 //int             (ODE_API *dWorldGetQuickStepNumIterations)(dWorldID);
594 //void            (ODE_API *dWorldSetQuickStepW)(dWorldID, dReal over_relaxation);
595 //dReal           (ODE_API *dWorldGetQuickStepW)(dWorldID);
596 //void            (ODE_API *dWorldSetContactMaxCorrectingVel)(dWorldID, dReal vel);
597 //dReal           (ODE_API *dWorldGetContactMaxCorrectingVel)(dWorldID);
598 void            (ODE_API *dWorldSetContactSurfaceLayer)(dWorldID, dReal depth);
599 //dReal           (ODE_API *dWorldGetContactSurfaceLayer)(dWorldID);
600 //void            (ODE_API *dWorldStepFast1)(dWorldID, dReal stepsize, int maxiterations);
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 //      {"dWorldStepFast1",                                                             (void **) &dWorldStepFast1},
1066 //      {"dWorldSetAutoEnableDepthSF1",                                 (void **) &dWorldSetAutoEnableDepthSF1},
1067 //      {"dWorldGetAutoEnableDepthSF1",                                 (void **) &dWorldGetAutoEnableDepthSF1},
1068 //      {"dWorldGetAutoDisableLinearThreshold",                 (void **) &dWorldGetAutoDisableLinearThreshold},
1069         {"dWorldSetAutoDisableLinearThreshold",                 (void **) &dWorldSetAutoDisableLinearThreshold},
1070 //      {"dWorldGetAutoDisableAngularThreshold",                (void **) &dWorldGetAutoDisableAngularThreshold},
1071         {"dWorldSetAutoDisableAngularThreshold",                (void **) &dWorldSetAutoDisableAngularThreshold},
1072 //      {"dWorldGetAutoDisableLinearAverageThreshold",  (void **) &dWorldGetAutoDisableLinearAverageThreshold},
1073 //      {"dWorldSetAutoDisableLinearAverageThreshold",  (void **) &dWorldSetAutoDisableLinearAverageThreshold},
1074 //      {"dWorldGetAutoDisableAngularAverageThreshold", (void **) &dWorldGetAutoDisableAngularAverageThreshold},
1075 //      {"dWorldSetAutoDisableAngularAverageThreshold", (void **) &dWorldSetAutoDisableAngularAverageThreshold},
1076 //      {"dWorldGetAutoDisableAverageSamplesCount",             (void **) &dWorldGetAutoDisableAverageSamplesCount},
1077         {"dWorldSetAutoDisableAverageSamplesCount",             (void **) &dWorldSetAutoDisableAverageSamplesCount},
1078 //      {"dWorldGetAutoDisableSteps",                                   (void **) &dWorldGetAutoDisableSteps},
1079         {"dWorldSetAutoDisableSteps",                                   (void **) &dWorldSetAutoDisableSteps},
1080 //      {"dWorldGetAutoDisableTime",                                    (void **) &dWorldGetAutoDisableTime},
1081         {"dWorldSetAutoDisableTime",                                    (void **) &dWorldSetAutoDisableTime},
1082 //      {"dWorldGetAutoDisableFlag",                                    (void **) &dWorldGetAutoDisableFlag},
1083         {"dWorldSetAutoDisableFlag",                                    (void **) &dWorldSetAutoDisableFlag},
1084 //      {"dWorldGetLinearDampingThreshold",                             (void **) &dWorldGetLinearDampingThreshold},
1085         {"dWorldSetLinearDampingThreshold",                             (void **) &dWorldSetLinearDampingThreshold},
1086 //      {"dWorldGetAngularDampingThreshold",                    (void **) &dWorldGetAngularDampingThreshold},
1087         {"dWorldSetAngularDampingThreshold",                    (void **) &dWorldSetAngularDampingThreshold},
1088 //      {"dWorldGetLinearDamping",                                              (void **) &dWorldGetLinearDamping},
1089         {"dWorldSetLinearDamping",                                              (void **) &dWorldSetLinearDamping},
1090 //      {"dWorldGetAngularDamping",                                             (void **) &dWorldGetAngularDamping},
1091         {"dWorldSetAngularDamping",                                             (void **) &dWorldSetAngularDamping},
1092 //      {"dWorldSetDamping",                                                    (void **) &dWorldSetDamping},
1093 //      {"dWorldGetMaxAngularSpeed",                                    (void **) &dWorldGetMaxAngularSpeed},
1094 //      {"dWorldSetMaxAngularSpeed",                                    (void **) &dWorldSetMaxAngularSpeed},
1095 //      {"dBodyGetAutoDisableLinearThreshold",                  (void **) &dBodyGetAutoDisableLinearThreshold},
1096 //      {"dBodySetAutoDisableLinearThreshold",                  (void **) &dBodySetAutoDisableLinearThreshold},
1097 //      {"dBodyGetAutoDisableAngularThreshold",                 (void **) &dBodyGetAutoDisableAngularThreshold},
1098 //      {"dBodySetAutoDisableAngularThreshold",                 (void **) &dBodySetAutoDisableAngularThreshold},
1099 //      {"dBodyGetAutoDisableAverageSamplesCount",              (void **) &dBodyGetAutoDisableAverageSamplesCount},
1100 //      {"dBodySetAutoDisableAverageSamplesCount",              (void **) &dBodySetAutoDisableAverageSamplesCount},
1101 //      {"dBodyGetAutoDisableSteps",                                    (void **) &dBodyGetAutoDisableSteps},
1102 //      {"dBodySetAutoDisableSteps",                                    (void **) &dBodySetAutoDisableSteps},
1103 //      {"dBodyGetAutoDisableTime",                                             (void **) &dBodyGetAutoDisableTime},
1104 //      {"dBodySetAutoDisableTime",                                             (void **) &dBodySetAutoDisableTime},
1105 //      {"dBodyGetAutoDisableFlag",                                             (void **) &dBodyGetAutoDisableFlag},
1106 //      {"dBodySetAutoDisableFlag",                                             (void **) &dBodySetAutoDisableFlag},
1107 //      {"dBodySetAutoDisableDefaults",                                 (void **) &dBodySetAutoDisableDefaults},
1108 //      {"dBodyGetWorld",                                                               (void **) &dBodyGetWorld},
1109         {"dBodyCreate",                                                                 (void **) &dBodyCreate},
1110         {"dBodyDestroy",                                                                (void **) &dBodyDestroy},
1111         {"dBodySetData",                                                                (void **) &dBodySetData},
1112         {"dBodyGetData",                                                                (void **) &dBodyGetData},
1113         {"dBodySetPosition",                                                    (void **) &dBodySetPosition},
1114         {"dBodySetRotation",                                                    (void **) &dBodySetRotation},
1115 //      {"dBodySetQuaternion",                                                  (void **) &dBodySetQuaternion},
1116         {"dBodySetLinearVel",                                                   (void **) &dBodySetLinearVel},
1117         {"dBodySetAngularVel",                                                  (void **) &dBodySetAngularVel},
1118         {"dBodyGetPosition",                                                    (void **) &dBodyGetPosition},
1119 //      {"dBodyCopyPosition",                                                   (void **) &dBodyCopyPosition},
1120         {"dBodyGetRotation",                                                    (void **) &dBodyGetRotation},
1121 //      {"dBodyCopyRotation",                                                   (void **) &dBodyCopyRotation},
1122 //      {"dBodyGetQuaternion",                                                  (void **) &dBodyGetQuaternion},
1123 //      {"dBodyCopyQuaternion",                                                 (void **) &dBodyCopyQuaternion},
1124         {"dBodyGetLinearVel",                                                   (void **) &dBodyGetLinearVel},
1125         {"dBodyGetAngularVel",                                                  (void **) &dBodyGetAngularVel},
1126         {"dBodySetMass",                                                                (void **) &dBodySetMass},
1127 //      {"dBodyGetMass",                                                                (void **) &dBodyGetMass},
1128         {"dBodyAddForce",                                                               (void **) &dBodyAddForce},
1129         {"dBodyAddTorque",                                                              (void **) &dBodyAddTorque},
1130 //      {"dBodyAddRelForce",                                                    (void **) &dBodyAddRelForce},
1131 //      {"dBodyAddRelTorque",                                                   (void **) &dBodyAddRelTorque},
1132         {"dBodyAddForceAtPos",                                                  (void **) &dBodyAddForceAtPos},
1133 //      {"dBodyAddForceAtRelPos",                                               (void **) &dBodyAddForceAtRelPos},
1134 //      {"dBodyAddRelForceAtPos",                                               (void **) &dBodyAddRelForceAtPos},
1135 //      {"dBodyAddRelForceAtRelPos",                                    (void **) &dBodyAddRelForceAtRelPos},
1136 //      {"dBodyGetForce",                                                               (void **) &dBodyGetForce},
1137 //      {"dBodyGetTorque",                                                              (void **) &dBodyGetTorque},
1138 //      {"dBodySetForce",                                                               (void **) &dBodySetForce},
1139 //      {"dBodySetTorque",                                                              (void **) &dBodySetTorque},
1140 //      {"dBodyGetRelPointPos",                                                 (void **) &dBodyGetRelPointPos},
1141 //      {"dBodyGetRelPointVel",                                                 (void **) &dBodyGetRelPointVel},
1142 //      {"dBodyGetPointVel",                                                    (void **) &dBodyGetPointVel},
1143 //      {"dBodyGetPosRelPoint",                                                 (void **) &dBodyGetPosRelPoint},
1144 //      {"dBodyVectorToWorld",                                                  (void **) &dBodyVectorToWorld},
1145 //      {"dBodyVectorFromWorld",                                                (void **) &dBodyVectorFromWorld},
1146 //      {"dBodySetFiniteRotationMode",                                  (void **) &dBodySetFiniteRotationMode},
1147 //      {"dBodySetFiniteRotationAxis",                                  (void **) &dBodySetFiniteRotationAxis},
1148 //      {"dBodyGetFiniteRotationMode",                                  (void **) &dBodyGetFiniteRotationMode},
1149 //      {"dBodyGetFiniteRotationAxis",                                  (void **) &dBodyGetFiniteRotationAxis},
1150         {"dBodyGetNumJoints",                                                   (void **) &dBodyGetNumJoints},
1151         {"dBodyGetJoint",                                                               (void **) &dBodyGetJoint},
1152 //      {"dBodySetDynamic",                                                             (void **) &dBodySetDynamic},
1153 //      {"dBodySetKinematic",                                                   (void **) &dBodySetKinematic},
1154 //      {"dBodyIsKinematic",                                                    (void **) &dBodyIsKinematic},
1155         {"dBodyEnable",                                                                 (void **) &dBodyEnable},
1156         {"dBodyDisable",                                                                (void **) &dBodyDisable},
1157         {"dBodyIsEnabled",                                                              (void **) &dBodyIsEnabled},
1158         {"dBodySetGravityMode",                                                 (void **) &dBodySetGravityMode},
1159         {"dBodyGetGravityMode",                                                 (void **) &dBodyGetGravityMode},
1160 //      {"dBodySetMovedCallback",                                               (void **) &dBodySetMovedCallback},
1161 //      {"dBodyGetFirstGeom",                                                   (void **) &dBodyGetFirstGeom},
1162 //      {"dBodyGetNextGeom",                                                    (void **) &dBodyGetNextGeom},
1163 //      {"dBodySetDampingDefaults",                                             (void **) &dBodySetDampingDefaults},
1164 //      {"dBodyGetLinearDamping",                                               (void **) &dBodyGetLinearDamping},
1165 //      {"dBodySetLinearDamping",                                               (void **) &dBodySetLinearDamping},
1166 //      {"dBodyGetAngularDamping",                                              (void **) &dBodyGetAngularDamping},
1167 //      {"dBodySetAngularDamping",                                              (void **) &dBodySetAngularDamping},
1168 //      {"dBodySetDamping",                                                             (void **) &dBodySetDamping},
1169 //      {"dBodyGetLinearDampingThreshold",                              (void **) &dBodyGetLinearDampingThreshold},
1170 //      {"dBodySetLinearDampingThreshold",                              (void **) &dBodySetLinearDampingThreshold},
1171 //      {"dBodyGetAngularDampingThreshold",                             (void **) &dBodyGetAngularDampingThreshold},
1172 //      {"dBodySetAngularDampingThreshold",                             (void **) &dBodySetAngularDampingThreshold},
1173 //      {"dBodyGetMaxAngularSpeed",                                             (void **) &dBodyGetMaxAngularSpeed},
1174 //      {"dBodySetMaxAngularSpeed",                                             (void **) &dBodySetMaxAngularSpeed},
1175 //      {"dBodyGetGyroscopicMode",                                              (void **) &dBodyGetGyroscopicMode},
1176 //      {"dBodySetGyroscopicMode",                                              (void **) &dBodySetGyroscopicMode},
1177         {"dJointCreateBall",                                                    (void **) &dJointCreateBall},
1178         {"dJointCreateHinge",                                                   (void **) &dJointCreateHinge},
1179         {"dJointCreateSlider",                                                  (void **) &dJointCreateSlider},
1180         {"dJointCreateContact",                                                 (void **) &dJointCreateContact},
1181         {"dJointCreateHinge2",                                                  (void **) &dJointCreateHinge2},
1182         {"dJointCreateUniversal",                                               (void **) &dJointCreateUniversal},
1183 //      {"dJointCreatePR",                                                              (void **) &dJointCreatePR},
1184 //      {"dJointCreatePU",                                                              (void **) &dJointCreatePU},
1185 //      {"dJointCreatePiston",                                                  (void **) &dJointCreatePiston},
1186         {"dJointCreateFixed",                                                   (void **) &dJointCreateFixed},
1187 //      {"dJointCreateNull",                                                    (void **) &dJointCreateNull},
1188 //      {"dJointCreateAMotor",                                                  (void **) &dJointCreateAMotor},
1189 //      {"dJointCreateLMotor",                                                  (void **) &dJointCreateLMotor},
1190 //      {"dJointCreatePlane2D",                                                 (void **) &dJointCreatePlane2D},
1191         {"dJointDestroy",                                                               (void **) &dJointDestroy},
1192         {"dJointGroupCreate",                                                   (void **) &dJointGroupCreate},
1193         {"dJointGroupDestroy",                                                  (void **) &dJointGroupDestroy},
1194         {"dJointGroupEmpty",                                                    (void **) &dJointGroupEmpty},
1195 //      {"dJointGetNumBodies",                                                  (void **) &dJointGetNumBodies},
1196         {"dJointAttach",                                                                (void **) &dJointAttach},
1197 //      {"dJointEnable",                                                                (void **) &dJointEnable},
1198 //      {"dJointDisable",                                                               (void **) &dJointDisable},
1199 //      {"dJointIsEnabled",                                                             (void **) &dJointIsEnabled},
1200         {"dJointSetData",                                                               (void **) &dJointSetData},
1201         {"dJointGetData",                                                               (void **) &dJointGetData},
1202 //      {"dJointGetType",                                                               (void **) &dJointGetType},
1203         {"dJointGetBody",                                                               (void **) &dJointGetBody},
1204 //      {"dJointSetFeedback",                                                   (void **) &dJointSetFeedback},
1205 //      {"dJointGetFeedback",                                                   (void **) &dJointGetFeedback},
1206         {"dJointSetBallAnchor",                                                 (void **) &dJointSetBallAnchor},
1207 //      {"dJointSetBallAnchor2",                                                (void **) &dJointSetBallAnchor2},
1208         {"dJointSetBallParam",                                                  (void **) &dJointSetBallParam},
1209         {"dJointSetHingeAnchor",                                                (void **) &dJointSetHingeAnchor},
1210 //      {"dJointSetHingeAnchorDelta",                                   (void **) &dJointSetHingeAnchorDelta},
1211         {"dJointSetHingeAxis",                                                  (void **) &dJointSetHingeAxis},
1212 //      {"dJointSetHingeAxisOffset",                                    (void **) &dJointSetHingeAxisOffset},
1213         {"dJointSetHingeParam",                                                 (void **) &dJointSetHingeParam},
1214 //      {"dJointAddHingeTorque",                                                (void **) &dJointAddHingeTorque},
1215         {"dJointSetSliderAxis",                                                 (void **) &dJointSetSliderAxis},
1216 //      {"dJointSetSliderAxisDelta",                                    (void **) &dJointSetSliderAxisDelta},
1217         {"dJointSetSliderParam",                                                (void **) &dJointSetSliderParam},
1218 //      {"dJointAddSliderForce",                                                (void **) &dJointAddSliderForce},
1219         {"dJointSetHinge2Anchor",                                               (void **) &dJointSetHinge2Anchor},
1220         {"dJointSetHinge2Axis1",                                                (void **) &dJointSetHinge2Axis1},
1221         {"dJointSetHinge2Axis2",                                                (void **) &dJointSetHinge2Axis2},
1222         {"dJointSetHinge2Param",                                                (void **) &dJointSetHinge2Param},
1223 //      {"dJointAddHinge2Torques",                                              (void **) &dJointAddHinge2Torques},
1224         {"dJointSetUniversalAnchor",                                    (void **) &dJointSetUniversalAnchor},
1225         {"dJointSetUniversalAxis1",                                             (void **) &dJointSetUniversalAxis1},
1226 //      {"dJointSetUniversalAxis1Offset",                               (void **) &dJointSetUniversalAxis1Offset},
1227         {"dJointSetUniversalAxis2",                                             (void **) &dJointSetUniversalAxis2},
1228 //      {"dJointSetUniversalAxis2Offset",                               (void **) &dJointSetUniversalAxis2Offset},
1229         {"dJointSetUniversalParam",                                             (void **) &dJointSetUniversalParam},
1230 //      {"dJointAddUniversalTorques",                                   (void **) &dJointAddUniversalTorques},
1231 //      {"dJointSetPRAnchor",                                                   (void **) &dJointSetPRAnchor},
1232 //      {"dJointSetPRAxis1",                                                    (void **) &dJointSetPRAxis1},
1233 //      {"dJointSetPRAxis2",                                                    (void **) &dJointSetPRAxis2},
1234 //      {"dJointSetPRParam",                                                    (void **) &dJointSetPRParam},
1235 //      {"dJointAddPRTorque",                                                   (void **) &dJointAddPRTorque},
1236 //      {"dJointSetPUAnchor",                                                   (void **) &dJointSetPUAnchor},
1237 //      {"dJointSetPUAnchorOffset",                                             (void **) &dJointSetPUAnchorOffset},
1238 //      {"dJointSetPUAxis1",                                                    (void **) &dJointSetPUAxis1},
1239 //      {"dJointSetPUAxis2",                                                    (void **) &dJointSetPUAxis2},
1240 //      {"dJointSetPUAxis3",                                                    (void **) &dJointSetPUAxis3},
1241 //      {"dJointSetPUAxisP",                                                    (void **) &dJointSetPUAxisP},
1242 //      {"dJointSetPUParam",                                                    (void **) &dJointSetPUParam},
1243 //      {"dJointAddPUTorque",                                                   (void **) &dJointAddPUTorque},
1244 //      {"dJointSetPistonAnchor",                                               (void **) &dJointSetPistonAnchor},
1245 //      {"dJointSetPistonAnchorOffset",                                 (void **) &dJointSetPistonAnchorOffset},
1246 //      {"dJointSetPistonParam",                                                (void **) &dJointSetPistonParam},
1247 //      {"dJointAddPistonForce",                                                (void **) &dJointAddPistonForce},
1248 //      {"dJointSetFixed",                                                              (void **) &dJointSetFixed},
1249 //      {"dJointSetFixedParam",                                                 (void **) &dJointSetFixedParam},
1250 //      {"dJointSetAMotorNumAxes",                                              (void **) &dJointSetAMotorNumAxes},
1251 //      {"dJointSetAMotorAxis",                                                 (void **) &dJointSetAMotorAxis},
1252 //      {"dJointSetAMotorAngle",                                                (void **) &dJointSetAMotorAngle},
1253 //      {"dJointSetAMotorParam",                                                (void **) &dJointSetAMotorParam},
1254 //      {"dJointSetAMotorMode",                                                 (void **) &dJointSetAMotorMode},
1255 //      {"dJointAddAMotorTorques",                                              (void **) &dJointAddAMotorTorques},
1256 //      {"dJointSetLMotorNumAxes",                                              (void **) &dJointSetLMotorNumAxes},
1257 //      {"dJointSetLMotorAxis",                                                 (void **) &dJointSetLMotorAxis},
1258 //      {"dJointSetLMotorParam",                                                (void **) &dJointSetLMotorParam},
1259 //      {"dJointSetPlane2DXParam",                                              (void **) &dJointSetPlane2DXParam},
1260 //      {"dJointSetPlane2DYParam",                                              (void **) &dJointSetPlane2DYParam},
1261 //      {"dJointSetPlane2DAngleParam",                                  (void **) &dJointSetPlane2DAngleParam},
1262 //      {"dJointGetBallAnchor",                                                 (void **) &dJointGetBallAnchor},
1263 //      {"dJointGetBallAnchor2",                                                (void **) &dJointGetBallAnchor2},
1264 //      {"dJointGetBallParam",                                                  (void **) &dJointGetBallParam},
1265 //      {"dJointGetHingeAnchor",                                                (void **) &dJointGetHingeAnchor},
1266 //      {"dJointGetHingeAnchor2",                                               (void **) &dJointGetHingeAnchor2},
1267 //      {"dJointGetHingeAxis",                                                  (void **) &dJointGetHingeAxis},
1268 //      {"dJointGetHingeParam",                                                 (void **) &dJointGetHingeParam},
1269 //      {"dJointGetHingeAngle",                                                 (void **) &dJointGetHingeAngle},
1270 //      {"dJointGetHingeAngleRate",                                             (void **) &dJointGetHingeAngleRate},
1271 //      {"dJointGetSliderPosition",                                             (void **) &dJointGetSliderPosition},
1272 //      {"dJointGetSliderPositionRate",                                 (void **) &dJointGetSliderPositionRate},
1273 //      {"dJointGetSliderAxis",                                                 (void **) &dJointGetSliderAxis},
1274 //      {"dJointGetSliderParam",                                                (void **) &dJointGetSliderParam},
1275 //      {"dJointGetHinge2Anchor",                                               (void **) &dJointGetHinge2Anchor},
1276 //      {"dJointGetHinge2Anchor2",                                              (void **) &dJointGetHinge2Anchor2},
1277 //      {"dJointGetHinge2Axis1",                                                (void **) &dJointGetHinge2Axis1},
1278 //      {"dJointGetHinge2Axis2",                                                (void **) &dJointGetHinge2Axis2},
1279 //      {"dJointGetHinge2Param",                                                (void **) &dJointGetHinge2Param},
1280 //      {"dJointGetHinge2Angle1",                                               (void **) &dJointGetHinge2Angle1},
1281 //      {"dJointGetHinge2Angle1Rate",                                   (void **) &dJointGetHinge2Angle1Rate},
1282 //      {"dJointGetHinge2Angle2Rate",                                   (void **) &dJointGetHinge2Angle2Rate},
1283 //      {"dJointGetUniversalAnchor",                                    (void **) &dJointGetUniversalAnchor},
1284 //      {"dJointGetUniversalAnchor2",                                   (void **) &dJointGetUniversalAnchor2},
1285 //      {"dJointGetUniversalAxis1",                                             (void **) &dJointGetUniversalAxis1},
1286 //      {"dJointGetUniversalAxis2",                                             (void **) &dJointGetUniversalAxis2},
1287 //      {"dJointGetUniversalParam",                                             (void **) &dJointGetUniversalParam},
1288 //      {"dJointGetUniversalAngles",                                    (void **) &dJointGetUniversalAngles},
1289 //      {"dJointGetUniversalAngle1",                                    (void **) &dJointGetUniversalAngle1},
1290 //      {"dJointGetUniversalAngle2",                                    (void **) &dJointGetUniversalAngle2},
1291 //      {"dJointGetUniversalAngle1Rate",                                (void **) &dJointGetUniversalAngle1Rate},
1292 //      {"dJointGetUniversalAngle2Rate",                                (void **) &dJointGetUniversalAngle2Rate},
1293 //      {"dJointGetPRAnchor",                                                   (void **) &dJointGetPRAnchor},
1294 //      {"dJointGetPRPosition",                                                 (void **) &dJointGetPRPosition},
1295 //      {"dJointGetPRPositionRate",                                             (void **) &dJointGetPRPositionRate},
1296 //      {"dJointGetPRAngle",                                                    (void **) &dJointGetPRAngle},
1297 //      {"dJointGetPRAngleRate",                                                (void **) &dJointGetPRAngleRate},
1298 //      {"dJointGetPRAxis1",                                                    (void **) &dJointGetPRAxis1},
1299 //      {"dJointGetPRAxis2",                                                    (void **) &dJointGetPRAxis2},
1300 //      {"dJointGetPRParam",                                                    (void **) &dJointGetPRParam},
1301 //      {"dJointGetPUAnchor",                                                   (void **) &dJointGetPUAnchor},
1302 //      {"dJointGetPUPosition",                                                 (void **) &dJointGetPUPosition},
1303 //      {"dJointGetPUPositionRate",                                             (void **) &dJointGetPUPositionRate},
1304 //      {"dJointGetPUAxis1",                                                    (void **) &dJointGetPUAxis1},
1305 //      {"dJointGetPUAxis2",                                                    (void **) &dJointGetPUAxis2},
1306 //      {"dJointGetPUAxis3",                                                    (void **) &dJointGetPUAxis3},
1307 //      {"dJointGetPUAxisP",                                                    (void **) &dJointGetPUAxisP},
1308 //      {"dJointGetPUAngles",                                                   (void **) &dJointGetPUAngles},
1309 //      {"dJointGetPUAngle1",                                                   (void **) &dJointGetPUAngle1},
1310 //      {"dJointGetPUAngle1Rate",                                               (void **) &dJointGetPUAngle1Rate},
1311 //      {"dJointGetPUAngle2",                                                   (void **) &dJointGetPUAngle2},
1312 //      {"dJointGetPUAngle2Rate",                                               (void **) &dJointGetPUAngle2Rate},
1313 //      {"dJointGetPUParam",                                                    (void **) &dJointGetPUParam},
1314 //      {"dJointGetPistonPosition",                                             (void **) &dJointGetPistonPosition},
1315 //      {"dJointGetPistonPositionRate",                                 (void **) &dJointGetPistonPositionRate},
1316 //      {"dJointGetPistonAngle",                                                (void **) &dJointGetPistonAngle},
1317 //      {"dJointGetPistonAngleRate",                                    (void **) &dJointGetPistonAngleRate},
1318 //      {"dJointGetPistonAnchor",                                               (void **) &dJointGetPistonAnchor},
1319 //      {"dJointGetPistonAnchor2",                                              (void **) &dJointGetPistonAnchor2},
1320 //      {"dJointGetPistonAxis",                                                 (void **) &dJointGetPistonAxis},
1321 //      {"dJointGetPistonParam",                                                (void **) &dJointGetPistonParam},
1322 //      {"dJointGetAMotorNumAxes",                                              (void **) &dJointGetAMotorNumAxes},
1323 //      {"dJointGetAMotorAxis",                                                 (void **) &dJointGetAMotorAxis},
1324 //      {"dJointGetAMotorAxisRel",                                              (void **) &dJointGetAMotorAxisRel},
1325 //      {"dJointGetAMotorAngle",                                                (void **) &dJointGetAMotorAngle},
1326 //      {"dJointGetAMotorAngleRate",                                    (void **) &dJointGetAMotorAngleRate},
1327 //      {"dJointGetAMotorParam",                                                (void **) &dJointGetAMotorParam},
1328 //      {"dJointGetAMotorMode",                                                 (void **) &dJointGetAMotorMode},
1329 //      {"dJointGetLMotorNumAxes",                                              (void **) &dJointGetLMotorNumAxes},
1330 //      {"dJointGetLMotorAxis",                                                 (void **) &dJointGetLMotorAxis},
1331 //      {"dJointGetLMotorParam",                                                (void **) &dJointGetLMotorParam},
1332 //      {"dJointGetFixedParam",                                                 (void **) &dJointGetFixedParam},
1333 //      {"dConnectingJoint",                                                    (void **) &dConnectingJoint},
1334 //      {"dConnectingJointList",                                                (void **) &dConnectingJointList},
1335         {"dAreConnected",                                                               (void **) &dAreConnected},
1336         {"dAreConnectedExcluding",                                              (void **) &dAreConnectedExcluding},
1337         {"dSimpleSpaceCreate",                                                  (void **) &dSimpleSpaceCreate},
1338         {"dHashSpaceCreate",                                                    (void **) &dHashSpaceCreate},
1339         {"dQuadTreeSpaceCreate",                                                (void **) &dQuadTreeSpaceCreate},
1340 //      {"dSweepAndPruneSpaceCreate",                                   (void **) &dSweepAndPruneSpaceCreate},
1341         {"dSpaceDestroy",                                                               (void **) &dSpaceDestroy},
1342 //      {"dHashSpaceSetLevels",                                                 (void **) &dHashSpaceSetLevels},
1343 //      {"dHashSpaceGetLevels",                                                 (void **) &dHashSpaceGetLevels},
1344 //      {"dSpaceSetCleanup",                                                    (void **) &dSpaceSetCleanup},
1345 //      {"dSpaceGetCleanup",                                                    (void **) &dSpaceGetCleanup},
1346 //      {"dSpaceSetSublevel",                                                   (void **) &dSpaceSetSublevel},
1347 //      {"dSpaceGetSublevel",                                                   (void **) &dSpaceGetSublevel},
1348 //      {"dSpaceSetManualCleanup",                                              (void **) &dSpaceSetManualCleanup},
1349 //      {"dSpaceGetManualCleanup",                                              (void **) &dSpaceGetManualCleanup},
1350 //      {"dSpaceAdd",                                                                   (void **) &dSpaceAdd},
1351 //      {"dSpaceRemove",                                                                (void **) &dSpaceRemove},
1352 //      {"dSpaceQuery",                                                                 (void **) &dSpaceQuery},
1353 //      {"dSpaceClean",                                                                 (void **) &dSpaceClean},
1354 //      {"dSpaceGetNumGeoms",                                                   (void **) &dSpaceGetNumGeoms},
1355 //      {"dSpaceGetGeom",                                                               (void **) &dSpaceGetGeom},
1356 //      {"dSpaceGetClass",                                                              (void **) &dSpaceGetClass},
1357         {"dGeomDestroy",                                                                (void **) &dGeomDestroy},
1358         {"dGeomSetData",                                                                (void **) &dGeomSetData},
1359         {"dGeomGetData",                                                                (void **) &dGeomGetData},
1360         {"dGeomSetBody",                                                                (void **) &dGeomSetBody},
1361         {"dGeomGetBody",                                                                (void **) &dGeomGetBody},
1362         {"dGeomSetPosition",                                                    (void **) &dGeomSetPosition},
1363         {"dGeomSetRotation",                                                    (void **) &dGeomSetRotation},
1364 //      {"dGeomSetQuaternion",                                                  (void **) &dGeomSetQuaternion},
1365 //      {"dGeomGetPosition",                                                    (void **) &dGeomGetPosition},
1366 //      {"dGeomCopyPosition",                                                   (void **) &dGeomCopyPosition},
1367 //      {"dGeomGetRotation",                                                    (void **) &dGeomGetRotation},
1368 //      {"dGeomCopyRotation",                                                   (void **) &dGeomCopyRotation},
1369 //      {"dGeomGetQuaternion",                                                  (void **) &dGeomGetQuaternion},
1370 //      {"dGeomGetAABB",                                                                (void **) &dGeomGetAABB},
1371         {"dGeomIsSpace",                                                                (void **) &dGeomIsSpace},
1372 //      {"dGeomGetSpace",                                                               (void **) &dGeomGetSpace},
1373 //      {"dGeomGetClass",                                                               (void **) &dGeomGetClass},
1374 //      {"dGeomSetCategoryBits",                                                (void **) &dGeomSetCategoryBits},
1375 //      {"dGeomSetCollideBits",                                                 (void **) &dGeomSetCollideBits},
1376 //      {"dGeomGetCategoryBits",                                                (void **) &dGeomGetCategoryBits},
1377 //      {"dGeomGetCollideBits",                                                 (void **) &dGeomGetCollideBits},
1378 //      {"dGeomEnable",                                                                 (void **) &dGeomEnable},
1379 //      {"dGeomDisable",                                                                (void **) &dGeomDisable},
1380 //      {"dGeomIsEnabled",                                                              (void **) &dGeomIsEnabled},
1381 //      {"dGeomSetOffsetPosition",                                              (void **) &dGeomSetOffsetPosition},
1382 //      {"dGeomSetOffsetRotation",                                              (void **) &dGeomSetOffsetRotation},
1383 //      {"dGeomSetOffsetQuaternion",                                    (void **) &dGeomSetOffsetQuaternion},
1384 //      {"dGeomSetOffsetWorldPosition",                                 (void **) &dGeomSetOffsetWorldPosition},
1385 //      {"dGeomSetOffsetWorldRotation",                                 (void **) &dGeomSetOffsetWorldRotation},
1386 //      {"dGeomSetOffsetWorldQuaternion",                               (void **) &dGeomSetOffsetWorldQuaternion},
1387 //      {"dGeomClearOffset",                                                    (void **) &dGeomClearOffset},
1388 //      {"dGeomIsOffset",                                                               (void **) &dGeomIsOffset},
1389 //      {"dGeomGetOffsetPosition",                                              (void **) &dGeomGetOffsetPosition},
1390 //      {"dGeomCopyOffsetPosition",                                             (void **) &dGeomCopyOffsetPosition},
1391 //      {"dGeomGetOffsetRotation",                                              (void **) &dGeomGetOffsetRotation},
1392 //      {"dGeomCopyOffsetRotation",                                             (void **) &dGeomCopyOffsetRotation},
1393 //      {"dGeomGetOffsetQuaternion",                                    (void **) &dGeomGetOffsetQuaternion},
1394         {"dCollide",                                                                    (void **) &dCollide},
1395         {"dSpaceCollide",                                                               (void **) &dSpaceCollide},
1396         {"dSpaceCollide2",                                                              (void **) &dSpaceCollide2},
1397         {"dCreateSphere",                                                               (void **) &dCreateSphere},
1398 //      {"dGeomSphereSetRadius",                                                (void **) &dGeomSphereSetRadius},
1399 //      {"dGeomSphereGetRadius",                                                (void **) &dGeomSphereGetRadius},
1400 //      {"dGeomSpherePointDepth",                                               (void **) &dGeomSpherePointDepth},
1401 //      {"dCreateConvex",                                                               (void **) &dCreateConvex},
1402 //      {"dGeomSetConvex",                                                              (void **) &dGeomSetConvex},
1403         {"dCreateBox",                                                                  (void **) &dCreateBox},
1404 //      {"dGeomBoxSetLengths",                                                  (void **) &dGeomBoxSetLengths},
1405 //      {"dGeomBoxGetLengths",                                                  (void **) &dGeomBoxGetLengths},
1406 //      {"dGeomBoxPointDepth",                                                  (void **) &dGeomBoxPointDepth},
1407 //      {"dGeomBoxPointDepth",                                                  (void **) &dGeomBoxPointDepth},
1408 //      {"dCreatePlane",                                                                (void **) &dCreatePlane},
1409 //      {"dGeomPlaneSetParams",                                                 (void **) &dGeomPlaneSetParams},
1410 //      {"dGeomPlaneGetParams",                                                 (void **) &dGeomPlaneGetParams},
1411 //      {"dGeomPlanePointDepth",                                                (void **) &dGeomPlanePointDepth},
1412         {"dCreateCapsule",                                                              (void **) &dCreateCapsule},
1413 //      {"dGeomCapsuleSetParams",                                               (void **) &dGeomCapsuleSetParams},
1414 //      {"dGeomCapsuleGetParams",                                               (void **) &dGeomCapsuleGetParams},
1415 //      {"dGeomCapsulePointDepth",                                              (void **) &dGeomCapsulePointDepth},
1416         {"dCreateCylinder",                                                             (void **) &dCreateCylinder},
1417 //      {"dGeomCylinderSetParams",                                              (void **) &dGeomCylinderSetParams},
1418 //      {"dGeomCylinderGetParams",                                              (void **) &dGeomCylinderGetParams},
1419 //      {"dCreateRay",                                                                  (void **) &dCreateRay},
1420 //      {"dGeomRaySetLength",                                                   (void **) &dGeomRaySetLength},
1421 //      {"dGeomRayGetLength",                                                   (void **) &dGeomRayGetLength},
1422 //      {"dGeomRaySet",                                                                 (void **) &dGeomRaySet},
1423 //      {"dGeomRayGet",                                                                 (void **) &dGeomRayGet},
1424         {"dCreateGeomTransform",                                                (void **) &dCreateGeomTransform},
1425         {"dGeomTransformSetGeom",                                               (void **) &dGeomTransformSetGeom},
1426 //      {"dGeomTransformGetGeom",                                               (void **) &dGeomTransformGetGeom},
1427         {"dGeomTransformSetCleanup",                                    (void **) &dGeomTransformSetCleanup},
1428 //      {"dGeomTransformGetCleanup",                                    (void **) &dGeomTransformGetCleanup},
1429 //      {"dGeomTransformSetInfo",                                               (void **) &dGeomTransformSetInfo},
1430 //      {"dGeomTransformGetInfo",                                               (void **) &dGeomTransformGetInfo},
1431         {"dGeomTriMeshDataCreate",                      (void **) &dGeomTriMeshDataCreate},
1432         {"dGeomTriMeshDataDestroy",                     (void **) &dGeomTriMeshDataDestroy},
1433 //      {"dGeomTriMeshDataSet",                         (void **) &dGeomTriMeshDataSet},
1434 //      {"dGeomTriMeshDataGet",                         (void **) &dGeomTriMeshDataGet},
1435 //      {"dGeomTriMeshSetLastTransform",                (void **) &dGeomTriMeshSetLastTransform},
1436 //      {"dGeomTriMeshGetLastTransform",                (void **) &dGeomTriMeshGetLastTransform},
1437         {"dGeomTriMeshDataBuildSingle",                 (void **) &dGeomTriMeshDataBuildSingle},
1438 //      {"dGeomTriMeshDataBuildSingle1",                (void **) &dGeomTriMeshDataBuildSingle1},
1439 //      {"dGeomTriMeshDataBuildDouble",                 (void **) &dGeomTriMeshDataBuildDouble},
1440 //      {"dGeomTriMeshDataBuildDouble1",                (void **) &dGeomTriMeshDataBuildDouble1},
1441 //      {"dGeomTriMeshDataBuildSimple",                 (void **) &dGeomTriMeshDataBuildSimple},
1442 //      {"dGeomTriMeshDataBuildSimple1",                (void **) &dGeomTriMeshDataBuildSimple1},
1443 //      {"dGeomTriMeshDataPreprocess",                  (void **) &dGeomTriMeshDataPreprocess},
1444 //      {"dGeomTriMeshDataGetBuffer",                   (void **) &dGeomTriMeshDataGetBuffer},
1445 //      {"dGeomTriMeshDataSetBuffer",                   (void **) &dGeomTriMeshDataSetBuffer},
1446 //      {"dGeomTriMeshSetCallback",                     (void **) &dGeomTriMeshSetCallback},
1447 //      {"dGeomTriMeshGetCallback",                     (void **) &dGeomTriMeshGetCallback},
1448 //      {"dGeomTriMeshSetArrayCallback",                (void **) &dGeomTriMeshSetArrayCallback},
1449 //      {"dGeomTriMeshGetArrayCallback",                (void **) &dGeomTriMeshGetArrayCallback},
1450 //      {"dGeomTriMeshSetRayCallback",                  (void **) &dGeomTriMeshSetRayCallback},
1451 //      {"dGeomTriMeshGetRayCallback",                  (void **) &dGeomTriMeshGetRayCallback},
1452 //      {"dGeomTriMeshSetTriMergeCallback",             (void **) &dGeomTriMeshSetTriMergeCallback},
1453 //      {"dGeomTriMeshGetTriMergeCallback",             (void **) &dGeomTriMeshGetTriMergeCallback},
1454         {"dCreateTriMesh",                              (void **) &dCreateTriMesh},
1455 //      {"dGeomTriMeshSetData",                         (void **) &dGeomTriMeshSetData},
1456 //      {"dGeomTriMeshGetData",                         (void **) &dGeomTriMeshGetData},
1457 //      {"dGeomTriMeshEnableTC",                        (void **) &dGeomTriMeshEnableTC},
1458 //      {"dGeomTriMeshIsTCEnabled",                     (void **) &dGeomTriMeshIsTCEnabled},
1459 //      {"dGeomTriMeshClearTCCache",                    (void **) &dGeomTriMeshClearTCCache},
1460 //      {"dGeomTriMeshGetTriMeshDataID",                (void **) &dGeomTriMeshGetTriMeshDataID},
1461 //      {"dGeomTriMeshGetTriangle",                     (void **) &dGeomTriMeshGetTriangle},
1462 //      {"dGeomTriMeshGetPoint",                        (void **) &dGeomTriMeshGetPoint},
1463 //      {"dGeomTriMeshGetTriangleCount",                (void **) &dGeomTriMeshGetTriangleCount},
1464 //      {"dGeomTriMeshDataUpdate",                      (void **) &dGeomTriMeshDataUpdate},
1465         {NULL, NULL}
1466 };
1467
1468 // Handle for ODE DLL
1469 dllhandle_t ode_dll = NULL;
1470 #endif
1471 #endif
1472
1473 static void World_Physics_Init(void)
1474 {
1475 #ifdef USEODE
1476 #ifdef ODE_DYNAMIC
1477         const char* dllnames [] =
1478         {
1479 # if defined(WIN32)
1480                 "libode1.dll",
1481 # elif defined(MACOSX)
1482                 "libode.1.dylib",
1483 # else
1484                 "libode.so.1",
1485 # endif
1486                 NULL
1487         };
1488 #endif
1489
1490         Cvar_RegisterVariable(&physics_ode_quadtree_depth);
1491         Cvar_RegisterVariable(&physics_ode_contactsurfacelayer);
1492         Cvar_RegisterVariable(&physics_ode_worldstep_iterations);
1493         Cvar_RegisterVariable(&physics_ode_contact_mu);
1494         Cvar_RegisterVariable(&physics_ode_contact_erp);
1495         Cvar_RegisterVariable(&physics_ode_contact_cfm);
1496         Cvar_RegisterVariable(&physics_ode_contact_maxpoints);
1497         Cvar_RegisterVariable(&physics_ode_world_erp);
1498         Cvar_RegisterVariable(&physics_ode_world_cfm);
1499         Cvar_RegisterVariable(&physics_ode_world_damping);
1500         Cvar_RegisterVariable(&physics_ode_world_damping_linear);
1501         Cvar_RegisterVariable(&physics_ode_world_damping_linear_threshold);
1502         Cvar_RegisterVariable(&physics_ode_world_damping_angular);
1503         Cvar_RegisterVariable(&physics_ode_world_damping_angular_threshold);
1504         Cvar_RegisterVariable(&physics_ode_world_gravitymod);
1505         Cvar_RegisterVariable(&physics_ode_iterationsperframe);
1506         Cvar_RegisterVariable(&physics_ode_constantstep);
1507         Cvar_RegisterVariable(&physics_ode_movelimit);
1508         Cvar_RegisterVariable(&physics_ode_spinlimit);
1509         Cvar_RegisterVariable(&physics_ode_trick_fixnan);
1510         Cvar_RegisterVariable(&physics_ode_autodisable);
1511         Cvar_RegisterVariable(&physics_ode_autodisable_steps);
1512         Cvar_RegisterVariable(&physics_ode_autodisable_time);
1513         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_linear);
1514         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_angular);
1515         Cvar_RegisterVariable(&physics_ode_autodisable_threshold_samples);
1516         Cvar_RegisterVariable(&physics_ode_printstats);
1517         Cvar_RegisterVariable(&physics_ode);
1518
1519 #ifdef ODE_DYNAMIC
1520         // Load the DLL
1521         if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs))
1522 #endif
1523         {
1524                 dInitODE();
1525 //              dInitODE2(0);
1526 #ifdef ODE_DYNAMIC
1527 # ifdef dSINGLE
1528                 if (!dCheckConfiguration("ODE_single_precision"))
1529 # else
1530                 if (!dCheckConfiguration("ODE_double_precision"))
1531 # endif
1532                 {
1533 # ifdef dSINGLE
1534                         Con_Printf("ODE library not compiled for single precision - incompatible!  Not using ODE physics.\n");
1535 # else
1536                         Con_Printf("ODE library not compiled for double precision - incompatible!  Not using ODE physics.\n");
1537 # endif
1538                         Sys_UnloadLibrary(&ode_dll);
1539                         ode_dll = NULL;
1540                 }
1541                 else
1542                 {
1543 # ifdef dSINGLE
1544                         Con_Printf("ODE library loaded with single precision.\n");
1545 # else
1546                         Con_Printf("ODE library loaded with double precision.\n");
1547 # endif
1548                         Con_Printf("ODE configuration list: %s\n", dGetConfiguration());
1549                 }
1550 #endif
1551         }
1552 #endif
1553 }
1554
1555 static void World_Physics_Shutdown(void)
1556 {
1557 #ifdef USEODE
1558 #ifdef ODE_DYNAMIC
1559         if (ode_dll)
1560 #endif
1561         {
1562                 dCloseODE();
1563 #ifdef ODE_DYNAMIC
1564                 Sys_UnloadLibrary(&ode_dll);
1565                 ode_dll = NULL;
1566 #endif
1567         }
1568 #endif
1569 }
1570
1571 #ifdef USEODE
1572 static void World_Physics_UpdateODE(world_t *world)
1573 {
1574         dWorldID odeworld;
1575
1576         odeworld = (dWorldID)world->physics.ode_world;
1577
1578         // ERP and CFM
1579         if (physics_ode_world_erp.value >= 0)
1580                 dWorldSetERP(odeworld, physics_ode_world_erp.value);
1581         if (physics_ode_world_cfm.value >= 0)
1582                 dWorldSetCFM(odeworld, physics_ode_world_cfm.value);
1583         // Damping
1584         if (physics_ode_world_damping.integer)
1585         {
1586                 dWorldSetLinearDamping(odeworld, (physics_ode_world_damping_linear.value >= 0) ? (physics_ode_world_damping_linear.value * physics_ode_world_damping.value) : 0);
1587                 dWorldSetLinearDampingThreshold(odeworld, (physics_ode_world_damping_linear_threshold.value >= 0) ? (physics_ode_world_damping_linear_threshold.value * physics_ode_world_damping.value) : 0);
1588                 dWorldSetAngularDamping(odeworld, (physics_ode_world_damping_angular.value >= 0) ? (physics_ode_world_damping_angular.value * physics_ode_world_damping.value) : 0);
1589                 dWorldSetAngularDampingThreshold(odeworld, (physics_ode_world_damping_angular_threshold.value >= 0) ? (physics_ode_world_damping_angular_threshold.value * physics_ode_world_damping.value) : 0);
1590         }
1591         else
1592         {
1593                 dWorldSetLinearDamping(odeworld, 0);
1594                 dWorldSetLinearDampingThreshold(odeworld, 0);
1595                 dWorldSetAngularDamping(odeworld, 0);
1596                 dWorldSetAngularDampingThreshold(odeworld, 0);
1597         }
1598         // Autodisable
1599         dWorldSetAutoDisableFlag(odeworld, (physics_ode_autodisable.integer) ? 1 : 0);
1600         if (physics_ode_autodisable.integer)
1601         {
1602                 dWorldSetAutoDisableSteps(odeworld, bound(1, physics_ode_autodisable_steps.integer, 100)); 
1603                 dWorldSetAutoDisableTime(odeworld, physics_ode_autodisable_time.value);
1604                 dWorldSetAutoDisableAverageSamplesCount(odeworld, bound(1, physics_ode_autodisable_threshold_samples.integer, 100));
1605                 dWorldSetAutoDisableLinearThreshold(odeworld, physics_ode_autodisable_threshold_linear.value); 
1606                 dWorldSetAutoDisableAngularThreshold(odeworld, physics_ode_autodisable_threshold_angular.value); 
1607         }
1608 }
1609
1610 static void World_Physics_EnableODE(world_t *world)
1611 {
1612         dVector3 center, extents;
1613         if (world->physics.ode)
1614                 return;
1615 #ifdef ODE_DYNAMIC
1616         if (!ode_dll)
1617                 return;
1618 #endif
1619         world->physics.ode = true;
1620         VectorMAM(0.5f, world->mins, 0.5f, world->maxs, center);
1621         VectorSubtract(world->maxs, center, extents);
1622         world->physics.ode_world = dWorldCreate();
1623         world->physics.ode_space = dQuadTreeSpaceCreate(NULL, center, extents, bound(1, physics_ode_quadtree_depth.integer, 10));
1624         world->physics.ode_contactgroup = dJointGroupCreate(0);
1625
1626         World_Physics_UpdateODE(world);
1627 }
1628 #endif
1629
1630 static void World_Physics_Start(world_t *world)
1631 {
1632 #ifdef USEODE
1633         if (world->physics.ode)
1634                 return;
1635         World_Physics_EnableODE(world);
1636 #endif
1637 }
1638
1639 static void World_Physics_End(world_t *world)
1640 {
1641 #ifdef USEODE
1642         if (world->physics.ode)
1643         {
1644                 dWorldDestroy((dWorldID)world->physics.ode_world);
1645                 dSpaceDestroy((dSpaceID)world->physics.ode_space);
1646                 dJointGroupDestroy((dJointGroupID)world->physics.ode_contactgroup);
1647                 world->physics.ode = false;
1648         }
1649 #endif
1650 }
1651
1652 void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed)
1653 {
1654         ed->priv.server->ode_joint_type = 0;
1655 #ifdef USEODE
1656         if(ed->priv.server->ode_joint)
1657                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1658         ed->priv.server->ode_joint = NULL;
1659 #endif
1660 }
1661
1662 void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
1663 {
1664         edict_odefunc_t *f, *nf;
1665
1666         // entity is not physics controlled, free any physics data
1667         ed->priv.server->ode_physics = false;
1668 #ifdef USEODE
1669         if (ed->priv.server->ode_geom)
1670                 dGeomDestroy((dGeomID)ed->priv.server->ode_geom);
1671         ed->priv.server->ode_geom = NULL;
1672         if (ed->priv.server->ode_body)
1673         {
1674                 dJointID j;
1675                 dBodyID b1, b2;
1676                 prvm_edict_t *ed2;
1677                 while(dBodyGetNumJoints((dBodyID)ed->priv.server->ode_body))
1678                 {
1679                         j = dBodyGetJoint((dBodyID)ed->priv.server->ode_body, 0);
1680                         ed2 = (prvm_edict_t *) dJointGetData(j);
1681                         b1 = dJointGetBody(j, 0);
1682                         b2 = dJointGetBody(j, 1);
1683                         if(b1 == (dBodyID)ed->priv.server->ode_body)
1684                         {
1685                                 b1 = 0;
1686                                 ed2->priv.server->ode_joint_enemy = 0;
1687                         }
1688                         if(b2 == (dBodyID)ed->priv.server->ode_body)
1689                         {
1690                                 b2 = 0;
1691                                 ed2->priv.server->ode_joint_aiment = 0;
1692                         }
1693                         dJointAttach(j, b1, b2);
1694                 }
1695                 dBodyDestroy((dBodyID)ed->priv.server->ode_body);
1696         }
1697         ed->priv.server->ode_body = NULL;
1698 #endif
1699         if (ed->priv.server->ode_vertex3f)
1700                 Mem_Free(ed->priv.server->ode_vertex3f);
1701         ed->priv.server->ode_vertex3f = NULL;
1702         ed->priv.server->ode_numvertices = 0;
1703         if (ed->priv.server->ode_element3i)
1704                 Mem_Free(ed->priv.server->ode_element3i);
1705         ed->priv.server->ode_element3i = NULL;
1706         ed->priv.server->ode_numtriangles = 0;
1707         if(ed->priv.server->ode_massbuf)
1708                 Mem_Free(ed->priv.server->ode_massbuf);
1709         ed->priv.server->ode_massbuf = NULL;
1710         // clear functions stack
1711         for(f = ed->priv.server->ode_func; f; f = nf)
1712         {
1713                 nf = f->next;
1714                 Mem_Free(f);
1715         }
1716         ed->priv.server->ode_func = NULL;
1717 }
1718
1719 void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f)
1720 {
1721         dBodyID body = (dBodyID)ed->priv.server->ode_body;
1722
1723 #ifdef USEODE
1724         switch(f->type)
1725         {
1726         case ODEFUNC_ENABLE:
1727                 dBodyEnable(body);
1728                 break;
1729         case ODEFUNC_DISABLE:
1730                 dBodyDisable(body);
1731                 break;
1732         case ODEFUNC_FORCE:
1733                 dBodyEnable(body);
1734                 dBodyAddForceAtPos(body, f->v1[0], f->v1[1], f->v1[2], f->v2[0], f->v2[1], f->v2[2]);
1735                 break;
1736         case ODEFUNC_TORQUE:
1737                 dBodyEnable(body);
1738                 dBodyAddTorque(body, f->v1[0], f->v1[1], f->v1[2]);
1739                 break;
1740         default:
1741                 break;
1742         }
1743 #endif
1744 }
1745
1746 #ifdef USEODE
1747 static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
1748 {
1749         prvm_prog_t *prog = world->prog;
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(prog == SVVM_prog) // FIXME some better way?
1816                 {
1817                         pitchsign = SV_GetPitchSign(prog, ed);
1818                 }
1819                 else if(prog == CLVM_prog)
1820                 {
1821                         pitchsign = CL_GetPitchSign(prog, 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(prog == SVVM_prog) // FIXME some better way?
1844         {
1845                 SV_LinkEdict(ed);
1846                 SV_LinkEdict_TouchAreaGrid(ed);
1847         }
1848 }
1849
1850 static void World_Physics_Frame_ForceFromEntity(world_t *world, prvm_edict_t *ed)
1851 {
1852         prvm_prog_t *prog = world->prog;
1853         int forcetype = 0, movetype = 0, enemy = 0;
1854         vec3_t movedir, origin;
1855
1856         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1857         forcetype = (int)PRVM_gameedictfloat(ed, forcetype);
1858         if (movetype == MOVETYPE_PHYSICS)
1859                 forcetype = FORCETYPE_NONE; // can't have both
1860         if (!forcetype)
1861                 return;
1862         enemy = PRVM_gameedictedict(ed, enemy);
1863         if (enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
1864                 return;
1865         VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
1866         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
1867         dBodyEnable((dBodyID)prog->edicts[enemy].priv.server->ode_body);
1868         switch(forcetype)
1869         {
1870                 case FORCETYPE_FORCE:
1871                         if (movedir[0] || movedir[1] || movedir[2])
1872                                 dBodyAddForce((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2]);
1873                         break;
1874                 case FORCETYPE_FORCEATPOS:
1875                         if (movedir[0] || movedir[1] || movedir[2])
1876                                 dBodyAddForceAtPos((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2], origin[0], origin[1], origin[2]);
1877                         break;
1878                 case FORCETYPE_TORQUE:
1879                         if (movedir[0] || movedir[1] || movedir[2])
1880                                 dBodyAddTorque((dBodyID)prog->edicts[enemy].priv.server->ode_body, movedir[0], movedir[1], movedir[2]);
1881                         break;
1882                 case FORCETYPE_NONE:
1883                 default:
1884                         // bad force
1885                         break;
1886         }
1887 }
1888
1889 static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed)
1890 {
1891         prvm_prog_t *prog = world->prog;
1892         dJointID j = 0;
1893         dBodyID b1 = 0;
1894         dBodyID b2 = 0;
1895         int movetype = 0;
1896         int jointtype = 0;
1897         int enemy = 0, aiment = 0;
1898         vec3_t origin, velocity, angles, forward, left, up, movedir;
1899         vec_t CFM, ERP, FMax, Stop, Vel;
1900
1901         movetype = (int)PRVM_gameedictfloat(ed, movetype);
1902         jointtype = (int)PRVM_gameedictfloat(ed, jointtype);
1903         VectorClear(origin);
1904         VectorClear(velocity);
1905         VectorClear(angles);
1906         VectorClear(movedir);
1907         enemy = PRVM_gameedictedict(ed, enemy);
1908         aiment = PRVM_gameedictedict(ed, aiment);
1909         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
1910         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
1911         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
1912         VectorCopy(PRVM_gameedictvector(ed, movedir), movedir);
1913         if(movetype == MOVETYPE_PHYSICS)
1914                 jointtype = JOINTTYPE_NONE; // can't have both
1915         if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
1916                 enemy = 0;
1917         if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
1918                 aiment = 0;
1919         // see http://www.ode.org/old_list_archives/2006-January/017614.html
1920         // we want to set ERP? make it fps independent and work like a spring constant
1921         // note: if movedir[2] is 0, it becomes ERP = 1, CFM = 1.0 / (H * K)
1922         if(movedir[0] > 0 && movedir[1] > 0)
1923         {
1924                 float K = movedir[0];
1925                 float D = movedir[1];
1926                 float R = 2.0 * D * sqrt(K); // we assume D is premultiplied by sqrt(sprungMass)
1927                 CFM = 1.0 / (world->physics.ode_step * K + R); // always > 0
1928                 ERP = world->physics.ode_step * K * CFM;
1929                 Vel = 0;
1930                 FMax = 0;
1931                 Stop = movedir[2];
1932         }
1933         else if(movedir[1] < 0)
1934         {
1935                 CFM = 0;
1936                 ERP = 0;
1937                 Vel = movedir[0];
1938                 FMax = -movedir[1]; // TODO do we need to multiply with world.physics.ode_step?
1939                 Stop = movedir[2] > 0 ? movedir[2] : dInfinity;
1940         }
1941         else // movedir[0] > 0, movedir[1] == 0 or movedir[0] < 0, movedir[1] >= 0
1942         {
1943                 CFM = 0;
1944                 ERP = 0;
1945                 Vel = 0;
1946                 FMax = 0;
1947                 Stop = dInfinity;
1948         }
1949         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))
1950                 return; // nothing to do
1951         AngleVectorsFLU(angles, forward, left, up);
1952         switch(jointtype)
1953         {
1954                 case JOINTTYPE_POINT:
1955                         j = dJointCreateBall((dWorldID)world->physics.ode_world, 0);
1956                         break;
1957                 case JOINTTYPE_HINGE:
1958                         j = dJointCreateHinge((dWorldID)world->physics.ode_world, 0);
1959                         break;
1960                 case JOINTTYPE_SLIDER:
1961                         j = dJointCreateSlider((dWorldID)world->physics.ode_world, 0);
1962                         break;
1963                 case JOINTTYPE_UNIVERSAL:
1964                         j = dJointCreateUniversal((dWorldID)world->physics.ode_world, 0);
1965                         break;
1966                 case JOINTTYPE_HINGE2:
1967                         j = dJointCreateHinge2((dWorldID)world->physics.ode_world, 0);
1968                         break;
1969                 case JOINTTYPE_FIXED:
1970                         j = dJointCreateFixed((dWorldID)world->physics.ode_world, 0);
1971                         break;
1972                 case JOINTTYPE_NONE:
1973                 default:
1974                         // no joint
1975                         j = 0;
1976                         break;
1977         }
1978         if(ed->priv.server->ode_joint)
1979         {
1980                 //Con_Printf("deleted old joint %i\n", (int) (ed - prog->edicts));
1981                 dJointAttach((dJointID)ed->priv.server->ode_joint, 0, 0);
1982                 dJointDestroy((dJointID)ed->priv.server->ode_joint);
1983         }
1984         ed->priv.server->ode_joint = (void *) j;
1985         ed->priv.server->ode_joint_type = jointtype;
1986         ed->priv.server->ode_joint_enemy = enemy;
1987         ed->priv.server->ode_joint_aiment = aiment;
1988         VectorCopy(origin, ed->priv.server->ode_joint_origin);
1989         VectorCopy(velocity, ed->priv.server->ode_joint_velocity);
1990         VectorCopy(angles, ed->priv.server->ode_joint_angles);
1991         VectorCopy(movedir, ed->priv.server->ode_joint_movedir);
1992         if(j)
1993         {
1994                 //Con_Printf("made new joint %i\n", (int) (ed - prog->edicts));
1995                 dJointSetData(j, (void *) ed);
1996                 if(enemy)
1997                         b1 = (dBodyID)prog->edicts[enemy].priv.server->ode_body;
1998                 if(aiment)
1999                         b2 = (dBodyID)prog->edicts[aiment].priv.server->ode_body;
2000                 dJointAttach(j, b1, b2);
2001
2002                 switch(jointtype)
2003                 {
2004                         case JOINTTYPE_POINT:
2005                                 dJointSetBallAnchor(j, origin[0], origin[1], origin[2]);
2006                                 break;
2007                         case JOINTTYPE_HINGE:
2008                                 dJointSetHingeAnchor(j, origin[0], origin[1], origin[2]);
2009                                 dJointSetHingeAxis(j, forward[0], forward[1], forward[2]);
2010                                 dJointSetHingeParam(j, dParamFMax, FMax);
2011                                 dJointSetHingeParam(j, dParamHiStop, Stop);
2012                                 dJointSetHingeParam(j, dParamLoStop, -Stop);
2013                                 dJointSetHingeParam(j, dParamStopCFM, CFM);
2014                                 dJointSetHingeParam(j, dParamStopERP, ERP);
2015                                 dJointSetHingeParam(j, dParamVel, Vel);
2016                                 break;
2017                         case JOINTTYPE_SLIDER:
2018                                 dJointSetSliderAxis(j, forward[0], forward[1], forward[2]);
2019                                 dJointSetSliderParam(j, dParamFMax, FMax);
2020                                 dJointSetSliderParam(j, dParamHiStop, Stop);
2021                                 dJointSetSliderParam(j, dParamLoStop, -Stop);
2022                                 dJointSetSliderParam(j, dParamStopCFM, CFM);
2023                                 dJointSetSliderParam(j, dParamStopERP, ERP);
2024                                 dJointSetSliderParam(j, dParamVel, Vel);
2025                                 break;
2026                         case JOINTTYPE_UNIVERSAL:
2027                                 dJointSetUniversalAnchor(j, origin[0], origin[1], origin[2]);
2028                                 dJointSetUniversalAxis1(j, forward[0], forward[1], forward[2]);
2029                                 dJointSetUniversalAxis2(j, up[0], up[1], up[2]);
2030                                 dJointSetUniversalParam(j, dParamFMax, FMax);
2031                                 dJointSetUniversalParam(j, dParamHiStop, Stop);
2032                                 dJointSetUniversalParam(j, dParamLoStop, -Stop);
2033                                 dJointSetUniversalParam(j, dParamStopCFM, CFM);
2034                                 dJointSetUniversalParam(j, dParamStopERP, ERP);
2035                                 dJointSetUniversalParam(j, dParamVel, Vel);
2036                                 dJointSetUniversalParam(j, dParamFMax2, FMax);
2037                                 dJointSetUniversalParam(j, dParamHiStop2, Stop);
2038                                 dJointSetUniversalParam(j, dParamLoStop2, -Stop);
2039                                 dJointSetUniversalParam(j, dParamStopCFM2, CFM);
2040                                 dJointSetUniversalParam(j, dParamStopERP2, ERP);
2041                                 dJointSetUniversalParam(j, dParamVel2, Vel);
2042                                 break;
2043                         case JOINTTYPE_HINGE2:
2044                                 dJointSetHinge2Anchor(j, origin[0], origin[1], origin[2]);
2045                                 dJointSetHinge2Axis1(j, forward[0], forward[1], forward[2]);
2046                                 dJointSetHinge2Axis2(j, velocity[0], velocity[1], velocity[2]);
2047                                 dJointSetHinge2Param(j, dParamFMax, FMax);
2048                                 dJointSetHinge2Param(j, dParamHiStop, Stop);
2049                                 dJointSetHinge2Param(j, dParamLoStop, -Stop);
2050                                 dJointSetHinge2Param(j, dParamStopCFM, CFM);
2051                                 dJointSetHinge2Param(j, dParamStopERP, ERP);
2052                                 dJointSetHinge2Param(j, dParamVel, Vel);
2053                                 dJointSetHinge2Param(j, dParamFMax2, FMax);
2054                                 dJointSetHinge2Param(j, dParamHiStop2, Stop);
2055                                 dJointSetHinge2Param(j, dParamLoStop2, -Stop);
2056                                 dJointSetHinge2Param(j, dParamStopCFM2, CFM);
2057                                 dJointSetHinge2Param(j, dParamStopERP2, ERP);
2058                                 dJointSetHinge2Param(j, dParamVel2, Vel);
2059                                 break;
2060                         case JOINTTYPE_FIXED:
2061                                 break;
2062                         case 0:
2063                         default:
2064                                 Sys_Error("what? but above the joint was valid...\n");
2065                                 break;
2066                 }
2067 #undef SETPARAMS
2068
2069         }
2070 }
2071
2072 static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
2073 {
2074         prvm_prog_t *prog = world->prog;
2075         const float *iv;
2076         const int *ie;
2077         dBodyID body = (dBodyID)ed->priv.server->ode_body;
2078         dMass mass;
2079         dReal test;
2080         const dReal *ovelocity, *ospinvelocity;
2081         void *dataID;
2082         dp_model_t *model;
2083         float *ov;
2084         int *oe;
2085         int axisindex;
2086         int modelindex = 0;
2087         int movetype = MOVETYPE_NONE;
2088         int numtriangles;
2089         int numvertices;
2090         int solid = SOLID_NOT, geomtype = 0;
2091         int triangleindex;
2092         int vertexindex;
2093         mempool_t *mempool;
2094         qboolean modified = false;
2095         vec3_t angles;
2096         vec3_t avelocity;
2097         vec3_t entmaxs;
2098         vec3_t entmins;
2099         vec3_t forward;
2100         vec3_t geomcenter;
2101         vec3_t geomsize;
2102         vec3_t left;
2103         vec3_t origin;
2104         vec3_t spinvelocity;
2105         vec3_t up;
2106         vec3_t velocity;
2107         vec_t f;
2108         vec_t length;
2109         vec_t massval = 1.0f;
2110         vec_t movelimit;
2111         vec_t radius;
2112         vec_t scale = 1.0f;
2113         vec_t spinlimit;
2114         qboolean gravity;
2115         vec3_t scalevec;
2116         edict_odefunc_t *func, *nextf;
2117
2118 #ifdef ODE_DYNAMIC
2119         if (!ode_dll)
2120                 return;
2121 #endif
2122         VectorClear(entmins);
2123         VectorClear(entmaxs);
2124
2125         solid = (int)PRVM_gameedictfloat(ed, solid);
2126         geomtype = (int)PRVM_gameedictfloat(ed, geomtype);
2127         movetype = (int)PRVM_gameedictfloat(ed, movetype);
2128         scale = PRVM_gameedictfloat(ed, scale);if (!scale) scale = 1.0f;
2129         modelindex = 0;
2130         mempool = prog->progs_mempool;
2131         model = NULL;
2132         if (!geomtype)
2133         {
2134                 // VorteX: keep support for deprecated solid fields to not break mods
2135                 if (solid == SOLID_PHYSICS_TRIMESH || solid == SOLID_BSP)
2136                         geomtype = GEOMTYPE_TRIMESH;
2137                 else if (solid == SOLID_NOT || solid == SOLID_TRIGGER)
2138                         geomtype = GEOMTYPE_NONE;
2139                 else if (solid == SOLID_PHYSICS_SPHERE)
2140                         geomtype = GEOMTYPE_SPHERE;
2141                 else if (solid == SOLID_PHYSICS_CAPSULE)
2142                         geomtype = GEOMTYPE_CAPSULE;
2143                 else if (solid == SOLID_PHYSICS_CYLINDER)
2144                         geomtype = GEOMTYPE_CYLINDER;
2145                 else if (solid == SOLID_PHYSICS_BOX)
2146                         geomtype = GEOMTYPE_BOX;
2147                 else
2148                         geomtype = GEOMTYPE_BOX;
2149         }
2150         if (geomtype == GEOMTYPE_TRIMESH)
2151         {
2152                 modelindex = (int)PRVM_gameedictfloat(ed, modelindex);
2153                 if (world == &sv.world)
2154                         model = SV_GetModelByIndex(modelindex);
2155                 else if (world == &cl.world)
2156                         model = CL_GetModelByIndex(modelindex);
2157                 else
2158                         model = NULL;
2159                 if (model)
2160                 {
2161                         VectorScale(model->normalmins, scale, entmins);
2162                         VectorScale(model->normalmaxs, scale, entmaxs);
2163                         massval = PRVM_gameedictfloat(ed, mass);
2164                 }
2165                 else
2166                 {
2167                         modelindex = 0;
2168                         massval = 1.0f;
2169                 }
2170         }
2171         else if (geomtype && geomtype != GEOMTYPE_NONE)
2172         {
2173                 VectorCopy(PRVM_gameedictvector(ed, mins), entmins);
2174                 VectorCopy(PRVM_gameedictvector(ed, maxs), entmaxs);
2175                 massval = PRVM_gameedictfloat(ed, mass);
2176         }
2177         else
2178         {
2179                 // geometry type not set, falling back
2180                 if (ed->priv.server->ode_physics)
2181                         World_Physics_RemoveFromEntity(world, ed);
2182                 return;
2183         }
2184
2185         VectorSubtract(entmaxs, entmins, geomsize);
2186         if (VectorLength2(geomsize) == 0)
2187         {
2188                 // we don't allow point-size physics objects...
2189                 if (ed->priv.server->ode_physics)
2190                         World_Physics_RemoveFromEntity(world, ed);
2191                 return;
2192         }
2193
2194         if (movetype != MOVETYPE_PHYSICS)
2195                 massval = 1.0f;
2196
2197         // get friction from entity
2198         if (PRVM_gameedictfloat(ed, friction))
2199                 ed->priv.server->ode_friction = PRVM_gameedictfloat(ed, friction);
2200         else
2201                 ed->priv.server->ode_friction = 1.0;
2202                 
2203         // check if we need to create or replace the geom
2204         if (!ed->priv.server->ode_physics
2205          || !VectorCompare(ed->priv.server->ode_mins, entmins)
2206          || !VectorCompare(ed->priv.server->ode_maxs, entmaxs)
2207          || ed->priv.server->ode_mass != massval
2208          || ed->priv.server->ode_modelindex != modelindex)
2209         {
2210                 modified = true;
2211                 World_Physics_RemoveFromEntity(world, ed);
2212                 ed->priv.server->ode_physics = true;
2213                 VectorCopy(entmins, ed->priv.server->ode_mins);
2214                 VectorCopy(entmaxs, ed->priv.server->ode_maxs);
2215                 ed->priv.server->ode_mass = massval;
2216                 ed->priv.server->ode_modelindex = modelindex;
2217                 VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2218                 if (PRVM_gameedictvector(ed, massofs))
2219                         VectorCopy(geomcenter, PRVM_gameedictvector(ed, massofs));
2220                 else
2221                         VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter);
2222                 ed->priv.server->ode_movelimit = min(geomsize[0], min(geomsize[1], geomsize[2]));
2223                 if (massval * geomsize[0] * geomsize[1] * geomsize[2] == 0)
2224                 {
2225                         if (movetype == MOVETYPE_PHYSICS)
2226                                 Con_Printf("entity %i (classname %s) .mass * .size_x * .size_y * .size_z == 0\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2227                         massval = 1.0f;
2228                         VectorSet(geomsize, 1.0f, 1.0f, 1.0f);
2229                 }
2230
2231                 switch(geomtype)
2232                 {
2233                 case GEOMTYPE_TRIMESH:
2234                         ed->priv.server->ode_offsetmatrix = identitymatrix;
2235                         // honor scale, support q3map2's/radiant's modelscale_vec
2236                         VectorCopy(PRVM_gameedictvector(ed, modelscale_vec), scalevec); 
2237                         if (scalevec[0] != 0.0 || scalevec[1] != 0.0 || scalevec[2] != 0.0)
2238                                 Matrix4x4_OriginScale3(&ed->priv.server->ode_offsetmatrix, scalevec[0], scalevec[1],scalevec[2]);
2239                         else if (PRVM_gameedictfloat(ed, scale))
2240                                 Matrix4x4_OriginScale3(&ed->priv.server->ode_offsetmatrix, PRVM_gameedictfloat(ed, scale), PRVM_gameedictfloat(ed, scale), PRVM_gameedictfloat(ed, scale));
2241                         // check model
2242                         if (!model)
2243                         {
2244                                 Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2245                                 goto treatasbox;
2246                         }
2247                         // add an optimized mesh to the model containing only the SUPERCONTENTS_SOLID surfaces
2248                         if (!model->brush.collisionmesh)
2249                                 Mod_CreateCollisionMesh(model);
2250                         if (!model->brush.collisionmesh || !model->brush.collisionmesh->numtriangles)
2251                         {
2252                                 Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)));
2253                                 goto treatasbox;
2254                         }
2255                         // ODE requires persistent mesh storage, so we need to copy out
2256                         // the data from the model because renderer restarts could free it
2257                         // during the game, additionally we need to flip the triangles...
2258                         // note: ODE does preprocessing of the mesh for culling, removing
2259                         // concave edges, etc., so this is not a lightweight operation
2260                         ed->priv.server->ode_numvertices = numvertices = model->brush.collisionmesh->numverts;
2261                         ed->priv.server->ode_vertex3f = (float *)Mem_Alloc(mempool, numvertices * sizeof(float[3]));
2262                         for (vertexindex = 0, ov = ed->priv.server->ode_vertex3f, iv = model->brush.collisionmesh->vertex3f;vertexindex < numvertices;vertexindex++, ov += 3, iv += 3)
2263                         {
2264                                 ov[0] = iv[0] - geomcenter[0];
2265                                 ov[1] = iv[1] - geomcenter[1];
2266                                 ov[2] = iv[2] - geomcenter[2];
2267                         }
2268                         ed->priv.server->ode_numtriangles = numtriangles = model->brush.collisionmesh->numtriangles;
2269                         ed->priv.server->ode_element3i = (int *)Mem_Alloc(mempool, numtriangles * sizeof(int[3]));
2270                         //memcpy(ed->priv.server->ode_element3i, model->brush.collisionmesh->element3i, ed->priv.server->ode_numtriangles * sizeof(int[3]));
2271                         for (triangleindex = 0, oe = ed->priv.server->ode_element3i, ie = model->brush.collisionmesh->element3i;triangleindex < numtriangles;triangleindex++, oe += 3, ie += 3)
2272                         {
2273                                 oe[0] = ie[2];
2274                                 oe[1] = ie[1];
2275                                 oe[2] = ie[0];
2276                         }
2277                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2278                         // now create the geom
2279                         dataID = dGeomTriMeshDataCreate();
2280                         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]));
2281                         ed->priv.server->ode_geom = (void *)dCreateTriMesh((dSpaceID)world->physics.ode_space, (dTriMeshDataID)dataID, NULL, NULL, NULL);
2282                         dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2283                         break;
2284                 case GEOMTYPE_BOX:
2285 treatasbox:
2286                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2287                         ed->priv.server->ode_geom = (void *)dCreateBox((dSpaceID)world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]);
2288                         dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
2289                         break;
2290                 case GEOMTYPE_SPHERE:
2291                         Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
2292                         ed->priv.server->ode_geom = (void *)dCreateSphere((dSpaceID)world->physics.ode_space, geomsize[0] * 0.5f);
2293                         dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f);
2294                         break;
2295                 case GEOMTYPE_CAPSULE:
2296                         axisindex = 0;
2297                         if (geomsize[axisindex] < geomsize[1])
2298                                 axisindex = 1;
2299                         if (geomsize[axisindex] < geomsize[2])
2300                                 axisindex = 2;
2301                         // the qc gives us 3 axis radius, the longest axis is the capsule
2302                         // axis, since ODE doesn't like this idea we have to create a
2303                         // capsule which uses the standard orientation, and apply a
2304                         // transform to it
2305                         if (axisindex == 0)
2306                         {
2307                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2308                                 radius = min(geomsize[1], geomsize[2]) * 0.5f;
2309                         }
2310                         else if (axisindex == 1)
2311                         {
2312                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2313                                 radius = min(geomsize[0], geomsize[2]) * 0.5f;
2314                         }
2315                         else
2316                         {
2317                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2318                                 radius = min(geomsize[0], geomsize[1]) * 0.5f;
2319                         }
2320                         length = geomsize[axisindex] - radius*2;
2321                         // because we want to support more than one axisindex, we have to
2322                         // create a transform, and turn on its cleanup setting (which will
2323                         // cause the child to be destroyed when it is destroyed)
2324                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2325                         dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length);
2326                         break;
2327                 case GEOMTYPE_CAPSULE_X:
2328                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2329                         radius = min(geomsize[1], geomsize[2]) * 0.5f;
2330                         length = geomsize[0] - radius*2;
2331                         // check if length is not enough, reduce radius then
2332                         if (length <= 0)
2333                         {
2334                                 radius -= (1 - length)*0.5;
2335                                 length = 1;
2336                         }
2337                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2338                         dMassSetCapsuleTotal(&mass, massval, 1, radius, length);
2339                         break;
2340                 case GEOMTYPE_CAPSULE_Y:
2341                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2342                         radius = min(geomsize[0], geomsize[2]) * 0.5f;
2343                         length = geomsize[1] - radius*2;
2344                         // check if length is not enough, reduce radius then
2345                         if (length <= 0)
2346                         {
2347                                 radius -= (1 - length)*0.5;
2348                                 length = 1;
2349                         }
2350                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2351                         dMassSetCapsuleTotal(&mass, massval, 2, radius, length);
2352                         break;
2353                 case GEOMTYPE_CAPSULE_Z:
2354                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2355                         radius = min(geomsize[1], geomsize[0]) * 0.5f;
2356                         length = geomsize[2] - radius*2;
2357                         // check if length is not enough, reduce radius then
2358                         if (length <= 0)
2359                         {
2360                                 radius -= (1 - length)*0.5;
2361                                 length = 1;
2362                         }
2363                         ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
2364                         dMassSetCapsuleTotal(&mass, massval, 3, radius, length);
2365                         break;
2366                 case GEOMTYPE_CYLINDER:
2367                         axisindex = 0;
2368                         if (geomsize[axisindex] < geomsize[1])
2369                                 axisindex = 1;
2370                         if (geomsize[axisindex] < geomsize[2])
2371                                 axisindex = 2;
2372                         // the qc gives us 3 axis radius, the longest axis is the capsule
2373                         // axis, since ODE doesn't like this idea we have to create a
2374                         // capsule which uses the standard orientation, and apply a
2375                         // transform to it
2376                         if (axisindex == 0)
2377                         {
2378                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2379                                 radius = min(geomsize[1], geomsize[2]) * 0.5f;
2380                         }
2381                         else if (axisindex == 1)
2382                         {
2383                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2384                                 radius = min(geomsize[0], geomsize[2]) * 0.5f;
2385                         }
2386                         else
2387                         {
2388                                 Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2389                                 radius = min(geomsize[0], geomsize[1]) * 0.5f;
2390                         }
2391                         length = geomsize[axisindex];
2392                         // check if length is not enough, reduce radius then
2393                         if (length <= 0)
2394                         {
2395                                 radius -= (1 - length)*0.5;
2396                                 length = 1;
2397                         }
2398                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2399                         dMassSetCylinderTotal(&mass, massval, axisindex+1, radius, length);
2400                         break;
2401                 case GEOMTYPE_CYLINDER_X:
2402                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 90, 1);
2403                         radius = min(geomsize[1], geomsize[2]) * 0.5f;
2404                         length = geomsize[0];
2405                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2406                         dMassSetCylinderTotal(&mass, massval, 1, radius, length);
2407                         break;
2408                 case GEOMTYPE_CYLINDER_Y:
2409                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 90, 0, 0, 1);
2410                         radius = min(geomsize[0], geomsize[2]) * 0.5f;
2411                         length = geomsize[1];
2412                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2413                         dMassSetCylinderTotal(&mass, massval, 2, radius, length);
2414                         break;
2415                 case GEOMTYPE_CYLINDER_Z:
2416                         Matrix4x4_CreateFromQuakeEntity(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2], 0, 0, 0, 1);
2417                         radius = min(geomsize[0], geomsize[1]) * 0.5f;
2418                         length = geomsize[2];
2419                         ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length);
2420                         dMassSetCylinderTotal(&mass, massval, 3, radius, length);
2421                         break;
2422                 default:
2423                         Sys_Error("World_Physics_BodyFromEntity: unrecognized geomtype value %i was accepted by filter\n", solid);
2424                         // this goto only exists to prevent warnings from the compiler
2425                         // about uninitialized variables (mass), while allowing it to
2426                         // catch legitimate uninitialized variable warnings
2427                         goto treatasbox;
2428                 }
2429                 Matrix4x4_Invert_Simple(&ed->priv.server->ode_offsetimatrix, &ed->priv.server->ode_offsetmatrix);
2430                 ed->priv.server->ode_massbuf = Mem_Alloc(mempool, sizeof(mass));
2431                 memcpy(ed->priv.server->ode_massbuf, &mass, sizeof(dMass));
2432         }
2433
2434         if (ed->priv.server->ode_geom)
2435                 dGeomSetData((dGeomID)ed->priv.server->ode_geom, (void*)ed);
2436         if (movetype == MOVETYPE_PHYSICS && ed->priv.server->ode_geom)
2437         {
2438                 if (ed->priv.server->ode_body == NULL)
2439                 {
2440                         ed->priv.server->ode_body = (void *)(body = dBodyCreate((dWorldID)world->physics.ode_world));
2441                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2442                         dBodySetData(body, (void*)ed);
2443                         dBodySetMass(body, (dMass *) ed->priv.server->ode_massbuf);
2444                         modified = true;
2445                 }
2446         }
2447         else
2448         {
2449                 if (ed->priv.server->ode_body != NULL)
2450                 {
2451                         if(ed->priv.server->ode_geom)
2452                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2453                         dBodyDestroy((dBodyID) ed->priv.server->ode_body);
2454                         ed->priv.server->ode_body = NULL;
2455                         modified = true;
2456                 }
2457         }
2458
2459         // get current data from entity
2460         VectorClear(origin);
2461         VectorClear(velocity);
2462         //VectorClear(forward);
2463         //VectorClear(left);
2464         //VectorClear(up);
2465         //VectorClear(spinvelocity);
2466         VectorClear(angles);
2467         VectorClear(avelocity);
2468         gravity = true;
2469         VectorCopy(PRVM_gameedictvector(ed, origin), origin);
2470         VectorCopy(PRVM_gameedictvector(ed, velocity), velocity);
2471         //VectorCopy(PRVM_gameedictvector(ed, axis_forward), forward);
2472         //VectorCopy(PRVM_gameedictvector(ed, axis_left), left);
2473         //VectorCopy(PRVM_gameedictvector(ed, axis_up), up);
2474         //VectorCopy(PRVM_gameedictvector(ed, spinvelocity), spinvelocity);
2475         VectorCopy(PRVM_gameedictvector(ed, angles), angles);
2476         VectorCopy(PRVM_gameedictvector(ed, avelocity), avelocity);
2477         if (PRVM_gameedictfloat(ed, gravity) != 0.0f && PRVM_gameedictfloat(ed, gravity) < 0.5f) gravity = false;
2478         if (ed == prog->edicts)
2479                 gravity = false;
2480
2481         // compatibility for legacy entities
2482         //if (!VectorLength2(forward) || solid == SOLID_BSP)
2483         {
2484                 float pitchsign = 1;
2485                 vec3_t qangles, qavelocity;
2486                 VectorCopy(angles, qangles);
2487                 VectorCopy(avelocity, qavelocity);
2488
2489                 if(prog == SVVM_prog) // FIXME some better way?
2490                 {
2491                         pitchsign = SV_GetPitchSign(prog, ed);
2492                 }
2493                 else if(prog == CLVM_prog)
2494                 {
2495                         pitchsign = CL_GetPitchSign(prog, ed);
2496                 }
2497                 qangles[PITCH] *= pitchsign;
2498                 qavelocity[PITCH] *= pitchsign;
2499
2500                 AngleVectorsFLU(qangles, forward, left, up);
2501                 // convert single-axis rotations in avelocity to spinvelocity
2502                 // FIXME: untested math - check signs
2503                 VectorSet(spinvelocity, DEG2RAD(qavelocity[PITCH]), DEG2RAD(qavelocity[ROLL]), DEG2RAD(qavelocity[YAW]));
2504         }
2505
2506         // compatibility for legacy entities
2507         switch (solid)
2508         {
2509         case SOLID_BBOX:
2510         case SOLID_SLIDEBOX:
2511         case SOLID_CORPSE:
2512                 VectorSet(forward, 1, 0, 0);
2513                 VectorSet(left, 0, 1, 0);
2514                 VectorSet(up, 0, 0, 1);
2515                 VectorSet(spinvelocity, 0, 0, 0);
2516                 break;
2517         }
2518
2519
2520         // we must prevent NANs...
2521         if (physics_ode_trick_fixnan.integer)
2522         {
2523                 test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity);
2524                 if (IS_NAN(test))
2525                 {
2526                         modified = true;
2527                         //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]);
2528                         if (physics_ode_trick_fixnan.integer >= 2)
2529                                 Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .velocity = '%f %f %f' .angles = '%f %f %f' .avelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(prog, PRVM_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], angles[0], angles[1], angles[2], avelocity[0], avelocity[1], avelocity[2]);
2530                         test = VectorLength2(origin);
2531                         if (IS_NAN(test))
2532                                 VectorClear(origin);
2533                         test = VectorLength2(forward) * VectorLength2(left) * VectorLength2(up);
2534                         if (IS_NAN(test))
2535                         {
2536                                 VectorSet(angles, 0, 0, 0);
2537                                 VectorSet(forward, 1, 0, 0);
2538                                 VectorSet(left, 0, 1, 0);
2539                                 VectorSet(up, 0, 0, 1);
2540                         }
2541                         test = VectorLength2(velocity);
2542                         if (IS_NAN(test))
2543                                 VectorClear(velocity);
2544                         test = VectorLength2(spinvelocity);
2545                         if (IS_NAN(test))
2546                         {
2547                                 VectorClear(avelocity);
2548                                 VectorClear(spinvelocity);
2549                         }
2550                 }
2551         }
2552
2553         // check if the qc edited any position data
2554         if (!VectorCompare(origin, ed->priv.server->ode_origin)
2555          || !VectorCompare(velocity, ed->priv.server->ode_velocity)
2556          || !VectorCompare(angles, ed->priv.server->ode_angles)
2557          || !VectorCompare(avelocity, ed->priv.server->ode_avelocity)
2558          || gravity != ed->priv.server->ode_gravity)
2559                 modified = true;
2560
2561         // store the qc values into the physics engine
2562         body = (dBodyID)ed->priv.server->ode_body;
2563         if (modified && ed->priv.server->ode_geom)
2564         {
2565                 dVector3 r[3];
2566                 matrix4x4_t entitymatrix;
2567                 matrix4x4_t bodymatrix;
2568
2569 #if 0
2570                 Con_Printf("entity %i got changed by QC\n", (int) (ed - prog->edicts));
2571                 if(!VectorCompare(origin, ed->priv.server->ode_origin))
2572                         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]);
2573                 if(!VectorCompare(velocity, ed->priv.server->ode_velocity))
2574                         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]);
2575                 if(!VectorCompare(angles, ed->priv.server->ode_angles))
2576                         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]);
2577                 if(!VectorCompare(avelocity, ed->priv.server->ode_avelocity))
2578                         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]);
2579                 if(gravity != ed->priv.server->ode_gravity)
2580                         Con_Printf("  gravity: %i -> %i\n", ed->priv.server->ode_gravity, gravity);
2581 #endif
2582                 // values for BodyFromEntity to check if the qc modified anything later
2583                 VectorCopy(origin, ed->priv.server->ode_origin);
2584                 VectorCopy(velocity, ed->priv.server->ode_velocity);
2585                 VectorCopy(angles, ed->priv.server->ode_angles);
2586                 VectorCopy(avelocity, ed->priv.server->ode_avelocity);
2587                 ed->priv.server->ode_gravity = gravity;
2588
2589                 Matrix4x4_FromVectors(&entitymatrix, forward, left, up, origin);
2590                 Matrix4x4_Concat(&bodymatrix, &entitymatrix, &ed->priv.server->ode_offsetmatrix);
2591                 Matrix4x4_ToVectors(&bodymatrix, forward, left, up, origin);
2592                 r[0][0] = forward[0];
2593                 r[1][0] = forward[1];
2594                 r[2][0] = forward[2];
2595                 r[0][1] = left[0];
2596                 r[1][1] = left[1];
2597                 r[2][1] = left[2];
2598                 r[0][2] = up[0];
2599                 r[1][2] = up[1];
2600                 r[2][2] = up[2];
2601                 if (body)
2602                 {
2603                         if (movetype == MOVETYPE_PHYSICS)
2604                         {
2605                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2606                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2607                                 dBodySetRotation(body, r[0]);
2608                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2609                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2610                                 dBodySetGravityMode(body, gravity);
2611                         }
2612                         else
2613                         {
2614                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
2615                                 dBodySetPosition(body, origin[0], origin[1], origin[2]);
2616                                 dBodySetRotation(body, r[0]);
2617                                 dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2618                                 dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2619                                 dBodySetGravityMode(body, gravity);
2620                                 dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
2621                         }
2622                 }
2623                 else
2624                 {
2625                         // no body... then let's adjust the parameters of the geom directly
2626                         dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0); // just in case we previously HAD a body (which should never happen)
2627                         dGeomSetPosition((dGeomID)ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
2628                         dGeomSetRotation((dGeomID)ed->priv.server->ode_geom, r[0]);
2629                 }
2630         }
2631
2632         if (body)
2633         {
2634
2635                 // limit movement speed to prevent missed collisions at high speed
2636                 ovelocity = dBodyGetLinearVel(body);
2637                 ospinvelocity = dBodyGetAngularVel(body);
2638                 movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit;
2639                 test = VectorLength2(ovelocity);
2640                 if (test > movelimit*movelimit)
2641                 {
2642                         // scale down linear velocity to the movelimit
2643                         // scale down angular velocity the same amount for consistency
2644                         f = movelimit / sqrt(test);
2645                         VectorScale(ovelocity, f, velocity);
2646                         VectorScale(ospinvelocity, f, spinvelocity);
2647                         dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
2648                         dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
2649                 }
2650
2651                 // make sure the angular velocity is not exploding
2652                 spinlimit = physics_ode_spinlimit.value;
2653                 test = VectorLength2(ospinvelocity);
2654                 if (test > spinlimit)
2655                 {
2656                         dBodySetAngularVel(body, 0, 0, 0);
2657                 }
2658
2659                 // apply functions and clear stack
2660                 for(func = ed->priv.server->ode_func; func; func = nextf)
2661                 {
2662                         nextf = func->next;
2663                         World_Physics_ApplyCmd(ed, func);
2664                         Mem_Free(func);
2665                 }
2666                 ed->priv.server->ode_func = NULL;
2667         }
2668 }
2669
2670 #define MAX_CONTACTS 32
2671 static void nearCallback (void *data, dGeomID o1, dGeomID o2)
2672 {
2673         world_t *world = (world_t *)data;
2674         prvm_prog_t *prog = world->prog;
2675         dContact contact[MAX_CONTACTS]; // max contacts per collision pair
2676         dBodyID b1;
2677         dBodyID b2;
2678         dJointID c;
2679         int i;
2680         int numcontacts;
2681         float bouncefactor1 = 0.0f;
2682         float bouncestop1 = 60.0f / 800.0f;
2683         float bouncefactor2 = 0.0f;
2684         float bouncestop2 = 60.0f / 800.0f;
2685         float erp;
2686         dVector3 grav;
2687         prvm_edict_t *ed1, *ed2;
2688
2689         if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
2690         {
2691                 // colliding a space with something
2692                 dSpaceCollide2(o1, o2, data, &nearCallback);
2693                 // Note we do not want to test intersections within a space,
2694                 // only between spaces.
2695                 //if (dGeomIsSpace(o1)) dSpaceCollide(o1, data, &nearCallback);
2696                 //if (dGeomIsSpace(o2)) dSpaceCollide(o2, data, &nearCallback);
2697                 return;
2698         }
2699
2700         b1 = dGeomGetBody(o1);
2701         b2 = dGeomGetBody(o2);
2702
2703         // at least one object has to be using MOVETYPE_PHYSICS or we just don't care
2704         if (!b1 && !b2)
2705                 return;
2706         
2707         // exit without doing anything if the two bodies are connected by a joint
2708         if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact))
2709                 return;
2710
2711         ed1 = (prvm_edict_t *) dGeomGetData(o1);
2712         if(ed1 && ed1->priv.server->free)
2713                 ed1 = NULL;
2714         if(ed1)
2715         {
2716                 bouncefactor1 = PRVM_gameedictfloat(ed1, bouncefactor);
2717                 bouncestop1 = PRVM_gameedictfloat(ed1, bouncestop);
2718                 if (!bouncestop1)
2719                         bouncestop1 = 60.0f / 800.0f;
2720         }
2721
2722         ed2 = (prvm_edict_t *) dGeomGetData(o2);
2723         if(ed2 && ed2->priv.server->free)
2724                 ed2 = NULL;
2725         if(ed2)
2726         {
2727                 bouncefactor2 = PRVM_gameedictfloat(ed2, bouncefactor);
2728                 bouncestop2 = PRVM_gameedictfloat(ed2, bouncestop);
2729                 if (!bouncestop2)
2730                         bouncestop2 = 60.0f / 800.0f;
2731         }
2732
2733         if(prog == SVVM_prog)
2734         {
2735                 if(ed1 && PRVM_serveredictfunction(ed1, touch))
2736                 {
2737                         SV_LinkEdict_TouchAreaGrid_Call(ed1, ed2 ? ed2 : prog->edicts);
2738                 }
2739                 if(ed2 && PRVM_serveredictfunction(ed2, touch))
2740                 {
2741                         SV_LinkEdict_TouchAreaGrid_Call(ed2, ed1 ? ed1 : prog->edicts);
2742                 }
2743         }
2744
2745         // merge bounce factors and bounce stop
2746         if(bouncefactor2 > 0)
2747         {
2748                 if(bouncefactor1 > 0)
2749                 {
2750                         // TODO possibly better logic to merge bounce factor data?
2751                         if(bouncestop2 < bouncestop1)
2752                                 bouncestop1 = bouncestop2;
2753                         if(bouncefactor2 > bouncefactor1)
2754                                 bouncefactor1 = bouncefactor2;
2755                 }
2756                 else
2757                 {
2758                         bouncestop1 = bouncestop2;
2759                         bouncefactor1 = bouncefactor2;
2760                 }
2761         }
2762         dWorldGetGravity((dWorldID)world->physics.ode_world, grav);
2763         bouncestop1 *= fabs(grav[2]);
2764
2765         // get erp
2766         // select object that moves faster ang get it's erp
2767         erp = (VectorLength2(PRVM_gameedictvector(ed1, velocity)) > VectorLength2(PRVM_gameedictvector(ed2, velocity))) ? PRVM_gameedictfloat(ed1, erp) : PRVM_gameedictfloat(ed2, erp);
2768
2769         // get max contact points for this collision
2770         numcontacts = (int)PRVM_gameedictfloat(ed1, maxcontacts);
2771         if (!numcontacts)
2772                 numcontacts = physics_ode_contact_maxpoints.integer;
2773         if (PRVM_gameedictfloat(ed2, maxcontacts))
2774                 numcontacts = max(numcontacts, (int)PRVM_gameedictfloat(ed2, maxcontacts));
2775         else
2776                 numcontacts = max(numcontacts, physics_ode_contact_maxpoints.integer);
2777
2778         // generate contact points between the two non-space geoms
2779         numcontacts = dCollide(o1, o2, min(MAX_CONTACTS, numcontacts), &(contact[0].geom), sizeof(contact[0]));
2780         // add these contact points to the simulation
2781         for (i = 0;i < numcontacts;i++)
2782         {
2783                 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);
2784                 contact[i].surface.mu = physics_ode_contact_mu.value * ed1->priv.server->ode_friction * ed2->priv.server->ode_friction;
2785                 contact[i].surface.soft_erp = physics_ode_contact_erp.value + erp;
2786                 contact[i].surface.soft_cfm = physics_ode_contact_cfm.value;
2787                 contact[i].surface.bounce = bouncefactor1;
2788                 contact[i].surface.bounce_vel = bouncestop1;
2789                 c = dJointCreateContact((dWorldID)world->physics.ode_world, (dJointGroupID)world->physics.ode_contactgroup, contact + i);
2790                 dJointAttach(c, b1, b2);
2791         }
2792 }
2793 #endif
2794
2795 void World_Physics_Frame(world_t *world, double frametime, double gravity)
2796 {
2797         prvm_prog_t *prog = world->prog;
2798         double tdelta, tdelta2, tdelta3, simulationtime, collisiontime;
2799
2800         tdelta = Sys_DirtyTime();
2801 #ifdef USEODE
2802         if (world->physics.ode && physics_ode.integer)
2803         {
2804                 int i;
2805                 prvm_edict_t *ed;
2806
2807                 if (!physics_ode_constantstep.value)
2808                 {
2809                         world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000);
2810                         world->physics.ode_step = frametime / world->physics.ode_iterations;
2811                 }
2812                 else
2813                 {
2814                         world->physics.ode_time += frametime;
2815                         // step size
2816                         if (physics_ode_constantstep.value > 0 && physics_ode_constantstep.value < 1)
2817                                 world->physics.ode_step = physics_ode_constantstep.value;
2818                         else
2819                                 world->physics.ode_step = sys_ticrate.value;
2820                         if (world->physics.ode_time > 0.2f)
2821                                 world->physics.ode_time = world->physics.ode_step;
2822                         // set number of iterations to process
2823                         world->physics.ode_iterations = 0;
2824                         while(world->physics.ode_time >= world->physics.ode_step)
2825                         {
2826                                 world->physics.ode_iterations++;
2827                                 world->physics.ode_time -= world->physics.ode_step;
2828                         }
2829                 }       
2830                 world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step;
2831                 World_Physics_UpdateODE(world);
2832
2833                 // copy physics properties from entities to physics engine
2834                 if (prog)
2835                 {
2836                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2837                                 if (!prog->edicts[i].priv.required->free)
2838                                         World_Physics_Frame_BodyFromEntity(world, ed);
2839                         // oh, and it must be called after all bodies were created
2840                         for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2841                                 if (!prog->edicts[i].priv.required->free)
2842                                         World_Physics_Frame_JointFromEntity(world, ed);
2843                 }
2844
2845                 tdelta2 = Sys_DirtyTime();
2846                 collisiontime = 0;
2847                 for (i = 0;i < world->physics.ode_iterations;i++)
2848                 {
2849                         // set the gravity
2850                         dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity * physics_ode_world_gravitymod.value);
2851                         // set the tolerance for closeness of objects
2852                         dWorldSetContactSurfaceLayer((dWorldID)world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value));
2853                         // run collisions for the current world state, creating JointGroup
2854                         tdelta3 = Sys_DirtyTime();
2855                         dSpaceCollide((dSpaceID)world->physics.ode_space, (void *)world, nearCallback);
2856                         collisiontime += (Sys_DirtyTime() - tdelta3)*10000;
2857                         // apply forces
2858                         if (prog)
2859                         {
2860                                 int j;
2861                                 for (j = 0, ed = prog->edicts + j;j < prog->num_edicts;j++, ed++)
2862                                         if (!prog->edicts[j].priv.required->free)
2863                                                 World_Physics_Frame_ForceFromEntity(world, ed);
2864                         }
2865                         // run physics (move objects, calculate new velocities)
2866                         // be sure not to pass 0 as step time because that causes an ODE error
2867                         dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldstep_iterations.integer, 200));
2868                         if (world->physics.ode_step > 0)
2869                                 dWorldQuickStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
2870                         // clear the JointGroup now that we're done with it
2871                         dJointGroupEmpty((dJointGroupID)world->physics.ode_contactgroup);
2872                 }
2873                 simulationtime = (Sys_DirtyTime() - tdelta2)*10000;
2874
2875                 // copy physics properties from physics engine to entities and do some stats
2876                 if (prog)
2877                 {
2878                         for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2879                                 if (!prog->edicts[i].priv.required->free)
2880                                         World_Physics_Frame_BodyToEntity(world, ed);
2881
2882                         // print stats
2883                         if (physics_ode_printstats.integer)
2884                         {
2885                                 dBodyID body;
2886
2887                                 world->physics.ode_numobjects = 0;
2888                                 world->physics.ode_activeovjects = 0;
2889                                 for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
2890                                 {
2891                                         if (prog->edicts[i].priv.required->free)
2892                                                 continue;
2893                                         body = (dBodyID)prog->edicts[i].priv.server->ode_body;
2894                                         if (!body)
2895                                                 continue;
2896                                         world->physics.ode_numobjects++;
2897                                         if (dBodyIsEnabled(body))
2898                                                 world->physics.ode_activeovjects++;
2899                                 }
2900                                 Con_Printf("ODE Stats(%s): %i iterations, %3.01f (%3.01f collision) %3.01f total : %i objects %i active %i disabled\n", prog->name, world->physics.ode_iterations, simulationtime, collisiontime, (Sys_DirtyTime() - tdelta)*10000, world->physics.ode_numobjects, world->physics.ode_activeovjects, (world->physics.ode_numobjects - world->physics.ode_activeovjects));
2901                         }
2902                 }
2903         }
2904 #endif
2905 }