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