summaryrefslogtreecommitdiff
path: root/src/engine/utils
diff options
context:
space:
mode:
authorElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
committerElizabeth Hunt <elizabeth.hunt@simponic.xyz>2024-03-01 18:56:58 -0700
commita8d07a790395e14fe7aedd3ba638db466f9c0842 (patch)
tree644f60a6bca79ceb55f24fcab7bdb3dee52c2d6e /src/engine/utils
parentaa08a8943a9a2d4a0e51893eebe6900bca7a7251 (diff)
downloadthe-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.tar.gz
the-abstraction-engine-a8d07a790395e14fe7aedd3ba638db466f9c0842.zip
boundingbox + draw player
Diffstat (limited to 'src/engine/utils')
-rw-r--r--src/engine/utils/clamp.ts2
-rw-r--r--src/engine/utils/dotProduct.ts4
-rw-r--r--src/engine/utils/index.ts3
-rw-r--r--src/engine/utils/rotateVector.ts15
4 files changed, 24 insertions, 0 deletions
diff --git a/src/engine/utils/clamp.ts b/src/engine/utils/clamp.ts
new file mode 100644
index 0000000..42e1764
--- /dev/null
+++ b/src/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/src/engine/utils/dotProduct.ts b/src/engine/utils/dotProduct.ts
new file mode 100644
index 0000000..59f8857
--- /dev/null
+++ b/src/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/src/engine/utils/index.ts b/src/engine/utils/index.ts
new file mode 100644
index 0000000..439e664
--- /dev/null
+++ b/src/engine/utils/index.ts
@@ -0,0 +1,3 @@
+export * from "./clamp";
+export * from "./dotProduct";
+export * from "./rotateVector";
diff --git a/src/engine/utils/rotateVector.ts b/src/engine/utils/rotateVector.ts
new file mode 100644
index 0000000..82bb54d
--- /dev/null
+++ b/src/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,
+ };
+};