summaryrefslogtreecommitdiff
path: root/src/engine/vector.js
diff options
context:
space:
mode:
authorniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-23 18:32:29 +0100
committerniliara-edu <nil.jimeno@estudiant.fjaverianas.com>2024-12-23 18:32:29 +0100
commita840990bdcabf45fb0d377478ba0ab27222434ae (patch)
tree030a6c3a6befce284733f5643d3972e3a1504bb2 /src/engine/vector.js
initial commit
Diffstat (limited to 'src/engine/vector.js')
-rw-r--r--src/engine/vector.js47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/engine/vector.js b/src/engine/vector.js
new file mode 100644
index 0000000..452e47b
--- /dev/null
+++ b/src/engine/vector.js
@@ -0,0 +1,47 @@
+export class Vector {
+ constructor(x=0, y=0) {
+ this.x = x
+ this.y = y
+ }
+
+ add(newVector) {
+ this.x += newVector.x
+ this.y += newVector.y
+ return this
+ }
+
+ normalize() {
+ if (this.x != 0 && 0 != this.y) {
+ let hyp = Math.abs(this.x)
+ let cSquared = Math.pow(hyp, 2) / 2
+ let c = Math.sqrt(cSquared, 2)
+
+ this.x = this.x / Math.abs(this.x) * c
+ this.y = this.y / Math.abs(this.x) * c
+ }
+ return this
+ }
+
+ multiply(multiplier) {
+ this.x *= multiplier
+ this.y *= multiplier
+ return this
+ }
+
+ clone() {
+ return new Vector(this.x, this.y)
+ }
+
+ static division(vector_a, vector_b) {
+ return new Vector(
+ vector_a.x / vector_b.x,
+ vector_a.y / vector_b.y
+ )
+ }
+
+ floor() {
+ this.x = Math.floor(this.x)
+ this.y = Math.floor(this.y)
+ return this
+ }
+}