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 int buttons_prev = this.buttons_old;
16 this.buttons_old = PHYS_INPUT_BUTTON_MASK(this);
17 this.movement_old = this.movement;
18 this.v_angle_old = this.v_angle;
22 sys_phys_pregame_hold(this);
25 if (PHYS_MOVETYPE(this) == MOVETYPE_NONE) { return; }
26 // when we get here, disableclientprediction cannot be 2
27 this.disableclientprediction = 0;
30 viewloc_PlayerPhysics(this);
32 PM_check_frozen(this);
34 PM_check_blocked(this);
36 float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown; // cvar("g_balance_swamp_moverate");
38 // conveyors: first fix velocity
39 if (this.conveyor.state) { this.velocity -= this.conveyor.movedir; }
40 MUTATOR_CALLHOOK(PlayerPhysics, this);
42 if (!IS_PLAYER(this)) {
43 sys_phys_spectator_control(this);
44 maxspeed_mod = this.spectatorspeed;
46 sys_phys_fixspeed(this, maxspeed_mod);
50 vector midpoint = ((this.absmin + this.absmax) * 0.5);
51 if (pointcontents(midpoint) == CONTENT_WATER) {
52 this.velocity = this.velocity * 0.5;
55 // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
56 // { this.velocity_z = 70; }
61 if (IS_SVQC && !PHYS_FIXANGLE(this)) { this.angles = '0 1 0' * this.v_angle.y; }
62 if (IS_PLAYER(this)) {
63 if (IS_ONGROUND(this)) {
64 PM_check_hitground(this);
66 } else if (IsFlying(this)) {
67 this.wasFlying = true;
69 CheckPlayerJump(this);
72 if (this.flags & FL_WATERJUMP) {
73 this.velocity_x = this.movedir.x;
74 this.velocity_y = this.movedir.y;
75 if (time > PHYS_TELEPORT_TIME(this)
76 || this.waterlevel == WATERLEVEL_NONE
77 || PHYS_WATERJUMP_TIME(this) <= 0
79 this.flags &= ~FL_WATERJUMP;
80 PHYS_TELEPORT_TIME(this) = 0;
81 PHYS_WATERJUMP_TIME(this) = 0;
83 } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod)) {
85 } else if (PHYS_MOVETYPE(this) == MOVETYPE_NOCLIP
86 || PHYS_MOVETYPE(this) == MOVETYPE_FLY
87 || PHYS_MOVETYPE(this) == MOVETYPE_FLY_WORLDONLY
88 || MUTATOR_CALLHOOK(IsFlying, this)) {
89 this.com_phys_friction = PHYS_FRICTION(this);
90 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
91 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
92 sys_phys_simulate(this, dt);
93 } else if (this.waterlevel >= WATERLEVEL_SWIMMING) {
94 PM_swim(this, maxspeed_mod);
95 } else if (time < this.ladder_time) {
96 this.com_phys_friction = PHYS_FRICTION(this);
97 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
98 this.com_phys_acc_rate = PHYS_ACCELERATE(this) * maxspeed_mod;
99 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
100 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
101 this.com_phys_ladder = true;
102 sys_phys_simulate(this, dt);
103 this.com_phys_ladder = false;
104 this.com_phys_gravity = '0 0 0';
105 } else if (ITEMS_STAT(this) & IT_USING_JETPACK) {
106 PM_jetpack(this, maxspeed_mod);
107 } else if (IS_ONGROUND(this)) {
108 if (!WAS_ONGROUND(this)) {
109 emit(phys_land, this);
110 if (this.lastground < time - 0.3) {
111 this.velocity *= (1 - PHYS_FRICTION_ONLAND(this));
114 this.com_phys_vel_max = PHYS_MAXSPEED(this) * maxspeed_mod;
115 this.com_phys_gravity = '0 0 -1' * PHYS_GRAVITY(this) * dt;
116 if (PHYS_ENTGRAVITY(this)) { this.com_phys_gravity *= PHYS_ENTGRAVITY(this); }
117 this.com_phys_ground = true;
118 this.com_phys_vel_2d = true;
119 sys_phys_simulate(this, dt);
120 this.com_phys_vel_2d = false;
121 this.com_phys_ground = false;
122 this.com_phys_gravity = '0 0 0';
124 PM_air(this, buttons_prev, maxspeed_mod);
128 if (IS_ONGROUND(this)) { this.lastground = time; }
129 // conveyors: then break velocity again
130 if (this.conveyor.state) { this.velocity += this.conveyor.movedir; }
131 this.lastflags = this.flags;
133 this.lastclassname = this.classname;
136 void sys_phys_simulate(entity this, float dt)
138 const float g = -this.com_phys_gravity.z;
139 if (!this.com_phys_ground) {
142 // on a spawnfunc_func_ladder
143 // swimming in spawnfunc_func_water
144 UNSET_ONGROUND(this);
146 this.velocity_z += g / 2;
147 this.velocity = this.velocity * (1 - dt * this.com_phys_friction);
148 this.velocity_z += g / 2;
151 makevectors(vmul(this.v_angle, (this.com_phys_vel_2d ? '0 1 0' : '1 1 1')));
152 // wishvel = v_forward * this.movement.x + v_right * this.movement.y + v_up * this.movement.z;
153 vector wishvel = v_forward * this.movement.x
154 + v_right * this.movement.y
155 + '0 0 1' * this.movement.z * (this.com_phys_vel_2d ? 0 : 1);
156 if (this.com_phys_ladder) {
158 wishvel.z = this.oldmovement.x;
160 if (this.ladder_entity.classname == "func_water") {
161 float f = vlen(wishvel);
162 if (f > this.ladder_entity.speed) {
163 wishvel *= (this.ladder_entity.speed / f);
166 this.watertype = this.ladder_entity.skin;
167 f = this.ladder_entity.origin_z + this.ladder_entity.maxs_z;
168 if ((this.origin_z + this.view_ofs_z) < f) {
169 this.waterlevel = WATERLEVEL_SUBMERGED;
170 } else if ((this.origin_z + (this.mins_z + this.maxs_z) * 0.5) < f) {
171 this.waterlevel = WATERLEVEL_SWIMMING;
172 } else if ((this.origin_z + this.mins_z + 1) < f) {
173 this.waterlevel = WATERLEVEL_WETFEET;
175 this.waterlevel = WATERLEVEL_NONE;
176 this.watertype = CONTENT_EMPTY;
181 const vector wishdir = normalize(wishvel);
182 float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
184 if (this.com_phys_ground) {
185 if (IS_DUCKED(this)) { wishspeed *= 0.5; }
187 // apply edge friction
188 const float f2 = vlen2(vec2(this.velocity));
190 trace_dphitq3surfaceflags = 0;
191 tracebox(this.origin, this.mins, this.maxs, this.origin - '0 0 1', MOVE_NOMONSTERS, this);
192 // TODO: apply edge friction
193 // apply ground friction
194 const int realfriction = (trace_dphitq3surfaceflags & Q3SURFACEFLAG_SLICK)
195 ? PHYS_FRICTION_SLICK(this)
196 : PHYS_FRICTION(this);
199 f = 1 - PHYS_INPUT_TIMELENGTH * realfriction
200 * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
204 Mathematical analysis time!
206 Our goal is to invert this mess.
208 For the two cases we get:
209 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * (PHYS_STOPSPEED(this) / v0) * PHYS_FRICTION(this))
210 = v0 - PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
211 v0 = v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this)
213 v = v0 * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
214 v0 = v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
216 These cases would be chosen ONLY if:
217 v0 < PHYS_STOPSPEED(this)
218 v + PHYS_INPUT_TIMELENGTH * PHYS_STOPSPEED(this) * PHYS_FRICTION(this) < PHYS_STOPSPEED(this)
219 v < PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
221 v0 >= PHYS_STOPSPEED(this)
222 v / (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this)) >= PHYS_STOPSPEED(this)
223 v >= PHYS_STOPSPEED(this) * (1 - PHYS_INPUT_TIMELENGTH * PHYS_FRICTION(this))
226 const float addspeed = wishspeed - this.velocity * wishdir;
228 const float accelspeed = min(PHYS_ACCELERATE(this) * PHYS_INPUT_TIMELENGTH * wishspeed, addspeed);
229 this.velocity += accelspeed * wishdir;
231 if (IS_CSQC && vdist(this.velocity, >, 0)) {
232 PM_ClientMovement_Move(this);
237 if (IS_CSQC || time >= PHYS_TELEPORT_TIME(this)) {
238 PM_Accelerate(this, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
240 PM_ClientMovement_Move(this);