X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=world.c;h=ec767e0415335322b39ccf87da497a7f9c12f1e9;hb=b0938d8646e4f35a30cdb945d4107b4488f1450c;hp=adc9558788b645501aa0114e6e06f372cfcf9dc6;hpb=9c5d11b0aa51204ef2d652037b061548f879dff4;p=xonotic%2Fdarkplaces.git diff --git a/world.c b/world.c index adc95587..ec767e04 100644 --- a/world.c +++ b/world.c @@ -173,24 +173,28 @@ void World_UnlinkEdict(prvm_edict_t *ent) } } -int World_EntitiesInBox(world_t *world, const vec3_t mins, const vec3_t maxs, int maxlist, prvm_edict_t **list) +int World_EntitiesInBox(world_t *world, const vec3_t requestmins, const vec3_t requestmaxs, int maxlist, prvm_edict_t **list) { int numlist; link_t *grid; link_t *l; prvm_edict_t *ent; + vec3_t paddedmins, paddedmaxs; int igrid[3], igridmins[3], igridmaxs[3]; + VectorSet(paddedmins, requestmins[0] - 1.0f, requestmins[1] - 1.0f, requestmins[2] - 1.0f); + VectorSet(paddedmaxs, requestmaxs[0] + 1.0f, requestmaxs[1] + 1.0f, requestmaxs[2] + 1.0f); + // FIXME: if areagrid_marknumber wraps, all entities need their // ent->priv.server->areagridmarknumber reset world->areagrid_stats_calls++; world->areagrid_marknumber++; - igridmins[0] = (int) floor((mins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]); - igridmins[1] = (int) floor((mins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]); - //igridmins[2] = (int) ((mins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]); - igridmaxs[0] = (int) floor((maxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1; - igridmaxs[1] = (int) floor((maxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1; - //igridmaxs[2] = (int) ((maxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1; + igridmins[0] = (int) floor((paddedmins[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]); + igridmins[1] = (int) floor((paddedmins[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]); + //igridmins[2] = (int) ((paddedmins[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]); + igridmaxs[0] = (int) floor((paddedmaxs[0] + world->areagrid_bias[0]) * world->areagrid_scale[0]) + 1; + igridmaxs[1] = (int) floor((paddedmaxs[1] + world->areagrid_bias[1]) * world->areagrid_scale[1]) + 1; + //igridmaxs[2] = (int) ((paddedmaxs[2] + world->areagrid_bias[2]) * world->areagrid_scale[2]) + 1; igridmins[0] = max(0, igridmins[0]); igridmins[1] = max(0, igridmins[1]); //igridmins[2] = max(0, igridmins[2]); @@ -198,6 +202,9 @@ int World_EntitiesInBox(world_t *world, const vec3_t mins, const vec3_t maxs, in igridmaxs[1] = min(AREA_GRID, igridmaxs[1]); //igridmaxs[2] = min(AREA_GRID, igridmaxs[2]); + // paranoid debugging + //VectorSet(igridmins, 0, 0, 0);VectorSet(igridmaxs, AREA_GRID, AREA_GRID, AREA_GRID); + numlist = 0; // add entities not linked into areagrid because they are too big or // outside the grid bounds @@ -210,7 +217,7 @@ int World_EntitiesInBox(world_t *world, const vec3_t mins, const vec3_t maxs, in if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber) { ent->priv.server->areagridmarknumber = world->areagrid_marknumber; - if (!ent->priv.server->free && BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs)) + if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs)) { if (numlist < maxlist) list[numlist] = ent; @@ -234,7 +241,7 @@ int World_EntitiesInBox(world_t *world, const vec3_t mins, const vec3_t maxs, in if (ent->priv.server->areagridmarknumber != world->areagrid_marknumber) { ent->priv.server->areagridmarknumber = world->areagrid_marknumber; - if (!ent->priv.server->free && BoxesOverlap(mins, maxs, ent->priv.server->areamins, ent->priv.server->areamaxs)) + if (!ent->priv.server->free && BoxesOverlap(paddedmins, paddedmaxs, ent->priv.server->areamins, ent->priv.server->areamaxs)) { if (numlist < maxlist) list[numlist] = ent; @@ -323,21 +330,37 @@ void World_LinkEdict(world_t *world, prvm_edict_t *ent, const vec3_t mins, const #define USEODE 1 #endif +// recent ODE trunk has dWorldStepFast1 removed +//#define ODE_USE_STEPFAST + #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"}; -cvar_t physics_ode_worldquickstep_iterations = {0, "physics_ode_worldquickstep_iterations","20", "parameter to dWorldQuickStep"}; -cvar_t physics_ode_worldstepfast = {0, "physics_ode_worldstepfast","0", "use dWorldStepFast1 rather than dWorldStep"}; -cvar_t physics_ode_worldstepfast_iterations = {0, "physics_ode_worldstepfast_iterations","20", "parameter to dWorldStepFast1"}; +cvar_t physics_ode_contactsurfacelayer = {0, "physics_ode_contactsurfacelayer","1", "allows objects to overlap this many units to reduce jitter"}; +cvar_t physics_ode_worldstep = {0, "physics_ode_worldstep","2", "step function to use, 0 - dWorldStep, 1 - dWorldStepFast1, 2 - dWorldQuickStep"}; +cvar_t physics_ode_worldstep_iterations = {0, "physics_ode_worldstep_iterations", "20", "parameter to dWorldQuickStep and dWorldStepFast1"}; 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_world_damping = {0, "physics_ode_world_damping", "1", "enabled damping scale (see ODE User Guide), this scales all damping values, be aware that behavior depends of step type"}; +cvar_t physics_ode_world_damping_linear = {0, "physics_ode_world_damping_linear", "0.005", "world linear damping scale (see ODE User Guide); use defaults when set to -1"}; +cvar_t physics_ode_world_damping_linear_threshold = {0, "physics_ode_world_damping_linear_threshold", "0.01", "world linear damping threshold (see ODE User Guide); use defaults when set to -1"}; +cvar_t physics_ode_world_damping_angular = {0, "physics_ode_world_damping_angular", "0.005", "world angular damping scale (see ODE User Guide); use defaults when set to -1"}; +cvar_t physics_ode_world_damping_angular_threshold = {0, "physics_ode_world_damping_angular_threshold", "0.01", "world angular damping threshold (see ODE User Guide); use defaults when set to -1"}; +cvar_t physics_ode_world_gravitymod = {0, "physics_ode_world_gravitymod", "1", "multiplies gravity got from sv_gravity, this may be needed to tweak if strong damping is used"}; cvar_t physics_ode_iterationsperframe = {0, "physics_ode_iterationsperframe", "1", "divisor for time step, runs multiple physics steps per frame"}; -cvar_t physics_ode_movelimit = {0, "physics_ode_movelimit", "0.5", "clamp velocity if a single move would exceed this percentage of object thickness, to prevent flying through walls"}; +cvar_t physics_ode_constantstep = {0, "physics_ode_constantstep", "1", "use constant step (sys_ticrate value) instead of variable step which tends to increase stability"}; +cvar_t physics_ode_autodisable = {0, "physics_ode_autodisable", "1", "automatic disabling of objects which dont move for long period of time, makes object stacking a lot faster"}; +cvar_t physics_ode_autodisable_steps = {0, "physics_ode_autodisable_steps", "10", "how many steps object should be dormant to be autodisabled"}; +cvar_t physics_ode_autodisable_time = {0, "physics_ode_autodisable_time", "0", "how many seconds object should be dormant to be autodisabled"}; +cvar_t physics_ode_autodisable_threshold_linear = {0, "physics_ode_autodisable_threshold_linear", "0.2", "body will be disabled if it's linear move below this value"}; +cvar_t physics_ode_autodisable_threshold_angular = {0, "physics_ode_autodisable_threshold_angular", "0.3", "body will be disabled if it's angular move below this value"}; +cvar_t physics_ode_autodisable_threshold_samples = {0, "physics_ode_autodisable_threshold_samples", "5", "average threshold with this number of samples"}; +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, be aware that behavior depends of step type"}; cvar_t physics_ode_spinlimit = {0, "physics_ode_spinlimit", "10000", "reset spin velocity if it gets too large"}; +cvar_t physics_ode_trick_fixnan = {0, "physics_ode_trick_fixnan", "1", "engine trick that checks and fixes NaN velocity/origin/angles on objects, a value of 2 makes console prints on each fix"}; +cvar_t physics_ode_printstats = {0, "physics_ode_printstats", "0", "print ODE stats each frame"}; cvar_t physics_ode = {0, "physics_ode", "0", "run ODE physics (VERY experimental and potentially buggy)"}; // LordHavoc: this large chunk of definitions comes from the ODE library @@ -543,7 +566,7 @@ void (ODE_API *dMassSetSphereTotal)(dMass *, dReal total_mass, dReal //void (ODE_API *dMassSetCapsule)(dMass *, dReal density, int direction, dReal radius, dReal length); void (ODE_API *dMassSetCapsuleTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length); //void (ODE_API *dMassSetCylinder)(dMass *, dReal density, int direction, dReal radius, dReal length); -//void (ODE_API *dMassSetCylinderTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length); +void (ODE_API *dMassSetCylinderTotal)(dMass *, dReal total_mass, int direction, dReal radius, dReal length); //void (ODE_API *dMassSetBox)(dMass *, dReal density, dReal lx, dReal ly, dReal lz); void (ODE_API *dMassSetBoxTotal)(dMass *, dReal total_mass, dReal lx, dReal ly, dReal lz); //void (ODE_API *dMassSetTrimesh)(dMass *, dReal density, dGeomID g); @@ -572,33 +595,35 @@ void (ODE_API *dWorldSetQuickStepNumIterations)(dWorldID, int num); //dReal (ODE_API *dWorldGetContactMaxCorrectingVel)(dWorldID); void (ODE_API *dWorldSetContactSurfaceLayer)(dWorldID, dReal depth); //dReal (ODE_API *dWorldGetContactSurfaceLayer)(dWorldID); +#ifdef ODE_USE_STEPFAST void (ODE_API *dWorldStepFast1)(dWorldID, dReal stepsize, int maxiterations); +#endif //void (ODE_API *dWorldSetAutoEnableDepthSF1)(dWorldID, int autoEnableDepth); //int (ODE_API *dWorldGetAutoEnableDepthSF1)(dWorldID); //dReal (ODE_API *dWorldGetAutoDisableLinearThreshold)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableLinearThreshold)(dWorldID, dReal linear_threshold); +void (ODE_API *dWorldSetAutoDisableLinearThreshold)(dWorldID, dReal linear_threshold); //dReal (ODE_API *dWorldGetAutoDisableAngularThreshold)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableAngularThreshold)(dWorldID, dReal angular_threshold); +void (ODE_API *dWorldSetAutoDisableAngularThreshold)(dWorldID, dReal angular_threshold); //dReal (ODE_API *dWorldGetAutoDisableLinearAverageThreshold)(dWorldID); //void (ODE_API *dWorldSetAutoDisableLinearAverageThreshold)(dWorldID, dReal linear_average_threshold); //dReal (ODE_API *dWorldGetAutoDisableAngularAverageThreshold)(dWorldID); //void (ODE_API *dWorldSetAutoDisableAngularAverageThreshold)(dWorldID, dReal angular_average_threshold); //int (ODE_API *dWorldGetAutoDisableAverageSamplesCount)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableAverageSamplesCount)(dWorldID, unsigned int average_samples_count ); +void (ODE_API *dWorldSetAutoDisableAverageSamplesCount)(dWorldID, unsigned int average_samples_count ); //int (ODE_API *dWorldGetAutoDisableSteps)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableSteps)(dWorldID, int steps); +void (ODE_API *dWorldSetAutoDisableSteps)(dWorldID, int steps); //dReal (ODE_API *dWorldGetAutoDisableTime)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableTime)(dWorldID, dReal time); +void (ODE_API *dWorldSetAutoDisableTime)(dWorldID, dReal time); //int (ODE_API *dWorldGetAutoDisableFlag)(dWorldID); -//void (ODE_API *dWorldSetAutoDisableFlag)(dWorldID, int do_auto_disable); +void (ODE_API *dWorldSetAutoDisableFlag)(dWorldID, int do_auto_disable); //dReal (ODE_API *dWorldGetLinearDampingThreshold)(dWorldID w); -//void (ODE_API *dWorldSetLinearDampingThreshold)(dWorldID w, dReal threshold); +void (ODE_API *dWorldSetLinearDampingThreshold)(dWorldID w, dReal threshold); //dReal (ODE_API *dWorldGetAngularDampingThreshold)(dWorldID w); -//void (ODE_API *dWorldSetAngularDampingThreshold)(dWorldID w, dReal threshold); +void (ODE_API *dWorldSetAngularDampingThreshold)(dWorldID w, dReal threshold); //dReal (ODE_API *dWorldGetLinearDamping)(dWorldID w); -//void (ODE_API *dWorldSetLinearDamping)(dWorldID w, dReal scale); +void (ODE_API *dWorldSetLinearDamping)(dWorldID w, dReal scale); //dReal (ODE_API *dWorldGetAngularDamping)(dWorldID w); -//void (ODE_API *dWorldSetAngularDamping)(dWorldID w, dReal scale); +void (ODE_API *dWorldSetAngularDamping)(dWorldID w, dReal scale); //void (ODE_API *dWorldSetDamping)(dWorldID w, dReal linear_scale, dReal angular_scale); //dReal (ODE_API *dWorldGetMaxAngularSpeed)(dWorldID w); //void (ODE_API *dWorldSetMaxAngularSpeed)(dWorldID w, dReal max_speed); @@ -638,9 +663,9 @@ void (ODE_API *dBodySetMass)(dBodyID, const dMass *mass); //void (ODE_API *dBodyAddForce)(dBodyID, dReal fx, dReal fy, dReal fz); //void (ODE_API *dBodyAddTorque)(dBodyID, dReal fx, dReal fy, dReal fz); //void (ODE_API *dBodyAddRelForce)(dBodyID, dReal fx, dReal fy, dReal fz); -//void (ODE_API *dBodyAddRelTorque)(dBodyID, dReal fx, dReal fy, dReal fz); +void (ODE_API *dBodyAddRelTorque)(dBodyID, dReal fx, dReal fy, dReal fz); //void (ODE_API *dBodyAddForceAtPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz); -//void (ODE_API *dBodyAddForceAtRelPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz); +void (ODE_API *dBodyAddForceAtRelPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz); //void (ODE_API *dBodyAddRelForceAtPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz); //void (ODE_API *dBodyAddRelForceAtRelPos)(dBodyID, dReal fx, dReal fy, dReal fz, dReal px, dReal py, dReal pz); //const dReal * (ODE_API *dBodyGetForce)(dBodyID); @@ -662,9 +687,9 @@ 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 *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 (*dBodySetMovedCallback)(dBodyID b, void(ODE_API *callback)(dBodyID)); @@ -932,7 +957,7 @@ dGeomID (ODE_API *dCreateCapsule)(dSpaceID space, dReal radius, dReal le //void (ODE_API *dGeomCapsuleGetParams)(dGeomID ccylinder, dReal *radius, dReal *length); //dReal (ODE_API *dGeomCapsulePointDepth)(dGeomID ccylinder, dReal x, dReal y, dReal z); // -//dGeomID (ODE_API *dCreateCylinder)(dSpaceID space, dReal radius, dReal length); +dGeomID (ODE_API *dCreateCylinder)(dSpaceID space, dReal radius, dReal length); //void (ODE_API *dGeomCylinderSetParams)(dGeomID cylinder, dReal radius, dReal length); //void (ODE_API *dGeomCylinderGetParams)(dGeomID cylinder, dReal *radius, dReal *length); // @@ -1008,7 +1033,7 @@ static dllfunction_t odefuncs[] = // {"dMassSetCapsule", (void **) &dMassSetCapsule}, {"dMassSetCapsuleTotal", (void **) &dMassSetCapsuleTotal}, // {"dMassSetCylinder", (void **) &dMassSetCylinder}, -// {"dMassSetCylinderTotal", (void **) &dMassSetCylinderTotal}, + {"dMassSetCylinderTotal", (void **) &dMassSetCylinderTotal}, // {"dMassSetBox", (void **) &dMassSetBox}, {"dMassSetBoxTotal", (void **) &dMassSetBoxTotal}, // {"dMassSetTrimesh", (void **) &dMassSetTrimesh}, @@ -1037,33 +1062,35 @@ static dllfunction_t odefuncs[] = // {"dWorldGetContactMaxCorrectingVel", (void **) &dWorldGetContactMaxCorrectingVel}, {"dWorldSetContactSurfaceLayer", (void **) &dWorldSetContactSurfaceLayer}, // {"dWorldGetContactSurfaceLayer", (void **) &dWorldGetContactSurfaceLayer}, +#ifdef ODE_USE_STEPFAST {"dWorldStepFast1", (void **) &dWorldStepFast1}, +#endif // {"dWorldSetAutoEnableDepthSF1", (void **) &dWorldSetAutoEnableDepthSF1}, // {"dWorldGetAutoEnableDepthSF1", (void **) &dWorldGetAutoEnableDepthSF1}, // {"dWorldGetAutoDisableLinearThreshold", (void **) &dWorldGetAutoDisableLinearThreshold}, -// {"dWorldSetAutoDisableLinearThreshold", (void **) &dWorldSetAutoDisableLinearThreshold}, + {"dWorldSetAutoDisableLinearThreshold", (void **) &dWorldSetAutoDisableLinearThreshold}, // {"dWorldGetAutoDisableAngularThreshold", (void **) &dWorldGetAutoDisableAngularThreshold}, -// {"dWorldSetAutoDisableAngularThreshold", (void **) &dWorldSetAutoDisableAngularThreshold}, + {"dWorldSetAutoDisableAngularThreshold", (void **) &dWorldSetAutoDisableAngularThreshold}, // {"dWorldGetAutoDisableLinearAverageThreshold", (void **) &dWorldGetAutoDisableLinearAverageThreshold}, // {"dWorldSetAutoDisableLinearAverageThreshold", (void **) &dWorldSetAutoDisableLinearAverageThreshold}, // {"dWorldGetAutoDisableAngularAverageThreshold", (void **) &dWorldGetAutoDisableAngularAverageThreshold}, // {"dWorldSetAutoDisableAngularAverageThreshold", (void **) &dWorldSetAutoDisableAngularAverageThreshold}, // {"dWorldGetAutoDisableAverageSamplesCount", (void **) &dWorldGetAutoDisableAverageSamplesCount}, -// {"dWorldSetAutoDisableAverageSamplesCount", (void **) &dWorldSetAutoDisableAverageSamplesCount}, + {"dWorldSetAutoDisableAverageSamplesCount", (void **) &dWorldSetAutoDisableAverageSamplesCount}, // {"dWorldGetAutoDisableSteps", (void **) &dWorldGetAutoDisableSteps}, -// {"dWorldSetAutoDisableSteps", (void **) &dWorldSetAutoDisableSteps}, + {"dWorldSetAutoDisableSteps", (void **) &dWorldSetAutoDisableSteps}, // {"dWorldGetAutoDisableTime", (void **) &dWorldGetAutoDisableTime}, -// {"dWorldSetAutoDisableTime", (void **) &dWorldSetAutoDisableTime}, + {"dWorldSetAutoDisableTime", (void **) &dWorldSetAutoDisableTime}, // {"dWorldGetAutoDisableFlag", (void **) &dWorldGetAutoDisableFlag}, -// {"dWorldSetAutoDisableFlag", (void **) &dWorldSetAutoDisableFlag}, + {"dWorldSetAutoDisableFlag", (void **) &dWorldSetAutoDisableFlag}, // {"dWorldGetLinearDampingThreshold", (void **) &dWorldGetLinearDampingThreshold}, -// {"dWorldSetLinearDampingThreshold", (void **) &dWorldSetLinearDampingThreshold}, + {"dWorldSetLinearDampingThreshold", (void **) &dWorldSetLinearDampingThreshold}, // {"dWorldGetAngularDampingThreshold", (void **) &dWorldGetAngularDampingThreshold}, -// {"dWorldSetAngularDampingThreshold", (void **) &dWorldSetAngularDampingThreshold}, + {"dWorldSetAngularDampingThreshold", (void **) &dWorldSetAngularDampingThreshold}, // {"dWorldGetLinearDamping", (void **) &dWorldGetLinearDamping}, -// {"dWorldSetLinearDamping", (void **) &dWorldSetLinearDamping}, + {"dWorldSetLinearDamping", (void **) &dWorldSetLinearDamping}, // {"dWorldGetAngularDamping", (void **) &dWorldGetAngularDamping}, -// {"dWorldSetAngularDamping", (void **) &dWorldSetAngularDamping}, + {"dWorldSetAngularDamping", (void **) &dWorldSetAngularDamping}, // {"dWorldSetDamping", (void **) &dWorldSetDamping}, // {"dWorldGetMaxAngularSpeed", (void **) &dWorldGetMaxAngularSpeed}, // {"dWorldSetMaxAngularSpeed", (void **) &dWorldSetMaxAngularSpeed}, @@ -1103,9 +1130,9 @@ static dllfunction_t odefuncs[] = // {"dBodyAddForce", (void **) &dBodyAddForce}, // {"dBodyAddTorque", (void **) &dBodyAddTorque}, // {"dBodyAddRelForce", (void **) &dBodyAddRelForce}, -// {"dBodyAddRelTorque", (void **) &dBodyAddRelTorque}, + {"dBodyAddRelTorque", (void **) &dBodyAddRelTorque}, // {"dBodyAddForceAtPos", (void **) &dBodyAddForceAtPos}, -// {"dBodyAddForceAtRelPos", (void **) &dBodyAddForceAtRelPos}, + {"dBodyAddForceAtRelPos", (void **) &dBodyAddForceAtRelPos}, // {"dBodyAddRelForceAtPos", (void **) &dBodyAddRelForceAtPos}, // {"dBodyAddRelForceAtRelPos", (void **) &dBodyAddRelForceAtRelPos}, // {"dBodyGetForce", (void **) &dBodyGetForce}, @@ -1127,9 +1154,9 @@ static dllfunction_t odefuncs[] = // {"dBodySetDynamic", (void **) &dBodySetDynamic}, // {"dBodySetKinematic", (void **) &dBodySetKinematic}, // {"dBodyIsKinematic", (void **) &dBodyIsKinematic}, -// {"dBodyEnable", (void **) &dBodyEnable}, -// {"dBodyDisable", (void **) &dBodyDisable}, -// {"dBodyIsEnabled", (void **) &dBodyIsEnabled}, + {"dBodyEnable", (void **) &dBodyEnable}, + {"dBodyDisable", (void **) &dBodyDisable}, + {"dBodyIsEnabled", (void **) &dBodyIsEnabled}, {"dBodySetGravityMode", (void **) &dBodySetGravityMode}, {"dBodyGetGravityMode", (void **) &dBodyGetGravityMode}, // {"dBodySetMovedCallback", (void **) &dBodySetMovedCallback}, @@ -1388,7 +1415,7 @@ static dllfunction_t odefuncs[] = // {"dGeomCapsuleSetParams", (void **) &dGeomCapsuleSetParams}, // {"dGeomCapsuleGetParams", (void **) &dGeomCapsuleGetParams}, // {"dGeomCapsulePointDepth", (void **) &dGeomCapsulePointDepth}, -// {"dCreateCylinder", (void **) &dCreateCylinder}, + {"dCreateCylinder", (void **) &dCreateCylinder}, // {"dGeomCylinderSetParams", (void **) &dGeomCylinderSetParams}, // {"dGeomCylinderGetParams", (void **) &dGeomCylinderGetParams}, // {"dCreateRay", (void **) &dCreateRay}, @@ -1464,18 +1491,31 @@ static void World_Physics_Init(void) Cvar_RegisterVariable(&physics_ode_quadtree_depth); Cvar_RegisterVariable(&physics_ode_contactsurfacelayer); - Cvar_RegisterVariable(&physics_ode_worldquickstep); - Cvar_RegisterVariable(&physics_ode_worldquickstep_iterations); - Cvar_RegisterVariable(&physics_ode_worldstepfast); - Cvar_RegisterVariable(&physics_ode_worldstepfast_iterations); + Cvar_RegisterVariable(&physics_ode_worldstep); + Cvar_RegisterVariable(&physics_ode_worldstep_iterations); 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_world_damping); + Cvar_RegisterVariable(&physics_ode_world_damping_linear); + Cvar_RegisterVariable(&physics_ode_world_damping_linear_threshold); + Cvar_RegisterVariable(&physics_ode_world_damping_angular); + Cvar_RegisterVariable(&physics_ode_world_damping_angular_threshold); + Cvar_RegisterVariable(&physics_ode_world_gravitymod); Cvar_RegisterVariable(&physics_ode_iterationsperframe); + Cvar_RegisterVariable(&physics_ode_constantstep); Cvar_RegisterVariable(&physics_ode_movelimit); Cvar_RegisterVariable(&physics_ode_spinlimit); + Cvar_RegisterVariable(&physics_ode_trick_fixnan); + Cvar_RegisterVariable(&physics_ode_autodisable); + Cvar_RegisterVariable(&physics_ode_autodisable_steps); + Cvar_RegisterVariable(&physics_ode_autodisable_time); + Cvar_RegisterVariable(&physics_ode_autodisable_threshold_linear); + Cvar_RegisterVariable(&physics_ode_autodisable_threshold_angular); + Cvar_RegisterVariable(&physics_ode_autodisable_threshold_samples); + Cvar_RegisterVariable(&physics_ode_printstats); Cvar_RegisterVariable(&physics_ode); #ifdef ODE_DYNAMIC @@ -1493,13 +1533,21 @@ static void World_Physics_Init(void) # endif { # ifdef dSINGLE - Con_Printf("ode library not compiled for single precision - incompatible! Not using ODE physics.\n"); + Con_Printf("ODE library not compiled for single precision - incompatible! Not using ODE physics.\n"); # else - Con_Printf("ode library not compiled for double precision - incompatible! Not using ODE physics.\n"); + Con_Printf("ODE library not compiled for double precision - incompatible! Not using ODE physics.\n"); # endif Sys_UnloadLibrary(&ode_dll); ode_dll = NULL; } + else + { +# ifdef dSINGLE + Con_Printf("ODE library loaded with single precision.\n"); +# else + Con_Printf("ODE library loaded with double precision.\n"); +# endif + } #endif } #endif @@ -1522,6 +1570,44 @@ static void World_Physics_Shutdown(void) } #ifdef USEODE +static void World_Physics_UpdateODE(world_t *world) +{ + dWorldID odeworld; + + odeworld = (dWorldID)world->physics.ode_world; + + // ERP and CFM + if (physics_ode_world_erp.value >= 0) + dWorldSetERP(odeworld, physics_ode_world_erp.value); + if (physics_ode_world_cfm.value >= 0) + dWorldSetCFM(odeworld, physics_ode_world_cfm.value); + // Damping + if (physics_ode_world_damping.integer) + { + dWorldSetLinearDamping(odeworld, (physics_ode_world_damping_linear.value >= 0) ? (physics_ode_world_damping_linear.value * physics_ode_world_damping.value) : 0); + dWorldSetLinearDampingThreshold(odeworld, (physics_ode_world_damping_linear_threshold.value >= 0) ? (physics_ode_world_damping_linear_threshold.value * physics_ode_world_damping.value) : 0); + dWorldSetAngularDamping(odeworld, (physics_ode_world_damping_angular.value >= 0) ? (physics_ode_world_damping_angular.value * physics_ode_world_damping.value) : 0); + dWorldSetAngularDampingThreshold(odeworld, (physics_ode_world_damping_angular_threshold.value >= 0) ? (physics_ode_world_damping_angular_threshold.value * physics_ode_world_damping.value) : 0); + } + else + { + dWorldSetLinearDamping(odeworld, 0); + dWorldSetLinearDampingThreshold(odeworld, 0); + dWorldSetAngularDamping(odeworld, 0); + dWorldSetAngularDampingThreshold(odeworld, 0); + } + // Autodisable + dWorldSetAutoDisableFlag(odeworld, (physics_ode_autodisable.integer) ? 1 : 0); + if (physics_ode_autodisable.integer) + { + dWorldSetAutoDisableSteps(odeworld, bound(1, physics_ode_autodisable_steps.integer, 100)); + dWorldSetAutoDisableTime(odeworld, physics_ode_autodisable_time.value); + dWorldSetAutoDisableAverageSamplesCount(odeworld, bound(1, physics_ode_autodisable_threshold_samples.integer, 100)); + dWorldSetAutoDisableLinearThreshold(odeworld, physics_ode_autodisable_threshold_linear.value); + dWorldSetAutoDisableAngularThreshold(odeworld, physics_ode_autodisable_threshold_angular.value); + } +} + static void World_Physics_EnableODE(world_t *world) { dVector3 center, extents; @@ -1537,10 +1623,8 @@ 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); - if(physics_ode_world_erp.value >= 0) - dWorldSetERP((dWorldID)world->physics.ode_world, physics_ode_world_erp.value); - if(physics_ode_world_cfm.value >= 0) - dWorldSetCFM((dWorldID)world->physics.ode_world, physics_ode_world_cfm.value); + + World_Physics_UpdateODE(world); } #endif @@ -1578,6 +1662,8 @@ void World_Physics_RemoveJointFromEntity(world_t *world, prvm_edict_t *ed) void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed) { + edict_odefunc_t *f, *nf; + // entity is not physics controlled, free any physics data ed->priv.server->ode_physics = false; #ifdef USEODE @@ -1622,6 +1708,40 @@ void World_Physics_RemoveFromEntity(world_t *world, prvm_edict_t *ed) if(ed->priv.server->ode_massbuf) Mem_Free(ed->priv.server->ode_massbuf); ed->priv.server->ode_massbuf = NULL; + // clear functions stack + for(f = ed->priv.server->ode_func; f; f = nf) + { + nf = f->next; + Mem_Free(f); + } + ed->priv.server->ode_func = NULL; +} + +void World_Physics_ApplyCmd(prvm_edict_t *ed, edict_odefunc_t *f) +{ + dBodyID body = (dBodyID)ed->priv.server->ode_body; + +#ifdef USEODE + switch(f->type) + { + case ODEFUNC_ENABLE: + dBodyEnable(body); + break; + case ODEFUNC_DISABLE: + dBodyDisable(body); + break; + case ODEFUNC_RELFORCEATPOS: + dBodyEnable(body); + dBodyAddForceAtRelPos(body, f->v1[0], f->v1[1], f->v1[2], f->v2[0], f->v2[1], f->v2[2]); + break; + case ODEFUNC_RELTORQUE: + dBodyEnable(body); + dBodyAddRelTorque(body, f->v1[0], f->v1[1], f->v1[2]); + break; + default: + break; + } +#endif } #ifdef USEODE @@ -1635,7 +1755,6 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed) int movetype; matrix4x4_t bodymatrix; matrix4x4_t entitymatrix; - prvm_eval_t *val; vec3_t angles; vec3_t avelocity; vec3_t forward, left, up; @@ -1645,11 +1764,10 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed) int jointtype; if (!body) return; - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.movetype); - movetype = (int)val->_float; + movetype = (int)PRVM_gameedictfloat(ed, movetype); if (movetype != MOVETYPE_PHYSICS) { - jointtype = 0;val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.jointtype);if (val) jointtype = (int)val->_float; + jointtype = (int)PRVM_gameedictfloat(ed, jointtype); switch(jointtype) { // TODO feed back data from physics @@ -1706,14 +1824,14 @@ static void World_Physics_Frame_BodyToEntity(world_t *world, prvm_edict_t *ed) avelocity[PITCH] *= pitchsign; } - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.origin);if (val) VectorCopy(origin, val->vector); - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.velocity);if (val) VectorCopy(velocity, 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.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); + VectorCopy(origin, PRVM_gameedictvector(ed, origin)); + VectorCopy(velocity, PRVM_gameedictvector(ed, velocity)); + //VectorCopy(forward, PRVM_gameedictvector(ed, axis_forward)); + //VectorCopy(left, PRVM_gameedictvector(ed, axis_left)); + //VectorCopy(up, PRVM_gameedictvector(ed, axis_up)); + //VectorCopy(spinvelocity, PRVM_gameedictvector(ed, spinvelocity)); + VectorCopy(angles, PRVM_gameedictvector(ed, angles)); + VectorCopy(avelocity, PRVM_gameedictvector(ed, avelocity)); // values for BodyFromEntity to check if the qc modified anything later VectorCopy(origin, ed->priv.server->ode_origin); @@ -1739,19 +1857,18 @@ static void World_Physics_Frame_JointFromEntity(world_t *world, prvm_edict_t *ed 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); + movetype = (int)PRVM_gameedictfloat(ed, movetype); + jointtype = (int)PRVM_gameedictfloat(ed, jointtype); + enemy = PRVM_gameedictedict(ed, enemy); + aiment = PRVM_gameedictedict(ed, aiment); + VectorCopy(PRVM_gameedictvector(ed, origin), origin); + VectorCopy(PRVM_gameedictvector(ed, velocity), velocity); + VectorCopy(PRVM_gameedictvector(ed, angles), angles); + VectorCopy(PRVM_gameedictvector(ed, movedir), 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) @@ -1918,6 +2035,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) dBodyID body = (dBodyID)ed->priv.server->ode_body; dMass mass; dReal test; + const dReal *ovelocity, *ospinvelocity; void *dataID; dVector3 capsulerot[3]; dp_model_t *model; @@ -1932,7 +2050,6 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) int triangleindex; int vertexindex; mempool_t *mempool; - prvm_eval_t *val; qboolean modified = false; vec3_t angles; vec3_t avelocity; @@ -1954,15 +2071,17 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) vec_t scale = 1.0f; vec_t spinlimit; qboolean gravity; + edict_odefunc_t *func, *nextf; + #ifdef ODE_DYNAMIC if (!ode_dll) return; #endif VectorClear(entmins); VectorClear(entmaxs); - 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; + solid = (int)PRVM_gameedictfloat(ed, solid); + movetype = (int)PRVM_gameedictfloat(ed, movetype); + scale = PRVM_gameedictfloat(ed, scale);if (!scale) scale = 1.0f; modelindex = 0; if (world == &sv.world) mempool = sv_mempool; @@ -1974,9 +2093,8 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) switch(solid) { case SOLID_BSP: - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.modelindex); - if (val) - modelindex = (int)val->_float; + case SOLID_PHYSICS_TRIMESH: + modelindex = (int)PRVM_gameedictfloat(ed, modelindex); if (world == &sv.world) model = SV_GetModelByIndex(modelindex); else if (world == &cl.world) @@ -1987,7 +2105,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) { VectorScale(model->normalmins, scale, entmins); VectorScale(model->normalmaxs, scale, entmaxs); - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.mass);if (val) massval = val->_float; + massval = PRVM_gameedictfloat(ed, mass); } else { @@ -2001,9 +2119,9 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) case SOLID_PHYSICS_BOX: case SOLID_PHYSICS_SPHERE: case SOLID_PHYSICS_CAPSULE: - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.mins);if (val) VectorCopy(val->vector, entmins); - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.maxs);if (val) VectorCopy(val->vector, entmaxs); - val = PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.mass);if (val) massval = val->_float; + VectorCopy(PRVM_gameedictvector(ed, mins), entmins); + VectorCopy(PRVM_gameedictvector(ed, maxs), entmaxs); + massval = PRVM_gameedictfloat(ed, mass); break; default: if (ed->priv.server->ode_physics) @@ -2023,6 +2141,12 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) if (movetype != MOVETYPE_PHYSICS) massval = 1.0f; + // get friction from entity + if (PRVM_gameedictfloat(ed, friction)) + ed->priv.server->ode_friction = PRVM_gameedictfloat(ed, friction); + else + ed->priv.server->ode_friction = 1.0; + // check if we need to create or replace the geom if (!ed->priv.server->ode_physics || !VectorCompare(ed->priv.server->ode_mins, entmins) @@ -2038,12 +2162,15 @@ 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; VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter); + if (PRVM_gameedictvector(ed, massofs)) + VectorCopy(geomcenter, PRVM_gameedictvector(ed, massofs)); + else + VectorMAM(0.5f, entmins, 0.5f, entmaxs, geomcenter); ed->priv.server->ode_movelimit = min(geomsize[0], min(geomsize[1], geomsize[2])); - if (massval * geomsize[0] * geomsize[1] * geomsize[2] == 0) { if (movetype == MOVETYPE_PHYSICS) - Con_Printf("entity %i (classname %s) .mass * .size_x * .size_y * .size_z == 0\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string)); + Con_Printf("entity %i (classname %s) .mass * .size_x * .size_y * .size_z == 0\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_gameedictstring(ed, classname))); massval = 1.0f; VectorSet(geomsize, 1.0f, 1.0f, 1.0f); } @@ -2051,10 +2178,11 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) switch(solid) { case SOLID_BSP: + case SOLID_PHYSICS_TRIMESH: ed->priv.server->ode_offsetmatrix = identitymatrix; if (!model) { - Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string)); + Con_Printf("entity %i (classname %s) has no model\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_gameedictstring(ed, classname))); goto treatasbox; } // add an optimized mesh to the model containing only the SUPERCONTENTS_SOLID surfaces @@ -2062,7 +2190,7 @@ static void World_Physics_Frame_BodyFromEntity(world_t *world, prvm_edict_t *ed) Mod_CreateCollisionMesh(model); if (!model->brush.collisionmesh || !model->brush.collisionmesh->numtriangles) { - Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_EDICTFIELDVALUE(ed, prog->fieldoffsets.classname)->string)); + Con_Printf("entity %i (classname %s) has no geometry\n", PRVM_NUM_FOR_EDICT(ed), PRVM_GetString(PRVM_gameedictstring(ed, classname))); goto treatasbox; } // ODE requires persistent mesh storage, so we need to copy out @@ -2109,6 +2237,7 @@ treatasbox: dMassSetSphereTotal(&mass, massval, geomsize[0] * 0.5f); break; case SOLID_PHYSICS_CAPSULE: + case SOLID_PHYSICS_CYLINDER: axisindex = 0; if (geomsize[axisindex] < geomsize[1]) axisindex = 1; @@ -2130,8 +2259,16 @@ treatasbox: // because we want to support more than one axisindex, we have to // create a transform, and turn on its cleanup setting (which will // cause the child to be destroyed when it is destroyed) - ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length); - dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length); + if (solid == SOLID_PHYSICS_CAPSULE) + { + ed->priv.server->ode_geom = (void *)dCreateCapsule((dSpaceID)world->physics.ode_space, radius, length); + dMassSetCapsuleTotal(&mass, massval, axisindex+1, radius, length); + } + else + { + ed->priv.server->ode_geom = (void *)dCreateCylinder((dSpaceID)world->physics.ode_space, radius, length); + dMassSetCylinderTotal(&mass, massval, axisindex+1, radius, length); + } break; default: Sys_Error("World_Physics_BodyFromEntity: unrecognized solid value %i was accepted by filter\n", solid); @@ -2145,7 +2282,7 @@ treatasbox: memcpy(ed->priv.server->ode_massbuf, &mass, sizeof(dMass)); } - if(ed->priv.server->ode_geom) + if (ed->priv.server->ode_geom) dGeomSetData((dGeomID)ed->priv.server->ode_geom, (void*)ed); if (movetype == MOVETYPE_PHYSICS && ed->priv.server->ode_geom) { @@ -2180,16 +2317,16 @@ treatasbox: 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.velocity);if (val) VectorCopy(val->vector, velocity); - //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) + VectorCopy(PRVM_gameedictvector(ed, origin), origin); + VectorCopy(PRVM_gameedictvector(ed, velocity), velocity); + //VectorCopy(PRVM_gameedictvector(ed, axis_forward), forward); + //VectorCopy(PRVM_gameedictvector(ed, axis_left), left); + //VectorCopy(PRVM_gameedictvector(ed, axis_up), up); + //VectorCopy(PRVM_gameedictvector(ed, spinvelocity), spinvelocity); + VectorCopy(PRVM_gameedictvector(ed, angles), angles); + VectorCopy(PRVM_gameedictvector(ed, avelocity), avelocity); + if (PRVM_gameedictfloat(ed, gravity) != 0.0f && PRVM_gameedictfloat(ed, gravity) < 0.5f) gravity = false; + if (ed == prog->edicts) gravity = false; // compatibility for legacy entities @@ -2232,31 +2369,35 @@ treatasbox: // we must prevent NANs... - test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity); - if (IS_NAN(test)) + if (physics_ode_trick_fixnan.integer) { - 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); - } - test = VectorLength2(velocity); - if (IS_NAN(test)) - VectorClear(velocity); - test = VectorLength2(spinvelocity); + test = VectorLength2(origin) + VectorLength2(forward) + VectorLength2(left) + VectorLength2(up) + VectorLength2(velocity) + VectorLength2(spinvelocity); if (IS_NAN(test)) { - VectorClear(avelocity); - VectorClear(spinvelocity); + 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_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], forward[0], forward[1], forward[2], left[0], left[1], left[2], up[0], up[1], up[2], spinvelocity[0], spinvelocity[1], spinvelocity[2]); + if (physics_ode_trick_fixnan.integer >= 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_gameedictstring(ed, classname)), origin[0], origin[1], origin[2], velocity[0], velocity[1], velocity[2], angles[0], angles[1], angles[2], avelocity[0], avelocity[1], avelocity[2]); + 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); + } + test = VectorLength2(velocity); + if (IS_NAN(test)) + VectorClear(velocity); + test = VectorLength2(spinvelocity); + if (IS_NAN(test)) + { + VectorClear(avelocity); + VectorClear(spinvelocity); + } } } @@ -2289,7 +2430,6 @@ treatasbox: 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); @@ -2342,9 +2482,10 @@ treatasbox: if(body) { + // limit movement speed to prevent missed collisions at high speed - const dReal *ovelocity = dBodyGetLinearVel(body); - const dReal *ospinvelocity = dBodyGetAngularVel(body); + ovelocity = dBodyGetLinearVel(body); + ospinvelocity = dBodyGetAngularVel(body); movelimit = ed->priv.server->ode_movelimit * world->physics.ode_movelimit; test = VectorLength2(ovelocity); if (test > movelimit*movelimit) @@ -2365,6 +2506,15 @@ treatasbox: { dBodySetAngularVel(body, 0, 0, 0); } + + // apply functions and clear stack + for(func = ed->priv.server->ode_func; func; func = nextf) + { + nextf = func->next; + World_Physics_ApplyCmd(ed, func); + Mem_Free(func); + } + ed->priv.server->ode_func = NULL; } } @@ -2378,7 +2528,6 @@ 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; @@ -2413,13 +2562,10 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2) 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; + bouncefactor1 = PRVM_gameedictfloat(ed1, bouncefactor); + bouncestop1 = PRVM_gameedictfloat(ed1, bouncestop); + if (!bouncestop1) + bouncestop1 = 60.0f / 800.0f; } ed2 = (prvm_edict_t *) dGeomGetData(o2); @@ -2427,22 +2573,19 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2) 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; + bouncefactor2 = PRVM_gameedictfloat(ed2, bouncefactor); + bouncestop2 = PRVM_gameedictfloat(ed2, bouncestop); + if (!bouncestop2) + bouncestop2 = 60.0f / 800.0f; } if(!strcmp(prog->name, "server")) { - if(ed1 && ed1->fields.server->touch) + if(ed1 && PRVM_serveredictfunction(ed1, touch)) { SV_LinkEdict_TouchAreaGrid_Call(ed1, ed2 ? ed2 : prog->edicts); } - if(ed2 && ed2->fields.server->touch) + if(ed2 && PRVM_serveredictfunction(ed2, touch)) { SV_LinkEdict_TouchAreaGrid_Call(ed2, ed1 ? ed1 : prog->edicts); } @@ -2474,7 +2617,7 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2) 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) | (bouncefactor1 > 0 ? dContactBounce : 0); - contact[i].surface.mu = physics_ode_contact_mu.value; + contact[i].surface.mu = physics_ode_contact_mu.value * ed1->priv.server->ode_friction * ed2->priv.server->ode_friction; 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; @@ -2487,6 +2630,9 @@ static void nearCallback (void *data, dGeomID o1, dGeomID o2) void World_Physics_Frame(world_t *world, double frametime, double gravity) { + double tdelta, tdelta2, tdelta3, simulationtime, collisiontime; + + tdelta = Sys_DoubleTime(); #ifdef USEODE if (world->physics.ode && physics_ode.integer) { @@ -2494,8 +2640,12 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity) prvm_edict_t *ed; world->physics.ode_iterations = bound(1, physics_ode_iterationsperframe.integer, 1000); - world->physics.ode_step = frametime / world->physics.ode_iterations; + if (physics_ode_constantstep.integer) + world->physics.ode_step = sys_ticrate.value / world->physics.ode_iterations; + else + world->physics.ode_step = frametime / world->physics.ode_iterations; world->physics.ode_movelimit = physics_ode_movelimit.value / world->physics.ode_step; + World_Physics_UpdateODE(world); // copy physics properties from entities to physics engine if (prog) @@ -2509,36 +2659,66 @@ void World_Physics_Frame(world_t *world, double frametime, double gravity) World_Physics_Frame_JointFromEntity(world, ed); } + tdelta2 = Sys_DoubleTime(); + collisiontime = 0; for (i = 0;i < world->physics.ode_iterations;i++) { // set the gravity - dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity); + dWorldSetGravity((dWorldID)world->physics.ode_world, 0, 0, -gravity * physics_ode_world_gravitymod.value); // set the tolerance for closeness of objects dWorldSetContactSurfaceLayer((dWorldID)world->physics.ode_world, max(0, physics_ode_contactsurfacelayer.value)); // run collisions for the current world state, creating JointGroup + tdelta3 = Sys_DoubleTime(); dSpaceCollide((dSpaceID)world->physics.ode_space, (void *)world, nearCallback); + collisiontime += (Sys_DoubleTime() - tdelta3)*10000; // run physics (move objects, calculate new velocities) - if (physics_ode_worldquickstep.integer) + if (physics_ode_worldstep.integer == 2) { - dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldquickstep_iterations.integer, 200)); + dWorldSetQuickStepNumIterations((dWorldID)world->physics.ode_world, bound(1, physics_ode_worldstep_iterations.integer, 200)); dWorldQuickStep((dWorldID)world->physics.ode_world, world->physics.ode_step); } - else if (physics_ode_worldstepfast.integer) - dWorldStepFast1((dWorldID)world->physics.ode_world, world->physics.ode_step, bound(1, physics_ode_worldstepfast_iterations.integer, 200)); +#ifdef ODE_USE_STEPFAST + else if (physics_ode_worldstep.integer == 1) + dWorldStepFast1((dWorldID)world->physics.ode_world, world->physics.ode_step, bound(1, physics_ode_worldstep_iterations.integer, 200)); +#endif else dWorldStep((dWorldID)world->physics.ode_world, world->physics.ode_step); // clear the JointGroup now that we're done with it dJointGroupEmpty((dJointGroupID)world->physics.ode_contactgroup); } + simulationtime = (Sys_DoubleTime() - tdelta2)*10000; - // copy physics properties from physics engine to entities + // copy physics properties from physics engine to entities and do some stats if (prog) + { for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++) if (!prog->edicts[i].priv.required->free) World_Physics_Frame_BodyToEntity(world, ed); + + // print stats + if (physics_ode_printstats.integer) + { + dBodyID body; + + world->physics.ode_numobjects = 0; + world->physics.ode_activeovjects = 0; + for (i = 1, ed = prog->edicts + i;i < prog->num_edicts;i++, ed++) + { + if (prog->edicts[i].priv.required->free) + continue; + body = (dBodyID)prog->edicts[i].priv.server->ode_body; + if (!body) + continue; + world->physics.ode_numobjects++; + if (dBodyIsEnabled(body)) + world->physics.ode_activeovjects++; + } + Con_Printf("ODE Stats(%s): %3.01f (%3.01f collision) %3.01f total : %i objects %i active %i disabled\n", prog->name, simulationtime, collisiontime, (Sys_DoubleTime() - tdelta)*10000, world->physics.ode_numobjects, world->physics.ode_activeovjects, (world->physics.ode_numobjects - world->physics.ode_activeovjects)); + } + } } #endif }