Advertisement
DirectXManiac

Untitled

Mar 20th, 2021
484
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1.  
  2. float UMathUtils::CalculateTrajectory(float distance, float gravity, float projectileVelocity)
  3. {
  4.     return 0.5f * FMath::Asin((-gravity * distance) / (projectileVelocity*projectileVelocity));
  5. }
  6.  
  7. float UMathUtils::FirstOrderInterceptTime(float speed, FVector targetRelativePosition, FVector targetRelativeVelocity)
  8. {
  9.     float velocitySquared = targetRelativeVelocity.SizeSquared();
  10.     if (velocitySquared < 0.001f)
  11.         velocitySquared = 0.0f;
  12.  
  13.     float a = velocitySquared - speed * speed;
  14.  
  15.     if (FMath::Abs(a) < 0.001f)
  16.     {
  17.         float t = -targetRelativePosition.SizeSquared() / (2.0f * FVector::DotProduct(targetRelativeVelocity, targetRelativePosition));
  18.         return FMath::Max(t, 0.0f);
  19.     }
  20.     float b = 2.0f * FVector::DotProduct(targetRelativeVelocity, targetRelativePosition);
  21.     float c = targetRelativeVelocity.SizeSquared();
  22.     float determinant = b * b - 4.0f * a * c;
  23.     if (determinant > 0.0f)
  24.     {
  25.         float t1 = (-b + FMath::Sqrt(determinant)) / (2.0f * a);
  26.         float t2 = (-b - FMath::Sqrt(determinant)) / (2.0f * a);
  27.         if (t1 > 0)
  28.         {
  29.             if (t2 > 0) return FMath::Min(t1, t2);
  30.             return t1;
  31.         }
  32.         return FMath::Max(t2, 0.0f);
  33.     }
  34.     if (determinant < 0) return 0;
  35.     return FMath::Max(-b / (2.0f * a), 0.0f);
  36. }
  37.  
  38. FVector UMathUtils::FirstOrderIntercept(FVector location, FVector velocity, float speed, FVector targetLocation, FVector targetVelocity)
  39. {
  40.     FVector targetRelativePosition = targetLocation - location;
  41.     FVector targetRelativeVelocity = targetVelocity - velocity;
  42.     float t = FirstOrderInterceptTime(speed, targetRelativePosition, targetRelativeVelocity);
  43.     return targetLocation + t * targetRelativeVelocity;
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement