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