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