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