Skip to content

Commit

Permalink
Added moveToward for vectors.
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvickram authored and endragor committed Dec 13, 2020
1 parent f25c3c7 commit 388bea9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions godot/core/vector2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import math, godotbase, hashes
import internal/godotinternaltypes, internal/godotstrings
import godotcoretypes, gdnativeapi

const EPSILON = 0.00001'f32

{.push stackTrace: off.}

proc vec2*(): Vector2 {.inline, noinit.} =
Expand Down Expand Up @@ -199,4 +201,14 @@ proc clamped*(self: Vector2; length: float32): Vector2 {.noinit.} =
result /= len
result *= length

proc moveToward*(vFrom, to: Vector2, delta: float32): Vector2 =
let
vd = to - vFrom
vLen = vd.length

if vLen <= delta or vLen < EPSILON:
result = to
else:
result = vFrom + (vd / vLen) * delta

{.pop.} # stackTrace: off
12 changes: 12 additions & 0 deletions godot/core/vector3.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import math, hashes
import godotbase, godotcoretypes

const EPSILON = 0.00001'f32

{.push stackTrace: off.}

proc vec3*(): Vector3 {.inline.} =
Expand Down Expand Up @@ -239,4 +241,14 @@ proc cubicInterpolate*(self, b, preA, postB: Vector3;
(2.0 * p0 - 5.0 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3.0 * p1 - 3.0 * p2 + p3) * t3)

proc moveToward*(vFrom, to: Vector3, delta: float32): Vector3 =
let
vd = to - vFrom
vLen = vd.length

if vLen <= delta or vLen < EPSILON:
result = to
else:
result = vFrom + (vd / vLen) * delta

{.pop.} # stackTrace: off

0 comments on commit 388bea9

Please sign in to comment.