4 .int disableclientprediction;
6 void sys_phys_simulate(entity this, float dt);
8 void sys_phys_update(entity this, float dt)
10 sys_in_update(this, dt);
12 sys_phys_fix(this, dt);
13 if (sys_phys_override(this)) { return; } sys_phys_monitor(this);
15 this.buttons_old = PHYS_INPUT_BUTTON_MASK(this);
16 this.movement_old = this.movement;
17 this.v_angle_old = this.v_angle;
21 sys_phys_pregame_hold(this);
24 if (PHYS_MOVETYPE(this) == MOVETYPE_NONE) { return; }
25 // when we get here, disableclientprediction cannot be 2
26 this.disableclientprediction = 0;
29 viewloc_PlayerPhysics(this);
31 PM_check_frozen(this);
33 PM_check_blocked(this);
35 float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown; // cvar("g_balance_swamp_moverate");
37 // conveyors: first fix velocity
38 if (this.conveyor.state) { this.velocity -= this.conveyor.movedir; }
39 MUTATOR_CALLHOOK(PlayerPhysics, this);
41 if (!IS_PLAYER(this)) {
42 sys_phys_spectator_control(this);
43 maxspeed_mod = this.spectatorspeed;
45 sys_phys_fixspeed(this, maxspeed_mod);
49 vector midpoint = ((this.absmin + this.absmax) * 0.5);
50 if (pointcontents(midpoint) == CONTENT_WATER) {
51 this.velocity = this.velocity * 0.5;
54 // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
55 // { this.velocity_z = 70; }
60 if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
61 if (IS_PLAYER(this)) {
62 if (IS_ONGROUND(this)) {
63 PM_check_hitground(this);
65 } else if (IsFlying(this)) {
66 this.wasFlying = true;
68 CheckPlayerJump(this);
71 if (this.flags & FL_WATERJUMP) {
72 this.velocity_x = this.movedir.x;
73 this.velocity_y = this.movedir.y;
74 if (time > PHYS_TELEPORT_TIME(this)
75 || this.waterlevel == WATERLEVEL_NONE
76 || PHYS_WATERJUMP_TIME(this) <= 0
78 this.flags &= ~FL_WATERJUMP;
79 PHYS_TELEPORT_TIME(this) = 0;
80 PHYS_WATERJUMP_TIME(this) = 0;
82 } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod)) {
84 } else if (PHYS_MOVETYPE(this) == MOVETYPE_NOCLIP
85 || PHYS_MOVETYPE(this) == MOVETYPE_FLY
86 || PHYS_MOVETYPE(this) == MOVETYPE_FLY_WORLDONLY
87 || MUTATOR_CALLHOOK(IsFlying, this)) {
88 this.com_phys_friction = PHYS_FRICTION(this);
89 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
90 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
91 this.com_phys_friction_air = true;
92 sys_phys_simulate(this, dt);
93 this.com_phys_friction_air = false;
94 } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
95 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
96 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
97 this.com_phys_water = true;
98 sys_phys_simulate(this, dt);
99 this.com_phys_water = false;
100 } else if (time < this.ladder_time) {
101 this.com_phys_friction = PHYS_FRICTION(this);
102 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
103 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
104 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
105 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
106 this.com_phys_ladder = true;
107 this.com_phys_friction_air = true;
108 sys_phys_simulate(this, dt);
109 this.com_phys_friction_air = false;
110 this.com_phys_ladder = false;
111 this.com_phys_gravity = '0 0 0';
112 } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
113 PM_jetpack(this, maxspeed_mod);
114 } else if (IS_ONGROUND(this)) {
115 if (!WAS_ONGROUND(this)) {
116 emit(phys_land, this);
117 if (this.lastground < time - 0.3) {
118 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
121 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
122 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
123 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
124 this.com_phys_ground = true;
125 this.com_phys_vel_2d = true;
126 sys_phys_simulate(this, dt);
127 this.com_phys_vel_2d = false;
128 this.com_phys_ground = false;
129 this.com_phys_gravity = '0 0 0';
131 this.com_phys_acc_rate_air = PHYS_AIRACCELERATE(this) * min(maxspeed_mod, 1);
132 this.com_phys_acc_rate_air_stop = PHYS_AIRSTOPACCELERATE(this) * maxspeed_mod;
133 this.com_phys_acc_rate_air_strafe = PHYS_AIRSTRAFEACCELERATE(this) * maxspeed_mod;
134 this.com_phys_vel_max_air_strafe = PHYS_MAXAIRSTRAFESPEED(this) * maxspeed_mod;
135 this.com_phys_vel_max_air = PHYS_MAXAIRSPEED(this) * maxspeed_mod;
136 this.com_phys_vel_max = PHYS_MAXAIRSPEED(this) * min(maxspeed_mod, 1);
137 this.com_phys_air = true;
138 this.com_phys_vel_2d = true;
139 sys_phys_simulate(this, dt);
140 this.com_phys_vel_2d = false;
141 this.com_phys_air = false;
145 if (IS_ONGROUND(this)) { this.lastground = time; }
146 // conveyors: then break velocity again
147 if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
148 this.lastflags = this.flags;
150 this.lastclassname = this.classname;
153 void sys_phys_simulate(entity this, float dt)
155 const vector g = -this.com_phys_gravity;
156 const bool jump = this.com_in_jump;
158 if (!this.com_phys_ground && !this.com_phys_air) {
161 // on a spawnfunc_func_ladder
162 // swimming in spawnfunc_func_water
164 UNSET_ONGROUND(this);
166 if (this.com_phys_friction_air) {
167 this.velocity_z += g.z / 2;
168 this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
169 this.velocity_z += g.z / 2;
173 if (this.com_phys_water) {
174 // water jump only in certain situations
175 // this mimics quakeworld code
176 if (jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc) {
177 vector yawangles = '0 1 0' * this.v_angle.y;
178 makevectors(yawangles);
179 vector forward = v_forward;
180 vector spot = this.origin + 24 * forward;
182 traceline(spot, spot, MOVE_NOMONSTERS, this);
183 if (trace_startsolid) {
185 traceline(spot, spot, MOVE_NOMONSTERS, this);
186 if (!trace_startsolid) {
187 this.velocity = forward * 50;
188 this.velocity_z = 310;
189 if (IS_CSQC) { PHYS_WATERJUMP_TIME(this) = 2; }
190 UNSET_ONGROUND(this);
196 makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
197 // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
198 vector wishvel = v_forward * this.movement.x
199 + v_right * this.movement.y
200 + '0 0 1' * this.movement.z * (this.com_phys_vel_2d ? 0 : 1);
201 if (this.com_phys_water) {
203 wishvel.z = -160; // drift anyway
204 } else if (wishvel == '0 0 0') {
205 wishvel = '0 0 -60'; // drift towards bottom
208 if (this.com_phys_ladder) {
210 wishvel.z = this.oldmovement.x;
212 if (this.ladder_entity.classname == "func_water") {
213 float f = vlen(wishvel);
214 if (f > this.ladder_entity.speed) {
215 wishvel *= (this.ladder_entity.speed / f);
218 this.watertype = this.ladder_entity.skin;
219 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
220 if ((this.origin_z + this.view_ofs_z) < f) {
221 this.waterlevel = WATERLEVEL_SUBMERGED;
222 } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
223 this.waterlevel = WATERLEVEL_SWIMMING;
224 } else if ((this.origin_z + this.mins_z + 1) < f) {
225 this.waterlevel = WATERLEVEL_WETFEET;
227 this.waterlevel = WATERLEVEL_NONE;
228 this.watertype = CONTENT_EMPTY;
233 const vector wishdir = normalize(wishvel);
234 float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
236 if (this.com_phys_air) {
237 if ((IS_SVQC && time >= PHYS_TELEPORT_TIME(this))
238 || (IS_CSQC && PHYS_WATERJUMP_TIME(this) <= 0)) {
239 // apply air speed limit
240 float airaccelqw = PHYS_AIRACCEL_QW(this);
241 float wishspeed0 = wishspeed;
242 const float maxairspd = this.com_phys_vel_max;
243 wishspeed = min(wishspeed, maxairspd);
244 if (IS_DUCKED(this)) {
247 float airaccel = this.com_phys_acc_rate_air;
249 float accelerating = (this.velocity * wishdir > 0);
250 float wishspeed2 = wishspeed;
253 if (PHYS_AIRSTOPACCELERATE(this)) {
254 vector curdir = normalize(vec2(this.velocity));
255 airaccel += (this.com_phys_acc_rate_air_stop - airaccel) * max(0, -(curdir * wishdir));
257 // note that for straight forward jumping:
258 // step = accel * PHYS_INPUT_TIMELENGTH * wishspeed0;
259 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
261 // dv/dt = accel * maxspeed (when slow)
262 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
263 // log dv/dt = logaccel + logmaxspeed (when slow)
264 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
265 float strafity = IsMoveInDirection(this.movement, -90) + IsMoveInDirection(this.movement, +90); // if one is nonzero, other is always zero
266 if (PHYS_MAXAIRSTRAFESPEED(this)) {
269 GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
271 if (PHYS_AIRSTRAFEACCELERATE(this)) {
272 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
274 if (PHYS_AIRSTRAFEACCEL_QW(this)) {
276 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
278 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
282 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && this.movement.y == 0 && this.movement.x != 0) {
283 PM_AirAccelerate(this, wishdir, wishspeed2);
285 float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
286 PM_Accelerate(this, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw,
287 PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
290 if (PHYS_AIRCONTROL(this)) {
291 CPM_PM_Aircontrol(this, wishdir, wishspeed2);
295 if (this.com_phys_ground || this.com_phys_water) {
296 if (IS_DUCKED(this)) { wishspeed *= 0.5; }
298 if (this.com_phys_water) {
301 // if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
304 float f = 1 - dt * PHYS_FRICTION(this);
305 f = min(max(0, f), 1);
308 f = wishspeed - this.velocity * wishdir;
310 float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
311 this.velocity += accelspeed * wishdir;
314 // holding jump button swims upward slowly
315 if (jump && !this.viewloc) {
320 // idea: double those
321 this.velocity_z = 200;
325 const float addspeed = wishspeed - this.velocity * wishdir;
327 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
328 this.velocity += accelspeed * wishdir;
331 // water acceleration
332 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
333 PM_ClientMovement_Move(this);
337 if (this.com_phys_ground) {
338 // apply edge friction
339 const float f2 = vlen2(vec2(this.velocity));
341 trace_dphitq3surfaceflags = 0;
342 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
343 // TODO: apply edge friction
344 // apply ground friction
345 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
346 ? PHYS_FRICTION_SLICK(this)
347 : PHYS_FRICTION(this);
350 f = 1 - dt * realfriction
351 * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
355 Mathematical analysis time!
357 Our goal is to invert this mess.
359 For the two cases we get:
360 v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
361 = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
362 v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
364 v = v0 * (1 - dt * PHYS_FRICTION(this))
365 v0 = v / (1 - dt * PHYS_FRICTION(this))
367 These cases would be chosen ONLY if:
368 v0 < PHYS_STOPSPEED(this)
369 v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
370 v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
372 v0 >= PHYS_STOPSPEED(this)
373 v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
374 v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
377 const float addspeed = wishspeed - this.velocity * wishdir;
379 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
380 this.velocity += accelspeed * wishdir;
382 if (IS_CSQC && vdist(this.velocity, >, 0)) {
383 PM_ClientMovement_Move(this);
388 if (IS_CSQC || time >= PHYS_TELEPORT_TIME(this)) {
389 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
392 PM_ClientMovement_Move(this);