Browse Source

✨ Add some more methods to the Vector2 Component

Pedro Schneider 3 years ago
parent
commit
023d9bac1e
3 changed files with 88 additions and 3 deletions
  1. 1 1
      pandora.min.js
  2. 84 0
      pandora/components/Vector2.js
  3. 3 2
      pandora/singletons/Collisions.js

File diff suppressed because it is too large
+ 1 - 1
pandora.min.js


+ 84 - 0
pandora/components/Vector2.js

@@ -157,6 +157,90 @@ class Vector2 extends Component
         return new Vector2(this.x / len, this.y / len);
     }
 
+    /**
+     * Normalizes this vector inplace.
+     */
+    normalize()
+    {
+        let len = this.length();
+        this.x /= len;
+        this.y /= len;
+    }
+
+    /**
+     * Creates a Vector2 rotated a radians clockwise in relation to this Vector2.
+     * 
+     * @param {number} a    angle in radians to rotate the vector.
+     * 
+     * @returns {Vector2}   rotated Vector2.
+     */
+    rotated(a)
+    {
+        return new Vector2(this.x * cos(-a) - this.y * sin(-a), this.x * sin(-a) + this.y * cos(-a))
+    }
+
+    /**
+     * Rotates this Vector2 inplace by a radians clockwise.
+     * 
+     * @param {number} a    angle in radians to rotate this vector.
+     */
+    rotate(a)
+    {
+        let rotX = this.x * cos(-a) - this.y * sin(-a),
+            rotY = this.x * sin(-a) + this.y * cos(-a);
+
+        this.x = rotX;
+        this.y = rotY;
+    }
+
+    /**
+     * Creates a Vector2 based on this Vector2 but scaled by s.
+     * 
+     * @param {number} s    factor to scale the Vector2.
+     * 
+     * @returns {Vector2}   scaled Vector2. 
+     */
+    scaled(s)
+    {
+        return new Vector2(this.x * s, this.y * s);
+    }
+
+    /**
+     * Scales this Vector2 inplace.
+     * 
+     * @param {number} s    factor to scale the Vector2.
+     */
+    scale(s)
+    {
+        this.x *= s;
+        this.y *= s;
+    }
+
+    /**
+     * Creates a Vector2 based on this Vector2 but translated by x on the X-axis and y on the Y-axis.
+     * 
+     * @param {number} x    X-axis translation.
+     * @param {number} y    Y-axis translation.
+     * 
+     * @returns {Vector2}   translated Vector2.
+     */
+    translated(x, y)
+    {
+        return new Vector2(this.x + x, this.y + y);
+    }
+
+    /**
+     * Translates this Vector2 by x on the X-axis and y on the Y-axis.
+     * 
+     * @param {number} x    X-axis translation.
+     * @param {number} y    Y-axis translation.
+     */
+    translate(x, y)
+    {
+        this.x += x;
+        this.y += y;
+    }
+
     /**
      * Returns this Vector2's distance squared to another Vector2.
      * 

+ 3 - 2
pandora/singletons/Collisions.js

@@ -43,10 +43,11 @@ const Collisions = {
     {
         var rotx = x,
             roty = y;
+        var ra = radians(a)
         if (a % 360 != 0)
         {
-            rotx = x * cos(-radians(a)) - y * sin(-radians(a));
-            roty = x * sin(-radians(a)) + y * cos(-radians(a));
+            rotx = x * cos(-ra) - y * sin(-ra);
+            roty = x * sin(-ra) + y * cos(-ra);
         }
 
         return Object.freeze([rotx, roty]);