1
// physics.cpp: no physics books were hurt nor consulted in the construction of this code.
2
// All physics computations and constants were invented on the fly and simply tweaked until
3
// they "felt right", and have no basis in reality. Collision detection is simplistic but
4
// very robust (uses discrete steps at fixed fps).
9
float raycube(const vec &o, const vec &ray, vec &surface)
11
surface = vec(0, 0, 0);
13
if(ray.iszero()) return -1;
16
float dist = 0, dx = 0, dy = 0, dz = 0;
20
int x = int(v.x), y = int(v.y);
21
if(x < 0 || y < 0 || x >= ssize || y >= ssize) return -1;
23
float floor = s->floor, ceil = s->ceil;
24
if(s->type==FHF) floor -= s->vdelta/4.0f;
25
if(s->type==CHF) ceil += s->vdelta/4.0f;
26
if(SOLID(s) || v.z < floor || v.z > ceil)
28
if((!dx && !dy) || s->wtex==DEFAULT_SKY || (!SOLID(s) && v.z > ceil && s->ctex==DEFAULT_SKY)) return -1;
29
if(s->type!=CORNER)// && s->type!=FHF && s->type!=CHF)
31
if(dx<dy) surface.x = ray.x>0 ? -1 : 1;
32
else surface.y = ray.y>0 ? -1 : 1;
33
sqr *n = S(x+(int)surface.x, y+(int)surface.y);
34
if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
36
surface = dx<dy ? vec(0, ray.y>0 ? -1 : 1, 0) : vec(ray.x>0 ? -1 : 1, 0, 0);
37
n = S(x+(int)surface.x, y+(int)surface.y);
38
if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
39
surface = vec(0, 0, ray.z>0 ? -1 : 1);
42
dist = max(dist-0.1f, 0.0f);
45
dx = ray.x ? (x + (ray.x > 0 ? 1 : 0) - v.x)/ray.x : 1e16f;
46
dy = ray.y ? (y + (ray.y > 0 ? 1 : 0) - v.y)/ray.y : 1e16f;
47
dz = ray.z ? ((ray.z > 0 ? ceil : floor) - v.z)/ray.z : 1e16f;
48
if(dz < dx && dz < dy)
50
if(ray.z>0 && s->ctex==DEFAULT_SKY) return -1;
51
if(s->type!=FHF && s->type!=CHF) surface.z = ray.z>0 ? -1 : 1;
55
float disttonext = 0.1f + min(dx, dy);
56
v.add(vec(ray).mul(disttonext));
62
bool raycubelos(const vec &from, const vec &to, float margin)
66
float limit = dir.magnitude();
69
float dist = raycube(from, dir, surface);
70
return dist > max(limit - margin, 0.0f);
73
physent *hitplayer = NULL;
75
bool plcollide(physent *d, physent *o, float &headspace, float &hi, float &lo) // collide with physent
77
if(o->state!=CS_ALIVE || !o->cancollide) return true;
78
const float r = o->radius+d->radius, dx = o->o.x-d->o.x, dy = o->o.y-d->o.y;
79
const float deyeheight = d->eyeheight, oeyeheight = o->eyeheight;
80
if(d->type==ENT_PLAYER && o->type==ENT_PLAYER ? dx*dx + dy*dy < r*r : fabs(dx)<r && fabs(dy)<r)
82
if(d->o.z-deyeheight<o->o.z-oeyeheight) { if(o->o.z-oeyeheight<hi) hi = o->o.z-oeyeheight-1; }
83
else if(o->o.z+o->aboveeye>lo) lo = o->o.z+o->aboveeye+1;
85
if(fabs(o->o.z-d->o.z)<o->aboveeye+deyeheight) { hitplayer = o; return false; }
86
headspace = d->o.z-o->o.z-o->aboveeye-deyeheight;
87
if(headspace<0) headspace = 10;
92
bool cornertest(int mip, int x, int y, int dx, int dy, int &bx, int &by, int &bs) // recursively collide with a mipmapped corner cube
95
int mfactor = sfactor - mip;
96
bool stest = SOLID(SWS(w, x+dx, y, mfactor)) && SOLID(SWS(w, x, y+dy, mfactor));
100
if(SWS(wmip[mip], x, y, mfactor-1)->type==CORNER)
105
return cornertest(mip, x, y, dx, dy, bx, by, bs);
110
bool mmcollide(physent *d, float &hi, float &lo) // collide with a mapmodel
112
const float eyeheight = d->eyeheight;
113
const float playerheight = eyeheight + d->aboveeye;
119
if(fabs(e.x-d->o.x) < e.attr2 + d->radius && fabs(e.y-d->o.y) < e.attr3 + d->radius)
121
const float cz = float(S(e.x, e.y)->floor+e.attr1), ch = float(e.attr4);
122
const float dz = d->o.z-d->eyeheight;
123
if(dz < cz - 0.001) { if(cz<hi) hi = cz; }
124
else if(cz+ch>lo) lo = cz+ch;
125
if(hi-lo < playerheight) return false;
128
else if(e.type==MAPMODEL)
130
mapmodelinfo &mmi = getmminfo(e.attr2);
131
if(!&mmi || !mmi.h) continue;
132
const float r = mmi.rad+d->radius;
133
if(fabs(e.x-d->o.x)<r && fabs(e.y-d->o.y)<r)
135
const float mmz = float(S(e.x, e.y)->floor+mmi.zoff+e.attr3);
136
const float dz = d->o.z-eyeheight;
137
if(dz<mmz) { if(mmz<hi) hi = mmz; }
138
else if(mmz+mmi.h>lo) lo = mmz+mmi.h;
139
if(hi-lo < playerheight) return false;
146
bool objcollide(physent *d, const vec &objpos, float objrad, float objheight) // collide with custom/typeless objects
148
const float r = d->radius+objrad;
149
if(fabs(objpos.x-d->o.x)<r && fabs(objpos.y-d->o.y)<r)
151
const float maxdist = (d->eyeheight+d->aboveeye+objheight)/2.0f;
152
const float dz = d->o.z+(-d->eyeheight+d->aboveeye)/2.0f;
153
const float objz = objpos.z+objheight/2.0f;
154
return dz-objz <= maxdist && dz-objz >= -maxdist;
159
// all collision happens here
160
// spawn is a dirty side effect used in spawning
161
// drop & rise are supplied by the physics below to indicate gravity/push for current mini-timestep
162
static int cornersurface = 0;
164
bool collide(physent *d, bool spawn, float drop, float rise)
167
const float fx1 = d->o.x-d->radius; // figure out integer cube rectangle this entity covers in map
168
const float fy1 = d->o.y-d->radius;
169
const float fx2 = d->o.x+d->radius;
170
const float fy2 = d->o.y+d->radius;
171
const int x1 = int(fx1);
172
const int y1 = int(fy1);
173
const int x2 = int(fx2);
174
const int y2 = int(fy2);
175
float hi = 127, lo = -128;
176
const float eyeheight = d->eyeheight;
177
const float playerheight = eyeheight + d->aboveeye;
179
for(int y = y1; y<=y2; y++) for(int x = x1; x<=x2; x++) // collide with map
181
if(OUTBORD(x,y)) return false;
183
float ceil = s->ceil;
184
float floor = s->floor;
192
int bx = x, by = y, bs = 1;
194
if((x==x1 && y==y2 && cornertest(0, x, y, -1, 1, bx, by, bs) && fx1-bx<=fy2-by)
195
|| (x==x2 && y==y1 && cornertest(0, x, y, 1, -1, bx, by, bs) && fx2-bx>=fy1-by) || !(++cornersurface)
196
|| (x==x1 && y==y1 && cornertest(0, x, y, -1, -1, bx, by, bs) && fx1-bx+fy1-by<=bs)
197
|| (x==x2 && y==y2 && cornertest(0, x, y, 1, 1, bx, by, bs) && fx2-bx+fy2-by>=bs))
203
case FHF: // FIXME: too simplistic collision with slopes, makes it feels like tiny stairs
204
floor -= (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
208
ceil += (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
211
if(ceil<hi) hi = ceil;
212
if(floor>lo) lo = floor;
215
if(hi-lo < playerheight) return false;
217
float headspace = 10;
219
if(d->type!=ENT_CAMERA)
221
loopv(players) // collide with other players
223
playerent *o = players[i];
224
if(!o || o==d || (o==player1 && d->type==ENT_CAMERA)) continue;
225
if(!plcollide(d, o, headspace, hi, lo)) return false;
227
if(d!=player1) if(!plcollide(d, player1, headspace, hi, lo)) return false;
231
if(!mmcollide(d, hi, lo)) return false; // collide with map models
235
d->o.z = lo+eyeheight; // just drop to floor (sideeffect)
240
const float spacelo = d->o.z-eyeheight-lo;
245
d->o.z = lo+eyeheight; // stick on step
247
else if(spacelo>-1.26f && d->type!=ENT_BOUNCE) d->o.z += rise; // rise thru stair
252
d->o.z -= min(min(drop, spacelo), headspace); // gravity
255
const float spacehi = hi-(d->o.z+d->aboveeye);
258
if(spacehi<-0.1) return false; // hack alert!
259
if(spacelo>0.1f) d->o.z = hi-d->aboveeye; // glue to ceiling if in midair
260
d->vel.z = 0; // cancel out jumping velocity
263
const float floorclamp = d->crouching ? 0.1f : 0.01f;
264
d->onfloor = d->o.z-eyeheight-lo < floorclamp;
269
VARP(maxroll, 0, 0, 20); // note: when changing max value, fix network transmission
270
VAR(recoilbackfade, 0, 100, 1000);
272
void resizephysent(physent *pl, int moveres, int curtime, float min, float max)
274
if(pl->eyeheightvel==0.0f) return;
276
const bool water = hdr.waterlevel>pl->o.z;
277
const float speed = curtime*pl->maxspeed/(water ? 2000.0f : 1000.0f);
278
float h = pl->eyeheightvel * speed / moveres;
286
pl->eyeheight -= h; // collided, revert mini-step
290
if(pl->eyeheight<min) // clamp to min
292
pl->o.z += min - pl->eyeheight;
294
pl->eyeheightvel = 0.0f;
297
if(pl->eyeheight>max)
299
pl->o.z -= pl->eyeheight - max;
301
pl->eyeheightvel = 0.0f;
307
// main physics routine, moves a player/monster for a curtime step
308
// moveres indicated the physics precision (which is lower for monsters and multiplayer prediction)
309
// local is false for multiplayer prediction
311
void clamproll(physent *pl)
313
if(pl->roll > maxroll) pl->roll = maxroll;
314
else if(pl->roll < -maxroll) pl->roll = -maxroll;
317
void moveplayer(physent *pl, int moveres, bool local, int curtime)
320
const bool editfly = pl->state==CS_EDITING;
321
const bool specfly = pl->type==ENT_PLAYER && ((playerent *)pl)->spectatemode==SM_FLY;
323
vec d; // vector of direction we ideally want to move in
325
float drop = 0, rise = 0;
327
if(pl->type==ENT_BOUNCE)
329
bounceent* bounce = (bounceent *) pl;
330
water = hdr.waterlevel>pl->o.z;
332
const float speed = curtime*pl->maxspeed/(water ? 2000.0f : 1000.0f);
333
const float friction = water ? 20.0f : (pl->onfloor || editfly || specfly ? 6.0f : 30.0f);
334
const float fpsfric = max(friction*20.0f/curtime, 1.0f);
336
if(pl->onfloor) // apply friction
338
pl->vel.mul(fpsfric-1);
339
pl->vel.div(fpsfric);
341
else // apply gravity
343
const float CUBES_PER_METER = 4; // assumes 4 cubes make up 1 meter
344
const float BOUNCE_MASS = 0.5f; // sane default mass of 0.5 kg
345
const float GRAVITY = BOUNCE_MASS*9.81f/CUBES_PER_METER/1000.0f;
346
bounce->vel.z -= GRAVITY*curtime;
351
if(water) d.div(6.0f); // incorrect
354
float rotspeed = bounce->rotspeed*d.magnitude();
355
pl->pitch = fmod(pl->pitch+rotspeed, 360.0f);
356
pl->yaw = fmod(pl->yaw+rotspeed, 360.0f);
358
else // fake physics for player ents to create _the_ cube movement (tm)
360
const int timeinair = pl->timeinair;
361
int move = pl->onladder && !pl->onfloor && pl->move == -1 ? 0 : pl->move; // movement on ladder
362
water = hdr.waterlevel>pl->o.z-0.5f;
364
const bool crouching = pl->crouching || pl->eyeheight < pl->maxeyeheight;
365
const float speed = curtime/(water ? 2000.0f : 1000.0f)*pl->maxspeed*(crouching ? 0.4f : 1.0f)*(specfly ? 2.0f : 1.0f);
366
const float friction = water ? 20.0f : (pl->onfloor || editfly || specfly ? 6.0f : (pl->onladder ? 1.5f : 30.0f));
367
const float fpsfric = max(friction/curtime*20.0f, 1.0f);
369
d.x = (float)(move*cosf(RAD*(pl->yaw-90)));
370
d.y = (float)(move*sinf(RAD*(pl->yaw-90)));
373
if(editfly || specfly || water)
375
d.x *= (float)cosf(RAD*(pl->pitch));
376
d.y *= (float)cosf(RAD*(pl->pitch));
377
d.z = (float)(move*sinf(RAD*(pl->pitch)));
380
d.x += (float)(pl->strafe*cosf(RAD*(pl->yaw-180)));
381
d.y += (float)(pl->strafe*sinf(RAD*(pl->yaw-180)));
383
pl->vel.mul(fpsfric-1); // slowly apply friction and direction to velocity, gives a smooth movement
385
pl->vel.div(fpsfric);
389
if(editfly) // just apply velocity
394
pl->jumpnext = false;
400
rise = speed/moveres/1.2f;
403
pl->jumpnext = false;
407
else // apply velocity with collisions
409
if(pl->type!=ENT_CAMERA)
413
const float climbspeed = 1.0f;
415
if(pl->type==ENT_BOT) pl->vel.z = climbspeed; // bots climb upwards only
416
else if(pl->type==ENT_PLAYER)
418
if(((playerent *)pl)->k_up) pl->vel.z = climbspeed;
419
else if(((playerent *)pl)->k_down) pl->vel.z = -climbspeed;
425
if(pl->onfloor || water)
429
pl->jumpnext = false;
430
pl->vel.z = 2.0f; // physics impulse upwards
431
if(water) { pl->vel.x /= 8; pl->vel.y /= 8; } // dampen velocity change even harder, gives correct water feel
432
else if(pl==player1 || pl->type!=ENT_PLAYER) playsoundc(S_JUMP, pl);
438
pl->timeinair += curtime;
442
if(timeinair > 200 && !pl->timeinair)
444
int sound = timeinair > 800 ? S_HARDLAND : S_SOFTLAND;
445
if(pl->state!=CS_DEAD)
447
if(pl==player1 || pl->type!=ENT_PLAYER) playsoundc(sound, pl);
452
const float gravity = 20.0f;
453
float dropf = (gravity-1)+pl->timeinair/15.0f; // incorrect, but works fine
454
if(water) { dropf = 5; pl->timeinair = 0; } // float slowly down in water
455
if(pl->onladder) { dropf = 0; pl->timeinair = 0; }
457
drop = dropf*curtime/gravity/100/moveres; // at high fps, gravity kicks in too fast
458
rise = speed/moveres/1.2f; // extra smoothness when lifting up stairs
459
if(pl->maxspeed-16.0f>0.5f) pl += 0xF0F0;
463
bool collided = false;
464
vec oldorigin = pl->o;
466
if(!editfly) loopi(moveres) // discrete steps collision detection & sliding
468
const float f = 1.0f/moveres;
475
if(collide(pl, false, drop, rise)) continue;
476
else collided = true;
477
if(pl->type==ENT_BOUNCE && cornersurface)
478
{ // try corner bounce
479
float ct2f = cornersurface == 2 ? -1.0 : 1.0;
480
vec oo = pl->o, xd = d;
483
pl->o.x += f * (-d.x + xd.x);
484
pl->o.y += f * (-d.y + xd.y);
485
if(collide(pl, false, drop, rise))
488
float sw = pl->vel.x * ct2f;
489
pl->vel.x = pl->vel.y * ct2f;
496
if(pl->type==ENT_CAMERA || (pl->type==ENT_PLAYER && pl->state==CS_DEAD && ((playerent *)pl)->spectatemode != SM_FLY))
503
if(pl->type==ENT_PLAYER && hitplayer)
505
float dx = hitplayer->o.x-pl->o.x, dy = hitplayer->o.y-pl->o.y,
506
push = (dx*d.x + dy*d.y)/max(dx*dx + dy*dy, 1e-3f),
507
px = push*dx, py = push*dy;
510
if(collide(pl, false, drop, rise)) continue;
514
// player stuck, try slide along y axis
516
if(collide(pl, false, drop, rise))
519
if(pl->type==ENT_BOUNCE) { pl->vel.x = -pl->vel.x; pl->vel.mul(0.7f); }
523
// still stuck, try x axis
525
if(collide(pl, false, drop, rise))
528
if(pl->type==ENT_BOUNCE) { pl->vel.y = -pl->vel.y; pl->vel.mul(0.7f); }
532
// try just dropping down
535
if(collide(pl, false, drop, rise))
541
if(pl->type==ENT_BOUNCE) { pl->vel.z = -pl->vel.z; pl->vel.mul(0.5f); }
545
pl->stuck = (oldorigin==pl->o);
546
if(collided) pl->oncollision();
547
else pl->onmoved(oldorigin.sub(pl->o));
549
if(pl->type==ENT_CAMERA) return;
551
if(pl->type!=ENT_BOUNCE && pl==player1)
553
// automatically apply smooth roll when strafing
556
pl->roll = pl->roll/(1+(float)sqrt((float)curtime)/25);
560
pl->roll += pl->strafe*curtime/-30.0f;
565
const float fric = 6.0f/curtime*20.0f;
566
pl->pitch += pl->pitchvel*(curtime/1000.0f)*pl->maxspeed*(pl->crouching ? 0.75f : 1.0f);
567
pl->pitchvel *= fric-3;
568
pl->pitchvel /= fric;
569
extern int recoiltest;
572
if(pl->pitchvel < 0.05f && pl->pitchvel > 0.001f) pl->pitchvel -= recoilbackfade/100.0f; // slide back
574
else if(pl->pitchvel < 0.05f && pl->pitchvel > 0.001f) pl->pitchvel -= ((playerent *)pl)->weaponsel->info.recoilbackfade/100.0f; // slide back
575
if(pl->pitchvel) fixcamerarange(pl); // fix pitch if necessary
578
// play sounds on water transitions
579
if(pl->type!=ENT_CAMERA)
581
if(!pl->inwater && water)
583
if(!pl->lastsplash || lastmillis-pl->lastsplash>500)
585
playsound(S_SPLASH2, pl);
586
pl->lastsplash = lastmillis;
588
if(pl==player1) pl->vel.z = 0;
590
else if(pl->inwater && !water) playsound(S_SPLASH1, &pl->o);
594
// store previous locations of all players/bots
595
if(pl->type==ENT_PLAYER || pl->type==ENT_BOT)
597
((playerent *)pl)->history.update(pl->o, lastmillis);
600
// apply volume-resize when crouching
601
if(pl->type==ENT_PLAYER)
603
if(pl==player1 && !(intermission || player1->onladder || (pl->trycrouch && !player1->onfloor && player1->timeinair > 50))) updatecrouch(player1, player1->trycrouch);
604
const float croucheyeheight = pl->maxeyeheight*3.0f/4.0f;
605
resizephysent(pl, moveres, curtime, croucheyeheight, pl->maxeyeheight);
609
const int PHYSFPS = 200;
610
const int PHYSFRAMETIME = 1000 / PHYSFPS;
611
int physsteps = 0, physframetime = PHYSFRAMETIME, lastphysframe = 0;
613
void physicsframe() // optimally schedule physics frames inside the graphics frames
615
int diff = lastmillis - lastphysframe;
616
if(diff <= 0) physsteps = 0;
619
extern int gamespeed;
620
physframetime = clamp((PHYSFRAMETIME*gamespeed)/100, 1, PHYSFRAMETIME);
621
physsteps = (diff + physframetime - 1)/physframetime;
622
lastphysframe += physsteps * physframetime;
626
VAR(physinterp, 0, 1, 1);
628
void interppos(physent *pl)
631
pl->o.z += pl->eyeheight;
633
int diff = lastphysframe - lastmillis;
634
if(diff <= 0 || !physinterp) return;
636
vec deltapos(pl->deltapos);
637
deltapos.mul(min(diff, physframetime)/float(physframetime));
641
void moveplayer(physent *pl, int moveres, bool local)
645
if(local) interppos(pl);
652
pl->o.z += pl->eyeheight;
654
loopi(physsteps-1) moveplayer(pl, moveres, local, physframetime);
655
if(local) pl->deltapos = pl->o;
656
moveplayer(pl, moveres, local, physframetime);
660
pl->deltapos.sub(pl->newpos);
661
pl->newpos.z -= pl->eyeheight;
666
void movebounceent(bounceent *p, int moveres, bool local)
668
moveplayer(p, moveres, local);
671
// movement input code
673
#define dir(name,v,d,s,os) void name(bool isdown) { player1->s = isdown; player1->v = isdown ? d : (player1->os ? -(d) : 0); player1->lastmove = lastmillis; }
675
dir(backward, move, -1, k_down, k_up)
676
dir(forward, move, 1, k_up, k_down)
677
dir(left, strafe, 1, k_left, k_right)
678
dir(right, strafe, -1, k_right, k_left)
682
if(intermission) return;
683
if(editmode) editdrag(on);
684
else if(player1->state==CS_DEAD)
686
if(!on) tryrespawn();
688
else player1->attacking = on;
693
if(intermission) return;
694
if(player1->isspectating())
696
if(lastmillis - player1->respawnoffset > 1000 && on) togglespect();
698
else if(player1->crouching) return;
699
else player1->jumpnext = on;
702
void updatecrouch(playerent *p, bool on)
704
if(p->crouching == on) return;
705
const float crouchspeed = 0.6f;
707
p->eyeheightvel = on ? -crouchspeed : crouchspeed;
708
if(p==player1) playsoundc(on ? S_CROUCH : S_UNCROUCH);
713
if(player1->isspectating()) return;
714
player1->trycrouch = on;
717
COMMAND(backward, ARG_DOWN);
718
COMMAND(forward, ARG_DOWN);
719
COMMAND(left, ARG_DOWN);
720
COMMAND(right, ARG_DOWN);
721
COMMANDN(jump, jumpn, ARG_DOWN);
722
COMMAND(attack, ARG_DOWN);
723
COMMAND(crouch, ARG_DOWN);
725
void fixcamerarange(physent *cam)
727
const float MAXPITCH = 90.0f;
728
if(cam->pitch>MAXPITCH) cam->pitch = MAXPITCH;
729
if(cam->pitch<-MAXPITCH) cam->pitch = -MAXPITCH;
730
while(cam->yaw<0.0f) cam->yaw += 360.0f;
731
while(cam->yaw>=360.0f) cam->yaw -= 360.0f;
734
FVARP(sensitivity, 1e-3f, 3.0f, 1000.0f);
735
VARP(invmouse, 0, 0, 1);
737
void mousemove(int dx, int dy)
739
if(intermission) return;
740
if(player1->isspectating() && player1->spectatemode==SM_FOLLOW1ST) return;
742
const float SENSF = 33.0f; // try match quake sens
743
camera1->yaw += (dx/SENSF)*sensitivity;
744
camera1->pitch -= (dy/SENSF)*sensitivity*(invmouse ? -1 : 1);
746
if(camera1!=player1 && player1->spectatemode!=SM_DEATHCAM)
748
player1->yaw = camera1->yaw;
749
player1->pitch = camera1->pitch;
753
void entinmap(physent *d) // brute force but effective way to find a free spawn spot in the map
756
loopi(100) // try max 100 times
758
float dx = (rnd(21)-10)/10.0f*i; // increasing distance
759
float dy = (rnd(21)-10)/10.0f*i;
769
// leave ent at original pos, possibly stuck
771
conoutf("can't find entity spawn spot! (%d, %d)", d->o.x, d->o.y);