Last active
February 22, 2022 17:39
-
-
Save foopis23/548669ceded46a932f55e81aba1168eb to your computer and use it in GitHub Desktop.
Game Math Functions for TS
This file contains 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
function clamp (v: number, min: number, max: number): number { | |
return Math.min(Math.max(v, min), max) | |
} |
This file contains 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
function lerp(start: number, end: number, step: number) { | |
return clamp((end-start) * step + start, start, end) | |
} |
This file contains 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
function moveTowards (current: number, towards: number, maxStep: number): number { | |
if (Math.abs(current - towards) < 0.01) return towards | |
const diff = towards - current | |
const direction = Math.abs(diff) / diff | |
if (Math.abs(diff) < maxStep) maxStep = Math.abs(diff) | |
return current + (direction * maxStep) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment