summaryrefslogtreecommitdiff
path: root/engine/utils
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-07-20 20:47:32 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2023-07-20 20:47:32 -0700
commit72c6c7de12e9833f52bf2d0718d70f044f8ab57e (patch)
tree152f5f31d59011bb8c617bfbcfc44cc8f47ecad5 /engine/utils
parent0fd9fb097552686f2257c1aa689d797e80057bd1 (diff)
downloadjumpstorm-72c6c7de12e9833f52bf2d0718d70f044f8ab57e.tar.gz
jumpstorm-72c6c7de12e9833f52bf2d0718d70f044f8ab57e.zip
a bit of refactoring; importing engine into bun for server
Diffstat (limited to 'engine/utils')
-rw-r--r--engine/utils/clamp.ts2
-rw-r--r--engine/utils/dotProduct.ts4
-rw-r--r--engine/utils/index.ts4
-rw-r--r--engine/utils/normalizeVector.ts8
-rw-r--r--engine/utils/rotateVector.ts15
5 files changed, 33 insertions, 0 deletions
diff --git a/engine/utils/clamp.ts b/engine/utils/clamp.ts
new file mode 100644
index 0000000..42e1764
--- /dev/null
+++ b/engine/utils/clamp.ts
@@ -0,0 +1,2 @@
+export const clamp = (num: number, min: number, max: number) =>
+ Math.min(Math.max(num, min), max);
diff --git a/engine/utils/dotProduct.ts b/engine/utils/dotProduct.ts
new file mode 100644
index 0000000..59f8857
--- /dev/null
+++ b/engine/utils/dotProduct.ts
@@ -0,0 +1,4 @@
+import type { Coord2D } from "../interfaces";
+
+export const dotProduct = (vector1: Coord2D, vector2: Coord2D): number =>
+ vector1.x * vector2.x + vector1.y * vector2.y;
diff --git a/engine/utils/index.ts b/engine/utils/index.ts
new file mode 100644
index 0000000..4e465c2
--- /dev/null
+++ b/engine/utils/index.ts
@@ -0,0 +1,4 @@
+export * from "./rotateVector";
+export * from "./normalizeVector";
+export * from "./dotProduct";
+export * from "./clamp";
diff --git a/engine/utils/normalizeVector.ts b/engine/utils/normalizeVector.ts
new file mode 100644
index 0000000..e6dfd7f
--- /dev/null
+++ b/engine/utils/normalizeVector.ts
@@ -0,0 +1,8 @@
+import type { Coord2D } from "../interfaces";
+
+export const normalizeVector = (vector: Coord2D): Coord2D => {
+ const { x, y } = vector;
+ const length = Math.sqrt(x * x + y * y);
+
+ return { x: x / length, y: y / length };
+};
diff --git a/engine/utils/rotateVector.ts b/engine/utils/rotateVector.ts
new file mode 100644
index 0000000..82bb54d
--- /dev/null
+++ b/engine/utils/rotateVector.ts
@@ -0,0 +1,15 @@
+import type { Coord2D } from "../interfaces";
+
+/**
+ * ([[cos(θ), -sin(θ),]) ([x,)
+ * ([sin(θ), cos(θ)] ]) ( y])
+ */
+export const rotateVector = (vector: Coord2D, theta: number): Coord2D => {
+ const rads = (theta * Math.PI) / 180;
+ const [cos, sin] = [Math.cos(rads), Math.sin(rads)];
+
+ return {
+ x: vector.x * cos - vector.y * sin,
+ y: vector.x * sin + vector.y * cos,
+ };
+};