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