Advertisement
Guest User

Untitled

a guest
May 14th, 2023
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1.     float radial(float vX, float vY, float X, float Y)
  2.     {
  3.         if (X!=0.0f && Y!=0.0f)
  4.             return (vX * X + vY * Y) / sqrt(X * X + Y * Y);
  5.         else
  6.             return 0.0f;
  7.     }
  8. ///////////////////////////////////////////////////////
  9.  
  10.     bool clippingIsActive = true;
  11.     bool clippingIsRadial = false;
  12.     float clippingThreshhold = 600.0f;
  13.     float clippingRampUp = 600.0f;
  14.     float factor_integral = 80.0f;
  15.     float factor_proportional = 80.0f;
  16.     float outer_deadzone = 0.05f;
  17.     float inner_deadzone = 0.15f;
  18.  
  19.  
  20.     bool inClippingZone = false;
  21.     float edgePushAmount = 0.0f;
  22.     const static int smoothingSteps = 8;
  23.     float previousOutputX[smoothingSteps] = {};
  24.     float previousOutputY[smoothingSteps] = {};
  25.     float previousOutputInward[smoothingSteps] = {};
  26.     float previousVelocitiesX[smoothingSteps] = {};
  27.     float previousVelocitiesY[smoothingSteps] = {};
  28.     int smoothingCounter = 0;
  29.     float smoothingDistance = 0.1f;
  30.  
  31. //////////////////////////////////////////////////////////////////////
  32.  
  33. else if (stickMode == StickMode::HYBRID_AIM)
  34.     {
  35.         // prepare new values
  36.         float velocityX = rawX - rawLastX;
  37.         float velocityY = -rawY + rawLastY;
  38.         float velocityRadial = radial(velocityX, velocityY, rawX, -rawY);
  39.         float deflection = rawLength;
  40.         float deflection_old = sqrt(rawLastX * rawLastX + rawLastY * rawLastY);
  41.         float magnitude = 0;
  42.         float angle = atan2f(-rawY, rawX);
  43.         bool outputIsInward = false;
  44.  
  45.         // check deadzones
  46.         if (deflection > jc->inner_deadzone)
  47.         {
  48.  
  49.             magnitude = (deflection - jc->inner_deadzone) / (1.0f - jc->inner_deadzone - jc->outer_deadzone);
  50.             jc->inClippingZone = true;
  51.             // check outer_deadzone
  52.             if (deflection > 1.0f - jc->outer_deadzone)
  53.             {
  54.                 // clip outward radial velocity
  55.                 if (velocityRadial > 0.0f)
  56.                 {
  57.                     float dotProduct = velocityX * sin(angle) + velocityY * -cos(angle);
  58.                     velocityX = dotProduct * sin(angle);
  59.                     velocityY = dotProduct * -cos(angle);
  60.                 }
  61.                 magnitude = 1.0f;
  62.                 // check entering
  63.                 if (deflection_old <= 1.0f - jc->outer_deadzone)
  64.                 {
  65.                     //change it so it smoothes not by time but by distance so fast movements are more accurate
  66.                     float cumulativeVX = 0.0f;
  67.                     float cumulativeVY = 0.0f;
  68.                     int steps = 0;
  69.                     int counter = jc->smoothingCounter;
  70.                     while (sqrt(cumulativeVX * cumulativeVX + cumulativeVY * cumulativeVY) < jc->smoothingDistance && steps < jc->smoothingSteps)
  71.                     {
  72.                         cumulativeVX += jc->previousVelocitiesX[counter];
  73.                         cumulativeVY += jc->previousVelocitiesY[counter];
  74.                         if (counter == 0)
  75.                             counter = jc->smoothingSteps - 1;
  76.                         else
  77.                             counter--;
  78.                         steps++;
  79.                     }
  80.                     jc->edgePushAmount = radial(cumulativeVX, cumulativeVY, rawX, -rawY) / steps;
  81.                 }
  82.             }
  83.         }
  84.         else
  85.         {
  86.  
  87.             jc->edgePushAmount = 0.0f;
  88.             // smooth reset inClippingZone too as the controller updates slower than JSM (?) therefore velocity is often zero
  89.             float cumulativeVX = velocityX;
  90.             float cumulativeVY = velocityY;
  91.             for (int i = 0; i < jc->smoothingSteps; i++)
  92.             {
  93.                 cumulativeVX += jc->previousVelocitiesX[i];
  94.                 cumulativeVY += jc->previousVelocitiesY[i];
  95.             }
  96.             if (radial(cumulativeVX, cumulativeVY, rawX, -rawY) >= 0.0f)
  97.             {
  98.                 jc->inClippingZone = false;
  99.             }
  100.             bool debug = jc->inClippingZone;
  101.  
  102.         }
  103.  
  104.         // compute output
  105.         float magnitudeX = magnitude * cos(angle);
  106.         float magnitudeY = magnitude * sin(angle);
  107.         float outputX = jc->factor_integral * magnitudeX * deltaTime;
  108.         float outputY = jc->factor_integral * magnitudeY * deltaTime;
  109.         outputX += jc->factor_proportional * magnitudeX * jc->edgePushAmount;
  110.         outputY += jc->factor_proportional * magnitudeY * jc->edgePushAmount;
  111.         outputX += jc->factor_proportional * velocityX;
  112.         outputY += jc->factor_proportional * velocityY;
  113.  
  114.  
  115.         float outputInward = radial(outputX, outputY, rawX, -rawY);
  116.         if (outputInward < 0.f)
  117.             outputIsInward = true;
  118.         else
  119.             outputInward = 0.f;
  120.  
  121.         // for smoothing edgePush and clipping on returning to center
  122.         if (jc->smoothingCounter < jc->smoothingSteps - 1)
  123.         {
  124.             jc->smoothingCounter++;
  125.         }
  126.         else
  127.         {
  128.             jc->smoothingCounter = 0;
  129.         }
  130.         jc->previousVelocitiesX[jc->smoothingCounter] = velocityX;
  131.         jc->previousVelocitiesY[jc->smoothingCounter] = velocityY;
  132.         jc->previousOutputInward[jc->smoothingCounter] = outputInward;
  133.         jc->previousOutputX[jc->smoothingCounter] = outputX;
  134.         jc->previousOutputY[jc->smoothingCounter] = outputY;
  135.        
  136.         // clip output while returning to center
  137.         if (jc->clippingIsActive && jc->inClippingZone)
  138.         {          
  139.             if (outputIsInward)
  140.             {              
  141.                 if (jc->clippingIsRadial)
  142.                 {
  143.                     float averageOutputInward = 0;
  144.                     for (int i = 0; i < jc->smoothingSteps; i++)
  145.                     {
  146.                         averageOutputInward += jc->previousOutputInward[i];
  147.                     }
  148.                     averageOutputInward /= jc->smoothingSteps;
  149.                     float fraction = ( (-averageOutputInward / deltaTime) - jc->clippingThreshhold) / jc->clippingRampUp;
  150.                     if (fraction < 0.f)
  151.                         fraction = 0.f;
  152.                     if (fraction > 1.f)
  153.                         fraction = 1.f;
  154.                     outputX += cos(angle) * (-1 + fraction) * outputInward;
  155.                     outputY += sin(angle) * (-1 + fraction) * outputInward;
  156.                 }
  157.                 else
  158.                 {
  159.                     float averageOutputX = 0.f;
  160.                     float averageOutputY = 0.f;
  161.                     for (int i = 0; i < jc->smoothingSteps; i++)
  162.                     {
  163.                         averageOutputX += jc->previousOutputX[i];
  164.                         averageOutputY += jc->previousOutputY[i];
  165.                     }
  166.                     float averageOutput = sqrt(averageOutputX * averageOutputX + averageOutputY * averageOutputY) / jc->smoothingSteps;
  167.                     float fraction = ( (averageOutput / deltaTime) - jc->clippingThreshhold) / jc->clippingRampUp;
  168.                     if (fraction < 0.f)
  169.                         fraction = 0.f;
  170.                     if (fraction > 1.f)
  171.                         fraction = 1.f;
  172.                     outputX *= fraction;
  173.                     outputY *= fraction;
  174.                 }              
  175.                 //outputX = 0.f; //for debugging
  176.                 //outputY = 0.f;
  177.             }      
  178.  
  179.         }
  180.         moveMouse(outputX, outputY);
  181.     }
  182.  
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement