Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.73 KB | None | 0 0
  1. ==================
  2. PM_SlideMove
  3.  
  4. Returns qtrue if the velocity was clipped in some way
  5. ==================
  6. */
  7. qboolean PM_SlideMove(qboolean gravity)
  8. {
  9.     int bumpcount, numbumps, extrabumps;
  10.     vec3_t dir;
  11.     float d;
  12.     int numplanes;
  13.     vec3_t planes[MAX_CLIP_PLANES];
  14.     vec3_t primal_velocity;
  15.     vec3_t clipVelocity;
  16.     int i, j, k;
  17.     trace_t trace;
  18.     vec3_t end;
  19.     float time_left;
  20.     float into;
  21.     vec3_t endVelocity;
  22.     vec3_t endClipVelocity;
  23.     numbumps = 4;
  24.     extrabumps = 0;
  25.  
  26.     VectorCopy(pm->ps->velocity, primal_velocity);
  27.  
  28.     if (gravity)
  29.     {
  30.         VectorCopy(pm->ps->velocity, endVelocity);
  31.         endVelocity[2] -= pm->ps->gravity * pml.frametime;
  32.         pm->ps->velocity[2] = (pm->ps->velocity[2] + endVelocity[2]) * 0.5;
  33.         primal_velocity[2] = endVelocity[2];
  34.  
  35.         if (pml.groundPlane)
  36.         {
  37.             // slide along the ground plane
  38.             PM_ClipVelocity(pm->ps->velocity, pml.groundTrace.plane.normal, pm->ps->velocity, OVERCLIP);
  39.         }
  40.     }
  41.     else
  42.     {
  43.         VectorClear(endVelocity);
  44.     }
  45.  
  46.     time_left = pml.frametime;
  47.  
  48.     // never turn against the ground plane
  49.     if (pml.groundPlane)
  50.     {
  51.         numplanes = 1;
  52.         VectorCopy(pml.groundTrace.plane.normal, planes[0]);
  53.     }
  54.     else
  55.     {
  56.         numplanes = 0;
  57.     }
  58.  
  59.     // never turn against original velocity
  60.     VectorNormalize2(pm->ps->velocity, planes[numplanes]);
  61.     numplanes++;
  62.  
  63.     for (bumpcount = 0; bumpcount < numbumps ; bumpcount++)
  64.     {
  65.         // calculate position we are trying to move to
  66.         VectorMA(pm->ps->origin, time_left, pm->ps->velocity, end);
  67.  
  68.         // see if we can make it there
  69.         PM_TraceAll(&trace, pm->ps->origin, end);
  70.  
  71.         if (pm->debugLevel > 1)
  72.         {
  73.             Com_Printf("%i:%d %d (%f %f %f)\n",
  74.                 c_pmove, trace.allsolid, trace.startsolid,
  75.                 trace.endpos[0],
  76.                 trace.endpos[1],
  77.                 trace.endpos[2]);
  78.         }
  79.  
  80.         if (trace.allsolid)
  81.         {
  82.             // entity is completely trapped in another solid
  83.             pm->ps->velocity[2] = 0; // don't build up falling damage, but allow sideways acceleration
  84.             return qtrue;
  85.         }
  86.  
  87.         if (trace.fraction > 0)
  88.         {
  89.             // actually covered some distance
  90.             VectorCopy(trace.endpos, pm->ps->origin);
  91.         }
  92.  
  93.         if (trace.fraction == 1)
  94.         {
  95.             break; // moved the entire distance
  96.         }
  97.  
  98.         // save entity for contact
  99.         PM_AddTouchEnt(trace.entityNum);
  100.  
  101.         time_left -= time_left * trace.fraction;
  102.  
  103.         if (numplanes >= MAX_CLIP_PLANES)
  104.         {
  105.             // this shouldn't really happen
  106.             VectorClear(pm->ps->velocity);
  107.  
  108.             return qtrue;
  109.         }
  110.  
  111.         //
  112.         // if this is the same plane we hit before, nudge velocity
  113.         // out along it, which fixes some epsilon issues with
  114.         // non-axial planes
  115.         //
  116.         for ( i = 0 ; i < numplanes ; i++ )
  117.         {
  118.             if (DotProduct(trace.plane.normal, planes[i]) > 0.99)
  119.             {
  120.                 if (extrabumps <= 0)
  121.                 {
  122.                     VectorAdd(trace.plane.normal, pm->ps->velocity, pm->ps->velocity);
  123.                     extrabumps++;
  124.                     numbumps++;
  125.  
  126.                     Com_Printf("%i:planevelocitynudge\n", c_pmove);
  127.                 }
  128.                 else
  129.                 {
  130.                     // zinx - if it happens again, nudge the origin instead,
  131.                     // and trace it, to make sure we don't end up in a solid
  132.  
  133.                     VectorAdd( pm->ps->origin, trace.plane.normal, end );
  134.                     PM_TraceAll( &trace, pm->ps->origin, end );
  135.                     VectorCopy( trace.endpos, pm->ps->origin );
  136.  
  137.                     Com_Printf("%i:planeoriginnudge\n", c_pmove);
  138.                 }
  139.  
  140.                 break;
  141.             }
  142.         }
  143.  
  144.         if (i < numplanes)
  145.         {
  146.             continue;
  147.         }
  148.  
  149.         VectorCopy(trace.plane.normal, planes[numplanes]);
  150.         numplanes++;
  151.  
  152.         //
  153.         // modify velocity so it parallels all of the clip planes
  154.         //
  155.  
  156.         // find a plane that it enters
  157.         for (i = 0 ; i < numplanes ; i++)
  158.         {
  159.             into = DotProduct(pm->ps->velocity, planes[i]);
  160.  
  161.             if (into >= 0.1)
  162.             {
  163.                 continue; // move doesn't interact with the plane
  164.             }
  165.  
  166.             // see how hard we are hitting things
  167.             if (-into > pml.impactSpeed)
  168.             {
  169.                 pml.impactSpeed = -into;
  170.             }
  171.  
  172.             // slide along the plane
  173.             PM_ClipVelocity(pm->ps->velocity, planes[i], clipVelocity, OVERCLIP);
  174.  
  175.             // slide along the plane
  176.             PM_ClipVelocity(endVelocity, planes[i], endClipVelocity, OVERCLIP);
  177.  
  178.             // see if there is a second plane that the new move enters
  179.             for (j = 0 ; j < numplanes ; j++)
  180.             {
  181.                 if (j == i)
  182.                 {
  183.                     continue;
  184.                 }
  185.                 if (DotProduct( clipVelocity, planes[j]) >= 0.1)
  186.                 {
  187.                     continue; // move doesn't interact with the plane
  188.                 }
  189.  
  190.                 // try clipping the move to the plane
  191.                 PM_ClipVelocity(clipVelocity, planes[j], clipVelocity, OVERCLIP);
  192.                 PM_ClipVelocity(endClipVelocity, planes[j], endClipVelocity, OVERCLIP);
  193.  
  194.                 // see if it goes back into the first clip plane
  195.                 if (DotProduct(clipVelocity, planes[i]) >= 0)
  196.                 {
  197.                     continue;
  198.                 }
  199.  
  200.                 // slide the original velocity along the crease
  201.                 CrossProduct(planes[i], planes[j], dir);
  202.                 VectorNormalize(dir);
  203.                 d = DotProduct(dir, pm->ps->velocity);
  204.                 VectorScale(dir, d, clipVelocity);
  205.  
  206.                 CrossProduct(planes[i], planes[j], dir);
  207.                 VectorNormalize(dir);
  208.                 d = DotProduct(dir, endVelocity);
  209.                 VectorScale(dir, d, endClipVelocity);
  210.  
  211.                 // see if there is a third plane the the new move enters
  212.                 for ( k = 0 ; k < numplanes ; k++ )
  213.                 {
  214.                     if (k == i || k == j)
  215.                     {
  216.                         continue;
  217.                     }
  218.                     if (DotProduct(clipVelocity, planes[k]) >= 0.1)
  219.                     {
  220.                         continue; // move doesn't interact with the plane
  221.                     }
  222.  
  223.                     // stop dead at a tripple plane interaction
  224.                     VectorClear(pm->ps->velocity);
  225.  
  226.                     return qtrue;
  227.                 }
  228.             }
  229.  
  230.             // if we have fixed all interactions, try another move
  231.             VectorCopy(clipVelocity, pm->ps->velocity);
  232.             VectorCopy(endClipVelocity, endVelocity);
  233.  
  234.             break;
  235.         }
  236.     }
  237.  
  238.     if (gravity)
  239.     {
  240.         VectorCopy(endVelocity, pm->ps->velocity);
  241.     }
  242.  
  243.     // don't change velocity if in a timer (FIXME: is this correct?)
  244.     if (pm->ps->pm_time)
  245.     {
  246.         VectorCopy(primal_velocity, pm->ps->velocity);
  247.     }
  248.  
  249.     return (qboolean)(bumpcount != 0);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement