Skip to content

Commit 388bea9

Browse files
arunvickramendragor
authored andcommitted
Added moveToward for vectors.
1 parent f25c3c7 commit 388bea9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

godot/core/vector2.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import math, godotbase, hashes
55
import internal/godotinternaltypes, internal/godotstrings
66
import godotcoretypes, gdnativeapi
77

8+
const EPSILON = 0.00001'f32
9+
810
{.push stackTrace: off.}
911

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

204+
proc moveToward*(vFrom, to: Vector2, delta: float32): Vector2 =
205+
let
206+
vd = to - vFrom
207+
vLen = vd.length
208+
209+
if vLen <= delta or vLen < EPSILON:
210+
result = to
211+
else:
212+
result = vFrom + (vd / vLen) * delta
213+
202214
{.pop.} # stackTrace: off

godot/core/vector3.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import math, hashes
44
import godotbase, godotcoretypes
55

6+
const EPSILON = 0.00001'f32
7+
68
{.push stackTrace: off.}
79

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

244+
proc moveToward*(vFrom, to: Vector3, delta: float32): Vector3 =
245+
let
246+
vd = to - vFrom
247+
vLen = vd.length
248+
249+
if vLen <= delta or vLen < EPSILON:
250+
result = to
251+
else:
252+
result = vFrom + (vd / vLen) * delta
253+
242254
{.pop.} # stackTrace: off

0 commit comments

Comments
 (0)