5 .int disableclientprediction;
7 void sys_phys_simulate(entity this, float dt);
8 void sys_phys_simulate_simple(entity this, float dt);
10 void sys_phys_postupdate(entity this);
12 void sys_phys_update(entity this, float dt)
14 if (!IS_CLIENT(this)) {
15 sys_phys_simulate_simple(this, dt);
18 sys_in_update(this, dt);
20 sys_phys_fix(this, dt);
21 if (sys_phys_override(this, dt))
24 sys_phys_monitor(this, dt);
26 PHYS_CS(this).movement_old = PHYS_CS(this).movement;
27 PHYS_CS(this).v_angle_old = this.v_angle;
28 PHYS_CS(this).buttons_old = PHYS_INPUT_BUTTON_MASK(this);
32 sys_phys_pregame_hold(this);
35 if (this.move_movetype == MOVETYPE_NONE) { return; }
36 // when we get here, disableclientprediction cannot be 2
37 this.disableclientprediction = (this.move_qcphysics) ? -1 : 0;
40 viewloc_PlayerPhysics(this);
42 PM_check_frozen(this);
44 PM_check_blocked(this);
46 float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown; // cvar("g_balance_swamp_moverate");
48 // conveyors: first fix velocity
49 if (this.conveyor.active) { this.velocity -= this.conveyor.movedir; }
50 MUTATOR_CALLHOOK(PlayerPhysics, this, dt);
52 if (!IS_PLAYER(this)) {
53 sys_phys_spectator_control(this);
54 maxspeed_mod = STAT(SPECTATORSPEED, this);
56 sys_phys_fixspeed(this, maxspeed_mod);
60 vector midpoint = ((this.absmin + this.absmax) * 0.5);
61 int cont = pointcontents(midpoint);
62 if (cont == CONTENT_WATER || cont == CONTENT_LAVA || cont == CONTENT_SLIME) {
63 this.velocity = this.velocity * 0.5;
66 // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
67 // { this.velocity_z = 70; }
69 sys_phys_postupdate(this);
75 if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
76 if (IS_PLAYER(this)) {
77 if (IS_ONGROUND(this)) {
78 PM_check_hitground(this);
80 } else if (IsFlying(this)) {
81 this.wasFlying = true;
83 CheckPlayerJump(this);
86 if (this.flags & FL_WATERJUMP) {
87 this.velocity_x = this.movedir.x;
88 this.velocity_y = this.movedir.y;
89 if (this.waterlevel == WATERLEVEL_NONE
90 || time > PHYS_TELEPORT_TIME(this)
91 || PHYS_WATERJUMP_TIME(this) <= 0
93 this.flags &= ~FL_WATERJUMP;
94 PHYS_TELEPORT_TIME(this) = 0;
95 PHYS_WATERJUMP_TIME(this) = 0;
97 } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod, dt)) {
99 } else if (this.move_movetype == MOVETYPE_NOCLIP
100 || this.move_movetype == MOVETYPE_FLY
101 || this.move_movetype == MOVETYPE_FLY_WORLDONLY
102 || MUTATOR_CALLHOOK(IsFlying, this)) {
103 this.com_phys_friction = PHYS_FRICTION(this);
104 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
105 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
106 this.com_phys_friction_air = true;
107 sys_phys_simulate(this, dt);
108 this.com_phys_friction_air = false;
109 } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
110 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
111 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
112 this.com_phys_water = true;
113 sys_phys_simulate(this, dt);
114 this.com_phys_water = false;
115 this.jumppadcount = 0;
116 } else if (time < this.ladder_time) {
117 this.com_phys_friction = PHYS_FRICTION(this);
118 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
119 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
120 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
121 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
122 this.com_phys_ladder = true;
123 this.com_phys_friction_air = true;
124 sys_phys_simulate(this, dt);
125 this.com_phys_friction_air = false;
126 this.com_phys_ladder = false;
127 this.com_phys_gravity = '0 0 0';
128 } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
129 PM_jetpack(this, maxspeed_mod, dt);
130 } else if (IS_ONGROUND(this)) {
131 if (!WAS_ONGROUND(this)) {
132 emit(phys_land, this);
133 if (this.lastground < time - 0.3) {
134 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
137 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
138 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
139 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
140 this.com_phys_ground = true;
141 this.com_phys_vel_2d = true;
142 sys_phys_simulate(this, dt);
143 this.com_phys_vel_2d = false;
144 this.com_phys_ground = false;
145 this.com_phys_gravity = '0 0 0';
147 this.com_phys_acc_rate_air = PHYS_AIRACCELERATE(this) * min(maxspeed_mod, 1);
148 this.com_phys_acc_rate_air_stop = PHYS_AIRSTOPACCELERATE(this) * maxspeed_mod;
149 this.com_phys_acc_rate_air_strafe = PHYS_AIRSTRAFEACCELERATE(this) * maxspeed_mod;
150 this.com_phys_vel_max_air_strafe = PHYS_MAXAIRSTRAFESPEED(this) * maxspeed_mod;
151 this.com_phys_vel_max_air = PHYS_MAXAIRSPEED(this) * maxspeed_mod;
152 this.com_phys_vel_max = PHYS_MAXAIRSPEED(this) * min(maxspeed_mod, 1);
153 this.com_phys_air = true;
154 this.com_phys_vel_2d = true;
155 sys_phys_simulate(this, dt);
156 this.com_phys_vel_2d = false;
157 this.com_phys_air = false;
160 sys_phys_postupdate(this);
163 void sys_phys_postupdate(entity this)
165 if (IS_ONGROUND(this)) { this.lastground = time; }
166 // conveyors: then break velocity again
167 if (this.conveyor.active) { this.velocity += this.conveyor.movedir; }
168 this.lastflags = this.flags;
170 this.lastclassname = this.classname;
174 void sys_phys_simulate(entity this, float dt)
176 if (!this.com_phys_ground && !this.com_phys_air) {
179 // on a spawnfunc_func_ladder
180 // swimming in spawnfunc_func_water
182 UNSET_ONGROUND(this);
184 if (this.com_phys_friction_air) {
185 const vector g = -this.com_phys_gravity;
186 this.velocity_z += g.z / 2;
187 this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
188 this.velocity_z += g.z / 2;
192 if (this.com_phys_water) {
193 // water jump only in certain situations
194 // this mimics quakeworld code
195 if (this.com_in_jump && this.waterlevel == WATERLEVEL_SWIMMING && this.velocity_z >= -180 && !this.viewloc) {
196 vector yawangles = '0 1 0' * this.v_angle.y;
197 makevectors(yawangles);
198 vector forward = v_forward;
199 vector spot = this.origin + 24 * forward;
201 traceline(spot, spot, MOVE_NOMONSTERS, this);
202 if (trace_startsolid) {
204 traceline(spot, spot, MOVE_NOMONSTERS, this);
205 if (!trace_startsolid) {
206 this.velocity = forward * 50;
207 this.velocity_z = 310;
208 UNSET_ONGROUND(this);
214 makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
215 // wishvel = v_forward * PHYS_CS(this).movement.x + v_right * PHYS_CS(this).movement.y + v_up * PHYS_CS(this).movement.z;
216 vector wishvel = v_forward * PHYS_CS(this).movement.x
217 + v_right * PHYS_CS(this).movement.y
218 + '0 0 1' * PHYS_CS(this).movement.z * (this.com_phys_vel_2d ? 0 : 1);
219 if (this.com_phys_water) {
220 if (PHYS_INPUT_BUTTON_CROUCH(this)) {
221 wishvel.z = -PHYS_MAXSPEED(this);
224 wishvel.z = -160; // drift anyway
225 } else if (wishvel == '0 0 0') {
226 wishvel = '0 0 -60'; // drift towards bottom
229 if (this.com_phys_ladder) {
231 wishvel.z = PHYS_CS(this).movement_old.x;
233 if (this.ladder_entity.classname == "func_water") {
234 float f = vlen(wishvel);
235 if (f > this.ladder_entity.speed) {
236 wishvel *= (this.ladder_entity.speed / f);
239 this.watertype = this.ladder_entity.skin;
240 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
241 if ((this.origin_z + this.view_ofs_z) < f) {
242 this.waterlevel = WATERLEVEL_SUBMERGED;
243 } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
244 this.waterlevel = WATERLEVEL_SWIMMING;
245 } else if ((this.origin_z + this.mins_z + 1) < f) {
246 this.waterlevel = WATERLEVEL_WETFEET;
248 this.waterlevel = WATERLEVEL_NONE;
249 this.watertype = CONTENT_EMPTY;
254 const vector wishdir = normalize(wishvel);
255 float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
257 if (this.com_phys_air) {
258 if ((IS_SVQC && time >= PHYS_TELEPORT_TIME(this))
259 || (IS_CSQC && PHYS_WATERJUMP_TIME(this) <= 0)) {
260 // apply air speed limit
261 float airaccelqw = PHYS_AIRACCEL_QW(this);
262 float wishspeed0 = wishspeed;
263 const float maxairspd = this.com_phys_vel_max;
264 wishspeed = min(wishspeed, maxairspd);
265 if (IS_DUCKED(this)) {
268 float airaccel = this.com_phys_acc_rate_air;
270 float accelerating = (this.velocity * wishdir > 0);
271 float wishspeed2 = wishspeed;
274 if (PHYS_AIRSTOPACCELERATE(this)) {
275 vector curdir = normalize(vec2(this.velocity));
276 airaccel += (this.com_phys_acc_rate_air_stop - airaccel) * max(0, -(curdir * wishdir));
278 // note that for straight forward jumping:
279 // step = accel * dt * wishspeed0;
280 // accel = bound(0, wishspeed - vel_xy_current, step) * accelqw + step * (1 - accelqw);
282 // dv/dt = accel * maxspeed (when slow)
283 // dv/dt = accel * maxspeed * (1 - accelqw) (when fast)
284 // log dv/dt = logaccel + logmaxspeed (when slow)
285 // log dv/dt = logaccel + logmaxspeed + log(1 - accelqw) (when fast)
286 float strafity = IsMoveInDirection(PHYS_CS(this).movement, -90) + IsMoveInDirection(PHYS_CS(this).movement, +90); // if one is nonzero, other is always zero
287 if (PHYS_MAXAIRSTRAFESPEED(this)) {
290 GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
292 if (PHYS_AIRSTRAFEACCELERATE(this)) {
293 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
295 if (PHYS_AIRSTRAFEACCEL_QW(this)) {
297 (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
299 (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
303 if (PHYS_WARSOWBUNNY_TURNACCEL(this) && accelerating && PHYS_CS(this).movement.y == 0 && PHYS_CS(this).movement.x != 0) {
304 PM_AirAccelerate(this, dt, wishdir, wishspeed2);
306 float sidefric = maxairspd ? (PHYS_AIRACCEL_SIDEWAYS_FRICTION(this) / maxairspd) : 0;
307 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed0, airaccel, airaccelqw,
308 PHYS_AIRACCEL_QW_STRETCHFACTOR(this), sidefric, PHYS_AIRSPEEDLIMIT_NONQW(this));
311 if (PHYS_AIRCONTROL(this)) {
312 CPM_PM_Aircontrol(this, dt, wishdir, wishspeed2);
316 if (this.com_phys_ground && IS_DUCKED(this)) { wishspeed *= 0.5; }
317 if (this.com_phys_water) {
320 // if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
323 float f = 1 - dt * PHYS_FRICTION(this);
324 f = min(max(0, f), 1);
327 f = wishspeed - this.velocity * wishdir;
329 float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
330 this.velocity += accelspeed * wishdir;
333 // holding jump button swims upward slowly
334 if (this.com_in_jump && !this.viewloc) {
339 // idea: double those
340 this.velocity_z = 200;
341 if (this.waterlevel >= WATERLEVEL_SUBMERGED) {
342 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
347 const float addspeed = wishspeed - this.velocity * wishdir;
349 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
350 this.velocity += accelspeed * wishdir;
353 // water acceleration
354 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
358 if (this.com_phys_ground) {
359 // apply edge friction
360 const float f2 = vlen2(vec2(this.velocity));
362 trace_dphitq3surfaceflags = 0;
363 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
364 // TODO: apply edge friction
365 // apply ground friction
366 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
367 ? PHYS_FRICTION_SLICK(this)
368 : PHYS_FRICTION(this);
371 f = 1 - dt * realfriction
372 * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
376 Mathematical analysis time!
378 Our goal is to invert this mess.
380 For the two cases we get:
381 v = v0 * (1 - dt * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
382 = v0 - dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
383 v0 = v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
385 v = v0 * (1 - dt * PHYS_FRICTION(this))
386 v0 = v / (1 - dt * PHYS_FRICTION(this))
388 These cases would be chosen ONLY if:
389 v0 < PHYS_STOPSPEED(this)
390 v + dt * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
391 v < PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
393 v0 >= PHYS_STOPSPEED(this)
394 v / (1 - dt * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
395 v >= PHYS_STOPSPEED(this) * (1 - dt * PHYS_FRICTION(this))
398 const float addspeed = wishspeed - this.velocity * wishdir;
400 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
401 this.velocity += accelspeed * wishdir;
406 if (IS_CSQC ? PHYS_WATERJUMP_TIME(this) <= 0 : time >= PHYS_TELEPORT_TIME(this)) {
407 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
412 .entity groundentity;
413 /** for other entities */
414 void sys_phys_simulate_simple(entity this, float dt)
416 vector mn = this.mins;
417 vector mx = this.maxs;
420 if (this.com_phys_gravity_factor && !g) g = '0 0 -1' * PHYS_GRAVITY(NULL);
422 vector acc = this.com_phys_acc;
423 vector vel = this.com_phys_vel;
424 vector pos = this.com_phys_pos;
430 this.angles += dt * this.avelocity;
432 for (int i = 0; i < MAX_CLIP_PLANES && movetime > 0; i++) {
433 vector push = vel * movetime;
435 vector p1 = p0 + push;
437 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
438 if (!trace_startsolid) {
439 bool hit = trace_fraction < 1;
441 entity ent = trace_ent;
442 // SV_LinkEdict_TouchAreaGrid
443 if (this.solid != SOLID_NOT) {
444 FOREACH_ENTITY_RADIUS_ORDERED(0.5 * (this.absmin + this.absmax), 0.5 * vlen(this.absmax - this.absmin), true, {
445 if (it.solid != SOLID_TRIGGER || it == this) continue;
446 if (gettouch(it) && boxesoverlap(it.absmin, it.absmax, this.absmin, this.absmax)) {
447 // SV_LinkEdict_TouchAreaGrid_Call
448 trace_allsolid = false;
449 trace_startsolid = false;
451 trace_inwater = false;
453 trace_endpos = it.origin;
454 trace_plane_normal = '0 0 1';
455 trace_plane_dist = 0;
457 trace_dpstartcontents = 0;
458 trace_dphitcontents = 0;
459 trace_dphitq3surfaceflags = 0;
460 trace_dphittexturename = string_null;
461 gettouch(it)(this, it);
466 if (hit && this.solid >= SOLID_TRIGGER && (!IS_ONGROUND(this) || this.groundentity != ent)) {
467 // SV_Impact (ent, trace);
468 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
469 void(entity, entity) touched = gettouch(this);
470 if (touched && this.solid != SOLID_NOT) {
473 void(entity, entity) touched2 = gettouch(ent);
474 if (this && ent && touched2 && ent.solid != SOLID_NOT) {
475 trace_endpos = ent.origin;
476 trace_plane_normal *= -1;
477 trace_plane_dist *= -1;
479 trace_dpstartcontents = 0;
480 trace_dphitcontents = 0;
481 trace_dphitq3surfaceflags = 0;
482 trace_dphittexturename = string_null;
488 if (wasfreed(this)) { return; }
489 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
490 if (trace_fraction == 1) { break; }
491 movetime *= 1 - min(1, trace_fraction);
492 ClipVelocity(vel, trace_plane_normal, vel, 1);
495 this.com_phys_acc = acc;
496 this.com_phys_vel = vel;
497 this.com_phys_pos = pos;
498 setorigin(this, this.com_phys_pos);
501 void sys_phys_update_single(entity this)
503 sys_phys_simulate_simple(this, frametime);
507 const int PHYSICS_TRACE_PLANE_MAX = 5;
509 // NOTE: currently unsuitable for players
510 void sys_phys_update(entity this, float dt)
512 // x: { 60: 0.5, 45: ~0.7, 30: ~0.9 }, from: 'vec2(0, 1) * vec2(cos(90 - x), sin(90 - x))'
513 // read as 'within x degrees'
514 float maxgrounddot = 0.5;
515 float maxstepdot = 0.7;
516 vector upvec = '0 0 1';
517 float groundsnap = 1;
518 bool jump = this.com_in_jump;
519 bool jumpstep = true;
521 vector mn = this.mins;
522 vector mx = this.maxs;
524 vector acc = this.com_phys_acc;
525 vector vel = this.com_phys_vel;
526 vector pos = this.com_phys_pos_prev = this.com_phys_pos;
527 bool onground = this.com_phys_ground;
529 bool nogravityonground = this.com_phys_nogravityonground;
530 float stepheight = this.com_phys_stepheight;
531 float stepdownheight = -stepheight;
532 float jumpvel = this.com_phys_jumpvel;
533 float bounce = this.com_phys_bounce;
534 float friction = this.com_phys_friction;
535 float gravity = this.com_phys_gravity.z;
536 bool noclip = this.com_phys_noclip;
540 nogravityonground = false;
543 vector g = upvec * -gravity;
545 // apply accelaration in two steps: https://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html
546 // alternatives: rk4, verlet, euler
547 vel += (acc + g) * dt / 2;
549 if (onground || noclip)
551 if (nogravityonground)
554 if (vel * upvec < 0) vel = vec_reflect(vel, upvec, 0); // kill downward velocity
558 vel += upvec * jumpvel;
560 else // the first landing frame is free
563 vector slide = noclip ? vel : vec_reflect(vel, upvec, 0);
564 vector push = vel - slide;
566 slide *= 1 - friction * dt;
570 vector step = vel * dt;
572 bool foundground = false; // assume until proven otherwise
573 if (nogravityonground) foundground = true; // override
581 for (int i = 0; i < PHYSICS_TRACE_PLANE_MAX; ++i)
584 vector p1 = p0 + step;
585 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
586 float frac = trace_fraction;
587 vector norm = trace_plane_normal;
591 if (steplimit > 0 && onground && vel * upvec <= 0)
594 tracebox(p1, mn, mx, p1 + upvec * stepdownheight, MOVE_NORMAL, this);
595 if (trace_fraction == 1)
599 else if (trace_plane_normal * upvec >= maxstepdot)
602 step += upvec * (stepdownheight * trace_fraction);
609 if (norm * upvec >= maxgrounddot) foundground = true;
610 if (steplimit > 0 && (jumpstep || onground)) // try: vel * upvec >= 0
613 vector slide = vec_reflect(step, upvec, 0); // remove fall component
614 vector p1 = p0 + slide; // step is here
615 tracebox(p1 + upvec * stepheight, mn, mx, p1, MOVE_NORMAL, this);
616 if (trace_fraction < 1 && trace_plane_normal * upvec >= maxstepdot)
618 // there is a step in front of us, get above it
619 // TODO: not if it's slippery (slick)
620 vector stepup = upvec * (1 - trace_fraction) * stepheight;
621 tracebox(p0, mn, mx, p0 + stepup, MOVE_NORMAL, this);
622 if (trace_fraction == 1)
625 tracebox(p0 + stepup, mn, mx, p1 + stepup, MOVE_NORMAL, this);
626 if (trace_fraction == 1)
631 if (vel * upvec < 0) vel = vec_reflect(vel, upvec, 0); // kill downward velocity
641 vel = vec_reflect(vel, norm, bounce);
642 step = (1 - frac) * vel * dt;
646 if (nogravityonground)
648 vector p1 = pos + step;
649 tracebox(p1, mn, mx, p1 - groundsnap * upvec, MOVE_NORMAL, this);
650 foundground = trace_plane_normal * upvec >= maxgrounddot;
657 if (onground) emit(phys_stepfall, this);
661 if (!onground) emit(phys_stepland, this);
663 onground = foundground;
666 vel += (acc + g) * dt / 2;
668 this.com_phys_acc = acc;
669 this.com_phys_vel = vel;
670 this.com_phys_pos = pos;
671 this.com_phys_ground = onground;