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