Guest User

Untitled

a guest
Dec 26th, 2023
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.28 KB | None | 0 0
  1. FVector ULowLevelBirdMathFunctionLibrary::SuggestProjectileVelocityFromPeakHeight(const UObject* WorldContext, FVector startPos, FVector endPos, double overrideGravityZ, double peakHeight)
  2. {
  3. // First, convert the 3D point to point into the equivalent 2D math. That's how all the ballistic equations are written! Don't make this harder than need be!
  4. FVector displacement3D = endPos - startPos;
  5. float displacementDistance = (displacement3D.X * displacement3D.X) + (displacement3D.Y * displacement3D.Y);
  6.  
  7. if (!ensureMsgf(displacementDistance > 0.001, TEXT("distance travelled must be non-zero"))) { return FVector(0, 0, 0); }
  8.  
  9. FVector displacementDirection = FVector(displacement3D.X, displacement3D.Y, 0);
  10. displacementDirection.Normalize();
  11.  
  12. displacementDistance = sqrt(displacementDistance);
  13. FVector2D displacement = FVector2D(displacementDistance, displacement3D.Z);
  14.  
  15. if (!ensureMsgf(peakHeight > 0.0, TEXT("peakHeight must be higher than the starting position"))) { return FVector(0, 0, 0); }
  16. if (!ensureMsgf(peakHeight > displacement.Y, TEXT("peakHeight must be higher than the end position"))) { return FVector(0, 0, 0); }
  17.  
  18. // Extract the current world gravity (assuming we can), and override if necessary
  19. double gravity = 0;
  20. if (WorldContext != NULL) { gravity = WorldContext->GetWorld()->GetGravityZ(); }
  21. if (overrideGravityZ != 0) { gravity = overrideGravityZ; }
  22.  
  23. // Now do the fancy math (care of @Rich in channel) to calculate the launch velocity necessary to meet the given parameters
  24. FVector2D launchVelocity;
  25. launchVelocity = FVector2D::ZeroVector;
  26. launchVelocity.Y = FMath::Sqrt(-2.0f * gravity * peakHeight);
  27.  
  28. double fallDisp = displacement.Y - peakHeight;
  29. double invG = 1.0f / gravity;
  30.  
  31. double tPeak = -launchVelocity.Y * invG; // Peak Time
  32. double tFall = FMath::Sqrt(2 * fallDisp * invG);
  33. double tTotal = tPeak + tFall; // Target Time
  34.  
  35. launchVelocity.X = displacement.X / tTotal;
  36.  
  37. // Done! Now convert it back into 3D space, and return
  38. FVector launchVelocity3D;
  39. launchVelocity3D.X = displacementDirection.X * launchVelocity.X;
  40. launchVelocity3D.Y = displacementDirection.Y * launchVelocity.X;
  41. launchVelocity3D.Z = launchVelocity.Y;
  42.  
  43. return launchVelocity3D;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment