Advertisement
ShadowTzu

Untitled

Jul 8th, 2025
68
0
6 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.55 KB | None | 0 0
  1. float InvSqrt(float x) {
  2.     float xhalf = 0.5f * x;
  3.     int32_t i = *(int32_t*)&x; // Bit-level hacking
  4.     i = 0x5f3759df - (i >> 1);
  5.     x = *(float*)&i;
  6.     x = x * (1.5f - xhalf * x * x); // One iteration of Newton's method
  7.     return x;
  8. }
  9.  
  10. float DistanceApproximated(const Vector3& left, const Vector3& right) {
  11.     float difx = left.x - right.x;
  12.     float dify = left.y - right.y;
  13.     float difz = left.z - right.z;
  14.     float squaredDistance = difx * difx + dify * dify + difz * difz;
  15.     return squaredDistance * InvSqrt(squaredDistance);
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement