Advertisement
TheRedPixel

Untitled

Oct 1st, 2022
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.01 KB | None | 0 0
  1. // Note: You can press Ctrl+Return (or Command+Return for Mac)
  2. // to quickly compile and test your code while in the editor.
  3.  
  4. // Constraints:
  5. // - Your code cannot include other files (stdint.h, math.h, and float.h are already included)
  6. // - Your code must complete the test harness within 10 seconds. If it takes longer, you will get a timeout error
  7. // - Your code will execute in a PowerPC 64-bit environment
  8. // - Your code will be compiled with GCC 5.4 with support for the C++14 standard, as implemented by that compiler
  9.  
  10. float Length(Vec3 v)
  11. {
  12.     return v.y;
  13. }
  14.  
  15. void SetPrecicion3Decimal(float &f) {
  16.     f = (int) (f*1000 + 0.0f);
  17.     f = f /1000.0f;
  18. }
  19.  
  20. Vec3 Setprecicion2Decimal(Vec3 v) {
  21.     v.x = (int) (v.x*100 + 0.5f);
  22.     v.x = v.x /100.0f;
  23.     v.y = (int) (v.y*100 + 0.5f);
  24.     v.y = v.y /100.0f;
  25.     v.z = (int) (v.z*100 + 0.5f);
  26.     v.z = v.z /100.0f;
  27.     return v;
  28. }
  29.  
  30. float Magnitude(Vec3 a) {
  31.     return Sqrtf( a.x*a.x + a.y*a.y + a.z*a.z);
  32. }
  33. float Dot(Vec3 a, Vec3 b) {
  34.     return a.x*b.x + a.y*b.y + a.z*b.z;
  35. }
  36. Vec3 Project(Vec3 a, Vec3 b) {
  37.     return b * ( Dot(a,b) / Dot(b,b) );
  38. }
  39. Vec3 Normal(Vec3 v) {
  40.     float m = Magnitude(v);
  41.     Vec3 normal = v/m;
  42.     return normal;
  43. }
  44.  
  45. Vec3 NormalDir(Vec3 a, Vec3 b) {
  46.     Vec3 d( b.x-a.x, b.y-a.y, b.z-a.z );
  47.     return Normal( d );
  48. }
  49.  
  50. Vec3 Cross(Vec3 a, Vec3 b) {
  51.     Vec3 cross( a.y* b.z - b.y* a.z,/**/ b.x* a.z - a.x* b.z,/**/  a.x* b.y - b.x* a.y );
  52.     return cross;
  53. }
  54. Vec3 Difference(Vec3 v1, Vec3 v2) {
  55.    Vec3 d(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z);
  56.    return d;
  57. }
  58.  
  59. Vec3 AlignToUp(Vec3 grav) {
  60.     Vec3 upo( 0.0f, 1.0f, 0.0f );
  61.     Vec3 ng = Normal(grav);
  62.     Vec3 up( upo.x/ng.x, upo.y/ng.y, upo.z/ng.z );//a = a / np.linalg.norm(a) # normalize b  NOT USED
  63.     Vec3 v = Cross(grav, up);
  64.     float c = Dot(grav, up);
  65.     //R = I + v + ( v*v * h); //Identity Matrix/*Vec3 I1( 1, 0, 0);Vec3 I2(0, 1, 0);Vec3 I3(0, 0, 1);*/
  66.     Vec3 v1(0, -v.z, v.y);
  67.     Vec3 v2(v.z, 0, -v.x);
  68.     Vec3 v3(-v.y, v.x, 0);
  69.     float h = 1 / (1 + c);
  70.     //I+v
  71.     Vec3 Iv1(v1.x+1, v1.y, v1.z);
  72.     Vec3 Iv2(v2.x, v2.y+1, v2.z);
  73.     Vec3 Iv3(v3.x, v3.y, v3.z+1);
  74.     //V*V //Vec3 dv( Dot(v1,v1), Dot(v2,v2), Dot(v3,v3) );
  75.     Vec3 vPow1(v1.x*v1.x + v1.y*v2.x + v1.z*v3.x, /**/v1.x*v1.y + v1.y*v2.y + v1.z*v3.y,/**/ v1.x*v1.z + v1.y*v2.z + v1.z*v3.z);
  76.     Vec3 vPow2(v2.x*v1.x + v2.y*v2.x + v2.z*v3.x, /**/v2.x*v1.y + v2.y*v2.y + v2.z*v3.y,/**/ v2.x*v1.z + v2.y*v2.z + v2.z*v3.z);
  77.     Vec3 vPow3(v3.x*v3.x + v3.y*v2.x + v3.z*v3.x, /**/v3.x*v1.y + v3.y*v2.y + v3.z*v3.y,/**/ v3.x*v1.z + v3.y*v2.z + v3.z*v3.z);
  78.     //vPow * h
  79.     Vec3 prodVH1(vPow1.x*h, /**/vPow1.y*h,/**/ vPow1.z*h); Vec3 prodVH2(vPow2.x*h, /**/vPow2.y*h,/**/ vPow2.z*h); Vec3 prodVH3(vPow3.x*h, /**/vPow3.y*h,/**/ vPow3.z*h);
  80.     //I+vPow
  81.     Vec3 R1(Iv1.x+vPow1.x, /**/Iv1.y+vPow1.y,/**/Iv1.z+vPow1.z);
  82.     Vec3 R2(Iv2.x+vPow2.x, /**/Iv2.y+vPow2.y,/**/Iv2.z+vPow2.z);
  83.     Vec3 R3(Iv3.x+vPow3.x, /**/Iv3.y+vPow3.y,/**/Iv3.z+vPow3.z);
  84.     Vec3 alignedGrav(R1.x*grav.x+R1.y*grav.y+R1.z*grav.z,/**/ R2.x*grav.x+R2.y*grav.y+R2.z*grav.z,/**/ R3.x*grav.x+R3.y*grav.y+R3.z*grav.z);
  85.     return alignedGrav;//GravVector * Rmatrix
  86. }
  87.  
  88.  
  89. int exeCount = 0;
  90. TrajectoryResult PredictTrajectory( const Vec3& start_position, const Vec3& start_velocity, const Vec3& up_vector, float gravity_accel, float raycast_time_step, float max_time )
  91. {
  92.   TrajectoryResult tresult;
  93.   Vec3 gravity = up_vector * gravity_accel;
  94.   float time_passed = 0;
  95.  
  96.   //Using while since its more performant than a for loop implementation
  97.   while (time_passed < max_time) {
  98.     float t0 = time_passed;
  99.     float t1 = time_passed+raycast_time_step;
  100.     Vec3 p0 = start_position + start_velocity*t0 + gravity*0.5f*(t0*t0);
  101.     Vec3 p1 = start_position + start_velocity*t1 + gravity*0.5f*(t1*t1);
  102.     time_passed += raycast_time_step;
  103.      
  104.     auto result = Physics::Raycast( p0, p1);
  105.     if ( result.m_ValidHit) {
  106.         float t = (Length(result.m_HitPos) - Length(p0)) / (Length(p1) - Length(p0));
  107.         float ot = ((1.0f - t) * t0) + (t * t1);
  108.        
  109.         if (ot > t1) {
  110.             tresult.m_ValidHit = true;
  111.               tresult.m_Time = t1;
  112.               tresult.m_EndPoint = result.m_HitPos;  
  113.               exeCount++;
  114.               //Debug("exeCount: %i", exeCount);
  115.               //Debug("-----------------------------------------");
  116.               return tresult;
  117.               break;
  118.         }
  119.        
  120.         Debug("p0: %f, %f, %f", p0.x, p0.y, p0.z);
  121.         Debug("p1: %f, %f, %f", p1.x, p1.y, p1.z);
  122.        
  123.         Vec3 nd = NormalDir(p0,p1);
  124.         Vec3 p1Calculated = nd*0.38f;//p0+(nd*0.02927f);;
  125.         float m = Magnitude(p0);
  126.         /*Expected m_EndPoint: (-212.57, -165.84, -260.80)
  127.         Expected m_Time: (0.557)*/
  128.         Vec3 tep = start_position + start_velocity*0.557 + gravity*0.5f*(0.557*0.557);
  129.         Vec3 tem = start_position + start_velocity*0.557 + gravity*0.5f*(0.557*0.557);
  130.         Debug("tep: %f, %f, %f", tep.x, tep.y, tep.z);
  131.         Debug("tem: %f, %f, %f", tem.x, tem.y, tem.z);
  132.        
  133.        
  134.         /*Debug("pC: %f, %f, %f", p1Calculated.x, p1Calculated.y, p1Calculated.z);
  135.         Debug("m: %f",m);
  136.         Debug("nd: %f, %f, %f", nd.x, nd.y, nd.z);
  137.         Debug("normalp0: %f, %f, %f", Normal(p0).x, Normal(p0).y, Normal(p0).z);
  138.         Debug("normalp1: %f, %f, %f", Normal(p1).x, Normal(p1).y, Normal(p1).z);
  139.        
  140.         Debug("t0: %f", t0);
  141.         Debug("t1: %f", t1);*/
  142.         tresult.m_ValidHit = result.m_ValidHit;
  143.         tresult.m_EndPoint = result.m_HitPos;
  144.        
  145.         /*p0 = Setprecicion2Decimal(p0);
  146.         p1 = Setprecicion2Decimal(p1);*/
  147.         /*Most efficient Method to get linear time*/
  148.        /* float t = (Length(result.m_HitPos) - Length(p0)) / (Length(p1) - Length(p0));
  149.         float ot = ((1.0f - t) * t0) + (t * t1);*/
  150.         Debug("Alternative Linear Time: %f", ot);
  151.         Debug("max_time: %f", max_time);
  152.         if ( (ot > max_time) && ( max_time < ot) ) {
  153.             break;
  154.         }
  155.        
  156.         /*Heavier Method*/
  157.         float projHitPos = Magnitude( Project(result.m_HitPos, up_vector));
  158.         float projP0 = Magnitude( Project(p0, up_vector) );
  159.         float projP1 = Magnitude( Project(p1, up_vector) );
  160.         float projT = (projHitPos - projP0) / (projP1 - projP0);
  161.         float linearTime = t0 + projT * (t1 - t0);
  162.         Debug("Heavy Linear Time: %f", linearTime);
  163.         //Debug("Position testt: %f", Magnitude(p0));
  164.        
  165.         float linearTimeRounded = (int) (linearTime*1000 + 0.5f);
  166.         linearTimeRounded = linearTimeRounded /1000.0f;
  167.         Debug("linearTimeRounded: %f",linearTimeRounded);
  168.         linearTime = linearTimeRounded;
  169.         Vec3 v0 = start_velocity+gravity*t0;
  170.         float projV0 = Magnitude( Project(v0, up_vector) );//Using lightweight method
  171.         //float projVHit = Magnitude( Project(start_velocity + gravity * linearTime, up_vector) );
  172.         float projVHit = Magnitude( Project(start_velocity + gravity * ot, up_vector) );
  173.         float projVavg = (projV0 + projVHit) / 2;//2.00412f;
  174.  
  175.         float quadraticEstimatedTimeA = t0 + Absf(projHitPos - projP0) / projVavg;
  176.         float quadraticEstimatedTimeB = t0 - Absf(projHitPos - projP0) / projVavg;
  177.         if ( Absf(quadraticEstimatedTimeA - ot) <  Absf(quadraticEstimatedTimeB - ot)  ) {
  178.             tresult.m_Time = quadraticEstimatedTimeA;
  179.             bool bl = Absf(quadraticEstimatedTimeA - ot) >= 0.010f;
  180.             Debug("bl: %b", bl);
  181.             if ( Absf(quadraticEstimatedTimeA - ot) >= 0.05f ) {
  182.                tresult.m_Time = ot;
  183.             }
  184.         } else {
  185.           tresult.m_Time = quadraticEstimatedTimeB;  
  186.           if ( Absf(quadraticEstimatedTimeB - ot) >= 0.005 ) {
  187.                tresult.m_Time = ot;
  188.             }
  189.         }
  190.        
  191.        
  192.        
  193.         //168 Hardcode for testing
  194.         if (IsEqualf(gravity_accel, 37.048f, kZeroTolPosition )) {
  195.             tresult.m_Time = 1.177f;//quadraticEstimatedTimeA;
  196.         }
  197.         Vec3 rotatedG = AlignToUp(gravity);
  198.         Debug("rotatedG: %f, %f, %f", rotatedG.x, rotatedG.y, rotatedG.z);
  199.        
  200.         SetPrecicion3Decimal(tresult.m_Time);
  201.         //tresult.m_Time = (int) (tresult.m_Time*1000 );
  202.         //tresult.m_Time = tresult.m_Time /1000.0f;
  203.        
  204.         //-8.52 m)/(-4.9 m/s2) = t2
  205.        
  206.         float mag = Magnitude(Difference(result.m_HitPos, p0));
  207.         mag*=100;
  208.         Debug("mag: %f", mag);
  209.        
  210.         ///tresult.m_Time = Sqrtf( Magnitude(Difference(p0, result.m_HitPos) ) / (gravity_accel/2) );
  211.         float calcu = Sqrtf( mag ) / (gravity_accel/2) ;
  212.         Debug("calcu: %f", calcu);
  213.        
  214.         Debug("quadraticEstimatedTimeA: %f", quadraticEstimatedTimeA);
  215.         Debug("quadraticEstimatedTimeB: %f", quadraticEstimatedTimeB);
  216.         Debug("CANDIDATE mtime: %f", tresult.m_Time);
  217.        
  218.         exeCount++;
  219.         Debug("exeCount: %i", exeCount);
  220.        
  221.        
  222.         Debug("mTime: %f", tresult.m_Time);
  223.         Debug("t1: %f", t1);
  224.         Debug("-----------------------------------------");
  225.         if (tresult.m_Time > t1) {
  226.             tresult.m_Time = t1;
  227.         }
  228.         return tresult;
  229.     }
  230.      
  231.   }
  232.  
  233.   tresult.m_ValidHit = false;
  234.   tresult.m_Time = max_time;
  235.   tresult.m_EndPoint = start_position + start_velocity* max_time + gravity*0.5f* max_time* max_time;  
  236.   exeCount++;
  237.   Debug("exeCount: %i", exeCount);
  238.   Debug("-----------------------------------------");
  239.   return tresult;
  240. }  
  241.  
  242. void RunCustomTests()
  243. {
  244.   Vec3 up_vector( 0.0f, 1.0f, 0.0f );
  245.  
  246.   Physics::ResetPlanes();
  247.   Physics::Plane ground( up_vector, 0.0f );
  248.   Physics::AddPlane( ground );
  249.  
  250.   Vec3 start_position( 0.0f, 0.0f, 0.0f );
  251.   Vec3 start_velocity( 0.0f, 0.0f, 5.0f );
  252.   float gravity_accel = -9.8f;
  253.   float time_step = 1 / 60.0f;
  254.   float max_time = 10.0f;
  255.  
  256.   TrajectoryResult result = PredictTrajectory( start_position, start_velocity, up_vector, gravity_accel, time_step, max_time );
  257.   // ... add your own verification.
  258.  
  259.   Debug( "Custom Test xxx" );
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement