Skip to content

Instantly share code, notes, and snippets.

@foopis23
Last active February 22, 2022 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foopis23/548669ceded46a932f55e81aba1168eb to your computer and use it in GitHub Desktop.
Save foopis23/548669ceded46a932f55e81aba1168eb to your computer and use it in GitHub Desktop.
Game Math Functions for TS
function clamp (v: number, min: number, max: number): number {
return Math.min(Math.max(v, min), max)
}
function lerp(start: number, end: number, step: number) {
return clamp((end-start) * step + start, start, end)
}
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