home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2007 September / maximum-cd-2007-09.iso / Assets / data / AssaultCube_v0.93.exe / source / src / physics.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-06-03  |  17.9 KB  |  523 lines

  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).
  5.  
  6. #include "cube.h"
  7.  
  8. float raycube(const vec &o, const vec &ray, vec &surface)
  9. {
  10.     surface = vec(0, 0, 0);
  11.  
  12.     if(ray.iszero()) return -1;
  13.  
  14.     vec v = o;
  15.     float dist = 0, dx = 0, dy = 0, dz = 0;
  16.  
  17.     for(;;)
  18.     {
  19.         int x = int(v.x), y = int(v.y);
  20.         if(x < 0 || y < 0 || x >= ssize || y >= ssize) return -1;
  21.         sqr *s = S(x, y);
  22.         float floor = s->floor, ceil = s->ceil;
  23.         if(s->type==FHF) floor -= s->vdelta/4.0f;
  24.         if(s->type==CHF) ceil += s->vdelta/4.0f;
  25.         if(SOLID(s) || v.z < floor || v.z > ceil) 
  26.         { 
  27.             if((!dx && !dy) || s->wtex==DEFAULT_SKY || (!SOLID(s) && v.z > ceil && s->ctex==DEFAULT_SKY)) return -1;
  28.             if(s->type!=CORNER)// && s->type!=FHF && s->type!=CHF)
  29.             {
  30.                 if(dx<dy) surface.x = ray.x>0 ? -1 : 1;
  31.                 else surface.y = ray.y>0 ? -1 : 1;
  32.                 sqr *n = S(x+(int)surface.x, y+(int)surface.y);
  33.                 if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
  34.                 {
  35.                     surface = dx<dy ? vec(0, ray.y>0 ? -1 : 1, 0) : vec(ray.x>0 ? -1 : 1, 0, 0);
  36.                     n = S(x+(int)surface.x, y+(int)surface.y);
  37.                     if(SOLID(n) || (v.z < floor && v.z < n->floor) || (v.z > ceil && v.z > n->ceil))
  38.                         surface = vec(0, 0, ray.z>0 ? -1 : 1);
  39.                 }
  40.             }
  41.             dist = max(dist-0.1f, 0);
  42.             break;
  43.         }
  44.         dx = ray.x ? (x + (ray.x > 0 ? 1 : 0) - v.x)/ray.x : 1e16f;
  45.         dy = ray.y ? (y + (ray.y > 0 ? 1 : 0) - v.y)/ray.y : 1e16f;
  46.         dz = ray.z ? ((ray.z > 0 ? ceil : floor) - v.z)/ray.z : 1e16f;
  47.         if(dz < dx && dz < dy)
  48.         {
  49.             if(ray.z>0 && s->ctex==DEFAULT_SKY) return -1;
  50.             if(s->type!=FHF && s->type!=CHF) surface.z = ray.z>0 ? -1 : 1;
  51.             dist += dz;
  52.             break;
  53.         }
  54.         float disttonext = 0.1f + min(dx, dy);
  55.         v.add(vec(ray).mul(disttonext));
  56.         dist += disttonext;
  57.     } 
  58.     return dist;    
  59.  
  60. physent *hitplayer = NULL;
  61.  
  62. bool plcollide(physent *d, physent *o, float &headspace, float &hi, float &lo)          // collide with player or monster
  63. {
  64.     if(o->state!=CS_ALIVE) return true;
  65.     const float r = o->radius+d->radius, dx = o->o.x-d->o.x, dy = o->o.y-d->o.y;
  66.     if(d->type==ENT_PLAYER && o->type==ENT_PLAYER ? dx*dx + dy*dy < r*r : fabs(dx)<r && fabs(dy)<r) 
  67.     {
  68.         if(d->o.z-d->eyeheight<o->o.z-o->eyeheight) { if(o->o.z-o->eyeheight<hi) hi = o->o.z-o->eyeheight-1; }
  69.         else if(o->o.z+o->aboveeye>lo) lo = o->o.z+o->aboveeye+1;
  70.     
  71.         if(fabs(o->o.z-d->o.z)<o->aboveeye+d->eyeheight) { hitplayer = o; return false; }
  72.         headspace = d->o.z-o->o.z-o->aboveeye-d->eyeheight;
  73.         if(headspace<0) headspace = 10;
  74.     }
  75.     return true;
  76. }
  77.  
  78. 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
  79. {
  80.     sqr *w = wmip[mip];
  81.     int sz = ssize>>mip;
  82.     bool stest = SOLID(SWS(w, x+dx, y, sz)) && SOLID(SWS(w, x, y+dy, sz));
  83.     mip++;
  84.     x /= 2;
  85.     y /= 2;
  86.     if(SWS(wmip[mip], x, y, ssize>>mip)->type==CORNER)
  87.     {
  88.         bx = x<<mip;
  89.         by = y<<mip;
  90.         bs = 1<<mip;
  91.         return cornertest(mip, x, y, dx, dy, bx, by, bs);
  92.     }
  93.     return stest;
  94. }
  95.  
  96. void mmcollide(physent *d, float &hi, float &lo)           // collide with a mapmodel
  97. {
  98.     loopv(ents)
  99.     {
  100.         entity &e = ents[i];
  101.         if(e.type!=MAPMODEL) continue;
  102.         mapmodelinfo &mmi = getmminfo(e.attr2);
  103.         if(!&mmi || !mmi.h) continue;
  104.         const float r = mmi.rad+d->radius;
  105.         if(fabs(e.x-d->o.x)<r && fabs(e.y-d->o.y)<r)
  106.         { 
  107.             float mmz = (float)(S(e.x, e.y)->floor+mmi.zoff+e.attr3);
  108.             if(d->o.z-d->eyeheight<mmz) { if(mmz<hi) hi = mmz; }
  109.             else if(mmz+mmi.h>lo) lo = mmz+mmi.h;
  110.         }
  111.     }
  112. }
  113.  
  114. // all collision happens here
  115. // spawn is a dirty side effect used in spawning
  116. // drop & rise are supplied by the physics below to indicate gravity/push for current mini-timestep
  117.  
  118. bool collide(physent *d, bool spawn, float drop, float rise)
  119. {
  120.     const float fx1 = d->o.x-d->radius;     // figure out integer cube rectangle this entity covers in map
  121.     const float fy1 = d->o.y-d->radius;
  122.     const float fx2 = d->o.x+d->radius;
  123.     const float fy2 = d->o.y+d->radius;
  124.     const int x1 = int(fx1);
  125.     const int y1 = int(fy1);
  126.     const int x2 = int(fx2);
  127.     const int y2 = int(fy2);
  128.     float hi = 127, lo = -128;
  129.  
  130.     for(int x = x1; x<=x2; x++) for(int y = y1; y<=y2; y++)     // collide with map
  131.     {
  132.         if(OUTBORD(x,y)) return false;
  133.         sqr *s = S(x,y);
  134.         float ceil = s->ceil;
  135.         float floor = s->floor;
  136.         switch(s->type)
  137.         {
  138.             case SOLID:
  139.                 return false;
  140.  
  141.             case CORNER:
  142.             {
  143.                 int bx = x, by = y, bs = 1;
  144.                 if(x==x1 && y==y1 && cornertest(0, x, y, -1, -1, bx, by, bs) && fx1-bx+fy1-by<=bs
  145.                 || x==x2 && y==y1 && cornertest(0, x, y,  1, -1, bx, by, bs) && fx2-bx>=fy1-by
  146.                 || x==x1 && y==y2 && cornertest(0, x, y, -1,  1, bx, by, bs) && fx1-bx<=fy2-by
  147.                 || x==x2 && y==y2 && cornertest(0, x, y,  1,  1, bx, by, bs) && fx2-bx+fy2-by>=bs)
  148.                    return false;
  149.                 break;
  150.             }
  151.  
  152.             case FHF:       // FIXME: too simplistic collision with slopes, makes it feels like tiny stairs
  153.                 floor -= (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
  154.                 break;
  155.  
  156.             case CHF:
  157.                 ceil += (s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f;
  158.  
  159.         }
  160.         if(ceil<hi) hi = ceil;
  161.         if(floor>lo) lo = floor;
  162.     }
  163.  
  164.     if(hi-lo < d->eyeheight+d->aboveeye) return false;
  165.  
  166.     // Modified by Rick: plcollide now takes hi and lo in account aswell, that way we can jump/walk on players
  167.     
  168.     float headspace = 10;
  169.     loopv(players)       // collide with other players
  170.     {
  171.         playerent *o = players[i]; 
  172.         if(!o || o==d || (o==player1 && d->type==ENT_CAMERA)) continue;
  173.         if(!plcollide(d, o, headspace, hi, lo)) return false;
  174.     }
  175.     
  176.     if(d!=player1 && !(d->type==ENT_BOUNCE && player1->inhandnade==((bounceent *)d))) if(!plcollide(d, player1, headspace, hi, lo)) return false;
  177.     headspace -= 0.01f;
  178.     
  179.     mmcollide(d, hi, lo);    // collide with map models
  180.  
  181.     if(spawn)
  182.     {
  183.         d->o.z = lo+d->eyeheight;       // just drop to floor (sideeffect)
  184.         d->onfloor = true;
  185.     }
  186.     else
  187.     {
  188.         const float spacetop = d->o.z-d->eyeheight-lo;
  189.         if(spacetop<0)
  190.         {
  191.             if(spacetop>-0.01) 
  192.             {
  193.                 d->o.z = lo+d->eyeheight;   // stick on step
  194.             }
  195.             else if(spacetop>-1.26f && d->type!=ENT_BOUNCE) d->o.z += rise;       // rise thru stair
  196.             else return false;
  197.         }
  198.         else
  199.         {
  200.             d->o.z -= min(min(drop, spacetop), headspace);       // gravity
  201.         }
  202.  
  203.         const float spacebottom = hi-(d->o.z+d->aboveeye);
  204.         if(spacebottom<0)
  205.         {
  206.             if(spacebottom<-0.1) return false;     // hack alert!
  207.             d->o.z = hi-d->aboveeye;          // glue to ceiling
  208.             d->vel.z = 0;                     // cancel out jumping velocity
  209.         }
  210.  
  211.         d->onfloor = d->o.z-d->eyeheight-lo<0.01f;
  212.     }
  213.     return true;
  214. }
  215.  
  216. float floor(short x, short y)
  217. {
  218.     sqr *s = S(x, y);
  219.     return s->type == FHF ? s->floor-(s->vdelta+S(x+1,y)->vdelta+S(x,y+1)->vdelta+S(x+1,y+1)->vdelta)/16.0f : s->floor;
  220. }
  221.  
  222. VARP(maxroll, 0, 0, 20);
  223.  
  224. // main physics routine, moves a player/monster for a curtime step
  225. // moveres indicated the physics precision (which is lower for monsters and multiplayer prediction)
  226. // local is false for multiplayer prediction
  227.  
  228. void moveplayer(physent *pl, int moveres, bool local, int curtime)
  229. {
  230.     const bool water = hdr.waterlevel>pl->o.z-0.5f;
  231.     const bool floating = (editmode && local) || pl->state==CS_EDITING;
  232.     
  233.     const float speed = curtime/(water ? 2000.0f : 1000.0f)*pl->maxspeed;
  234.     const float friction = water ? 20.0f : (pl->onfloor || floating ? 6.0f : (pl->onladder ? 1.5f : 30.0f));
  235.     const float fpsfric = friction/curtime*20.0f;
  236.  
  237.     vec d;      // vector of direction we ideally want to move in
  238.  
  239.     float drop, rise;
  240.  
  241.     if(pl->type==ENT_BOUNCE)
  242.     {
  243.         bounceent* bounce = (bounceent *) pl;
  244.         drop = rise = 0;
  245.  
  246.         if(pl->onfloor) // apply friction
  247.         {
  248.             pl->vel.mul(fpsfric-1);
  249.             pl->vel.div(fpsfric);
  250.         }
  251.         else // apply gravity
  252.         {
  253.             const float gravity = 9.81f/1000.0f*bounce->maxspeed;
  254.             const float heightvel = (gravity)*pow(speed, 2.0f);
  255.             bounce->vel.z -= heightvel;
  256.         }
  257.  
  258.         d = bounce->vel;
  259.         d.mul(speed);
  260.  
  261.         if(water) { d.x /= 6; d.y /= 6; }
  262.  
  263.         // rotate
  264.         float rotspeed = bounce->rotspeed*d.magnitude();
  265.         pl->pitch = fmod(pl->pitch+rotspeed, 360.0f);
  266.         pl->yaw = fmod(pl->yaw+rotspeed, 360.0f);
  267.     }
  268.     else // fake physics for player ents to create _the_ cube movement (tm)
  269.     {
  270.         int move = pl->onladder && !pl->onfloor && pl->move == -1 ? 0 : pl->move; // movement on ladder
  271.         
  272.         d.x = (float)(move*cosf(RAD*(pl->yaw-90)));
  273.         d.y = (float)(move*sinf(RAD*(pl->yaw-90)));
  274.         d.z = 0.0f;
  275.         
  276.         if(floating || water)
  277.         {
  278.             d.x *= (float)cosf(RAD*(pl->pitch));
  279.             d.y *= (float)cosf(RAD*(pl->pitch));
  280.             d.z = (float)(move*sinf(RAD*(pl->pitch)));
  281.         }
  282.  
  283.         d.x += (float)(pl->strafe*cosf(RAD*(pl->yaw-180)));
  284.         d.y += (float)(pl->strafe*sinf(RAD*(pl->yaw-180)));
  285.         
  286.         pl->vel.mul(fpsfric-1);   // slowly apply friction and direction to velocity, gives a smooth movement
  287.         pl->vel.add(d);
  288.         pl->vel.div(fpsfric);
  289.         d = pl->vel;
  290.         d.mul(speed);
  291.  
  292.         if(floating)                // just apply velocity
  293.         {
  294.             pl->o.add(d);
  295.             if(pl->jumpnext) { pl->jumpnext = false; pl->vel.z = 2; }
  296.         }
  297.         else                        // apply velocity with collisions
  298.         {   
  299.             if(pl->onladder)
  300.             {
  301.                 const float climbspeed = 1.0f;
  302.  
  303.                 if(pl->type==ENT_BOT) pl->vel.z = climbspeed; // bots climb upwards only
  304.                 else if(pl->type==ENT_PLAYER)
  305.                 {
  306.                     if(((playerent *)pl)->k_up) pl->vel.z = climbspeed;
  307.                     else if(((playerent *)pl)->k_down) pl->vel.z = -climbspeed;
  308.                 }
  309.                 pl->timeinair = 0;
  310.             }
  311.             else
  312.             {
  313.                 if(pl->onfloor || water)
  314.                 {   
  315.                     if(pl->jumpnext)
  316.                     {
  317.                         pl->jumpnext = false;
  318.                         pl->vel.z = 2.0f; //1.7f;                           // physics impulse upwards
  319.                         if(water) { pl->vel.x /= 8; pl->vel.y /= 8; }      // dampen velocity change even harder, gives correct water feel
  320.                         if(local) playsoundc(S_JUMP);
  321.                         else if(pl->type==ENT_BOT) playsound(S_JUMP, &pl->o); // Added by Rick
  322.                     }
  323.                     pl->timeinair = 0;
  324.                 }
  325.                 else
  326.                 {
  327.                     pl->timeinair += curtime;
  328.                 }
  329.             }
  330.         }
  331.  
  332.         const float gravity = 20.0f;
  333.         float dropf = (gravity-1)+pl->timeinair/15.0f;            // incorrect, but works fine
  334.         if(water) { dropf = 5; pl->timeinair = 0; }            // float slowly down in water
  335.         if(pl->onladder) { dropf = 0; pl->timeinair = 0; }
  336.  
  337.         drop = dropf*curtime/gravity/100/moveres;                // at high fps, gravity kicks in too fast
  338.         rise = speed/moveres/1.2f;                                // extra smoothness when lifting up stairs
  339.         if(pl->maxspeed-16>0.5f) pl += 0xF0F0;
  340.     }
  341.  
  342.     if(!floating) loopi(moveres)                                // discrete steps collision detection & sliding
  343.     {
  344.         const float f = 1.0f/moveres;
  345.  
  346.         // try move forward
  347.         pl->o.x += f*d.x;
  348.         pl->o.y += f*d.y;
  349.         pl->o.z += f*d.z;
  350.         hitplayer = NULL;
  351.         if(collide(pl, false, drop, rise)) continue;                     
  352.         if(pl->type==ENT_CAMERA) return;
  353.         if(pl->type==ENT_PLAYER && hitplayer)
  354.         {
  355.             float dx = hitplayer->o.x-pl->o.x, dy = hitplayer->o.y-pl->o.y, mag = sqrtf(dx*dx+dy*dy);
  356.             dx /= mag;
  357.             dy /= mag;
  358.             pl->o.x -= f*(dx + d.x);
  359.             pl->o.y -= f*(dy + d.y);
  360.             if(collide(pl, false, drop, rise)) continue;
  361.             pl->o.x += f*(dx + d.y);
  362.             pl->o.y += f*(dy + d.y);
  363.         }
  364.         // player stuck, try slide along y axis
  365.         pl->o.x -= f*d.x;
  366.         if(collide(pl, false, drop, rise))
  367.         { 
  368.             d.x = 0; 
  369.             if(pl->type==ENT_BOUNCE) { pl->vel.x = -pl->vel.x; pl->vel.mul(0.7f); }
  370.             continue; 
  371.         }
  372.         pl->o.x += f*d.x;
  373.         // still stuck, try x axis
  374.         pl->o.y -= f*d.y;
  375.         if(collide(pl, false, drop, rise)) 
  376.         { 
  377.             d.y = 0; 
  378.             if(pl->type==ENT_BOUNCE) { pl->vel.y = -pl->vel.y; pl->vel.mul(0.7f); }
  379.             continue; 
  380.         }
  381.         pl->o.y += f*d.y;
  382.         // try just dropping down
  383.         pl->o.x -= f*d.x;
  384.         pl->o.y -= f*d.y;
  385.         if(collide(pl, false, drop, rise))
  386.         { 
  387.             d.y = d.x = 0;
  388.             continue;
  389.         }
  390.         pl->o.z -= f*d.z;
  391.         if(pl->type==ENT_BOUNCE) { pl->vel.z = -pl->vel.z; pl->vel.mul(0.5f); }
  392.         break;
  393.     }
  394.  
  395.     if(pl->type==ENT_CAMERA) return;
  396.     else if(pl->type!=ENT_BOUNCE)
  397.     {
  398.         // automatically apply smooth roll when strafing
  399.         if(pl->strafe==0) 
  400.         {
  401.             pl->roll = pl->roll/(1+(float)sqrt((float)curtime)/25);
  402.         }
  403.         else
  404.         {
  405.             pl->roll += pl->strafe*curtime/-30.0f;
  406.             if(pl->roll>maxroll) pl->roll = (float)maxroll;
  407.             if(pl->roll<-maxroll) pl->roll = (float)-maxroll;
  408.         }
  409.     }
  410.  
  411.     // play sounds on water transitions
  412.     if(!pl->inwater && water) { playsound(S_SPLASH2, &pl->o); pl->vel.z = 0; }
  413.     else if(pl->inwater && !water) playsound(S_SPLASH1, &pl->o);
  414.     pl->inwater = water;
  415.     // Added by Rick: Easy hack to store previous locations of all players/monsters/bots
  416.     if(pl->type==ENT_PLAYER || pl->type==ENT_BOT) ((playerent *)pl)->history.update(pl->o, lastmillis);
  417.     // End add
  418. }
  419.  
  420. VARP(minframetime, 5, 10, 20);
  421.  
  422. int physicsfraction = 0, physicsrepeat = 0;
  423.  
  424. void physicsframe()          // optimally schedule physics frames inside the graphics frames
  425. {
  426.     if(curtime>=minframetime)
  427.     {
  428.         int faketime = curtime+physicsfraction;
  429.         physicsrepeat = faketime/minframetime;
  430.         physicsfraction = faketime%minframetime;
  431.     }
  432.     else
  433.     {
  434.         physicsrepeat = 1;
  435.     }
  436. }
  437.  
  438. void moveplayer(physent *p, int moveres, bool local)
  439. {
  440.     loopi(physicsrepeat) moveplayer(p, moveres, local, min(curtime, minframetime));
  441. }
  442.  
  443. // movement input code
  444.  
  445. #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; }
  446.  
  447. dir(backward, move,   -1, k_down,  k_up);
  448. dir(forward,  move,    1, k_up,    k_down);
  449. dir(left,     strafe,  1, k_left,  k_right);
  450. dir(right,    strafe, -1, k_right, k_left); 
  451.  
  452. void attack(bool on)
  453. {
  454.     if(intermission) return;
  455.     if(editmode) editdrag(on);
  456.     else if(demoplayback && on) shiftdemoplayer(1);
  457.     else if(player1->state==CS_DEAD) respawn();
  458.     else player1->attacking = on;
  459. }   
  460.  
  461. void jumpn(bool on)
  462.     if(intermission) return;
  463.     if(demoplayback && on) demopaused = !demopaused;
  464.     else if(player1->state==CS_DEAD)
  465.     {
  466.         if(on) respawn();
  467.     }
  468.     else player1->jumpnext = on;
  469. }
  470.  
  471. COMMAND(backward, ARG_DOWN);
  472. COMMAND(forward, ARG_DOWN);
  473. COMMAND(left, ARG_DOWN);
  474. COMMAND(right, ARG_DOWN);
  475. COMMANDN(jump, jumpn, ARG_DOWN);
  476. COMMAND(attack, ARG_DOWN);
  477.  
  478. void fixcamerarange(physent *cam)
  479. {
  480.     const float MAXPITCH = 90.0f;
  481.     if(cam->pitch>MAXPITCH) cam->pitch = MAXPITCH;
  482.     if(cam->pitch<-MAXPITCH) cam->pitch = -MAXPITCH;
  483.     while(cam->yaw<0.0f) cam->yaw += 360.0f;
  484.     while(cam->yaw>=360.0f) cam->yaw -= 360.0f;
  485. }
  486.  
  487. VARP(sensitivity, 0, 30, 10000);
  488. VARP(sensitivityscale, 1, 10, 10000);
  489. VARP(invmouse, 0, 0, 1);
  490.  
  491. void mousemove(int dx, int dy)
  492. {
  493.     if(intermission) return;
  494.     const float SENSF = 33.0f;     // try match quake sens
  495.     camera1->yaw += (dx/SENSF)*(sensitivity/(float)sensitivityscale);
  496.     camera1->pitch -= (dy/SENSF)*(sensitivity/(float)sensitivityscale)*(invmouse ? -1 : 1);
  497.     fixcamerarange();
  498.     if(camera1!=player1 && player1->state!=CS_DEAD && !demoplayback)
  499.     {
  500.         player1->yaw = camera1->yaw;
  501.         player1->pitch = camera1->pitch;
  502.     }
  503. }
  504.  
  505. void entinmap(physent *d)    // brute force but effective way to find a free spawn spot in the map
  506. {
  507.     loopi(100)              // try max 100 times
  508.     {
  509.         float dx = (rnd(21)-10)/10.0f*i;  // increasing distance
  510.         float dy = (rnd(21)-10)/10.0f*i;
  511.         d->o.x += dx;
  512.         d->o.y += dy;
  513.         if(collide(d, true, 0, 0)) return;
  514.         d->o.x -= dx;
  515.         d->o.y -= dy;
  516.     }
  517.     conoutf("can't find entity spawn spot! (%d, %d)", d->o.x, d->o.y);
  518.     // leave ent at original pos, possibly stuck
  519. }
  520.  
  521.