]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - world.c
Store the data pointer and free it on Font_UnloadFont
[xonotic/darkplaces.git] / world.c
diff --git a/world.c b/world.c
index 8f89295aed1ceca72ba7556def430e52ef6e0391..d74f58ccceb5c7114b16a778d770d7b0990e39c6 100644 (file)
--- a/world.c
+++ b/world.c
@@ -21,6 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 #include "quakedef.h"
 #include "clvm_cmds.h"
+#include "cl_collision.h"
 
 /*
 
@@ -113,16 +114,16 @@ void World_SetSize(world_t *world, const char *filename, const vec3_t mins, cons
        if (world->areagrid_marknumber < 1)
                world->areagrid_marknumber = 1;
        // choose either the world box size, or a larger box to ensure the grid isn't too fine
-       world->areagrid_size[0] = max(world->areagrid_maxs[0] - world->areagrid_mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
-       world->areagrid_size[1] = max(world->areagrid_maxs[1] - world->areagrid_mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
-       world->areagrid_size[2] = max(world->areagrid_maxs[2] - world->areagrid_mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
+       world->areagrid_size[0] = max(world->maxs[0] - world->mins[0], AREA_GRID * sv_areagrid_mingridsize.value);
+       world->areagrid_size[1] = max(world->maxs[1] - world->mins[1], AREA_GRID * sv_areagrid_mingridsize.value);
+       world->areagrid_size[2] = max(world->maxs[2] - world->mins[2], AREA_GRID * sv_areagrid_mingridsize.value);
        // figure out the corners of such a box, centered at the center of the world box
-       world->areagrid_mins[0] = (world->areagrid_mins[0] + world->areagrid_maxs[0] - world->areagrid_size[0]) * 0.5f;
-       world->areagrid_mins[1] = (world->areagrid_mins[1] + world->areagrid_maxs[1] - world->areagrid_size[1]) * 0.5f;
-       world->areagrid_mins[2] = (world->areagrid_mins[2] + world->areagrid_maxs[2] - world->areagrid_size[2]) * 0.5f;
-       world->areagrid_maxs[0] = (world->areagrid_mins[0] + world->areagrid_maxs[0] + world->areagrid_size[0]) * 0.5f;
-       world->areagrid_maxs[1] = (world->areagrid_mins[1] + world->areagrid_maxs[1] + world->areagrid_size[1]) * 0.5f;
-       world->areagrid_maxs[2] = (world->areagrid_mins[2] + world->areagrid_maxs[2] + world->areagrid_size[2]) * 0.5f;
+       world->areagrid_mins[0] = (world->mins[0] + world->maxs[0] - world->areagrid_size[0]) * 0.5f;
+       world->areagrid_mins[1] = (world->mins[1] + world->maxs[1] - world->areagrid_size[1]) * 0.5f;
+       world->areagrid_mins[2] = (world->mins[2] + world->maxs[2] - world->areagrid_size[2]) * 0.5f;
+       world->areagrid_maxs[0] = (world->mins[0] + world->maxs[0] + world->areagrid_size[0]) * 0.5f;
+       world->areagrid_maxs[1] = (world->mins[1] + world->maxs[1] + world->areagrid_size[1]) * 0.5f;
+       world->areagrid_maxs[2] = (world->mins[2] + world->maxs[2] + world->areagrid_size[2]) * 0.5f;
        // now calculate the actual useful info from that
        VectorNegate(world->areagrid_mins, world->areagrid_bias);
        world->areagrid_scale[0] = AREA_GRID / world->areagrid_size[0];
@@ -131,8 +132,8 @@ void World_SetSize(world_t *world, const char *filename, const vec3_t mins, cons
        World_ClearLink(&world->areagrid_outside);
        for (i = 0;i < AREA_GRIDNODES;i++)
                World_ClearLink(&world->areagrid[i]);
-       if (developer.integer >= 10)
-               Con_Printf("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);
+       if (developer_extra.integer)
+               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);
 }
 
 /*
@@ -314,9 +315,9 @@ void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const
 // physics engine support
 //============================================================================
 
-//#ifndef ODE_STATIC
-//#define ODE_DYNAMIC 1
-//#endif
+#ifndef ODE_STATIC
+define ODE_DYNAMIC 1
+#endif
 
 #if defined(ODE_STATIC) || defined(ODE_DYNAMIC)
 #define USEODE 1
@@ -334,9 +335,10 @@ cvar_t physics_ode_contact_erp = {0, "physics_ode_contact_erp", "0.96", "contact
 cvar_t physics_ode_contact_cfm = {0, "physics_ode_contact_cfm", "0", "contact solver cfm parameter - Constraint Force Mixing (see ODE User Guide)"};
 cvar_t physics_ode_world_erp = {0, "physics_ode_world_erp", "-1", "world solver erp parameter - Error Restitution Percent (see ODE User Guide); use defaults when set to -1"};
 cvar_t physics_ode_world_cfm = {0, "physics_ode_world_cfm", "-1", "world solver cfm parameter - Constraint Force Mixing (see ODE User Guide); not touched when -1"};
-cvar_t physics_ode_iterationsperframe = {0, "physics_ode_iterationsperframe", "4", "divisor for time step, runs multiple physics steps per frame"};
+cvar_t physics_ode_iterationsperframe = {0, "physics_ode_iterationsperframe", "1", "divisor for time step, runs multiple physics steps per frame"};
 cvar_t physics_ode_movelimit = {0, "physics_ode_movelimit", "0.5", "clamp velocity if a single move would exceed this percentage of object thickness, to prevent flying through walls"};
 cvar_t physics_ode_spinlimit = {0, "physics_ode_spinlimit", "10000", "reset spin velocity if it gets too large"};
+cvar_t physics_ode = {0, "physics_ode", "0", "run ODE physics (VERY experimental and potentially buggy)"};
 
 // LordHavoc: this large chunk of definitions comes from the ODE library
 // include files.
@@ -371,6 +373,8 @@ struct dxJointNode;
 struct dxJointGroup;
 struct dxTriMeshData;
 
+#define dInfinity 3.402823466e+38f
+
 typedef struct dxWorld *dWorldID;
 typedef struct dxSpace *dSpaceID;
 typedef struct dxBody *dBodyID;
@@ -524,7 +528,7 @@ typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2);
 #define dSAP_AXES_ZYX  ((2)|(1<<2)|(0<<4))
 
 //const char*     (ODE_API *dGetConfiguration)(void);
-//int             (ODE_API *dCheckConfiguration)( const char* token );
+int             (ODE_API *dCheckConfiguration)( const char* token );
 int             (ODE_API *dInitODE)(void);
 //int             (ODE_API *dInitODE2)(unsigned int uiInitFlags);
 //int             (ODE_API *dAllocateODEDataForThread)(unsigned int uiAllocateFlags);
@@ -689,7 +693,7 @@ dJointID        (ODE_API *dJointCreateUniversal)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreatePR)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreatePU)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreatePiston)(dWorldID, dJointGroupID);
-//dJointID        (ODE_API *dJointCreateFixed)(dWorldID, dJointGroupID);
+dJointID        (ODE_API *dJointCreateFixed)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreateNull)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreateAMotor)(dWorldID, dJointGroupID);
 //dJointID        (ODE_API *dJointCreateLMotor)(dWorldID, dJointGroupID);
@@ -990,7 +994,7 @@ dGeomID         (ODE_API *dCreateTriMesh)(dSpaceID space, dTriMeshDataID Data, d
 static dllfunction_t odefuncs[] =
 {
 //     {"dGetConfiguration",                                                   (void **) &dGetConfiguration},
-//     {"dCheckConfiguration",                                                 (void **) &dCheckConfiguration},
+       {"dCheckConfiguration",                                                 (void **) &dCheckConfiguration},
        {"dInitODE",                                                                    (void **) &dInitODE},
 //     {"dInitODE2",                                                                   (void **) &dInitODE2},
 //     {"dAllocateODEDataForThread",                                   (void **) &dAllocateODEDataForThread},
@@ -1154,7 +1158,7 @@ static dllfunction_t odefuncs[] =
 //     {"dJointCreatePR",                                                              (void **) &dJointCreatePR},
 //     {"dJointCreatePU",                                                              (void **) &dJointCreatePU},
 //     {"dJointCreatePiston",                                                  (void **) &dJointCreatePiston},
-//     {"dJointCreateFixed",                                                   (void **) &dJointCreateFixed},
+       {"dJointCreateFixed",                                                   (void **) &dJointCreateFixed},
 //     {"dJointCreateNull",                                                    (void **) &dJointCreateNull},
 //     {"dJointCreateAMotor",                                                  (void **) &dJointCreateAMotor},
 //     {"dJointCreateLMotor",                                                  (void **) &dJointCreateLMotor},
@@ -1447,9 +1451,7 @@ static void World_Physics_Init(void)
 #ifdef ODE_DYNAMIC
        const char* dllnames [] =
        {
-# if defined(WIN64)
-               "libode1_64.dll",
-# elif defined(WIN32)
+# if defined(WIN32)
                "libode1.dll",
 # elif defined(MACOSX)
                "libode.1.dylib",
@@ -1474,6 +1476,7 @@ static void World_Physics_Init(void)
        Cvar_RegisterVariable(&physics_ode_iterationsperframe);
        Cvar_RegisterVariable(&physics_ode_movelimit);
        Cvar_RegisterVariable(&physics_ode_spinlimit);
+       Cvar_RegisterVariable(&physics_ode);
 
 #ifdef ODE_DYNAMIC
        // Load the DLL
@@ -1482,7 +1485,7 @@ static void World_Physics_Init(void)
        {
                dInitODE();
 //             dInitODE2(0);
-#ifdef ODE_DNYAMIC
+#ifdef ODE_DYNAMIC
 # ifdef dSINGLE
                if (!dCheckConfiguration("ODE_single_precision"))
 # else
@@ -1535,9 +1538,9 @@ static void World_Physics_EnableODE(world_t *world)
        world->physics.ode_space = dQuadTreeSpaceCreate(NULL, center, extents, bound(1, physics_ode_quadtree_depth.integer, 10));
        world->physics.ode_contactgroup = dJointGroupCreate(0);
        if(physics_ode_world_erp.value >= 0)
-               dWorldSetERP(world->physics.ode_world, physics_ode_world_erp.value);
+               dWorldSetERP((dWorldID)world->physics.ode_world, physics_ode_world_erp.value);
        if(physics_ode_world_cfm.value >= 0)
-               dWorldSetCFM(world->physics.ode_world, physics_ode_world_cfm.value);
+               dWorldSetCFM((dWorldID)world->physics.ode_world, physics_ode_world_cfm.value);
 }
 #endif
 
@@ -1555,9 +1558,9 @@ static void World_Physics_End(world_t *world)
 #ifdef USEODE
        if (world->physics.ode)
        {
-               dWorldDestroy(world->physics.ode_world);
-               dSpaceDestroy(world->physics.ode_space);
-               dJointGroupDestroy(world->physics.ode_contactgroup);
+               dWorldDestroy((dWorldID)world->physics.ode_world);
+               dSpaceDestroy((dSpaceID)world->physics.ode_space);
+               dJointGroupDestroy((dJointGroupID)world->physics.ode_contactgroup);
                world->physics.ode = false;
        }
 #endif
@@ -1616,6 +1619,9 @@ void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
                Mem_Free(ed->priv.server->ode_element3i);
        ed->priv.server->ode_element3i = NULL;
        ed->priv.server->ode_numtriangles = 0;
+       if(ed->priv.server->ode_massbuf)
+               Mem_Free(ed->priv.server->ode_massbuf);
+       ed->priv.server->ode_massbuf = NULL;
 }
 
 #ifdef USEODE
@@ -1643,7 +1649,7 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
        movetype = (int)val->_float;
        if (movetype != MOVETYPE_PHYSICS)
        {
-               val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.jointtype);if (val) jointtype = (int)val->_float;
+               jointtype = 0;val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.jointtype);if (val) jointtype = (int)val->_float;
                switch(jointtype)
                {
                        // TODO feed back data from physics
@@ -1657,6 +1663,8 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
                                break;
                        case JOINTTYPE_HINGE2:
                                break;
+                       case JOINTTYPE_FIXED:
+                               break;
                }
                return;
        }
@@ -1712,7 +1720,7 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
        VectorCopy(velocity, ed->priv.server->ode_velocity);
        VectorCopy(angles, ed->priv.server->ode_angles);
        VectorCopy(avelocity, ed->priv.server->ode_avelocity);
-       ed->priv.server->ode_gravity = dBodyGetGravityMode(body);
+       ed->priv.server->ode_gravity = dBodyGetGravityMode(body) != 0;
 
        if(!strcmp(prog->name, "server")) // FIXME some better way?
        {
@@ -1730,11 +1738,12 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
        int jointtype = 0;
        int enemy = 0, aiment = 0;
        vec3_t origin, velocity, angles, forward, left, up, movedir;
+       vec_t CFM, ERP, FMax, Stop, Vel;
        prvm_eval_t *val;
-       float H = (!strcmp(prog->name, "server") ? sv.frametime : cl.mtime[0] - cl.mtime[1]) / bound(1, physics_ode_iterationsperframe.integer, 1000);
        VectorClear(origin);
        VectorClear(velocity);
        VectorClear(angles);
+       VectorClear(movedir);
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);if (val) movetype = (int)val->_float;
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.jointtype);if (val) jointtype = (int)val->_float;
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.enemy);if (val) enemy = val->_int;
@@ -1750,38 +1759,57 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
        if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
                aiment = 0;
        // see http://www.ode.org/old_list_archives/2006-January/017614.html
-       if(movedir[0] > 0)
        // we want to set ERP? make it fps independent and work like a spring constant
        // note: if movedir[2] is 0, it becomes ERP = 1, CFM = 1.0 / (H * K)
+       if(movedir[0] > 0 && movedir[1] > 0)
        {
                float K = movedir[0];
-               float D = movedir[2];
+               float D = movedir[1];
                float R = 2.0 * D * sqrt(K); // we assume D is premultiplied by sqrt(sprungMass)
-               float ERP = (H * K) / (H * K + R);
-               float CFM = 1.0 / (H * K + R);
-               movedir[0] = CFM;
-               movedir[2] = ERP;
+               CFM = 1.0 / (world->physics.ode_step * K + R); // always > 0
+               ERP = world->physics.ode_step * K * CFM;
+               Vel = 0;
+               FMax = 0;
+               Stop = movedir[2];
+       }
+       else if(movedir[1] < 0)
+       {
+               CFM = 0;
+               ERP = 0;
+               Vel = movedir[0];
+               FMax = -movedir[1]; // TODO do we need to multiply with world.physics.ode_step?
+               Stop = movedir[2] > 0 ? movedir[2] : dInfinity;
+       }
+       else // movedir[0] > 0, movedir[1] == 0 or movedir[0] < 0, movedir[1] >= 0
+       {
+               CFM = 0;
+               ERP = 0;
+               Vel = 0;
+               FMax = 0;
+               Stop = dInfinity;
        }
-       movedir[1] *= H; // make movedir[1] actually "force per second" to allow this to be used for non-springs
        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))
                return; // nothing to do
        AngleVectorsFLU(angles, forward, left, up);
        switch(jointtype)
        {
                case JOINTTYPE_POINT:
-                       j = dJointCreateBall(world->physics.ode_world, 0);
+                       j = dJointCreateBall((dWorldID)world->physics.ode_world, 0);
                        break;
                case JOINTTYPE_HINGE:
-                       j = dJointCreateHinge(world->physics.ode_world, 0);
+                       j = dJointCreateHinge((dWorldID)world->physics.ode_world, 0);
                        break;
                case JOINTTYPE_SLIDER:
-                       j = dJointCreateSlider(world->physics.ode_world, 0);
+                       j = dJointCreateSlider((dWorldID)world->physics.ode_world, 0);
                        break;
                case JOINTTYPE_UNIVERSAL:
-                       j = dJointCreateUniversal(world->physics.ode_world, 0);
+                       j = dJointCreateUniversal((dWorldID)world->physics.ode_world, 0);
                        break;
                case JOINTTYPE_HINGE2:
-                       j = dJointCreateHinge2(world->physics.ode_world, 0);
+                       j = dJointCreateHinge2((dWorldID)world->physics.ode_world, 0);
+                       break;
+               case JOINTTYPE_FIXED:
+                       j = dJointCreateFixed((dWorldID)world->physics.ode_world, 0);
                        break;
                case 0:
                default:
@@ -1792,8 +1820,8 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
        if(ed->priv.server->ode_joint)
        {
                //Con_Printf("deleted old joint %i\n", (int) (ed - prog->edicts));
-               dJointAttach(ed->priv.server->ode_joint, 0, 0);
-               dJointDestroy(ed->priv.server->ode_joint);
+               dJointAttach((dJointID)ed->priv.server->ode_joint, 0, 0);
+               dJointDestroy((dJointID)ed->priv.server->ode_joint);
        }
        ed->priv.server->ode_joint = (void *) j;
        ed->priv.server->ode_joint_type = jointtype;
@@ -1812,21 +1840,7 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
                if(aiment)
                        b2 = (dBodyID)prog->edicts[aiment].priv.server->ode_body;
                dJointAttach(j, b1, b2);
-#define SETPARAMS(t,id) \
-                               dJointSet##t##Param(j, dParamStopCFM##id, movedir[0]); \
-                               if(movedir[1] > 0) \
-                               { \
-                                       dJointSet##t##Param(j, dParamLoStop##id, 0); \
-                                       dJointSet##t##Param(j, dParamHiStop##id, 0); \
-                                       dJointSet##t##Param(j, dParamFMax##id, movedir[1]); \
-                               } \
-                               else \
-                               { \
-                                       dJointSet##t##Param(j, dParamLoStop##id, -dInfinity); \
-                                       dJointSet##t##Param(j, dParamHiStop##id, dInfinity); \
-                                       dJointSet##t##Param(j, dParamFMax##id, -movedir[1]); \
-                               } \
-                               dJointSet##t##Param(j, dParamStopERP##id, movedir[2])
+
                switch(jointtype)
                {
                        case JOINTTYPE_POINT:
@@ -1835,31 +1849,65 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed
                        case JOINTTYPE_HINGE:
                                dJointSetHingeAnchor(j, origin[0], origin[1], origin[2]);
                                dJointSetHingeAxis(j, forward[0], forward[1], forward[2]);
-                               SETPARAMS(Hinge,);
+                               dJointSetHingeParam(j, dParamFMax, FMax);
+                               dJointSetHingeParam(j, dParamHiStop, Stop);
+                               dJointSetHingeParam(j, dParamLoStop, -Stop);
+                               dJointSetHingeParam(j, dParamStopCFM, CFM);
+                               dJointSetHingeParam(j, dParamStopERP, ERP);
+                               dJointSetHingeParam(j, dParamVel, Vel);
                                break;
                        case JOINTTYPE_SLIDER:
                                dJointSetSliderAxis(j, forward[0], forward[1], forward[2]);
-                               SETPARAMS(Slider,);
+                               dJointSetSliderParam(j, dParamFMax, FMax);
+                               dJointSetSliderParam(j, dParamHiStop, Stop);
+                               dJointSetSliderParam(j, dParamLoStop, -Stop);
+                               dJointSetSliderParam(j, dParamStopCFM, CFM);
+                               dJointSetSliderParam(j, dParamStopERP, ERP);
+                               dJointSetSliderParam(j, dParamVel, Vel);
                                break;
                        case JOINTTYPE_UNIVERSAL:
                                dJointSetUniversalAnchor(j, origin[0], origin[1], origin[2]);
                                dJointSetUniversalAxis1(j, forward[0], forward[1], forward[2]);
                                dJointSetUniversalAxis2(j, up[0], up[1], up[2]);
-                               SETPARAMS(Universal,);
-                               SETPARAMS(Universal,2);
+                               dJointSetUniversalParam(j, dParamFMax, FMax);
+                               dJointSetUniversalParam(j, dParamHiStop, Stop);
+                               dJointSetUniversalParam(j, dParamLoStop, -Stop);
+                               dJointSetUniversalParam(j, dParamStopCFM, CFM);
+                               dJointSetUniversalParam(j, dParamStopERP, ERP);
+                               dJointSetUniversalParam(j, dParamVel, Vel);
+                               dJointSetUniversalParam(j, dParamFMax2, FMax);
+                               dJointSetUniversalParam(j, dParamHiStop2, Stop);
+                               dJointSetUniversalParam(j, dParamLoStop2, -Stop);
+                               dJointSetUniversalParam(j, dParamStopCFM2, CFM);
+                               dJointSetUniversalParam(j, dParamStopERP2, ERP);
+                               dJointSetUniversalParam(j, dParamVel2, Vel);
                                break;
                        case JOINTTYPE_HINGE2:
                                dJointSetHinge2Anchor(j, origin[0], origin[1], origin[2]);
                                dJointSetHinge2Axis1(j, forward[0], forward[1], forward[2]);
                                dJointSetHinge2Axis2(j, velocity[0], velocity[1], velocity[2]);
-                               SETPARAMS(Hinge2,);
-                               SETPARAMS(Hinge2,2);
+                               dJointSetHinge2Param(j, dParamFMax, FMax);
+                               dJointSetHinge2Param(j, dParamHiStop, Stop);
+                               dJointSetHinge2Param(j, dParamLoStop, -Stop);
+                               dJointSetHinge2Param(j, dParamStopCFM, CFM);
+                               dJointSetHinge2Param(j, dParamStopERP, ERP);
+                               dJointSetHinge2Param(j, dParamVel, Vel);
+                               dJointSetHinge2Param(j, dParamFMax2, FMax);
+                               dJointSetHinge2Param(j, dParamHiStop2, Stop);
+                               dJointSetHinge2Param(j, dParamLoStop2, -Stop);
+                               dJointSetHinge2Param(j, dParamStopCFM2, CFM);
+                               dJointSetHinge2Param(j, dParamStopERP2, ERP);
+                               dJointSetHinge2Param(j, dParamVel2, Vel);
+                               break;
+                       case JOINTTYPE_FIXED:
                                break;
                        case 0:
                        default:
-                               Host_Error("what? but above the joint was valid...\n");
+                               Sys_Error("what? but above the joint was valid...\n");
                                break;
                }
+#undef SETPARAMS
+
        }
 }
 
@@ -1914,28 +1962,25 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);if (val) movetype = (int)val->_float;
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.scale);if (val && val->_float) scale = val->_float;
        modelindex = 0;
+       if (world == &sv.world)
+               mempool = sv_mempool;
+       else if (world == &cl.world)
+               mempool = cls.levelmempool;
+       else
+               mempool = NULL;
+       model = NULL;
        switch(solid)
        {
        case SOLID_BSP:
                val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.modelindex);
                if (val)
                        modelindex = (int)val->_float;
-               if (world == &sv.world && modelindex >= 1 && modelindex < MAX_MODELS)
-               {
-                       model = sv.models[modelindex];
-                       mempool = sv_mempool;
-               }
-               else if (world == &cl.world && modelindex >= 1 && modelindex < MAX_MODELS)
-               {
-                       model = cl.model_precache[modelindex];
-                       mempool = cls.levelmempool;
-               }
+               if (world == &sv.world)
+                       model = SV_GetModelByIndex(modelindex);
+               else if (world == &cl.world)
+                       model = CL_GetModelByIndex(modelindex);
                else
-               {
                        model = NULL;
-                       mempool = NULL;
-                       modelindex = 0;
-               }
                if (model)
                {
                        VectorScale(model->normalmins, scale, entmins);
@@ -2008,7 +2053,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        if (!model)
                        {
                                Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string));
-                               break;
+                               goto treatasbox;
                        }
                        // add an optimized mesh to the model containing only the SUPERCONTENTS_SOLID surfaces
                        if (!model->brush.collisionmesh)
@@ -2016,7 +2061,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        if (!model->brush.collisionmesh || !model->brush.collisionmesh->numtriangles)
                        {
                                Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string));
-                               break;
+                               goto treatasbox;
                        }
                        // ODE requires persistent mesh storage, so we need to copy out
                        // the data from the model because renderer restarts could free it
@@ -2043,21 +2088,22 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
                        // now create the geom
                        dataID = dGeomTriMeshDataCreate();
-                       dGeomTriMeshDataBuildSingle(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]));
-                       ed->priv.server->ode_geom = (void *)dCreateTriMesh(world->physics.ode_space, dataID, NULL, NULL, NULL);
+                       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]));
+                       ed->priv.server->ode_geom = (void *)dCreateTriMesh((dSpaceID)world->physics.ode_space, (dTriMeshDataID)dataID, NULL, NULL, NULL);
                        dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
                        break;
                case SOLID_BBOX:
                case SOLID_SLIDEBOX:
                case SOLID_CORPSE:
                case SOLID_PHYSICS_BOX:
+treatasbox:
                        Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
-                       ed->priv.server->ode_geom = (void *)dCreateBox(world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]);
+                       ed->priv.server->ode_geom = (void *)dCreateBox((dSpaceID)world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]);
                        dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
                        break;
                case SOLID_PHYSICS_SPHERE:
                        Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
-                       ed->priv.server->ode_geom = (void *)dCreateSphere(world->physics.ode_space, geomsize[0] * 0.5f);
+                       ed->priv.server->ode_geom = (void *)dCreateSphere((dSpaceID)world->physics.ode_space, geomsize[0] * 0.5f);
                        dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f);
                        break;
                case SOLID_PHYSICS_CAPSULE:
@@ -2082,30 +2128,44 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        // because we want to support more than one axisindex, we have to
                        // create a transform, and turn on its cleanup setting (which will
                        // cause the child to be destroyed when it is destroyed)
-                       ed->priv.server->ode_geom = (void *)dCreateCapsule(world->physics.ode_space, radius, length);
+                       ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length);
                        dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length);
                        break;
                default:
                        Sys_Error("World_Physics_BodyFromEntity: unrecognized solid value %i was accepted by filter\n", solid);
+                       // this goto only exists to prevent warnings from the compiler
+                       // about uninitialized variables (mass), while allowing it to
+                       // catch legitimate uninitialized variable warnings
+                       goto treatasbox;
                }
                Matrix4x4_Invert_Simple(&ed->priv.server->ode_offsetimatrix, &ed->priv.server->ode_offsetmatrix);
+               ed->priv.server->ode_massbuf = Mem_Alloc(mempool, sizeof(mass));
+               memcpy(ed->priv.server->ode_massbuf, &mass, sizeof(dMass));
        }
 
        if(ed->priv.server->ode_geom)
-               dGeomSetData(ed->priv.server->ode_geom, (void*)ed);
+               dGeomSetData((dGeomID)ed->priv.server->ode_geom, (void*)ed);
        if (movetype == MOVETYPE_PHYSICS && ed->priv.server->ode_geom)
        {
                if (ed->priv.server->ode_body == NULL)
                {
-                       ed->priv.server->ode_body = (void *)(body = dBodyCreate(world->physics.ode_world));
-                       dGeomSetBody(ed->priv.server->ode_geom, body);
+                       ed->priv.server->ode_body = (void *)(body = dBodyCreate((dWorldID)world->physics.ode_world));
+                       dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
                        dBodySetData(body, (void*)ed);
-                       dBodySetMass(body, &mass);
+                       dBodySetMass(body, (dMass *) ed->priv.server->ode_massbuf);
+                       modified = true;
                }
        }
        else
        {
-               // let's keep the body around in case we need it again (in case QC toggles between MOVETYPE_PHYSICS and MOVETYPE_NONE)
+               if (ed->priv.server->ode_body != NULL)
+               {
+                       if(ed->priv.server->ode_geom)
+                               dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
+                       dBodyDestroy((dBodyID) ed->priv.server->ode_body);
+                       ed->priv.server->ode_body = NULL;
+                       modified = true;
+               }
        }
 
        // get current data from entity
@@ -2133,10 +2193,26 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        // compatibility for legacy entities
        //if (!VectorLength2(forward) || solid == SOLID_BSP)
        {
-               AngleVectorsFLU(angles, forward, left, up);
+               float pitchsign = 1;
+               vec3_t qangles, qavelocity;
+               VectorCopy(angles, qangles);
+               VectorCopy(avelocity, qavelocity);
+
+               if(!strcmp(prog->name, "server")) // FIXME some better way?
+               {
+                       pitchsign = SV_GetPitchSign(ed);
+               }
+               else if(!strcmp(prog->name, "client"))
+               {
+                       pitchsign = CL_GetPitchSign(ed);
+               }
+               qangles[PITCH] *= pitchsign;
+               qavelocity[PITCH] *= pitchsign;
+
+               AngleVectorsFLU(qangles, forward, left, up);
                // convert single-axis rotations in avelocity to spinvelocity
                // FIXME: untested math - check signs
-               VectorSet(spinvelocity, DEG2RAD(avelocity[PITCH]), DEG2RAD(avelocity[ROLL]), DEG2RAD(avelocity[YAW]));
+               VectorSet(spinvelocity, DEG2RAD(qavelocity[PITCH]), DEG2RAD(qavelocity[ROLL]), DEG2RAD(qavelocity[YAW]));
        }
 
        // compatibility for legacy entities
@@ -2191,8 +2267,8 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                modified = true;
 
        // store the qc values into the physics engine
-       body = ed->priv.server->ode_body;
-       if (modified)
+       body = (dBodyID)ed->priv.server->ode_body;
+       if (modified && ed->priv.server->ode_geom)
        {
                dVector3 r[3];
                matrix4x4_t entitymatrix;
@@ -2219,20 +2295,6 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                VectorCopy(avelocity, ed->priv.server->ode_avelocity);
                ed->priv.server->ode_gravity = gravity;
 
-               {
-                       float pitchsign = 1;
-                       if(!strcmp(prog->name, "server")) // FIXME some better way?
-                       {
-                               pitchsign = SV_GetPitchSign(ed);
-                       }
-                       else if(!strcmp(prog->name, "client"))
-                       {
-                               pitchsign = CL_GetPitchSign(ed);
-                       }
-                       angles[PITCH] *= pitchsign;
-                       avelocity[PITCH] *= pitchsign;
-               }
-
                Matrix4x4_FromVectors(&entitymatrix, forward, left, up, origin);
                Matrix4x4_Concat(&bodymatrix, &entitymatrix, &ed->priv.server->ode_offsetmatrix);
                Matrix4x4_ToVectors(&bodymatrix, forward, left, up, origin);
@@ -2249,7 +2311,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                {
                        if(movetype == MOVETYPE_PHYSICS)
                        {
-                               dGeomSetBody(ed->priv.server->ode_geom, ed->priv.server->ode_body);
+                               dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
                                dBodySetPosition(body, origin[0], origin[1], origin[2]);
                                dBodySetRotation(body, r[0]);
                                dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
@@ -2258,17 +2320,21 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        }
                        else
                        {
-                               dGeomSetBody(ed->priv.server->ode_geom, 0);
-                               dGeomSetPosition(ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
-                               dGeomSetRotation(ed->priv.server->ode_geom, r[0]);
+                               dGeomSetBody((dGeomID)ed->priv.server->ode_geom, body);
+                               dBodySetPosition(body, origin[0], origin[1], origin[2]);
+                               dBodySetRotation(body, r[0]);
+                               dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
+                               dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
+                               dBodySetGravityMode(body, gravity);
+                               dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0);
                        }
                }
                else
                {
                        // no body... then let's adjust the parameters of the geom directly
-                       dGeomSetBody(ed->priv.server->ode_geom, 0); // just in case we previously HAD a body (which should never happen)
-                       dGeomSetPosition(ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
-                       dGeomSetRotation(ed->priv.server->ode_geom, r[0]);
+                       dGeomSetBody((dGeomID)ed->priv.server->ode_geom, 0); // just in case we previously HAD a body (which should never happen)
+                       dGeomSetPosition((dGeomID)ed->priv.server->ode_geom, origin[0], origin[1], origin[2]);
+                       dGeomSetRotation((dGeomID)ed->priv.server->ode_geom, r[0]);
                }
        }
 
@@ -2397,7 +2463,7 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
                        bouncefactor1 = bouncefactor2;
                }
        }
-       dWorldGetGravity(world->physics.ode_world, grav);
+       dWorldGetGravity((dWorldID)world->physics.ode_world, grav);
        bouncestop1 *= fabs(grav[2]);
 
        // generate contact points between the two non-space geoms
@@ -2411,7 +2477,7 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
                contact[i].surface.soft_cfm = physics_ode_contact_cfm.value;
                contact[i].surface.bounce = bouncefactor1;
                contact[i].surface.bounce_vel = bouncestop1;
-               c = dJointCreateContact(world->physics.ode_world, world->physics.ode_contactgroup, contact + i);
+               c = dJointCreateContact((dWorldID)world->physics.ode_world, (dJointGroupID)world->physics.ode_contactgroup, contact + i);
                dJointAttach(c, b1, b2);
        }
 }
@@ -2420,11 +2486,15 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
 void World_Physics_Frame(world_t *world, double frametime, double gravity)
 {
 #ifdef USEODE
-       if (world->physics.ode)
+       if (world->physics.ode && physics_ode.integer)
        {
                int i;
                prvm_edict_t *ed;
 
+               world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000);
+               world->physics.ode_step = frametime / world->physics.ode_iterations;
+               world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step;
+
                // copy physics properties from entities to physics engine
                if (prog)
                {
@@ -2437,32 +2507,29 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
                                        World_Physics_Frame_JointFromEntity(world, ed);
                }
 
-               world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000);
-               world->physics.ode_step = frametime / world->physics.ode_iterations;
-               world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step;
                for (i = 0;i < world->physics.ode_iterations;i++)
                {
                        // set the gravity
-                       dWorldSetGravity(world->physics.ode_world, 0, 0, -gravity);
+                       dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity);
                        // set the tolerance for closeness of objects
-                       dWorldSetContactSurfaceLayer(world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value));
+                       dWorldSetContactSurfaceLayer((dWorldID)world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value));
 
                        // run collisions for the current world state, creating JointGroup
-                       dSpaceCollide(world->physics.ode_space, (void *)world, nearCallback);
+                       dSpaceCollide((dSpaceID)world->physics.ode_space, (void *)world, nearCallback);
 
                        // run physics (move objects, calculate new velocities)
                        if (physics_ode_worldquickstep.integer)
                        {
-                               dWorldSetQuickStepNumIterations(world->physics.ode_world, bound(1, physics_ode_worldquickstep_iterations.integer, 200));
-                               dWorldQuickStep(world->physics.ode_world, world->physics.ode_step);
+                               dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldquickstep_iterations.integer, 200));
+                               dWorldQuickStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
                        }
                        else if (physics_ode_worldstepfast.integer)
-                               dWorldStepFast1(world->physics.ode_world, world->physics.ode_step, bound(1, physics_ode_worldstepfast_iterations.integer, 200));
+                               dWorldStepFast1((dWorldID)world->physics.ode_world, world->physics.ode_step, bound(1, physics_ode_worldstepfast_iterations.integer, 200));
                        else
-                               dWorldStep(world->physics.ode_world, world->physics.ode_step);
+                               dWorldStep((dWorldID)world->physics.ode_world, world->physics.ode_step);
 
                        // clear the JointGroup now that we're done with it
-                       dJointGroupEmpty(world->physics.ode_contactgroup);
+                       dJointGroupEmpty((dJointGroupID)world->physics.ode_contactgroup);
                }
 
                // copy physics properties from physics engine to entities