Created
January 13, 2025 10:11
-
-
Save xThuby/03d1bc42140d8173e0655b8cc992f913 to your computer and use it in GitHub Desktop.
Jai approach/move_towards utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
approach :: (from: float, target: float, max_delta: float) -> float { | |
if from > target then return max(from - max_delta, target); | |
return min(from + max_delta, target); | |
} | |
approach :: (from: *float, target: float, max_delta: float) { | |
if from.* > target { | |
from.* = max(from.* - max_delta, target); | |
return; | |
} | |
from.* = min(from.* + max_delta, target); | |
} | |
approach :: (from: $R, target: R, max_delta: float) -> R | |
#modify { | |
// This lerp only applies to the types below: | |
return (R == Vector2) || (R == Vector3) || (R == Vector4); | |
} { | |
if (from == target) { | |
return target; | |
} | |
diff := target - from; | |
if (length_squared(diff) <= max_delta * max_delta) { | |
return target; | |
} | |
return from + normalize(diff) * max_delta; | |
} | |
approach :: (from: *$R, target: R, max_delta: float) | |
#modify { | |
// This lerp only applies to the types below: | |
return (R == Vector2) || (R == Vector3) || (R == Vector4); | |
} { | |
if (from.* == target) return; | |
diff := target - from.*; | |
if (length_squared(diff) <= max_delta * max_delta) { | |
from.* = target; | |
return; | |
} | |
from.* += normalize(diff) * max_delta; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment