]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - world.c
add cvars for world erp and cfm; remove pistons for ODE 0.9 support
[xonotic/darkplaces.git] / world.c
diff --git a/world.c b/world.c
index 9db8a11c41ac4a403411e14984342539e2ca3dad..9a032bb20173555cfa3d7ca0f56b0bf95448a176 100644 (file)
--- a/world.c
+++ b/world.c
@@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 // world.c -- world query functions
 
 #include "quakedef.h"
+#include "clvm_cmds.h"
 
 /*
 
@@ -313,6 +314,15 @@ 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
+
+#if defined(ODE_STATIC) || defined(ODE_DYNAMIC)
+#define USEODE 1
+#endif
+
+#ifdef USEODE
 cvar_t physics_ode_quadtree_depth = {0, "physics_ode_quadtree_depth","5", "desired subdivision level of quadtree culling space"};
 cvar_t physics_ode_contactsurfacelayer = {0, "physics_ode_contactsurfacelayer","0", "allows objects to overlap this many units to reduce jitter"};
 cvar_t physics_ode_worldquickstep = {0, "physics_ode_worldquickstep","1", "use dWorldQuickStep rather than dWorldStepFast1 or dWorldStep"};
@@ -322,6 +332,8 @@ cvar_t physics_ode_worldstepfast_iterations = {0, "physics_ode_worldstepfast_ite
 cvar_t physics_ode_contact_mu = {0, "physics_ode_contact_mu", "1", "contact solver mu parameter - friction pyramid approximation 1 (see ODE User Guide)"};
 cvar_t physics_ode_contact_erp = {0, "physics_ode_contact_erp", "0.96", "contact solver erp parameter - Error Restitution Percent (see ODE User Guide)"};
 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_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"};
@@ -329,20 +341,12 @@ cvar_t physics_ode_spinlimit = {0, "physics_ode_spinlimit", "10000", "reset spin
 // LordHavoc: this large chunk of definitions comes from the ODE library
 // include files.
 
-#ifndef ODE_STATIC
-#define ODE_DYNAMIC 1
-#endif
-
-#if defined(ODE_STATIC) || defined(ODE_DYNAMIC)
-#define USEODE 1
-#endif
-
-#ifdef USEODE
 #ifdef ODE_STATIC
 #include "ode/ode.h"
 #else
 #ifdef WINAPI
-#define ODE_API WINAPI
+// ODE does not use WINAPI
+#define ODE_API
 #else
 #define ODE_API
 #endif
@@ -476,11 +480,12 @@ typedef void dNearCallback (void *data, dGeomID o1, dGeomID o2);
 #define dSAP_AXES_ZXY  ((2)|(0<<2)|(1<<4))
 #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 *dInitODE2)(unsigned int uiInitFlags);
-int             (ODE_API *dAllocateODEDataForThread)(unsigned int uiAllocateFlags);
-void            (ODE_API *dCleanupODEAllDataForThread)(void);
+//const char*     (ODE_API *dGetConfiguration)(void);
+//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);
+//void            (ODE_API *dCleanupODEAllDataForThread)(void);
 void            (ODE_API *dCloseODE)(void);
 
 //int             (ODE_API *dMassCheck)(const dMass *m);
@@ -504,7 +509,7 @@ void            (ODE_API *dMassSetBoxTotal)(dMass *, dReal total_mass, dReal lx,
 dWorldID        (ODE_API *dWorldCreate)(void);
 void            (ODE_API *dWorldDestroy)(dWorldID world);
 void            (ODE_API *dWorldSetGravity)(dWorldID, dReal x, dReal y, dReal z);
-//void            (ODE_API *dWorldGetGravity)(dWorldID, dVector3 gravity);
+void            (ODE_API *dWorldGetGravity)(dWorldID, dVector3 gravity);
 //void            (ODE_API *dWorldSetERP)(dWorldID, dReal erp);
 //dReal           (ODE_API *dWorldGetERP)(dWorldID);
 //void            (ODE_API *dWorldSetCFM)(dWorldID, dReal cfm);
@@ -567,7 +572,7 @@ void            (ODE_API *dWorldStepFast1)(dWorldID, dReal stepsize, int maxiter
 dBodyID         (ODE_API *dBodyCreate)(dWorldID);
 void            (ODE_API *dBodyDestroy)(dBodyID);
 void            (ODE_API *dBodySetData)(dBodyID, void *data);
-//void *          (ODE_API *dBodyGetData)(dBodyID);
+void *          (ODE_API *dBodyGetData)(dBodyID);
 void            (ODE_API *dBodySetPosition)(dBodyID, dReal x, dReal y, dReal z);
 void            (ODE_API *dBodySetRotation)(dBodyID, const dMatrix3 R);
 //void            (ODE_API *dBodySetQuaternion)(dBodyID, const dQuaternion q);
@@ -613,8 +618,8 @@ void            (ODE_API *dBodySetMass)(dBodyID, const dMass *mass);
 //void            (ODE_API *dBodyEnable)(dBodyID);
 //void            (ODE_API *dBodyDisable)(dBodyID);
 //int             (ODE_API *dBodyIsEnabled)(dBodyID);
-//void            (ODE_API *dBodySetGravityMode)(dBodyID b, int mode);
-//int             (ODE_API *dBodyGetGravityMode)(dBodyID b);
+void            (ODE_API *dBodySetGravityMode)(dBodyID b, int mode);
+int             (ODE_API *dBodyGetGravityMode)(dBodyID b);
 //void            (*dBodySetMovedCallback)(dBodyID b, void(ODE_API *callback)(dBodyID));
 //dGeomID         (ODE_API *dBodyGetFirstGeom)(dBodyID b);
 //dGeomID         (ODE_API *dBodyGetNextGeom)(dGeomID g);
@@ -796,7 +801,7 @@ int             (ODE_API *dAreConnectedExcluding)(dBodyID body1, dBodyID body2,
 dSpaceID        (ODE_API *dSimpleSpaceCreate)(dSpaceID space);
 dSpaceID        (ODE_API *dHashSpaceCreate)(dSpaceID space);
 dSpaceID        (ODE_API *dQuadTreeSpaceCreate)(dSpaceID space, const dVector3 Center, const dVector3 Extents, int Depth);
-dSpaceID        (ODE_API *dSweepAndPruneSpaceCreate)( dSpaceID space, int axisorder );
+//dSpaceID        (ODE_API *dSweepAndPruneSpaceCreate)( dSpaceID space, int axisorder );
 void            (ODE_API *dSpaceDestroy)(dSpaceID);
 //void            (ODE_API *dHashSpaceSetLevels)(dSpaceID space, int minlevel, int maxlevel);
 //void            (ODE_API *dHashSpaceGetLevels)(dSpaceID space, int *minlevel, int *maxlevel);
@@ -941,11 +946,12 @@ dGeomID         (ODE_API *dCreateTriMesh)(dSpaceID space, dTriMeshDataID Data, d
 
 static dllfunction_t odefuncs[] =
 {
-       {"dGetConfiguration",                                                   (void **) &dGetConfiguration},
-       {"dCheckConfiguration",                                                 (void **) &dCheckConfiguration},
-       {"dInitODE2",                                                                   (void **) &dInitODE2},
-       {"dAllocateODEDataForThread",                                   (void **) &dAllocateODEDataForThread},
-       {"dCleanupODEAllDataForThread",                                 (void **) &dCleanupODEAllDataForThread},
+//     {"dGetConfiguration",                                                   (void **) &dGetConfiguration},
+//     {"dCheckConfiguration",                                                 (void **) &dCheckConfiguration},
+       {"dInitODE",                                                                    (void **) &dInitODE},
+//     {"dInitODE2",                                                                   (void **) &dInitODE2},
+//     {"dAllocateODEDataForThread",                                   (void **) &dAllocateODEDataForThread},
+//     {"dCleanupODEAllDataForThread",                                 (void **) &dCleanupODEAllDataForThread},
        {"dCloseODE",                                                                   (void **) &dCloseODE},
 //     {"dMassCheck",                                                                  (void **) &dMassCheck},
 //     {"dMassSetZero",                                                                (void **) &dMassSetZero},
@@ -968,7 +974,7 @@ static dllfunction_t odefuncs[] =
        {"dWorldCreate",                                                                (void **) &dWorldCreate},
        {"dWorldDestroy",                                                               (void **) &dWorldDestroy},
        {"dWorldSetGravity",                                                    (void **) &dWorldSetGravity},
-//     {"dWorldGetGravity",                                                    (void **) &dWorldGetGravity},
+       {"dWorldGetGravity",                                                    (void **) &dWorldGetGravity},
 //     {"dWorldSetERP",                                                                (void **) &dWorldSetERP},
 //     {"dWorldGetERP",                                                                (void **) &dWorldGetERP},
 //     {"dWorldSetCFM",                                                                (void **) &dWorldSetCFM},
@@ -1031,7 +1037,7 @@ static dllfunction_t odefuncs[] =
        {"dBodyCreate",                                                                 (void **) &dBodyCreate},
        {"dBodyDestroy",                                                                (void **) &dBodyDestroy},
        {"dBodySetData",                                                                (void **) &dBodySetData},
-//     {"dBodyGetData",                                                                (void **) &dBodyGetData},
+       {"dBodyGetData",                                                                (void **) &dBodyGetData},
        {"dBodySetPosition",                                                    (void **) &dBodySetPosition},
        {"dBodySetRotation",                                                    (void **) &dBodySetRotation},
 //     {"dBodySetQuaternion",                                                  (void **) &dBodySetQuaternion},
@@ -1077,8 +1083,8 @@ static dllfunction_t odefuncs[] =
 //     {"dBodyEnable",                                                                 (void **) &dBodyEnable},
 //     {"dBodyDisable",                                                                (void **) &dBodyDisable},
 //     {"dBodyIsEnabled",                                                              (void **) &dBodyIsEnabled},
-//     {"dBodySetGravityMode",                                                 (void **) &dBodySetGravityMode},
-//     {"dBodyGetGravityMode",                                                 (void **) &dBodyGetGravityMode},
+       {"dBodySetGravityMode",                                                 (void **) &dBodySetGravityMode},
+       {"dBodyGetGravityMode",                                                 (void **) &dBodyGetGravityMode},
 //     {"dBodySetMovedCallback",                                               (void **) &dBodySetMovedCallback},
 //     {"dBodyGetFirstGeom",                                                   (void **) &dBodyGetFirstGeom},
 //     {"dBodyGetNextGeom",                                                    (void **) &dBodyGetNextGeom},
@@ -1259,7 +1265,7 @@ static dllfunction_t odefuncs[] =
        {"dSimpleSpaceCreate",                                                  (void **) &dSimpleSpaceCreate},
        {"dHashSpaceCreate",                                                    (void **) &dHashSpaceCreate},
        {"dQuadTreeSpaceCreate",                                                (void **) &dQuadTreeSpaceCreate},
-       {"dSweepAndPruneSpaceCreate",                                   (void **) &dSweepAndPruneSpaceCreate},
+//     {"dSweepAndPruneSpaceCreate",                                   (void **) &dSweepAndPruneSpaceCreate},
        {"dSpaceDestroy",                                                               (void **) &dSpaceDestroy},
 //     {"dHashSpaceSetLevels",                                                 (void **) &dHashSpaceSetLevels},
 //     {"dHashSpaceGetLevels",                                                 (void **) &dHashSpaceGetLevels},
@@ -1395,7 +1401,7 @@ dllhandle_t ode_dll = NULL;
 static void World_Physics_Init(void)
 {
 #ifdef USEODE
-#ifndef ODE_STATIC
+#ifdef ODE_DYNAMIC
        const char* dllnames [] =
        {
 # if defined(WIN64)
@@ -1420,17 +1426,20 @@ static void World_Physics_Init(void)
        Cvar_RegisterVariable(&physics_ode_contact_mu);
        Cvar_RegisterVariable(&physics_ode_contact_erp);
        Cvar_RegisterVariable(&physics_ode_contact_cfm);
+       Cvar_RegisterVariable(&physics_ode_world_erp);
+       Cvar_RegisterVariable(&physics_ode_world_cfm);
        Cvar_RegisterVariable(&physics_ode_iterationsperframe);
        Cvar_RegisterVariable(&physics_ode_movelimit);
        Cvar_RegisterVariable(&physics_ode_spinlimit);
 
-#ifndef ODE_STATIC
+#ifdef ODE_DYNAMIC
        // Load the DLL
        if (Sys_LoadLibrary (dllnames, &ode_dll, odefuncs))
 #endif
        {
-               dInitODE2(0);
-#ifndef ODE_STATIC
+               dInitODE();
+//             dInitODE2(0);
+#ifdef ODE_DNYAMIC
 # ifdef dSINGLE
                if (!dCheckConfiguration("ODE_single_precision"))
 # else
@@ -1442,7 +1451,7 @@ static void World_Physics_Init(void)
 # else
                        Con_Printf("ode library not compiled for double precision - incompatible!  Not using ODE physics.\n");
 # endif
-                       Sys_UnloadLibrary(ode_dll);
+                       Sys_UnloadLibrary(&ode_dll);
                        ode_dll = NULL;
                }
 #endif
@@ -1453,13 +1462,13 @@ static void World_Physics_Init(void)
 static void World_Physics_Shutdown(void)
 {
 #ifdef USEODE
-#ifndef ODE_STATIC
+#ifdef ODE_DYNAMIC
        if (ode_dll)
 #endif
        {
                dCloseODE();
-#ifndef ODE_STATIC
-               Sys_UnloadLibrary(ode_dll);
+#ifdef ODE_DYNAMIC
+               Sys_UnloadLibrary(&ode_dll);
                ode_dll = NULL;
 #endif
        }
@@ -1472,7 +1481,7 @@ static void World_Physics_EnableODE(world_t *world)
        dVector3 center, extents;
        if (world->physics.ode)
                return;
-#ifndef ODE_STATIC
+#ifdef ODE_DYNAMIC
        if (!ode_dll)
                return;
 #endif
@@ -1482,7 +1491,10 @@ static void World_Physics_EnableODE(world_t *world)
        world->physics.ode_world = dWorldCreate();
        world->physics.ode_space = dQuadTreeSpaceCreate(NULL, center, extents, bound(1, physics_ode_quadtree_depth.integer, 10));
        world->physics.ode_contactgroup = dJointGroupCreate(0);
-       // we don't currently set dWorldSetCFM or dWorldSetERP because the defaults seem fine
+       if(physics_ode_world_erp.value >= 0)
+               dWorldSetERP(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);
 }
 #endif
 
@@ -1508,17 +1520,51 @@ static void World_Physics_End(world_t *world)
 #endif
 }
 
-void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
+void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed)
 {
+       ed->priv.server->ode_joint_type = 0;
 #ifdef USEODE
+       if(ed->priv.server->ode_joint)
+               dJointDestroy((dJointID)ed->priv.server->ode_joint);
+       ed->priv.server->ode_joint = NULL;
+#endif
+}
+
+void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed)
+{
        // entity is not physics controlled, free any physics data
+       prvm_edict_t *ed2;
        ed->priv.server->ode_physics = false;
+#ifdef USEODE
        if (ed->priv.server->ode_geom)
                dGeomDestroy((dGeomID)ed->priv.server->ode_geom);
        ed->priv.server->ode_geom = NULL;
        if (ed->priv.server->ode_body)
+       {
+               dJointID j;
+               dBodyID b1, b2;
+               while(dBodyGetNumJoints((dBodyID)ed->priv.server->ode_body))
+               {
+                       j = dBodyGetJoint((dBodyID)ed->priv.server->ode_body, 0);
+                       ed2 = (prvm_edict_t *) dJointGetData(j);
+                       b1 = dJointGetBody(j, 0);
+                       b2 = dJointGetBody(j, 1);
+                       if(b1 == (dBodyID)ed->priv.server->ode_body)
+                       {
+                               b1 = 0;
+                               ed2->priv.server->ode_joint_enemy = 0;
+                       }
+                       if(b2 == (dBodyID)ed->priv.server->ode_body)
+                       {
+                               b2 = 0;
+                               ed2->priv.server->ode_joint_aiment = 0;
+                       }
+                       dJointAttach(j, b1, b2);
+               }
                dBodyDestroy((dBodyID)ed->priv.server->ode_body);
+       }
        ed->priv.server->ode_body = NULL;
+#endif
        if (ed->priv.server->ode_vertex3f)
                Mem_Free(ed->priv.server->ode_vertex3f);
        ed->priv.server->ode_vertex3f = NULL;
@@ -1527,7 +1573,6 @@ 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;
-#endif
 }
 
 #ifdef USEODE
@@ -1542,16 +1587,36 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
        matrix4x4_t bodymatrix;
        matrix4x4_t entitymatrix;
        prvm_eval_t *val;
+       vec3_t angles;
+       vec3_t avelocity;
        vec3_t forward, left, up;
        vec3_t origin;
        vec3_t spinvelocity;
        vec3_t velocity;
+       int jointtype;
        if (!body)
                return;
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);
        movetype = (int)val->_float;
        if (movetype != MOVETYPE_PHYSICS)
+       {
+               val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.jointtype);if (val) jointtype = (int)val->_float;
+               switch(jointtype)
+               {
+                       // TODO feed back data from physics
+                       case JOINTTYPE_POINT:
+                               break;
+                       case JOINTTYPE_HINGE:
+                               break;
+                       case JOINTTYPE_SLIDER:
+                               break;
+                       case JOINTTYPE_UNIVERSAL:
+                               break;
+                       case JOINTTYPE_HINGE2:
+                               break;
+               }
                return;
+       }
        // store the physics engine data into the entity
        o = dBodyGetPosition(body);
        r = dBodyGetRotation(body);
@@ -1572,13 +1637,136 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed)
        Matrix4x4_FromVectors(&bodymatrix, forward, left, up, origin);
        Matrix4x4_Concat(&entitymatrix, &bodymatrix, &ed->priv.server->ode_offsetimatrix);
        Matrix4x4_ToVectors(&entitymatrix, forward, left, up, origin);
+
+       AnglesFromVectors(angles, forward, up, false);
+       VectorSet(avelocity, RAD2DEG(spinvelocity[PITCH]), RAD2DEG(spinvelocity[ROLL]), RAD2DEG(spinvelocity[YAW]));
+
+       {
+               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;
+       }
+
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.origin);if (val) VectorCopy(origin, val->vector);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_forward);if (val) VectorCopy(forward, val->vector);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_left);if (val) VectorCopy(left, val->vector);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_up);if (val) VectorCopy(up, val->vector);
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.velocity);if (val) VectorCopy(velocity, val->vector);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.spinvelocity);if (val) VectorCopy(spinvelocity, val->vector);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.angles);if (val) AnglesFromVectors(val->vector, forward, up, true);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_forward);if (val) VectorCopy(forward, val->vector);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_left);if (val) VectorCopy(left, val->vector);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_up);if (val) VectorCopy(up, val->vector);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.spinvelocity);if (val) VectorCopy(spinvelocity, val->vector);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.angles);if (val) VectorCopy(angles, val->vector);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.avelocity);if (val) VectorCopy(avelocity, val->vector);
+
+       // values for BodyFromEntity to check if the qc modified anything later
+       VectorCopy(origin, ed->priv.server->ode_origin);
+       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);
+}
+
+static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed)
+{
+       dJointID j = 0;
+       dBodyID b1 = 0;
+       dBodyID b2 = 0;
+       int movetype = 0;
+       int jointtype = 0;
+       int enemy = 0, aiment = 0;
+       vec3_t origin, velocity, angles, forward, left, up;
+       prvm_eval_t *val;
+       VectorClear(origin);
+       VectorClear(velocity);
+       VectorClear(angles);
+       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;
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.aiment);if (val) aiment = val->_int;
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.origin);if (val) VectorCopy(val->vector, origin);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.velocity);if (val) VectorCopy(val->vector, velocity);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.angles);if (val) VectorCopy(val->vector, angles);
+       if(movetype == MOVETYPE_PHYSICS)
+               jointtype = 0; // can't have both
+       if(enemy <= 0 || enemy >= prog->num_edicts || prog->edicts[enemy].priv.required->free || prog->edicts[enemy].priv.server->ode_body == 0)
+               enemy = 0;
+       if(aiment <= 0 || aiment >= prog->num_edicts || prog->edicts[aiment].priv.required->free || prog->edicts[aiment].priv.server->ode_body == 0)
+               aiment = 0;
+       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)
+               return; // nothing to do
+       AngleVectorsFLU(angles, forward, left, up);
+       switch(jointtype)
+       {
+               case JOINTTYPE_POINT:
+                       j = dJointCreateBall(world->physics.ode_world, 0);
+                       break;
+               case JOINTTYPE_HINGE:
+                       j = dJointCreateHinge(world->physics.ode_world, 0);
+                       break;
+               case JOINTTYPE_SLIDER:
+                       j = dJointCreateSlider(world->physics.ode_world, 0);
+                       break;
+               case JOINTTYPE_UNIVERSAL:
+                       j = dJointCreateUniversal(world->physics.ode_world, 0);
+                       break;
+               case JOINTTYPE_HINGE2:
+                       j = dJointCreateHinge2(world->physics.ode_world, 0);
+                       break;
+               case 0:
+               default:
+                       // no joint
+                       j = 0;
+                       break;
+       }
+       ed->priv.server->ode_joint = (void *) j;
+       ed->priv.server->ode_joint_type = jointtype;
+       ed->priv.server->ode_joint_enemy = enemy;
+       ed->priv.server->ode_joint_aiment = aiment;
+       VectorCopy(origin, ed->priv.server->ode_joint_origin);
+       VectorCopy(velocity, ed->priv.server->ode_joint_velocity);
+       VectorCopy(angles, ed->priv.server->ode_joint_angles);
+       if(j)
+       {
+               dJointSetData(j, (void *) ed);
+               if(enemy)
+                       b1 = (dBodyID)prog->edicts[enemy].priv.server->ode_body;
+               if(aiment)
+                       b2 = (dBodyID)prog->edicts[aiment].priv.server->ode_body;
+               dJointAttach(j, b1, b2);
+               switch(jointtype)
+               {
+                       case JOINTTYPE_POINT:
+                               dJointSetBallAnchor(j, origin[0], origin[1], origin[2]);
+                               break;
+                       case JOINTTYPE_HINGE:
+                               dJointSetHingeAnchor(j, origin[0], origin[1], origin[2]);
+                               dJointSetHingeAxis(j, forward[0], forward[1], forward[2]);
+                               break;
+                       case JOINTTYPE_SLIDER:
+                               dJointSetSliderAxis(j, forward[0], forward[1], forward[2]);
+                               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]);
+                               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]);
+                               break;
+                       case 0:
+                       default:
+                               Host_Error("what? but above the joint was valid...\n");
+                               break;
+               }
+       }
 }
 
 static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
@@ -1595,14 +1783,15 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        int *oe;
        int axisindex;
        int modelindex = 0;
-       int movetype;
+       int movetype = MOVETYPE_NONE;
        int numtriangles;
        int numvertices;
-       int solid;
+       int solid = SOLID_NOT;
        int triangleindex;
        int vertexindex;
        mempool_t *mempool;
        prvm_eval_t *val;
+       qboolean modified = false;
        vec3_t angles;
        vec3_t avelocity;
        vec3_t entmaxs;
@@ -1620,20 +1809,23 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        vec_t massval = 1.0f;
        vec_t movelimit;
        vec_t radius;
+       vec_t scale = 1.0f;
        vec_t spinlimit;
-#ifndef ODE_STATIC
+       qboolean gravity;
+#ifdef ODE_DYNAMIC
        if (!ode_dll)
                return;
 #endif
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.solid);
-       solid = (int)val->_float;
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);
-       movetype = (int)val->_float;
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.solid);if (val) solid = (int)val->_float;
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);if (val) movetype = (int)val->_float;
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype);if (val && val->_float) scale = val->_float;
+       modelindex = 0;
        switch(solid)
        {
        case SOLID_BSP:
                val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.modelindex);
-               modelindex = (int)val->_float;
+               if (val)
+                       modelindex = (int)val->_float;
                if (world == &sv.world && modelindex >= 1 && modelindex < MAX_MODELS)
                {
                        model = sv.models[modelindex];
@@ -1652,8 +1844,8 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                }
                if (model)
                {
-                       VectorCopy(model->normalmins, entmins);
-                       VectorCopy(model->normalmaxs, entmaxs);
+                       VectorScale(model->normalmins, scale, entmins);
+                       VectorScale(model->normalmaxs, scale, entmaxs);
                        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.mass);if (val) massval = val->_float;
                }
                else
@@ -1697,6 +1889,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
         || ed->priv.server->ode_mass != massval
         || ed->priv.server->ode_modelindex != modelindex)
        {
+               modified = true;
                World_Physics_RemoveFromEntity(world, ed);
                ed->priv.server->ode_physics = true;
                VectorCopy(entmins, ed->priv.server->ode_mins);
@@ -1761,8 +1954,6 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        ed->priv.server->ode_geom = (void *)dCreateTriMesh(world->physics.ode_space, dataID, NULL, NULL, NULL);
                        dGeomSetBody(ed->priv.server->ode_geom, body);
                        dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
-                       dBodySetMass(body, &mass);
-                       dBodySetData(body, (void*)ed);
                        break;
                case SOLID_BBOX:
                case SOLID_SLIDEBOX:
@@ -1774,7 +1965,6 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        dMassSetBoxTotal(&mass, massval, geomsize[0], geomsize[1], geomsize[2]);
                        dGeomSetBody(ed->priv.server->ode_geom, body);
                        dBodySetMass(body, &mass);
-                       dBodySetData(body, (void*)ed);
                        break;
                case SOLID_PHYSICS_SPHERE:
                        Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]);
@@ -1812,7 +2002,6 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                        dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length);
                        dGeomSetBody(ed->priv.server->ode_geom, body);
                        dBodySetMass(body, &mass);
-                       dBodySetData(body, (void*)ed);
                        break;
                default:
                        Sys_Error("World_Physics_BodyFromEntity: unrecognized solid value %i was accepted by filter\n", solid);
@@ -1822,36 +2011,41 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
 
        // get current data from entity
        VectorClear(origin);
-       VectorClear(forward);
-       VectorClear(left);
-       VectorClear(up);
        VectorClear(velocity);
-       VectorClear(spinvelocity);
+       //VectorClear(forward);
+       //VectorClear(left);
+       //VectorClear(up);
+       //VectorClear(spinvelocity);
+       VectorClear(angles);
+       VectorClear(avelocity);
+       gravity = true;
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.origin);if (val) VectorCopy(val->vector, origin);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_forward);if (val) VectorCopy(val->vector, forward);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_left);if (val) VectorCopy(val->vector, left);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_up);if (val) VectorCopy(val->vector, up);
        val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.velocity);if (val) VectorCopy(val->vector, velocity);
-       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.spinvelocity);if (val) VectorCopy(val->vector, spinvelocity);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_forward);if (val) VectorCopy(val->vector, forward);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_left);if (val) VectorCopy(val->vector, left);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.axis_up);if (val) VectorCopy(val->vector, up);
+       //val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.spinvelocity);if (val) VectorCopy(val->vector, spinvelocity);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.angles);if (val) VectorCopy(val->vector, angles);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.avelocity);if (val) VectorCopy(val->vector, avelocity);
+       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.gravity);if (val) { if(val->_float != 0.0f && val->_float < 0.5f) gravity = false; }
+       if(ed == prog->edicts)
+               gravity = false;
 
        // compatibility for legacy entities
-       switch (solid)
+       //if (!VectorLength2(forward) || solid == SOLID_BSP)
        {
-       case SOLID_BSP:
-               //VectorClear(velocity);
-               VectorClear(angles);
-               VectorClear(avelocity);
-               val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.angles);if (val) VectorCopy(val->vector, angles);
-               val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.velocity);if (val) VectorCopy(val->vector, avelocity);
                AngleVectorsFLU(angles, forward, left, up);
                // convert single-axis rotations in avelocity to spinvelocity
                // FIXME: untested math - check signs
-               VectorSet(spinvelocity, avelocity[PITCH] * ((float)M_PI / 180.0f), avelocity[ROLL] * ((float)M_PI / 180.0f), avelocity[YAW] * ((float)M_PI / 180.0f));
-               break;
+               VectorSet(spinvelocity, DEG2RAD(avelocity[PITCH]), DEG2RAD(avelocity[ROLL]), DEG2RAD(avelocity[YAW]));
+       }
+
+       // compatibility for legacy entities
+       switch (solid)
+       {
        case SOLID_BBOX:
        case SOLID_SLIDEBOX:
        case SOLID_CORPSE:
-               //VectorClear(velocity);
                VectorSet(forward, 1, 0, 0);
                VectorSet(left, 0, 1, 0);
                VectorSet(up, 0, 0, 1);
@@ -1859,17 +2053,21 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                break;
        }
 
+
        // we must prevent NANs...
        test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity);
        if (IS_NAN(test))
        {
-               Con_Printf("Fixing NAN values on entity %i : .classname = \"%s\" .origin = '%f %f %f' .axis_forward = '%f %f %f' .axis_left = '%f %f %f' .axis_up = '%f %f %f' .velocity = '%f %f %f' .spinvelocity = '%f %f %f'\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string), origin[0], origin[1], origin[2], forward[0], forward[1], forward[2], left[0], left[1], left[2], up[0], up[1], up[2], velocity[0], velocity[1], velocity[2], spinvelocity[0], spinvelocity[1], spinvelocity[2]);
+               modified = true;
+               //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_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string), 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]);
+               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(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], angles[0], angles[1], angles[2], avelocity[0], avelocity[1], avelocity[2]);
                test = VectorLength2(origin);
                if (IS_NAN(test))
                        VectorClear(origin);
                test = VectorLength2(forward) * VectorLength2(left) * VectorLength2(up);
                if (IS_NAN(test))
                {
+                       VectorSet(angles, 0, 0, 0);
                        VectorSet(forward, 1, 0, 0);
                        VectorSet(left, 0, 1, 0);
                        VectorSet(up, 0, 0, 1);
@@ -1880,6 +2078,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                test = VectorLength2(spinvelocity);
                if (IS_NAN(test))
                {
+                       VectorClear(avelocity);
                        VectorClear(spinvelocity);
                }
        }
@@ -1889,10 +2088,12 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        test = VectorLength2(velocity);
        if (test > movelimit*movelimit)
        {
+               modified = true;
                // scale down linear velocity to the movelimit
                // scale down angular velocity the same amount for consistency
                f = movelimit / sqrt(test);
                VectorScale(velocity, f, velocity);
+               VectorScale(avelocity, f, avelocity);
                VectorScale(spinvelocity, f, spinvelocity);
        }
 
@@ -1900,15 +2101,42 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
        spinlimit = physics_ode_spinlimit.value;
        test = VectorLength2(spinvelocity);
        if (test > spinlimit)
+       {
+               modified = true;
+               VectorClear(avelocity);
                VectorClear(spinvelocity);
+       }
+
+       // check if the qc edited any position data
+       if (!VectorCompare(origin, ed->priv.server->ode_origin)
+        || !VectorCompare(velocity, ed->priv.server->ode_velocity)
+        || !VectorCompare(angles, ed->priv.server->ode_angles)
+        || !VectorCompare(avelocity, ed->priv.server->ode_avelocity)
+        || gravity != ed->priv.server->ode_gravity)
+               modified = true;
 
-       // store the values into the physics engine
+       // store the qc values into the physics engine
        body = ed->priv.server->ode_body;
-       if (body)
+       if (body && modified)
        {
                dVector3 r[3];
                matrix4x4_t entitymatrix;
                matrix4x4_t bodymatrix;
+
+               {
+                       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);
@@ -1926,6 +2154,8 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed)
                dBodySetRotation(body, r[0]);
                dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]);
                dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]);
+               dBodySetGravityMode(body, gravity);
+               dBodySetData(body, (void*)ed);
                // setting body to NULL makes an immovable object
                if (movetype != MOVETYPE_PHYSICS)
                        dGeomSetBody(ed->priv.server->ode_geom, 0);
@@ -1942,6 +2172,13 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
        dJointID c;
        int i;
        int numcontacts;
+       prvm_eval_t *val;
+       float bouncefactor1 = 0.0f;
+       float bouncestop1 = 60.0f / 800.0f;
+       float bouncefactor2 = 0.0f;
+       float bouncestop2 = 60.0f / 800.0f;
+       dVector3 grav;
+       prvm_edict_t *ed;
 
        if (dGeomIsSpace(o1) || dGeomIsSpace(o2))
        {
@@ -1965,15 +2202,67 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2)
        if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact))
                return;
 
+       if(b1)
+       {
+               ed = (prvm_edict_t *) dBodyGetData(b1);
+               if(ed)
+               {
+                       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.bouncefactor);
+                       if (val!=0 && val->_float)
+                               bouncefactor1 = val->_float;
+
+                       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.bouncestop);
+                       if (val!=0 && val->_float)
+                               bouncestop1 = val->_float;
+               }
+       }
+
+       if(b2)
+       {
+               ed = (prvm_edict_t *) dBodyGetData(b2);
+               if(ed)
+               {
+                       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.bouncefactor);
+                       if (val!=0 && val->_float)
+                               bouncefactor2 = val->_float;
+
+                       val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.bouncestop);
+                       if (val!=0 && val->_float)
+                               bouncestop2 = val->_float;
+               }
+       }
+
+       // merge bounce factors and bounce stop
+       if(bouncefactor2 > 0)
+       {
+               if(bouncefactor1 > 0)
+               {
+                       // TODO possibly better logic to merge bounce factor data?
+                       if(bouncestop2 < bouncestop1)
+                               bouncestop1 = bouncestop2;
+                       if(bouncefactor2 > bouncefactor1)
+                               bouncefactor1 = bouncefactor2;
+               }
+               else
+               {
+                       bouncestop1 = bouncestop2;
+                       bouncefactor1 = bouncefactor2;
+               }
+       }
+       dWorldGetGravity(world->physics.ode_world, grav);
+       bouncestop1 *= fabs(grav[2]);
+
        // generate contact points between the two non-space geoms
        numcontacts = dCollide(o1, o2, MAX_CONTACTS, &(contact[0].geom), sizeof(contact[0]));
        // add these contact points to the simulation
        for (i = 0;i < numcontacts;i++)
        {
-               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);
+               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);
                contact[i].surface.mu = physics_ode_contact_mu.value;
                contact[i].surface.soft_erp = physics_ode_contact_erp.value;
                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);
                dJointAttach(c, b1, b2);
        }
@@ -1990,9 +2279,15 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity)
 
                // copy physics properties from entities to physics engine
                if (prog)
+               {
                        for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
                                if (!prog->edicts[i].priv.required->free)
                                        World_Physics_Frame_BodyFromEntity(world, ed);
+                       // oh, and it must be called after all bodies were created
+                       for (i = 0, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++)
+                               if (!prog->edicts[i].priv.required->free)
+                                       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;