arcade-physics
Version:
Use Arcade Physics without Phaser.
744 lines • 22.9 kB
JavaScript
"use strict";
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
// Adapted from [gl-matrix](https://github.com/toji/gl-matrix) by toji
// and [vecmath](https://github.com/mattdesl/vecmath) by mattdesl
const Matrix3_1 = __importDefault(require("./Matrix3"));
const NOOP_1 = __importDefault(require("../utils/NOOP"));
const Vector3_1 = __importDefault(require("./Vector3"));
const EPSILON = 0.000001;
// Some shared 'private' arrays
const siNext = new Int8Array([1, 2, 0]);
const tmp = new Float32Array([0, 0, 0]);
const xUnitVec3 = new Vector3_1.default(1, 0, 0);
const yUnitVec3 = new Vector3_1.default(0, 1, 0);
const tmpvec = new Vector3_1.default();
const tmpMat3 = new Matrix3_1.default();
/**
* @classdesc
* A quaternion.
*
* @class Quaternion
* @memberof Phaser.Math
* @constructor
* @since 3.0.0
*
* @param {number} [x=0] - The x component.
* @param {number} [y=0] - The y component.
* @param {number} [z=0] - The z component.
* @param {number} [w=1] - The w component.
*/
class Quaternion {
constructor(x, y, z, w) {
/**
* The x component of this Quaternion.
*
* @name Phaser.Math.Quaternion#_x
* @type {number}
* @default 0
* @private
* @since 3.50.0
*/
/**
* The y component of this Quaternion.
*
* @name Phaser.Math.Quaternion#_y
* @type {number}
* @default 0
* @private
* @since 3.50.0
*/
/**
* The z component of this Quaternion.
*
* @name Phaser.Math.Quaternion#_z
* @type {number}
* @default 0
* @private
* @since 3.50.0
*/
/**
* The w component of this Quaternion.
*
* @name Phaser.Math.Quaternion#_w
* @type {number}
* @default 0
* @private
* @since 3.50.0
*/
/**
* This callback is invoked, if set, each time a value in this quaternion is changed.
* The callback is passed one argument, a reference to this quaternion.
*
* @name Phaser.Math.Quaternion#onChangeCallback
* @type {function}
* @since 3.50.0
*/
this.onChangeCallback = NOOP_1.default;
this.set(x, y, z, w);
}
/**
* The x component of this Quaternion.
*
* @name Phaser.Math.Quaternion#x
* @type {number}
* @default 0
* @since 3.0.0
*/
get x() {
return this._x;
}
set x(value) {
this._x = value;
this.onChangeCallback(this);
}
/**
* The y component of this Quaternion.
*
* @name Phaser.Math.Quaternion#y
* @type {number}
* @default 0
* @since 3.0.0
*/
get y() {
return this._y;
}
set y(value) {
this._y = value;
this.onChangeCallback(this);
}
/**
* The z component of this Quaternion.
*
* @name Phaser.Math.Quaternion#z
* @type {number}
* @default 0
* @since 3.0.0
*/
get z() {
return this._z;
}
set z(value) {
this._z = value;
this.onChangeCallback(this);
}
/**
* The w component of this Quaternion.
*
* @name Phaser.Math.Quaternion#w
* @type {number}
* @default 0
* @since 3.0.0
*/
get w() {
return this._w;
}
set w(value) {
this._w = value;
this.onChangeCallback(this);
}
/**
* Copy the components of a given Quaternion or Vector into this Quaternion.
*
* @method Phaser.Math.Quaternion#copy
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} src - The Quaternion or Vector to copy the components from.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
copy(src) {
return this.set(src);
}
/**
* Set the components of this Quaternion and optionally call the `onChangeCallback`.
*
* @method Phaser.Math.Quaternion#set
* @since 3.0.0
*
* @param {(number|object)} [x=0] - The x component, or an object containing x, y, z, and w components.
* @param {number} [y=0] - The y component.
* @param {number} [z=0] - The z component.
* @param {number} [w=0] - The w component.
* @param {boolean} [update=true] - Call the `onChangeCallback`?
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
set(x, y, z, w, update) {
if (update === undefined) {
update = true;
}
if (typeof x === 'object') {
this._x = x.x || 0;
this._y = x.y || 0;
this._z = x.z || 0;
this._w = x.w || 0;
}
else {
this._x = x || 0;
this._y = y || 0;
this._z = z || 0;
this._w = w || 0;
}
if (update) {
this.onChangeCallback(this);
}
return this;
}
/**
* Add a given Quaternion or Vector to this Quaternion. Addition is component-wise.
*
* @method Phaser.Math.Quaternion#add
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to add to this Quaternion.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
add(v) {
this._x += v.x;
this._y += v.y;
this._z += v.z;
this._w += v.w;
this.onChangeCallback(this);
return this;
}
/**
* Subtract a given Quaternion or Vector from this Quaternion. Subtraction is component-wise.
*
* @method Phaser.Math.Quaternion#subtract
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to subtract from this Quaternion.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
subtract(v) {
this._x -= v.x;
this._y -= v.y;
this._z -= v.z;
this._w -= v.w;
this.onChangeCallback(this);
return this;
}
/**
* Scale this Quaternion by the given value.
*
* @method Phaser.Math.Quaternion#scale
* @since 3.0.0
*
* @param {number} scale - The value to scale this Quaternion by.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
scale(scale) {
this._x *= scale;
this._y *= scale;
this._z *= scale;
this._w *= scale;
this.onChangeCallback(this);
return this;
}
/**
* Calculate the length of this Quaternion.
*
* @method Phaser.Math.Quaternion#length
* @since 3.0.0
*
* @return {number} The length of this Quaternion.
*/
length() {
const x = this.x;
const y = this.y;
const z = this.z;
const w = this.w;
return Math.sqrt(x * x + y * y + z * z + w * w);
}
/**
* Calculate the length of this Quaternion squared.
*
* @method Phaser.Math.Quaternion#lengthSq
* @since 3.0.0
*
* @return {number} The length of this Quaternion, squared.
*/
lengthSq() {
const x = this.x;
const y = this.y;
const z = this.z;
const w = this.w;
return x * x + y * y + z * z + w * w;
}
/**
* Normalize this Quaternion.
*
* @method Phaser.Math.Quaternion#normalize
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
normalize() {
const x = this.x;
const y = this.y;
const z = this.z;
const w = this.w;
let len = x * x + y * y + z * z + w * w;
if (len > 0) {
len = 1 / Math.sqrt(len);
this._x = x * len;
this._y = y * len;
this._z = z * len;
this._w = w * len;
}
this.onChangeCallback(this);
return this;
}
/**
* Calculate the dot product of this Quaternion and the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#dot
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to dot product with this Quaternion.
*
* @return {number} The dot product of this Quaternion and the given Quaternion or Vector.
*/
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
}
/**
* Linearly interpolate this Quaternion towards the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#lerp
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} v - The Quaternion or Vector to interpolate towards.
* @param {number} [t=0] - The percentage of interpolation.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
lerp(v, t) {
if (t === undefined) {
t = 0;
}
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
return this.set(ax + t * (v.x - ax), ay + t * (v.y - ay), az + t * (v.z - az), aw + t * (v.w - aw));
}
/**
* Rotates this Quaternion based on the two given vectors.
*
* @method Phaser.Math.Quaternion#rotationTo
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} a - The transform rotation vector.
* @param {Phaser.Math.Vector3} b - The target rotation vector.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotationTo(a, b) {
const dot = a.x * b.x + a.y * b.y + a.z * b.z;
if (dot < -0.999999) {
if (tmpvec.copy(xUnitVec3).cross(a).length() < EPSILON) {
tmpvec.copy(yUnitVec3).cross(a);
}
tmpvec.normalize();
return this.setAxisAngle(tmpvec, Math.PI);
}
else if (dot > 0.999999) {
return this.set(0, 0, 0, 1);
}
else {
tmpvec.copy(a).cross(b);
this._x = tmpvec.x;
this._y = tmpvec.y;
this._z = tmpvec.z;
this._w = 1 + dot;
return this.normalize();
}
}
/**
* Set the axes of this Quaternion.
*
* @method Phaser.Math.Quaternion#setAxes
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} view - The view axis.
* @param {Phaser.Math.Vector3} right - The right axis.
* @param {Phaser.Math.Vector3} up - The upwards axis.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setAxes(view, right, up) {
const m = tmpMat3.val;
m[0] = right.x;
m[3] = right.y;
m[6] = right.z;
m[1] = up.x;
m[4] = up.y;
m[7] = up.z;
m[2] = -view.x;
m[5] = -view.y;
m[8] = -view.z;
return this.fromMat3(tmpMat3).normalize();
}
/**
* Reset this Matrix to an identity (default) Quaternion.
*
* @method Phaser.Math.Quaternion#identity
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
identity() {
return this.set(0, 0, 0, 1);
}
/**
* Set the axis angle of this Quaternion.
*
* @method Phaser.Math.Quaternion#setAxisAngle
* @since 3.0.0
*
* @param {Phaser.Math.Vector3} axis - The axis.
* @param {number} rad - The angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setAxisAngle(axis, rad) {
rad = rad * 0.5;
const s = Math.sin(rad);
return this.set(s * axis.x, s * axis.y, s * axis.z, Math.cos(rad));
}
/**
* Multiply this Quaternion by the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#multiply
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} b - The Quaternion or Vector to multiply this Quaternion by.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
multiply(b) {
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
const bx = b.x;
const by = b.y;
const bz = b.z;
const bw = b.w;
return this.set(ax * bw + aw * bx + ay * bz - az * by, ay * bw + aw * by + az * bx - ax * bz, az * bw + aw * bz + ax * by - ay * bx, aw * bw - ax * bx - ay * by - az * bz);
}
/**
* Smoothly linearly interpolate this Quaternion towards the given Quaternion or Vector.
*
* @method Phaser.Math.Quaternion#slerp
* @since 3.0.0
*
* @param {(Phaser.Math.Quaternion|Phaser.Math.Vector4)} b - The Quaternion or Vector to interpolate towards.
* @param {number} t - The percentage of interpolation.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
slerp(b, t) {
// benchmarks: http://jsperf.com/quaternion-slerp-implementations
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
let bx = b.x;
let by = b.y;
let bz = b.z;
let bw = b.w;
// calc cosine
let cosom = ax * bx + ay * by + az * bz + aw * bw;
// adjust signs (if necessary)
if (cosom < 0) {
cosom = -cosom;
bx = -bx;
by = -by;
bz = -bz;
bw = -bw;
}
// "from" and "to" quaternions are very close
// ... so we can do a linear interpolation
let scale0 = 1 - t;
let scale1 = t;
// calculate coefficients
if (1 - cosom > EPSILON) {
// standard case (slerp)
const omega = Math.acos(cosom);
const sinom = Math.sin(omega);
scale0 = Math.sin((1.0 - t) * omega) / sinom;
scale1 = Math.sin(t * omega) / sinom;
}
// calculate final values
return this.set(scale0 * ax + scale1 * bx, scale0 * ay + scale1 * by, scale0 * az + scale1 * bz, scale0 * aw + scale1 * bw);
}
/**
* Invert this Quaternion.
*
* @method Phaser.Math.Quaternion#invert
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
invert() {
const a0 = this.x;
const a1 = this.y;
const a2 = this.z;
const a3 = this.w;
const dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
const invDot = dot ? 1 / dot : 0;
return this.set(-a0 * invDot, -a1 * invDot, -a2 * invDot, a3 * invDot);
}
/**
* Convert this Quaternion into its conjugate.
*
* Sets the x, y and z components.
*
* @method Phaser.Math.Quaternion#conjugate
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
conjugate() {
this._x = -this.x;
this._y = -this.y;
this._z = -this.z;
this.onChangeCallback(this);
return this;
}
/**
* Rotate this Quaternion on the X axis.
*
* @method Phaser.Math.Quaternion#rotateX
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateX(rad) {
rad *= 0.5;
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
const bx = Math.sin(rad);
const bw = Math.cos(rad);
return this.set(ax * bw + aw * bx, ay * bw + az * bx, az * bw - ay * bx, aw * bw - ax * bx);
}
/**
* Rotate this Quaternion on the Y axis.
*
* @method Phaser.Math.Quaternion#rotateY
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateY(rad) {
rad *= 0.5;
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
const by = Math.sin(rad);
const bw = Math.cos(rad);
return this.set(ax * bw - az * by, ay * bw + aw * by, az * bw + ax * by, aw * bw - ay * by);
}
/**
* Rotate this Quaternion on the Z axis.
*
* @method Phaser.Math.Quaternion#rotateZ
* @since 3.0.0
*
* @param {number} rad - The rotation angle in radians.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
rotateZ(rad) {
rad *= 0.5;
const ax = this.x;
const ay = this.y;
const az = this.z;
const aw = this.w;
const bz = Math.sin(rad);
const bw = Math.cos(rad);
return this.set(ax * bw + ay * bz, ay * bw - ax * bz, az * bw + aw * bz, aw * bw - az * bz);
}
/**
* Create a unit (or rotation) Quaternion from its x, y, and z components.
*
* Sets the w component.
*
* @method Phaser.Math.Quaternion#calculateW
* @since 3.0.0
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
calculateW() {
const x = this.x;
const y = this.y;
const z = this.z;
this.w = -Math.sqrt(1.0 - x * x - y * y - z * z);
return this;
}
/**
* Set this Quaternion from the given Euler, based on Euler order.
*
* @method Phaser.Math.Quaternion#setFromEuler
* @since 3.50.0
*
* @param {Phaser.Math.Euler} euler - The Euler to convert from.
* @param {boolean} [update=true] - Run the `onChangeCallback`?
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setFromEuler(euler, update) {
const x = euler.x / 2;
const y = euler.y / 2;
const z = euler.z / 2;
const c1 = Math.cos(x);
const c2 = Math.cos(y);
const c3 = Math.cos(z);
const s1 = Math.sin(x);
const s2 = Math.sin(y);
const s3 = Math.sin(z);
switch (euler.order) {
case 'XYZ': {
this.set(s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3, update);
break;
}
case 'YXZ': {
this.set(s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3, update);
break;
}
case 'ZXY': {
this.set(s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3, update);
break;
}
case 'ZYX': {
this.set(s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3, update);
break;
}
case 'YZX': {
this.set(s1 * c2 * c3 + c1 * s2 * s3, c1 * s2 * c3 + s1 * c2 * s3, c1 * c2 * s3 - s1 * s2 * c3, c1 * c2 * c3 - s1 * s2 * s3, update);
break;
}
case 'XZY': {
this.set(s1 * c2 * c3 - c1 * s2 * s3, c1 * s2 * c3 - s1 * c2 * s3, c1 * c2 * s3 + s1 * s2 * c3, c1 * c2 * c3 + s1 * s2 * s3, update);
break;
}
}
return this;
}
/**
* Sets the rotation of this Quaternion from the given Matrix4.
*
* @method Phaser.Math.Quaternion#setFromRotationMatrix
* @since 3.50.0
*
* @param {Phaser.Math.Matrix4} mat4 - The Matrix4 to set the rotation from.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
setFromRotationMatrix(mat4) {
const m = mat4.val;
const m11 = m[0];
const m12 = m[4];
const m13 = m[8];
const m21 = m[1];
const m22 = m[5];
const m23 = m[9];
const m31 = m[2];
const m32 = m[6];
const m33 = m[10];
const trace = m11 + m22 + m33;
let s;
if (trace > 0) {
s = 0.5 / Math.sqrt(trace + 1.0);
this.set((m32 - m23) * s, (m13 - m31) * s, (m21 - m12) * s, 0.25 / s);
}
else if (m11 > m22 && m11 > m33) {
s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
this.set(0.25 * s, (m12 + m21) / s, (m13 + m31) / s, (m32 - m23) / s);
}
else if (m22 > m33) {
s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
this.set((m12 + m21) / s, 0.25 * s, (m23 + m32) / s, (m13 - m31) / s);
}
else {
s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
this.set((m13 + m31) / s, (m23 + m32) / s, 0.25 * s, (m21 - m12) / s);
}
return this;
}
/**
* Convert the given Matrix into this Quaternion.
*
* @method Phaser.Math.Quaternion#fromMat3
* @since 3.0.0
*
* @param {Phaser.Math.Matrix3} mat - The Matrix to convert from.
*
* @return {Phaser.Math.Quaternion} This Quaternion.
*/
fromMat3(mat) {
// benchmarks:
// http://jsperf.com/typed-array-access-speed
// http://jsperf.com/conversion-of-3x3-matrix-to-quaternion
// Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
// article "Quaternion Calculus and Fast Animation".
const m = mat.val;
const fTrace = m[0] + m[4] + m[8];
let fRoot;
if (fTrace > 0) {
// |w| > 1/2, may as well choose w > 1/2
fRoot = Math.sqrt(fTrace + 1.0); // 2w
this.w = 0.5 * fRoot;
fRoot = 0.5 / fRoot; // 1/(4w)
this._x = (m[7] - m[5]) * fRoot;
this._y = (m[2] - m[6]) * fRoot;
this._z = (m[3] - m[1]) * fRoot;
}
else {
// |w| <= 1/2
let i = 0;
if (m[4] > m[0]) {
i = 1;
}
if (m[8] > m[i * 3 + i]) {
i = 2;
}
const j = siNext[i];
const k = siNext[j];
// This isn't quite as clean without array access
fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1);
tmp[i] = 0.5 * fRoot;
fRoot = 0.5 / fRoot;
tmp[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
tmp[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
this._x = tmp[0];
this._y = tmp[1];
this._z = tmp[2];
this._w = (m[k * 3 + j] - m[j * 3 + k]) * fRoot;
}
this.onChangeCallback(this);
return this;
}
}
exports.default = Quaternion;
//# sourceMappingURL=Quaternion.js.map