]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/ecs/systems/physics.qc
Merge branch 'master' into Mario/ecs_halfbaked
[xonotic/xonotic-data.pk3dir.git] / qcsrc / ecs / systems / physics.qc
1 #include "physics.qh"
2 #include "input.qh"
3
4 #if XONOTIC
5 .int disableclientprediction;
6
7 void sys_phys_simulate(entity this, float dt);
8 void sys_phys_simulate_simple(entity this, float dt);
9
10 void sys_phys_postupdate(entity this);
11
12 void sys_phys_update(entity this, float dt)
13 {
14         if (!IS_CLIENT(this)) {
15                 sys_phys_simulate_simple(this, dt);
16                 return;
17         }
18         sys_in_update(this, dt);
19
20         sys_phys_fix(this, dt);
21         if (sys_phys_override(this, dt))
22                 return;
23
24         sys_phys_monitor(this, dt);
25
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);
29
30         sys_phys_ai(this);
31
32         sys_phys_pregame_hold(this);
33
34         if (IS_SVQC) {
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;
38         }
39
40         viewloc_PlayerPhysics(this);
41
42         PM_check_frozen(this);
43
44         PM_check_blocked(this);
45
46         float maxspeed_mod = (!this.in_swamp) ? 1 : this.swamp_slowdown;  // cvar("g_balance_swamp_moverate");
47
48 // conveyors: first fix velocity
49         if (this.conveyor.active) { this.velocity -= this.conveyor.movedir; }
50         MUTATOR_CALLHOOK(PlayerPhysics, this, dt);
51
52         if (!IS_PLAYER(this)) {
53                 sys_phys_spectator_control(this);
54                 maxspeed_mod = STAT(SPECTATORSPEED, this);
55         }
56         sys_phys_fixspeed(this, maxspeed_mod);
57
58         if (IS_DEAD(this)) {
59                 // handle water here
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;
64
65                         // do we want this?
66                         // if(pointcontents(midpoint + '0 0 2') == CONTENT_WATER)
67                         // { this.velocity_z = 70; }
68                 }
69                 sys_phys_postupdate(this);
70                 return;
71         }
72
73         PM_check_slick(this);
74
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);
79                         PM_Footsteps(this);
80                 } else if (IsFlying(this)) {
81                         this.wasFlying = true;
82                 }
83                 CheckPlayerJump(this);
84         }
85
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
92                    ) {
93                         this.flags &= ~FL_WATERJUMP;
94                         PHYS_TELEPORT_TIME(this) = 0;
95                         PHYS_WATERJUMP_TIME(this) = 0;
96                 }
97         } else if (MUTATOR_CALLHOOK(PM_Physics, this, maxspeed_mod, dt)) {
98                 // handled
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));
135                         }
136                 }
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';
146         } else {
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;
158         }
159
160         sys_phys_postupdate(this);
161 }
162
163 void sys_phys_postupdate(entity this)
164 {
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;
169
170         this.lastclassname = this.classname;
171 }
172
173 /** for players */
174 void sys_phys_simulate(entity this, float dt)
175 {
176         if (!this.com_phys_ground && !this.com_phys_air) {
177                 // noclipping
178                 // flying
179                 // on a spawnfunc_func_ladder
180                 // swimming in spawnfunc_func_water
181                 // swimming
182                 UNSET_ONGROUND(this);
183
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;
189                 }
190         }
191
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;
200                         spot_z += 8;
201                         traceline(spot, spot, MOVE_NOMONSTERS, this);
202                         if (trace_startsolid) {
203                                 spot_z += 24;
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);
209                                         SET_JUMP_HELD(this);
210                                 }
211                         }
212                 }
213         }
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);
222                 }
223                 if (this.viewloc) {
224                         wishvel.z = -160;    // drift anyway
225                 } else if (wishvel == '0 0 0') {
226                         wishvel = '0 0 -60'; // drift towards bottom
227                 }
228         }
229         if (this.com_phys_ladder) {
230                 if (this.viewloc) {
231                         wishvel.z = PHYS_CS(this).movement_old.x;
232                 }
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);
237                         }
238
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;
247                         } else {
248                                 this.waterlevel = WATERLEVEL_NONE;
249                                 this.watertype = CONTENT_EMPTY;
250                         }
251                 }
252         }
253         // acceleration
254         const vector wishdir = normalize(wishvel);
255         float wishspeed = min(vlen(wishvel), this.com_phys_vel_max);
256
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)) {
266                                 wishspeed *= 0.5;
267                         }
268                         float airaccel = this.com_phys_acc_rate_air;
269
270                         float accelerating = (this.velocity * wishdir > 0);
271                         float wishspeed2 = wishspeed;
272
273                         // CPM: air control
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));
277                         }
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);
281                         // -->
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)) {
288                                 wishspeed =
289                                     min(wishspeed,
290                                         GeomLerp(this.com_phys_vel_max_air, strafity, this.com_phys_vel_max_air_strafe));
291                         }
292                         if (PHYS_AIRSTRAFEACCELERATE(this)) {
293                                 airaccel = GeomLerp(airaccel, strafity, this.com_phys_acc_rate_air_strafe);
294                         }
295                         if (PHYS_AIRSTRAFEACCEL_QW(this)) {
296                                 airaccelqw =
297                                     (((strafity > 0.5 ? PHYS_AIRSTRAFEACCEL_QW(this) : PHYS_AIRACCEL_QW(this)) >= 0) ? +1 : -1)
298                                     *
299                                     (1 - GeomLerp(1 - fabs(PHYS_AIRACCEL_QW(this)), strafity, 1 - fabs(PHYS_AIRSTRAFEACCEL_QW(this))));
300                         }
301                         // !CPM
302
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);
305                         } else {
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));
309                         }
310
311                         if (PHYS_AIRCONTROL(this)) {
312                                 CPM_PM_Aircontrol(this, dt, wishdir, wishspeed2);
313                         }
314                 }
315         } else {
316                 if (this.com_phys_ground && IS_DUCKED(this)) { wishspeed *= 0.5; }
317                 if (this.com_phys_water) {
318                         wishspeed *= 0.7;
319
320                         //      if (PHYS_WATERJUMP_TIME(this) <= 0) // TODO: use
321                         {
322                                 // water friction
323                                 float f = 1 - dt * PHYS_FRICTION(this);
324                                 f = min(max(0, f), 1);
325                                 this.velocity *= f;
326
327                                 f = wishspeed - this.velocity * wishdir;
328                                 if (f > 0) {
329                                         float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, f);
330                                         this.velocity += accelspeed * wishdir;
331                                 }
332
333                                 // holding jump button swims upward slowly
334                                 if (this.com_in_jump && !this.viewloc) {
335                                         // was:
336                                         // lava: 50
337                                         // slime: 80
338                                         // water: 100
339                                         // idea: double those
340                                         this.velocity_z = 200;
341                                         if (this.waterlevel >= WATERLEVEL_SUBMERGED) {
342                                                 this.velocity_z = PHYS_MAXSPEED(this) * 0.7;
343                                         }
344                                 }
345                         }
346                         if (this.viewloc) {
347                                 const float addspeed = wishspeed - this.velocity * wishdir;
348                                 if (addspeed > 0) {
349                                         const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
350                                         this.velocity += accelspeed * wishdir;
351                                 }
352                         } else {
353                                 // water acceleration
354                                 PM_Accelerate(this, dt, wishdir, wishspeed, wishspeed, this.com_phys_acc_rate, 1, 0, 0, 0);
355                         }
356                         return;
357                 }
358                 if (this.com_phys_ground) {
359                         // apply edge friction
360                         const float f2 = vlen2(vec2(this.velocity));
361                         if (f2 > 0) {
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);
369
370                                 float f = sqrt(f2);
371                                 f = 1 - dt * realfriction
372                                     * ((f < PHYS_STOPSPEED(this)) ? (PHYS_STOPSPEED(this) / f) : 1);
373                                 f = max(0, f);
374                                 this.velocity *= f;
375                                 /*
376                                    Mathematical analysis time!
377
378                                    Our goal is to invert this mess.
379
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)
384                                    and
385                                     v = v0 * (1 - dt * PHYS_FRICTION(this))
386                                     v0 = v / (1 - dt * PHYS_FRICTION(this))
387
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))
392                                    and, respectively:
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))
396                                  */
397                         }
398                         const float addspeed = wishspeed - this.velocity * wishdir;
399                         if (addspeed > 0) {
400                                 const float accelspeed = min(PHYS_ACCELERATE(this) * dt * wishspeed, addspeed);
401                                 this.velocity += accelspeed * wishdir;
402                         }
403                         return;
404                 }
405
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);
408                 }
409         }
410 }
411
412 .entity groundentity;
413 /** for other entities */
414 void sys_phys_simulate_simple(entity this, float dt)
415 {
416         vector mn = this.mins;
417         vector mx = this.maxs;
418
419         vector g = '0 0 0';
420         if (this.com_phys_gravity_factor && !g) g = '0 0 -1' * PHYS_GRAVITY(NULL);
421
422         vector acc = this.com_phys_acc;
423         vector vel = this.com_phys_vel;
424         vector pos = this.com_phys_pos;
425
426         // SV_Physics_Toss
427
428         vel += g * dt;
429
430         this.angles += dt * this.avelocity;
431         float movetime = dt;
432         for (int i = 0; i < MAX_CLIP_PLANES && movetime > 0; i++) {
433                 vector push = vel * movetime;
434                 vector p0 = pos;
435                 vector p1 = p0 + push;
436                 // SV_PushEntity
437                 tracebox(p0, mn, mx, p1, MOVE_NORMAL, this);
438                 if (!trace_startsolid) {
439                         bool hit = trace_fraction < 1;
440                         pos = trace_endpos;
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;
450                                             trace_fraction = 1;
451                                             trace_inwater = false;
452                                             trace_inopen = true;
453                                             trace_endpos = it.origin;
454                                             trace_plane_normal = '0 0 1';
455                                             trace_plane_dist = 0;
456                                             trace_ent = this;
457                                             trace_dpstartcontents = 0;
458                                             trace_dphitcontents = 0;
459                                             trace_dphitq3surfaceflags = 0;
460                                             trace_dphittexturename = string_null;
461                                             gettouch(it)(this, it);
462                                             vel = this.velocity;
463                                         }
464                                 });
465                         }
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) {
471                                         touched(ent, this);
472                                 }
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;
478                                         trace_ent = this;
479                                         trace_dpstartcontents = 0;
480                                         trace_dphitcontents = 0;
481                                         trace_dphitq3surfaceflags = 0;
482                                         trace_dphittexturename = string_null;
483                                         touched2(this, ent);
484                                 }
485                         }
486                 }
487                 // end SV_PushEntity
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);
493         }
494
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);
499 }
500
501 void sys_phys_update_single(entity this)
502 {
503         sys_phys_simulate_simple(this, frametime);
504 }
505
506 #else
507 const int PHYSICS_TRACE_PLANE_MAX = 5;
508
509 // NOTE: currently unsuitable for players
510 void sys_phys_update(entity this, float dt)
511 {
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;
520
521         vector mn = this.mins;
522         vector mx = this.maxs;
523
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;
528
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;
537         if (noclip)
538         {
539                 jump = false;
540                 nogravityonground = false;
541         }
542
543         vector g = upvec * -gravity;
544
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;
548         {
549                 if (onground || noclip)
550                 {
551                         if (nogravityonground)
552                         {
553                                 g = '0 0 0';
554                                 if (vel * upvec < 0) vel = vec_reflect(vel, upvec, 0);  // kill downward velocity
555                         }
556                         if (jump)
557                         {
558                                 vel += upvec * jumpvel;
559                         }
560                         else  // the first landing frame is free
561                         {
562                                 // friction
563                                 vector slide = noclip ? vel : vec_reflect(vel, upvec, 0);
564                                 vector push = vel - slide;
565                                 // TODO: slick
566                                 slide *= 1 - friction * dt;
567                                 vel = slide + push;
568                         }
569                 }
570                 vector step = vel * dt;
571                 bool pass = false;
572                 bool foundground = false;                  // assume until proven otherwise
573                 if (nogravityonground) foundground = true; // override
574                 bool steplimit = 1;
575                 if (noclip)
576                 {
577                         pass = true;
578                 }
579                 else
580                 {
581                         for (int i = 0; i < PHYSICS_TRACE_PLANE_MAX; ++i)
582                         {
583                                 vector p0 = pos;
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;
588                                 if (frac == 1)
589                                 {
590                                         // all clear
591                                         if (steplimit > 0 && onground && vel * upvec <= 0)
592                                         {
593                                                 // can we step down?
594                                                 tracebox(p1, mn, mx, p1 + upvec * stepdownheight, MOVE_NORMAL, this);
595                                                 if (trace_fraction == 1)
596                                                 {
597                                                         // no stairs here
598                                                 }
599                                                 else if (trace_plane_normal * upvec >= maxstepdot)
600                                                 {
601                                                         // step down
602                                                         step += upvec * (stepdownheight * trace_fraction);
603                                                 }
604                                         }
605                                         pass = true;
606                                         break;
607                                 }
608                                 // hit something
609                                 if (norm * upvec >= maxgrounddot) foundground = true;
610                                 if (steplimit > 0 && (jumpstep || onground))    // try: vel * upvec >= 0
611                                 {
612                                         // can we step up?
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)
617                                         {
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)
623                                                 {
624                                                         // go over
625                                                         tracebox(p0 + stepup, mn, mx, p1 + stepup, MOVE_NORMAL, this);
626                                                         if (trace_fraction == 1)
627                                                         {
628                                                                 // all clear
629                                                                 steplimit -= 1;
630                                                                 pos += stepup;
631                                                                 if (vel * upvec < 0) vel = vec_reflect(vel, upvec, 0);  // kill downward velocity
632                                                                 step = p1 - p0;
633                                                                 pass = true;
634                                                                 break;
635                                                         }
636                                                 }
637                                         }
638                                 }
639                                 // no stairs here
640                                 pos += frac * step;
641                                 vel = vec_reflect(vel, norm, bounce);
642                                 step = (1 - frac) * vel * dt;
643                                 continue;
644                         }
645                 }
646                 if (nogravityonground)
647                 {
648                         vector p1 = pos + step;
649                         tracebox(p1, mn, mx, p1 - groundsnap * upvec, MOVE_NORMAL, this);
650                         foundground = trace_plane_normal * upvec >= maxgrounddot;
651                 }
652                 if (pass)
653                 {
654                         pos += step;
655                         if (!foundground)
656                         {
657                                 if (onground) emit(phys_stepfall, this);
658                         }
659                         else
660                         {
661                                 if (!onground) emit(phys_stepland, this);
662                         }
663                         onground = foundground;
664                 }
665         }
666         vel += (acc + g) * dt / 2;
667
668         this.com_phys_acc = acc;
669         this.com_phys_vel = vel;
670         this.com_phys_pos = pos;
671         this.com_phys_ground = onground;
672 }
673 #endif