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