From 474a7253b6b67e2ed33936f6b633587d5d304b66 Mon Sep 17 00:00:00 2001 From: niliara-edu Date: Thu, 26 Dec 2024 14:24:53 +0100 Subject: hello kitty stage 1 and 2 done --- src/engine/vector.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src/engine/vector.js') diff --git a/src/engine/vector.js b/src/engine/vector.js index f596c51..739daaf 100644 --- a/src/engine/vector.js +++ b/src/engine/vector.js @@ -11,12 +11,17 @@ export class Vector { 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 is what it should be doing: */ + // 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 + // this.x = this.x / Math.abs(this.x) * c + // this.y = this.y / Math.abs(this.x) * c + + /* but we need to save resources */ + this.x = this.x / 4 * 3 + this.y = this.y / 4 * 3 } } @@ -29,6 +34,13 @@ export class Vector { return new Vector(this.x, this.y) } + static substraction(vector_a, vector_b) { + return new Vector( + vector_a.x - vector_b.x, + vector_a.y - vector_b.y + ) + } + static division(vector_a, vector_b) { return new Vector( vector_a.x / vector_b.x, @@ -41,4 +53,18 @@ export class Vector { this.y = Math.floor(this.y) return this } + + toDeg() { + var angle = Math.atan2(this.y, this.x) //radians + var degrees = 180*angle/Math.PI //degrees + return (360+Math.round(degrees))%360 //round number, avoid decimal fragments + } + + static fromDeg(degrees) { + let radians = degrees*Math.PI/180 + return new Vector( + Math.cos(radians), + Math.sin(radians) + ) + } } -- cgit v1.2.3