X-Git-Url: http://de.git.xonotic.org/?a=blobdiff_plain;f=world.c;h=f705982ea63237f82e0bf9148dc5193977520601;hb=fcdaa4edcc329da6cebc246e0e2db876603f0e05;hp=a6941b4ad8aa115bb758b65164d66c12161c810e;hpb=fccf54908ef68ea026dc0e01cde077b0e3004d04;p=xonotic%2Fdarkplaces.git diff --git a/world.c b/world.c index a6941b4a..f705982e 100644 --- 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" /* @@ -112,16 +113,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]; @@ -130,8 +131,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); } /* @@ -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 @@ -367,6 +371,8 @@ struct dxJointNode; struct dxJointGroup; struct dxTriMeshData; +#define dInfinity 3.402823466e+38f + typedef struct dxWorld *dWorldID; typedef struct dxSpace *dSpaceID; typedef struct dxBody *dBodyID; @@ -404,6 +410,49 @@ typedef enum dJointType } dJointType; +#define D_ALL_PARAM_NAMES(start) \ + /* parameters for limits and motors */ \ + dParamLoStop = start, \ + dParamHiStop, \ + dParamVel, \ + dParamFMax, \ + dParamFudgeFactor, \ + dParamBounce, \ + dParamCFM, \ + dParamStopERP, \ + dParamStopCFM, \ + /* parameters for suspension */ \ + dParamSuspensionERP, \ + dParamSuspensionCFM, \ + dParamERP, \ + +#define D_ALL_PARAM_NAMES_X(start,x) \ + /* parameters for limits and motors */ \ + dParamLoStop ## x = start, \ + dParamHiStop ## x, \ + dParamVel ## x, \ + dParamFMax ## x, \ + dParamFudgeFactor ## x, \ + dParamBounce ## x, \ + dParamCFM ## x, \ + dParamStopERP ## x, \ + dParamStopCFM ## x, \ + /* parameters for suspension */ \ + dParamSuspensionERP ## x, \ + dParamSuspensionCFM ## x, \ + dParamERP ## x, + +enum { + D_ALL_PARAM_NAMES(0) + D_ALL_PARAM_NAMES_X(0x100,2) + D_ALL_PARAM_NAMES_X(0x200,3) + + /* add a multiple of this constant to the basic parameter numbers to get + * the parameters for the second, third etc axes. + */ + dParamGroup=0x100 +}; + typedef struct dMass { dReal mass; @@ -476,12 +525,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); +//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); +//int (ODE_API *dAllocateODEDataForThread)(unsigned int uiAllocateFlags); +//void (ODE_API *dCleanupODEAllDataForThread)(void); void (ODE_API *dCloseODE)(void); //int (ODE_API *dMassCheck)(const dMass *m); @@ -505,10 +554,10 @@ 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 *dWorldSetERP)(dWorldID, dReal erp); +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); +void (ODE_API *dWorldSetCFM)(dWorldID, dReal cfm); //dReal (ODE_API *dWorldGetCFM)(dWorldID); void (ODE_API *dWorldStep)(dWorldID, dReal stepsize); //void (ODE_API *dWorldImpulseToForce)(dWorldID, dReal stepsize, dReal ix, dReal iy, dReal iz, dVector3 force); @@ -568,7 +617,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); @@ -606,16 +655,16 @@ void (ODE_API *dBodySetMass)(dBodyID, const dMass *mass); //void (ODE_API *dBodySetFiniteRotationAxis)(dBodyID, dReal x, dReal y, dReal z); //int (ODE_API *dBodyGetFiniteRotationMode)(dBodyID); //void (ODE_API *dBodyGetFiniteRotationAxis)(dBodyID, dVector3 result); -//int (ODE_API *dBodyGetNumJoints)(dBodyID b); -//dJointID (ODE_API *dBodyGetJoint)(dBodyID, int index); +int (ODE_API *dBodyGetNumJoints)(dBodyID b); +dJointID (ODE_API *dBodyGetJoint)(dBodyID, int index); //void (ODE_API *dBodySetDynamic)(dBodyID); //void (ODE_API *dBodySetKinematic)(dBodyID); //int (ODE_API *dBodyIsKinematic)(dBodyID); //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); @@ -633,21 +682,21 @@ void (ODE_API *dBodySetMass)(dBodyID, const dMass *mass); //void (ODE_API *dBodySetMaxAngularSpeed)(dBodyID b, dReal max_speed); //int (ODE_API *dBodyGetGyroscopicMode)(dBodyID b); //void (ODE_API *dBodySetGyroscopicMode)(dBodyID b, int enabled); -//dJointID (ODE_API *dJointCreateBall)(dWorldID, dJointGroupID); -//dJointID (ODE_API *dJointCreateHinge)(dWorldID, dJointGroupID); -//dJointID (ODE_API *dJointCreateSlider)(dWorldID, dJointGroupID); +dJointID (ODE_API *dJointCreateBall)(dWorldID, dJointGroupID); +dJointID (ODE_API *dJointCreateHinge)(dWorldID, dJointGroupID); +dJointID (ODE_API *dJointCreateSlider)(dWorldID, dJointGroupID); dJointID (ODE_API *dJointCreateContact)(dWorldID, dJointGroupID, const dContact *); -//dJointID (ODE_API *dJointCreateHinge2)(dWorldID, dJointGroupID); -//dJointID (ODE_API *dJointCreateUniversal)(dWorldID, dJointGroupID); +dJointID (ODE_API *dJointCreateHinge2)(dWorldID, dJointGroupID); +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); //dJointID (ODE_API *dJointCreatePlane2D)(dWorldID, dJointGroupID); -//void (ODE_API *dJointDestroy)(dJointID); +void (ODE_API *dJointDestroy)(dJointID); dJointGroupID (ODE_API *dJointGroupCreate)(int max_size); void (ODE_API *dJointGroupDestroy)(dJointGroupID); void (ODE_API *dJointGroupEmpty)(dJointGroupID); @@ -656,36 +705,36 @@ void (ODE_API *dJointAttach)(dJointID, dBodyID body1, dBodyID body2); //void (ODE_API *dJointEnable)(dJointID); //void (ODE_API *dJointDisable)(dJointID); //int (ODE_API *dJointIsEnabled)(dJointID); -//void (ODE_API *dJointSetData)(dJointID, void *data); -//void * (ODE_API *dJointGetData)(dJointID); +void (ODE_API *dJointSetData)(dJointID, void *data); +void * (ODE_API *dJointGetData)(dJointID); //dJointType (ODE_API *dJointGetType)(dJointID); -//dBodyID (ODE_API *dJointGetBody)(dJointID, int index); +dBodyID (ODE_API *dJointGetBody)(dJointID, int index); //void (ODE_API *dJointSetFeedback)(dJointID, dJointFeedback *); //dJointFeedback *(ODE_API *dJointGetFeedback)(dJointID); -//void (ODE_API *dJointSetBallAnchor)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetBallAnchor)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetBallAnchor2)(dJointID, dReal x, dReal y, dReal z); -//void (ODE_API *dJointSetBallParam)(dJointID, int parameter, dReal value); -//void (ODE_API *dJointSetHingeAnchor)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetBallParam)(dJointID, int parameter, dReal value); +void (ODE_API *dJointSetHingeAnchor)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetHingeAnchorDelta)(dJointID, dReal x, dReal y, dReal z, dReal ax, dReal ay, dReal az); -//void (ODE_API *dJointSetHingeAxis)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetHingeAxis)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetHingeAxisOffset)(dJointID j, dReal x, dReal y, dReal z, dReal angle); -//void (ODE_API *dJointSetHingeParam)(dJointID, int parameter, dReal value); +void (ODE_API *dJointSetHingeParam)(dJointID, int parameter, dReal value); //void (ODE_API *dJointAddHingeTorque)(dJointID joint, dReal torque); -//void (ODE_API *dJointSetSliderAxis)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetSliderAxis)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetSliderAxisDelta)(dJointID, dReal x, dReal y, dReal z, dReal ax, dReal ay, dReal az); -//void (ODE_API *dJointSetSliderParam)(dJointID, int parameter, dReal value); +void (ODE_API *dJointSetSliderParam)(dJointID, int parameter, dReal value); //void (ODE_API *dJointAddSliderForce)(dJointID joint, dReal force); -//void (ODE_API *dJointSetHinge2Anchor)(dJointID, dReal x, dReal y, dReal z); -//void (ODE_API *dJointSetHinge2Axis1)(dJointID, dReal x, dReal y, dReal z); -//void (ODE_API *dJointSetHinge2Axis2)(dJointID, dReal x, dReal y, dReal z); -//void (ODE_API *dJointSetHinge2Param)(dJointID, int parameter, dReal value); +void (ODE_API *dJointSetHinge2Anchor)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetHinge2Axis1)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetHinge2Axis2)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetHinge2Param)(dJointID, int parameter, dReal value); //void (ODE_API *dJointAddHinge2Torques)(dJointID joint, dReal torque1, dReal torque2); -//void (ODE_API *dJointSetUniversalAnchor)(dJointID, dReal x, dReal y, dReal z); -//void (ODE_API *dJointSetUniversalAxis1)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetUniversalAnchor)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetUniversalAxis1)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetUniversalAxis1Offset)(dJointID, dReal x, dReal y, dReal z, dReal offset1, dReal offset2); -//void (ODE_API *dJointSetUniversalAxis2)(dJointID, dReal x, dReal y, dReal z); +void (ODE_API *dJointSetUniversalAxis2)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetUniversalAxis2Offset)(dJointID, dReal x, dReal y, dReal z, dReal offset1, dReal offset2); -//void (ODE_API *dJointSetUniversalParam)(dJointID, int parameter, dReal value); +void (ODE_API *dJointSetUniversalParam)(dJointID, int parameter, dReal value); //void (ODE_API *dJointAddUniversalTorques)(dJointID joint, dReal torque1, dReal torque2); //void (ODE_API *dJointSetPRAnchor)(dJointID, dReal x, dReal y, dReal z); //void (ODE_API *dJointSetPRAxis1)(dJointID, dReal x, dReal y, dReal z); @@ -797,7 +846,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); @@ -816,11 +865,11 @@ void (ODE_API *dSpaceDestroy)(dSpaceID); //int (ODE_API *dSpaceGetClass)(dSpaceID space); // void (ODE_API *dGeomDestroy)(dGeomID geom); -//void (ODE_API *dGeomSetData)(dGeomID geom, void* data); -//void * (ODE_API *dGeomGetData)(dGeomID geom); +void (ODE_API *dGeomSetData)(dGeomID geom, void* data); +void * (ODE_API *dGeomGetData)(dGeomID geom); void (ODE_API *dGeomSetBody)(dGeomID geom, dBodyID body); dBodyID (ODE_API *dGeomGetBody)(dGeomID geom); -//void (ODE_API *dGeomSetPosition)(dGeomID geom, dReal x, dReal y, dReal z); +void (ODE_API *dGeomSetPosition)(dGeomID geom, dReal x, dReal y, dReal z); void (ODE_API *dGeomSetRotation)(dGeomID geom, const dMatrix3 R); //void (ODE_API *dGeomSetQuaternion)(dGeomID geom, const dQuaternion Q); //const dReal * (ODE_API *dGeomGetPosition)(dGeomID geom); @@ -942,12 +991,12 @@ dGeomID (ODE_API *dCreateTriMesh)(dSpaceID space, dTriMeshDataID Data, d static dllfunction_t odefuncs[] = { - {"dGetConfiguration", (void **) &dGetConfiguration}, +// {"dGetConfiguration", (void **) &dGetConfiguration}, {"dCheckConfiguration", (void **) &dCheckConfiguration}, {"dInitODE", (void **) &dInitODE}, // {"dInitODE2", (void **) &dInitODE2}, - {"dAllocateODEDataForThread", (void **) &dAllocateODEDataForThread}, - {"dCleanupODEAllDataForThread", (void **) &dCleanupODEAllDataForThread}, +// {"dAllocateODEDataForThread", (void **) &dAllocateODEDataForThread}, +// {"dCleanupODEAllDataForThread", (void **) &dCleanupODEAllDataForThread}, {"dCloseODE", (void **) &dCloseODE}, // {"dMassCheck", (void **) &dMassCheck}, // {"dMassSetZero", (void **) &dMassSetZero}, @@ -970,10 +1019,10 @@ static dllfunction_t odefuncs[] = {"dWorldCreate", (void **) &dWorldCreate}, {"dWorldDestroy", (void **) &dWorldDestroy}, {"dWorldSetGravity", (void **) &dWorldSetGravity}, -// {"dWorldGetGravity", (void **) &dWorldGetGravity}, -// {"dWorldSetERP", (void **) &dWorldSetERP}, + {"dWorldGetGravity", (void **) &dWorldGetGravity}, + {"dWorldSetERP", (void **) &dWorldSetERP}, // {"dWorldGetERP", (void **) &dWorldGetERP}, -// {"dWorldSetCFM", (void **) &dWorldSetCFM}, + {"dWorldSetCFM", (void **) &dWorldSetCFM}, // {"dWorldGetCFM", (void **) &dWorldGetCFM}, {"dWorldStep", (void **) &dWorldStep}, // {"dWorldImpulseToForce", (void **) &dWorldImpulseToForce}, @@ -1033,7 +1082,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}, @@ -1071,16 +1120,16 @@ static dllfunction_t odefuncs[] = // {"dBodySetFiniteRotationAxis", (void **) &dBodySetFiniteRotationAxis}, // {"dBodyGetFiniteRotationMode", (void **) &dBodyGetFiniteRotationMode}, // {"dBodyGetFiniteRotationAxis", (void **) &dBodyGetFiniteRotationAxis}, -// {"dBodyGetNumJoints", (void **) &dBodyGetNumJoints}, -// {"dBodyGetJoint", (void **) &dBodyGetJoint}, + {"dBodyGetNumJoints", (void **) &dBodyGetNumJoints}, + {"dBodyGetJoint", (void **) &dBodyGetJoint}, // {"dBodySetDynamic", (void **) &dBodySetDynamic}, // {"dBodySetKinematic", (void **) &dBodySetKinematic}, // {"dBodyIsKinematic", (void **) &dBodyIsKinematic}, // {"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}, @@ -1098,21 +1147,21 @@ static dllfunction_t odefuncs[] = // {"dBodySetMaxAngularSpeed", (void **) &dBodySetMaxAngularSpeed}, // {"dBodyGetGyroscopicMode", (void **) &dBodyGetGyroscopicMode}, // {"dBodySetGyroscopicMode", (void **) &dBodySetGyroscopicMode}, -// {"dJointCreateBall", (void **) &dJointCreateBall}, -// {"dJointCreateHinge", (void **) &dJointCreateHinge}, -// {"dJointCreateSlider", (void **) &dJointCreateSlider}, + {"dJointCreateBall", (void **) &dJointCreateBall}, + {"dJointCreateHinge", (void **) &dJointCreateHinge}, + {"dJointCreateSlider", (void **) &dJointCreateSlider}, {"dJointCreateContact", (void **) &dJointCreateContact}, -// {"dJointCreateHinge2", (void **) &dJointCreateHinge2}, -// {"dJointCreateUniversal", (void **) &dJointCreateUniversal}, + {"dJointCreateHinge2", (void **) &dJointCreateHinge2}, + {"dJointCreateUniversal", (void **) &dJointCreateUniversal}, // {"dJointCreatePR", (void **) &dJointCreatePR}, // {"dJointCreatePU", (void **) &dJointCreatePU}, // {"dJointCreatePiston", (void **) &dJointCreatePiston}, -// {"dJointCreateFixed", (void **) &dJointCreateFixed}, + {"dJointCreateFixed", (void **) &dJointCreateFixed}, // {"dJointCreateNull", (void **) &dJointCreateNull}, // {"dJointCreateAMotor", (void **) &dJointCreateAMotor}, // {"dJointCreateLMotor", (void **) &dJointCreateLMotor}, // {"dJointCreatePlane2D", (void **) &dJointCreatePlane2D}, -// {"dJointDestroy", (void **) &dJointDestroy}, + {"dJointDestroy", (void **) &dJointDestroy}, {"dJointGroupCreate", (void **) &dJointGroupCreate}, {"dJointGroupDestroy", (void **) &dJointGroupDestroy}, {"dJointGroupEmpty", (void **) &dJointGroupEmpty}, @@ -1121,36 +1170,36 @@ static dllfunction_t odefuncs[] = // {"dJointEnable", (void **) &dJointEnable}, // {"dJointDisable", (void **) &dJointDisable}, // {"dJointIsEnabled", (void **) &dJointIsEnabled}, -// {"dJointSetData", (void **) &dJointSetData}, -// {"dJointGetData", (void **) &dJointGetData}, + {"dJointSetData", (void **) &dJointSetData}, + {"dJointGetData", (void **) &dJointGetData}, // {"dJointGetType", (void **) &dJointGetType}, -// {"dJointGetBody", (void **) &dJointGetBody}, + {"dJointGetBody", (void **) &dJointGetBody}, // {"dJointSetFeedback", (void **) &dJointSetFeedback}, // {"dJointGetFeedback", (void **) &dJointGetFeedback}, -// {"dJointSetBallAnchor", (void **) &dJointSetBallAnchor}, + {"dJointSetBallAnchor", (void **) &dJointSetBallAnchor}, // {"dJointSetBallAnchor2", (void **) &dJointSetBallAnchor2}, -// {"dJointSetBallParam", (void **) &dJointSetBallParam}, -// {"dJointSetHingeAnchor", (void **) &dJointSetHingeAnchor}, + {"dJointSetBallParam", (void **) &dJointSetBallParam}, + {"dJointSetHingeAnchor", (void **) &dJointSetHingeAnchor}, // {"dJointSetHingeAnchorDelta", (void **) &dJointSetHingeAnchorDelta}, -// {"dJointSetHingeAxis", (void **) &dJointSetHingeAxis}, + {"dJointSetHingeAxis", (void **) &dJointSetHingeAxis}, // {"dJointSetHingeAxisOffset", (void **) &dJointSetHingeAxisOffset}, -// {"dJointSetHingeParam", (void **) &dJointSetHingeParam}, + {"dJointSetHingeParam", (void **) &dJointSetHingeParam}, // {"dJointAddHingeTorque", (void **) &dJointAddHingeTorque}, -// {"dJointSetSliderAxis", (void **) &dJointSetSliderAxis}, + {"dJointSetSliderAxis", (void **) &dJointSetSliderAxis}, // {"dJointSetSliderAxisDelta", (void **) &dJointSetSliderAxisDelta}, -// {"dJointSetSliderParam", (void **) &dJointSetSliderParam}, + {"dJointSetSliderParam", (void **) &dJointSetSliderParam}, // {"dJointAddSliderForce", (void **) &dJointAddSliderForce}, -// {"dJointSetHinge2Anchor", (void **) &dJointSetHinge2Anchor}, -// {"dJointSetHinge2Axis1", (void **) &dJointSetHinge2Axis1}, -// {"dJointSetHinge2Axis2", (void **) &dJointSetHinge2Axis2}, -// {"dJointSetHinge2Param", (void **) &dJointSetHinge2Param}, + {"dJointSetHinge2Anchor", (void **) &dJointSetHinge2Anchor}, + {"dJointSetHinge2Axis1", (void **) &dJointSetHinge2Axis1}, + {"dJointSetHinge2Axis2", (void **) &dJointSetHinge2Axis2}, + {"dJointSetHinge2Param", (void **) &dJointSetHinge2Param}, // {"dJointAddHinge2Torques", (void **) &dJointAddHinge2Torques}, -// {"dJointSetUniversalAnchor", (void **) &dJointSetUniversalAnchor}, -// {"dJointSetUniversalAxis1", (void **) &dJointSetUniversalAxis1}, + {"dJointSetUniversalAnchor", (void **) &dJointSetUniversalAnchor}, + {"dJointSetUniversalAxis1", (void **) &dJointSetUniversalAxis1}, // {"dJointSetUniversalAxis1Offset", (void **) &dJointSetUniversalAxis1Offset}, -// {"dJointSetUniversalAxis2", (void **) &dJointSetUniversalAxis2}, + {"dJointSetUniversalAxis2", (void **) &dJointSetUniversalAxis2}, // {"dJointSetUniversalAxis2Offset", (void **) &dJointSetUniversalAxis2Offset}, -// {"dJointSetUniversalParam", (void **) &dJointSetUniversalParam}, + {"dJointSetUniversalParam", (void **) &dJointSetUniversalParam}, // {"dJointAddUniversalTorques", (void **) &dJointAddUniversalTorques}, // {"dJointSetPRAnchor", (void **) &dJointSetPRAnchor}, // {"dJointSetPRAxis1", (void **) &dJointSetPRAxis1}, @@ -1261,7 +1310,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}, @@ -1279,11 +1328,11 @@ static dllfunction_t odefuncs[] = // {"dSpaceGetGeom", (void **) &dSpaceGetGeom}, // {"dSpaceGetClass", (void **) &dSpaceGetClass}, {"dGeomDestroy", (void **) &dGeomDestroy}, -// {"dGeomSetData", (void **) &dGeomSetData}, -// {"dGeomGetData", (void **) &dGeomGetData}, + {"dGeomSetData", (void **) &dGeomSetData}, + {"dGeomGetData", (void **) &dGeomGetData}, {"dGeomSetBody", (void **) &dGeomSetBody}, {"dGeomGetBody", (void **) &dGeomGetBody}, -// {"dGeomSetPosition", (void **) &dGeomSetPosition}, + {"dGeomSetPosition", (void **) &dGeomSetPosition}, {"dGeomSetRotation", (void **) &dGeomSetRotation}, // {"dGeomSetQuaternion", (void **) &dGeomSetQuaternion}, // {"dGeomGetPosition", (void **) &dGeomGetPosition}, @@ -1397,12 +1446,10 @@ 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) - "libode1_64.dll", -# elif defined(WIN32) +# if defined(WIN32) "libode1.dll", # elif defined(MACOSX) "libode.1.dylib", @@ -1422,18 +1469,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 { dInitODE(); // dInitODE2(0); -#ifndef ODE_STATIC +#ifdef ODE_DYNAMIC # ifdef dSINGLE if (!dCheckConfiguration("ODE_single_precision")) # else @@ -1456,12 +1505,12 @@ 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 +#ifdef ODE_DYNAMIC Sys_UnloadLibrary(&ode_dll); ode_dll = NULL; #endif @@ -1475,7 +1524,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 @@ -1485,7 +1534,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 @@ -1511,17 +1563,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 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; + prvm_edict_t *ed2; + 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; @@ -1530,7 +1616,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; -#endif + if(ed->priv.server->ode_massbuf) + Mem_Free(ed->priv.server->ode_massbuf); + ed->priv.server->ode_massbuf = NULL; } #ifdef USEODE @@ -1545,16 +1633,38 @@ 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; + case JOINTTYPE_FIXED: + break; + } return; + } // store the physics engine data into the entity o = dBodyGetPosition(body); r = dBodyGetRotation(body); @@ -1575,13 +1685,227 @@ 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); + + if(!strcmp(prog->name, "server")) // FIXME some better way? + { + SV_LinkEdict(ed); + SV_LinkEdict_TouchAreaGrid(ed); + } +} + +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, movedir; + vec_t CFM, ERP, FMax, Stop, Vel; + prvm_eval_t *val; + 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; + 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); + val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movedir);if (val) VectorCopy(val->vector, movedir); + 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; + // see http://www.ode.org/old_list_archives/2006-January/017614.html + // 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[1]; + float R = 2.0 * D * sqrt(K); // we assume D is premultiplied by sqrt(sprungMass) + 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; + } + 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); + 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 JOINTTYPE_FIXED: + j = dJointCreateFixed(world->physics.ode_world, 0); + break; + case 0: + default: + // no joint + j = 0; + break; + } + 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); + } + 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); + VectorCopy(movedir, ed->priv.server->ode_joint_movedir); + if(j) + { + //Con_Printf("made new joint %i\n", (int) (ed - prog->edicts)); + 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]); + 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]); + 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]); + 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]); + 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: + Sys_Error("what? but above the joint was valid...\n"); + break; + } +#undef SETPARAMS + + } } static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) @@ -1598,14 +1922,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; @@ -1623,40 +1948,39 @@ 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.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; switch(solid) { case SOLID_BSP: val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.modelindex); - 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 (val) + modelindex = (int)val->_float; + 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) { - 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 @@ -1700,6 +2024,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); @@ -1760,33 +2085,21 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) // 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_body = (void *)(body = dBodyCreate(world->physics.ode_world)); 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: case SOLID_CORPSE: case SOLID_PHYSICS_BOX: Matrix4x4_CreateTranslate(&ed->priv.server->ode_offsetmatrix, geomcenter[0], geomcenter[1], geomcenter[2]); - ed->priv.server->ode_body = (void *)(body = dBodyCreate(world->physics.ode_world)); ed->priv.server->ode_geom = (void *)dCreateBox(world->physics.ode_space, geomsize[0], geomsize[1], geomsize[2]); 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]); - ed->priv.server->ode_body = (void *)(body = dBodyCreate(world->physics.ode_world)); ed->priv.server->ode_geom = (void *)dCreateSphere(world->physics.ode_space, geomsize[0] * 0.5f); dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f); - dGeomSetBody(ed->priv.server->ode_geom, body); - dBodySetMass(body, &mass); - dBodySetData(body, (void*)ed); break; case SOLID_PHYSICS_CAPSULE: axisindex = 0; @@ -1810,51 +2123,95 @@ 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_body = (void *)(body = dBodyCreate(world->physics.ode_world)); ed->priv.server->ode_geom = (void *)dCreateCapsule(world->physics.ode_space, radius, length); 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); } 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); + 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); + dBodySetData(body, (void*)ed); + dBodySetMass(body, (dMass *) ed->priv.server->ode_massbuf); + modified = true; + } + } + else + { + if (ed->priv.server->ode_body != NULL) + { + if(ed->priv.server->ode_geom) + dGeomSetBody(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 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); + 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, 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(qavelocity[PITCH]), DEG2RAD(qavelocity[ROLL]), DEG2RAD(qavelocity[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); @@ -1862,17 +2219,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); @@ -1883,35 +2244,48 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) test = VectorLength2(spinvelocity); if (IS_NAN(test)) { + VectorClear(avelocity); VectorClear(spinvelocity); } } - // limit movement speed to prevent missed collisions at high speed - movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit; - test = VectorLength2(velocity); - if (test > movelimit*movelimit) - { - // 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(spinvelocity, f, spinvelocity); - } - - // make sure the angular velocity is not exploding - spinlimit = physics_ode_spinlimit.value; - test = VectorLength2(spinvelocity); - if (test > spinlimit) - 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 (modified && ed->priv.server->ode_geom) { dVector3 r[3]; matrix4x4_t entitymatrix; matrix4x4_t bodymatrix; + +#if 0 + Con_Printf("entity %i got changed by QC\n", (int) (ed - prog->edicts)); + if(!VectorCompare(origin, ed->priv.server->ode_origin)) + 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]); + if(!VectorCompare(velocity, ed->priv.server->ode_velocity)) + 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]); + if(!VectorCompare(angles, ed->priv.server->ode_angles)) + 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]); + if(!VectorCompare(avelocity, ed->priv.server->ode_avelocity)) + 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]); + if(gravity != ed->priv.server->ode_gravity) + Con_Printf(" gravity: %i -> %i\n", ed->priv.server->ode_gravity, gravity); +#endif + + // 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 = gravity; + Matrix4x4_FromVectors(&entitymatrix, forward, left, up, origin); Matrix4x4_Concat(&bodymatrix, &entitymatrix, &ed->priv.server->ode_offsetmatrix); Matrix4x4_ToVectors(&bodymatrix, forward, left, up, origin); @@ -1924,14 +2298,62 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) r[0][2] = up[0]; r[1][2] = up[1]; r[2][2] = up[2]; - dGeomSetBody(ed->priv.server->ode_geom, ed->priv.server->ode_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]); - // setting body to NULL makes an immovable object - if (movetype != MOVETYPE_PHYSICS) - dGeomSetBody(ed->priv.server->ode_geom, 0); + if(body) + { + if(movetype == MOVETYPE_PHYSICS) + { + dGeomSetBody(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); + } + else + { + dGeomSetBody(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(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]); + } + } + + if(body) + { + // limit movement speed to prevent missed collisions at high speed + const dReal *ovelocity = dBodyGetLinearVel(body); + const dReal *ospinvelocity = dBodyGetAngularVel(body); + movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit; + test = VectorLength2(ovelocity); + if (test > movelimit*movelimit) + { + // scale down linear velocity to the movelimit + // scale down angular velocity the same amount for consistency + f = movelimit / sqrt(test); + VectorScale(ovelocity, f, velocity); + VectorScale(ospinvelocity, f, spinvelocity); + dBodySetLinearVel(body, velocity[0], velocity[1], velocity[2]); + dBodySetAngularVel(body, spinvelocity[0], spinvelocity[1], spinvelocity[2]); + } + + // make sure the angular velocity is not exploding + spinlimit = physics_ode_spinlimit.value; + test = VectorLength2(ospinvelocity); + if (test > spinlimit) + { + dBodySetAngularVel(body, 0, 0, 0); + } } } @@ -1945,6 +2367,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 *ed1, *ed2; if (dGeomIsSpace(o1) || dGeomIsSpace(o2)) { @@ -1968,15 +2397,77 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2) if (b1 && b2 && dAreConnectedExcluding(b1, b2, dJointTypeContact)) return; + ed1 = (prvm_edict_t *) dGeomGetData(o1); + if(ed1 && ed1->priv.server->free) + ed1 = NULL; + if(ed1) + { + val = PRVM_EDICTFIELDVALUE(ed1, prog->fieldoffsets.bouncefactor); + if (val!=0 && val->_float) + bouncefactor1 = val->_float; + + val = PRVM_EDICTFIELDVALUE(ed1, prog->fieldoffsets.bouncestop); + if (val!=0 && val->_float) + bouncestop1 = val->_float; + } + + ed2 = (prvm_edict_t *) dGeomGetData(o2); + if(ed2 && ed2->priv.server->free) + ed2 = NULL; + if(ed2) + { + val = PRVM_EDICTFIELDVALUE(ed2, prog->fieldoffsets.bouncefactor); + if (val!=0 && val->_float) + bouncefactor2 = val->_float; + + val = PRVM_EDICTFIELDVALUE(ed2, prog->fieldoffsets.bouncestop); + if (val!=0 && val->_float) + bouncestop2 = val->_float; + } + + if(!strcmp(prog->name, "server")) + { + if(ed1 && ed1->fields.server->touch) + { + SV_LinkEdict_TouchAreaGrid_Call(ed1, ed2 ? ed2 : prog->edicts); + } + if(ed2 && ed2->fields.server->touch) + { + SV_LinkEdict_TouchAreaGrid_Call(ed2, ed1 ? ed1 : prog->edicts); + } + } + + // 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); } @@ -1991,15 +2482,22 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity) 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) + { 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; - world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step; for (i = 0;i < world->physics.ode_iterations;i++) { // set the gravity @@ -2028,7 +2526,7 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity) // copy physics properties from physics engine to entities if (prog) for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++) - if (!prog->edicts[i].priv.required->free && PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype)->_float == MOVETYPE_PHYSICS) + if (!prog->edicts[i].priv.required->free) World_Physics_Frame_BodyToEntity(world, ed); } #endif