Skip to content

Commit

Permalink
feat(vector): normalize rads
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Sep 26, 2019
1 parent e429025 commit a8bbfc1
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions src/vector.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @ts-nocheck
import { isArray } from './util';
import { isArray, multQuatVec, normRad } from './util';
import {
cachedMethod,
cachedGetter,
Expand All @@ -8,8 +8,8 @@ import {
defineVectorLength,
cachedFactory
} from './operator';
import formatNumber from './formatter';
import { ipoint } from './point';
import { formatNumber } from './formatter';
import { IPoint } from './point';

const X = 0;
const Y = 1;
Expand All @@ -20,6 +20,10 @@ function square(val) {
return val * val;
}

/**
* @typedef {IPoint & number} IPointType
*/

/**
* @typedef {Victor & number} VictorType
* @typedef {Vector & number} VectorType
Expand Down Expand Up @@ -131,7 +135,7 @@ class AVector {
* @returns {number}
*/
angleTo(v) {
return Math.acos(this.dot(v) / (this.length * v.length));
return normRad(Math.acos(this.dot(v) / (this.length * v.length)));
}

// http://schteppe.github.io/cannon.js/docs/files/src_math_Quaternion.js.html
Expand All @@ -141,22 +145,7 @@ class AVector {
* @returns {AVectorType}
*/
rotate(quat) {
const { x, y, z } = this;

const {
x: qx, y: qy, z: qz, w: qw
} = quat;

const ix = qw * x + qy * z - qz * y;
const iy = qw * y + qz * x - qx * z;
const iz = qw * z + qx * y - qy * x;
const iw = -qx * x - qy * y - qz * z;

return new this.constructor(
ix * qw + iw * -qx + iy * -qz - iz * -qy,
iy * qw + iw * -qy + iz * -qx - ix * -qz,
iz * qw + iw * -qz + ix * -qy - iy * -qx
);
return multQuatVec(quat, this);
}

/**
Expand Down Expand Up @@ -185,8 +174,16 @@ class AVector {
return [this.x, this.y, this.z];
}


/**
* @param {string} target
* @returns {VectorType | VictorType | IPointType}
*/
swizzle(target) {
const data = target.split('').map(t => this[t]);
if (data.length === 2) {
return new IPoint(data[0], data[1]);
}
return new this.constructor(data[0], data[1], data[2]);
}

Expand Down Expand Up @@ -306,11 +303,10 @@ class AVector {
}

/**
* @typedef {((import("./point").IPoint) & number)} IPointType
* @returns {IPointType}
*/
get xy() {
return ipoint(this[AXES][X], this[AXES][Y]);
return new IPoint(this[AXES][X], this[AXES][Y]);
}

/**
Expand All @@ -324,7 +320,7 @@ class AVector {
* @returns {IPointType}
*/
get xz() {
return ipoint(this[AXES][X], this[AXES][Z]);
return new IPoint(this[AXES][X], this[AXES][Z]);
}

/**
Expand All @@ -338,7 +334,7 @@ class AVector {
* @returns {IPointType}
*/
get yz() {
return ipoint(this[AXES][Y], this[AXES][Z]);
return new IPoint(this[AXES][Y], this[AXES][Z]);
}

/**
Expand Down

0 comments on commit a8bbfc1

Please sign in to comment.