the-world-engine
Version:
three.js based, unity like game engine for browser
1,828 lines (1,816 loc) • 655 kB
JavaScript
(function(t, s) {
typeof exports === "object" && typeof module !== "undefined" ? s(exports) : typeof define === "function" && define.amd ? define("B2D", [ "exports" ], s) : (t = typeof globalThis !== "undefined" ? globalThis : t || self,
s(t.B2D = {}));
})(this, (function(t) {
"use strict";
class b2BlockAllocator {}
class b2Color {
constructor(t = .5, s = .5, i = .5, e = 1) {
this.r = t;
this.g = s;
this.b = i;
this.a = e;
}
Clone() {
return (new b2Color).Copy(this);
}
Copy(t) {
this.r = t.r;
this.g = t.g;
this.b = t.b;
this.a = t.a;
return this;
}
IsEqual(t) {
return this.r === t.r && this.g === t.g && this.b === t.b && this.a === t.a;
}
IsZero() {
return this.r === 0 && this.g === 0 && this.b === 0 && this.a === 0;
}
Set(t, s, i, e = this.a) {
this.SetRGBA(t, s, i, e);
}
SetByteRGB(t, s, i) {
this.r = t / 255;
this.g = s / 255;
this.b = i / 255;
return this;
}
SetByteRGBA(t, s, i, e) {
this.r = t / 255;
this.g = s / 255;
this.b = i / 255;
this.a = e / 255;
return this;
}
SetRGB(t, s, i) {
this.r = t;
this.g = s;
this.b = i;
return this;
}
SetRGBA(t, s, i, e) {
this.r = t;
this.g = s;
this.b = i;
this.a = e;
return this;
}
SelfAdd(t) {
this.r += t.r;
this.g += t.g;
this.b += t.b;
this.a += t.a;
return this;
}
Add(t, s) {
s.r = this.r + t.r;
s.g = this.g + t.g;
s.b = this.b + t.b;
s.a = this.a + t.a;
return s;
}
SelfSub(t) {
this.r -= t.r;
this.g -= t.g;
this.b -= t.b;
this.a -= t.a;
return this;
}
Sub(t, s) {
s.r = this.r - t.r;
s.g = this.g - t.g;
s.b = this.b - t.b;
s.a = this.a - t.a;
return s;
}
SelfMul(t) {
this.r *= t;
this.g *= t;
this.b *= t;
this.a *= t;
return this;
}
Mul(t, s) {
s.r = this.r * t;
s.g = this.g * t;
s.b = this.b * t;
s.a = this.a * t;
return s;
}
Mix(t, s) {
b2Color.MixColors(this, t, s);
}
static MixColors(t, s, i) {
const e = i * (s.r - t.r);
const n = i * (s.g - t.g);
const h = i * (s.b - t.b);
const o = i * (s.a - t.a);
t.r += e;
t.g += n;
t.b += h;
t.a += o;
s.r -= e;
s.g -= n;
s.b -= h;
s.a -= o;
}
MakeStyleString(t = this.a) {
return b2Color.MakeStyleString(this.r, this.g, this.b, t);
}
static MakeStyleString(t, s, i, e = 1) {
t *= 255;
s *= 255;
i *= 255;
if (e < 1) {
return `rgba(${t},${s},${i},${e})`;
} else {
return `rgb(${t},${s},${i})`;
}
}
}
b2Color.ZERO = new b2Color(0, 0, 0, 0);
b2Color.RED = new b2Color(1, 0, 0);
b2Color.GREEN = new b2Color(0, 1, 0);
b2Color.BLUE = new b2Color(0, 0, 1);
class b2TypedColor {
constructor(...t) {
if (t[0] instanceof Float32Array) {
if (t[0].length !== 4) {
throw new Error;
}
this.data = t[0];
} else {
const s = typeof t[0] === "number" ? t[0] : .5;
const i = typeof t[1] === "number" ? t[1] : .5;
const e = typeof t[2] === "number" ? t[2] : .5;
const n = typeof t[3] === "number" ? t[3] : 1;
this.data = new Float32Array([ s, i, e, n ]);
}
}
get r() {
return this.data[0];
}
set r(t) {
this.data[0] = t;
}
get g() {
return this.data[1];
}
set g(t) {
this.data[1] = t;
}
get b() {
return this.data[2];
}
set b(t) {
this.data[2] = t;
}
get a() {
return this.data[3];
}
set a(t) {
this.data[3] = t;
}
Clone() {
return new b2TypedColor(new Float32Array(this.data));
}
Copy(t) {
if (t instanceof b2TypedColor) {
this.data.set(t.data);
} else {
this.r = t.r;
this.g = t.g;
this.b = t.b;
this.a = t.a;
}
return this;
}
IsEqual(t) {
return this.r === t.r && this.g === t.g && this.b === t.b && this.a === t.a;
}
IsZero() {
return this.r === 0 && this.g === 0 && this.b === 0 && this.a === 0;
}
Set(t, s, i, e = this.a) {
this.SetRGBA(t, s, i, e);
}
SetByteRGB(t, s, i) {
this.r = t / 255;
this.g = s / 255;
this.b = i / 255;
return this;
}
SetByteRGBA(t, s, i, e) {
this.r = t / 255;
this.g = s / 255;
this.b = i / 255;
this.a = e / 255;
return this;
}
SetRGB(t, s, i) {
this.r = t;
this.g = s;
this.b = i;
return this;
}
SetRGBA(t, s, i, e) {
this.r = t;
this.g = s;
this.b = i;
this.a = e;
return this;
}
SelfAdd(t) {
this.r += t.r;
this.g += t.g;
this.b += t.b;
this.a += t.a;
return this;
}
Add(t, s) {
s.r = this.r + t.r;
s.g = this.g + t.g;
s.b = this.b + t.b;
s.a = this.a + t.a;
return s;
}
SelfSub(t) {
this.r -= t.r;
this.g -= t.g;
this.b -= t.b;
this.a -= t.a;
return this;
}
Sub(t, s) {
s.r = this.r - t.r;
s.g = this.g - t.g;
s.b = this.b - t.b;
s.a = this.a - t.a;
return s;
}
SelfMul(t) {
this.r *= t;
this.g *= t;
this.b *= t;
this.a *= t;
return this;
}
Mul(t, s) {
s.r = this.r * t;
s.g = this.g * t;
s.b = this.b * t;
s.a = this.a * t;
return s;
}
Mix(t, s) {
b2Color.MixColors(this, t, s);
}
MakeStyleString(t = this.a) {
return b2Color.MakeStyleString(this.r, this.g, this.b, t);
}
}
t.DrawFlags = void 0;
(function(t) {
t[t["e_none"] = 0] = "e_none";
t[t["e_shapeBit"] = 1] = "e_shapeBit";
t[t["e_jointBit"] = 2] = "e_jointBit";
t[t["e_aabbBit"] = 4] = "e_aabbBit";
t[t["e_pairBit"] = 8] = "e_pairBit";
t[t["e_centerOfMassBit"] = 16] = "e_centerOfMassBit";
t[t["e_particleBit"] = 32] = "e_particleBit";
t[t["e_controllerBit"] = 64] = "e_controllerBit";
t[t["e_all"] = 63] = "e_all";
})(t.DrawFlags || (t.DrawFlags = {}));
class b2Draw {
constructor() {
this.m_drawFlags = 0;
}
SetFlags(t) {
this.m_drawFlags = t;
}
GetFlags() {
return this.m_drawFlags;
}
AppendFlags(t) {
this.m_drawFlags |= t;
}
ClearFlags(t) {
this.m_drawFlags &= ~t;
}
}
function b2Assert(t, ...s) {
if (!t) {
throw new Error(...s);
}
}
function b2Maybe(t, s) {
return t !== undefined ? t : s;
}
const s = 1e37;
const i = 1e-5;
const e = i * i;
const n = 3.14159265359;
const h = 1;
const o = 8;
const c = 2;
const r = .1 * h;
const b = 4;
const l = .005 * h;
const a = 2 / 180 * n;
const u = 2 * l;
const f = 8;
const V = 32;
const w = .2 * h;
const S = 8 / 180 * n;
const d = 2 * h;
const y = d * d;
const m = .5 * n;
const C = m * m;
const P = .2;
const p = .75;
const M = -1;
const g = 2147483647;
const A = .75;
const G = 1;
const x = .25;
const J = .5;
const R = 2 * h;
const D = R * R;
const T = 256;
const v = 2.5;
const _ = .5;
const B = .01 * h;
const F = 2 / 180 * n;
class b2Version {
constructor(t = 0, s = 0, i = 0) {
this.major = 0;
this.minor = 0;
this.revision = 0;
this.major = t;
this.minor = s;
this.revision = i;
}
toString() {
return this.major + "." + this.minor + "." + this.revision;
}
}
const I = new b2Version(2, 4, 1);
const k = "master";
const E = "9ebbbcd960ad424e03e5de6e66a40764c16f51bc";
function b2ParseInt(t) {
return parseInt(t, 10);
}
function b2ParseUInt(t) {
return Math.abs(parseInt(t, 10));
}
function b2MakeArray(t, s) {
const i = new Array(t);
for (let e = 0; e < t; ++e) {
i[e] = s(e);
}
return i;
}
function b2MakeNullArray(t) {
const s = new Array(t);
for (let i = 0; i < t; ++i) {
s[i] = null;
}
return s;
}
function b2MakeNumberArray(t, s = 0) {
const i = new Array(t);
for (let e = 0; e < t; ++e) {
i[e] = s;
}
return i;
}
function b2Alloc(t) {
return null;
}
function b2Free(t) {}
function b2Log(t, ...s) {}
class b2GrowableStack {
constructor(t) {
this.m_stack = [];
this.m_count = 0;
this.m_stack = b2MakeArray(t, (t => null));
this.m_count = 0;
}
Reset() {
this.m_count = 0;
return this;
}
Push(t) {
this.m_stack[this.m_count] = t;
this.m_count++;
}
Pop() {
this.m_count--;
const t = this.m_stack[this.m_count];
this.m_stack[this.m_count] = null;
return t;
}
GetCount() {
return this.m_count;
}
}
const W = n / 180;
const L = 180 / n;
const j = 2 * n;
const O = Math.abs;
function b2Min(t, s) {
return t < s ? t : s;
}
function b2Max(t, s) {
return t > s ? t : s;
}
function b2Clamp(t, s, i) {
return t < s ? s : t > i ? i : t;
}
function b2Swap(t, s) {
const i = t[0];
t[0] = s[0];
s[0] = i;
}
const N = isFinite;
function b2Sq(t) {
return t * t;
}
function b2InvSqrt(t) {
return 1 / Math.sqrt(t);
}
const q = Math.sqrt;
const U = Math.pow;
function b2DegToRad(t) {
return t * W;
}
function b2RadToDeg(t) {
return t * L;
}
const Q = Math.cos;
const z = Math.sin;
const X = Math.acos;
const Z = Math.asin;
const H = Math.atan2;
function b2NextPowerOfTwo(t) {
t |= t >> 1 & 2147483647;
t |= t >> 2 & 1073741823;
t |= t >> 4 & 268435455;
t |= t >> 8 & 16777215;
t |= t >> 16 & 65535;
return t + 1;
}
function b2IsPowerOfTwo(t) {
return t > 0 && (t & t - 1) === 0;
}
function b2Random() {
return Math.random() * 2 - 1;
}
function b2RandomRange(t, s) {
return (s - t) * Math.random() + t;
}
class b2Vec2 {
constructor(t = 0, s = 0) {
this.x = t;
this.y = s;
}
Clone() {
return new b2Vec2(this.x, this.y);
}
SetZero() {
this.x = 0;
this.y = 0;
return this;
}
Set(t, s) {
this.x = t;
this.y = s;
return this;
}
Copy(t) {
this.x = t.x;
this.y = t.y;
return this;
}
SelfAdd(t) {
this.x += t.x;
this.y += t.y;
return this;
}
SelfAddXY(t, s) {
this.x += t;
this.y += s;
return this;
}
SelfSub(t) {
this.x -= t.x;
this.y -= t.y;
return this;
}
SelfSubXY(t, s) {
this.x -= t;
this.y -= s;
return this;
}
SelfMul(t) {
this.x *= t;
this.y *= t;
return this;
}
SelfMulAdd(t, s) {
this.x += t * s.x;
this.y += t * s.y;
return this;
}
SelfMulSub(t, s) {
this.x -= t * s.x;
this.y -= t * s.y;
return this;
}
Dot(t) {
return this.x * t.x + this.y * t.y;
}
Cross(t) {
return this.x * t.y - this.y * t.x;
}
Length() {
const t = this.x, s = this.y;
return Math.sqrt(t * t + s * s);
}
LengthSquared() {
const t = this.x, s = this.y;
return t * t + s * s;
}
Normalize() {
const t = this.Length();
if (t >= i) {
const s = 1 / t;
this.x *= s;
this.y *= s;
}
return t;
}
SelfNormalize() {
const t = this.Length();
if (t >= i) {
const s = 1 / t;
this.x *= s;
this.y *= s;
}
return this;
}
SelfRotate(t) {
const s = Math.cos(t);
const i = Math.sin(t);
const e = this.x;
this.x = s * e - i * this.y;
this.y = i * e + s * this.y;
return this;
}
SelfRotateCosSin(t, s) {
const i = this.x;
this.x = t * i - s * this.y;
this.y = s * i + t * this.y;
return this;
}
IsValid() {
return isFinite(this.x) && isFinite(this.y);
}
SelfCrossVS(t) {
const s = this.x;
this.x = t * this.y;
this.y = -t * s;
return this;
}
SelfCrossSV(t) {
const s = this.x;
this.x = -t * this.y;
this.y = t * s;
return this;
}
SelfMinV(t) {
this.x = b2Min(this.x, t.x);
this.y = b2Min(this.y, t.y);
return this;
}
SelfMaxV(t) {
this.x = b2Max(this.x, t.x);
this.y = b2Max(this.y, t.y);
return this;
}
SelfAbs() {
this.x = O(this.x);
this.y = O(this.y);
return this;
}
SelfNeg() {
this.x = -this.x;
this.y = -this.y;
return this;
}
SelfSkew() {
const t = this.x;
this.x = -this.y;
this.y = t;
return this;
}
static MakeArray(t) {
return b2MakeArray(t, (t => new b2Vec2));
}
static AbsV(t, s) {
s.x = O(t.x);
s.y = O(t.y);
return s;
}
static MinV(t, s, i) {
i.x = b2Min(t.x, s.x);
i.y = b2Min(t.y, s.y);
return i;
}
static MaxV(t, s, i) {
i.x = b2Max(t.x, s.x);
i.y = b2Max(t.y, s.y);
return i;
}
static ClampV(t, s, i, e) {
e.x = b2Clamp(t.x, s.x, i.x);
e.y = b2Clamp(t.y, s.y, i.y);
return e;
}
static RotateV(t, s, i) {
const e = t.x, n = t.y;
const h = Math.cos(s);
const o = Math.sin(s);
i.x = h * e - o * n;
i.y = o * e + h * n;
return i;
}
static DotVV(t, s) {
return t.x * s.x + t.y * s.y;
}
static CrossVV(t, s) {
return t.x * s.y - t.y * s.x;
}
static CrossVS(t, s, i) {
const e = t.x;
i.x = s * t.y;
i.y = -s * e;
return i;
}
static CrossVOne(t, s) {
const i = t.x;
s.x = t.y;
s.y = -i;
return s;
}
static CrossSV(t, s, i) {
const e = s.x;
i.x = -t * s.y;
i.y = t * e;
return i;
}
static CrossOneV(t, s) {
const i = t.x;
s.x = -t.y;
s.y = i;
return s;
}
static AddVV(t, s, i) {
i.x = t.x + s.x;
i.y = t.y + s.y;
return i;
}
static SubVV(t, s, i) {
i.x = t.x - s.x;
i.y = t.y - s.y;
return i;
}
static MulSV(t, s, i) {
i.x = s.x * t;
i.y = s.y * t;
return i;
}
static MulVS(t, s, i) {
i.x = t.x * s;
i.y = t.y * s;
return i;
}
static AddVMulSV(t, s, i, e) {
e.x = t.x + s * i.x;
e.y = t.y + s * i.y;
return e;
}
static SubVMulSV(t, s, i, e) {
e.x = t.x - s * i.x;
e.y = t.y - s * i.y;
return e;
}
static AddVCrossSV(t, s, i, e) {
const n = i.x;
e.x = t.x - s * i.y;
e.y = t.y + s * n;
return e;
}
static MidVV(t, s, i) {
i.x = (t.x + s.x) * .5;
i.y = (t.y + s.y) * .5;
return i;
}
static ExtVV(t, s, i) {
i.x = (s.x - t.x) * .5;
i.y = (s.y - t.y) * .5;
return i;
}
static IsEqualToV(t, s) {
return t.x === s.x && t.y === s.y;
}
static DistanceVV(t, s) {
const i = t.x - s.x;
const e = t.y - s.y;
return Math.sqrt(i * i + e * e);
}
static DistanceSquaredVV(t, s) {
const i = t.x - s.x;
const e = t.y - s.y;
return i * i + e * e;
}
static NegV(t, s) {
s.x = -t.x;
s.y = -t.y;
return s;
}
}
b2Vec2.ZERO = new b2Vec2(0, 0);
b2Vec2.UNITX = new b2Vec2(1, 0);
b2Vec2.UNITY = new b2Vec2(0, 1);
b2Vec2.s_t0 = new b2Vec2;
b2Vec2.s_t1 = new b2Vec2;
b2Vec2.s_t2 = new b2Vec2;
b2Vec2.s_t3 = new b2Vec2;
const Y = new b2Vec2(0, 0);
class b2TypedVec2 {
constructor(...t) {
if (t[0] instanceof Float32Array) {
if (t[0].length !== 2) {
throw new Error;
}
this.data = t[0];
} else {
const s = typeof t[0] === "number" ? t[0] : 0;
const i = typeof t[1] === "number" ? t[1] : 0;
this.data = new Float32Array([ s, i ]);
}
}
get x() {
return this.data[0];
}
set x(t) {
this.data[0] = t;
}
get y() {
return this.data[1];
}
set y(t) {
this.data[1] = t;
}
Clone() {
return new b2TypedVec2(new Float32Array(this.data));
}
SetZero() {
this.x = 0;
this.y = 0;
return this;
}
Set(t, s) {
this.x = t;
this.y = s;
return this;
}
Copy(t) {
if (t instanceof b2TypedVec2) {
this.data.set(t.data);
} else {
this.x = t.x;
this.y = t.y;
}
return this;
}
SelfAdd(t) {
this.x += t.x;
this.y += t.y;
return this;
}
SelfAddXY(t, s) {
this.x += t;
this.y += s;
return this;
}
SelfSub(t) {
this.x -= t.x;
this.y -= t.y;
return this;
}
SelfSubXY(t, s) {
this.x -= t;
this.y -= s;
return this;
}
SelfMul(t) {
this.x *= t;
this.y *= t;
return this;
}
SelfMulAdd(t, s) {
this.x += t * s.x;
this.y += t * s.y;
return this;
}
SelfMulSub(t, s) {
this.x -= t * s.x;
this.y -= t * s.y;
return this;
}
Dot(t) {
return this.x * t.x + this.y * t.y;
}
Cross(t) {
return this.x * t.y - this.y * t.x;
}
Length() {
const t = this.x, s = this.y;
return Math.sqrt(t * t + s * s);
}
LengthSquared() {
const t = this.x, s = this.y;
return t * t + s * s;
}
Normalize() {
const t = this.Length();
if (t >= i) {
const s = 1 / t;
this.x *= s;
this.y *= s;
}
return t;
}
SelfNormalize() {
const t = this.Length();
if (t >= i) {
const s = 1 / t;
this.x *= s;
this.y *= s;
}
return this;
}
SelfRotate(t) {
const s = Math.cos(t);
const i = Math.sin(t);
const e = this.x;
this.x = s * e - i * this.y;
this.y = i * e + s * this.y;
return this;
}
SelfRotateCosSin(t, s) {
const i = this.x;
this.x = t * i - s * this.y;
this.y = s * i + t * this.y;
return this;
}
IsValid() {
return isFinite(this.x) && isFinite(this.y);
}
SelfCrossVS(t) {
const s = this.x;
this.x = t * this.y;
this.y = -t * s;
return this;
}
SelfCrossSV(t) {
const s = this.x;
this.x = -t * this.y;
this.y = t * s;
return this;
}
SelfMinV(t) {
this.x = b2Min(this.x, t.x);
this.y = b2Min(this.y, t.y);
return this;
}
SelfMaxV(t) {
this.x = b2Max(this.x, t.x);
this.y = b2Max(this.y, t.y);
return this;
}
SelfAbs() {
this.x = O(this.x);
this.y = O(this.y);
return this;
}
SelfNeg() {
this.x = -this.x;
this.y = -this.y;
return this;
}
SelfSkew() {
const t = this.x;
this.x = -this.y;
this.y = t;
return this;
}
}
class b2Vec3 {
constructor(...t) {
if (t[0] instanceof Float32Array) {
if (t[0].length !== 3) {
throw new Error;
}
this.data = t[0];
} else {
const s = typeof t[0] === "number" ? t[0] : 0;
const i = typeof t[1] === "number" ? t[1] : 0;
const e = typeof t[2] === "number" ? t[2] : 0;
this.data = new Float32Array([ s, i, e ]);
}
}
get x() {
return this.data[0];
}
set x(t) {
this.data[0] = t;
}
get y() {
return this.data[1];
}
set y(t) {
this.data[1] = t;
}
get z() {
return this.data[2];
}
set z(t) {
this.data[2] = t;
}
Clone() {
return new b2Vec3(this.x, this.y, this.z);
}
SetZero() {
this.x = 0;
this.y = 0;
this.z = 0;
return this;
}
SetXYZ(t, s, i) {
this.x = t;
this.y = s;
this.z = i;
return this;
}
Copy(t) {
this.x = t.x;
this.y = t.y;
this.z = t.z;
return this;
}
SelfNeg() {
this.x = -this.x;
this.y = -this.y;
this.z = -this.z;
return this;
}
SelfAdd(t) {
this.x += t.x;
this.y += t.y;
this.z += t.z;
return this;
}
SelfAddXYZ(t, s, i) {
this.x += t;
this.y += s;
this.z += i;
return this;
}
SelfSub(t) {
this.x -= t.x;
this.y -= t.y;
this.z -= t.z;
return this;
}
SelfSubXYZ(t, s, i) {
this.x -= t;
this.y -= s;
this.z -= i;
return this;
}
SelfMul(t) {
this.x *= t;
this.y *= t;
this.z *= t;
return this;
}
static DotV3V3(t, s) {
return t.x * s.x + t.y * s.y + t.z * s.z;
}
static CrossV3V3(t, s, i) {
const e = t.x, n = t.y, h = t.z;
const o = s.x, c = s.y, r = s.z;
i.x = n * r - h * c;
i.y = h * o - e * r;
i.z = e * c - n * o;
return i;
}
}
b2Vec3.ZERO = new b2Vec3(0, 0, 0);
b2Vec3.s_t0 = new b2Vec3;
class b2Mat22 {
constructor() {
this.ex = new b2Vec2(1, 0);
this.ey = new b2Vec2(0, 1);
}
Clone() {
return (new b2Mat22).Copy(this);
}
static FromVV(t, s) {
return (new b2Mat22).SetVV(t, s);
}
static FromSSSS(t, s, i, e) {
return (new b2Mat22).SetSSSS(t, s, i, e);
}
static FromAngle(t) {
return (new b2Mat22).SetAngle(t);
}
SetSSSS(t, s, i, e) {
this.ex.Set(t, i);
this.ey.Set(s, e);
return this;
}
SetVV(t, s) {
this.ex.Copy(t);
this.ey.Copy(s);
return this;
}
SetAngle(t) {
const s = Math.cos(t);
const i = Math.sin(t);
this.ex.Set(s, i);
this.ey.Set(-i, s);
return this;
}
Copy(t) {
this.ex.Copy(t.ex);
this.ey.Copy(t.ey);
return this;
}
SetIdentity() {
this.ex.Set(1, 0);
this.ey.Set(0, 1);
return this;
}
SetZero() {
this.ex.SetZero();
this.ey.SetZero();
return this;
}
GetAngle() {
return Math.atan2(this.ex.y, this.ex.x);
}
GetInverse(t) {
const s = this.ex.x;
const i = this.ey.x;
const e = this.ex.y;
const n = this.ey.y;
let h = s * n - i * e;
if (h !== 0) {
h = 1 / h;
}
t.ex.x = h * n;
t.ey.x = -h * i;
t.ex.y = -h * e;
t.ey.y = h * s;
return t;
}
Solve(t, s, i) {
const e = this.ex.x, n = this.ey.x;
const h = this.ex.y, o = this.ey.y;
let c = e * o - n * h;
if (c !== 0) {
c = 1 / c;
}
i.x = c * (o * t - n * s);
i.y = c * (e * s - h * t);
return i;
}
SelfAbs() {
this.ex.SelfAbs();
this.ey.SelfAbs();
return this;
}
SelfInv() {
this.GetInverse(this);
return this;
}
SelfAddM(t) {
this.ex.SelfAdd(t.ex);
this.ey.SelfAdd(t.ey);
return this;
}
SelfSubM(t) {
this.ex.SelfSub(t.ex);
this.ey.SelfSub(t.ey);
return this;
}
static AbsM(t, s) {
const i = t.ex, e = t.ey;
s.ex.x = O(i.x);
s.ex.y = O(i.y);
s.ey.x = O(e.x);
s.ey.y = O(e.y);
return s;
}
static MulMV(t, s, i) {
const e = t.ex, n = t.ey;
const h = s.x, o = s.y;
i.x = e.x * h + n.x * o;
i.y = e.y * h + n.y * o;
return i;
}
static MulTMV(t, s, i) {
const e = t.ex, n = t.ey;
const h = s.x, o = s.y;
i.x = e.x * h + e.y * o;
i.y = n.x * h + n.y * o;
return i;
}
static AddMM(t, s, i) {
const e = t.ex, n = t.ey;
const h = s.ex, o = s.ey;
i.ex.x = e.x + h.x;
i.ex.y = e.y + h.y;
i.ey.x = n.x + o.x;
i.ey.y = n.y + o.y;
return i;
}
static MulMM(t, s, i) {
const e = t.ex.x, n = t.ex.y;
const h = t.ey.x, o = t.ey.y;
const c = s.ex.x, r = s.ex.y;
const b = s.ey.x, l = s.ey.y;
i.ex.x = e * c + h * r;
i.ex.y = n * c + o * r;
i.ey.x = e * b + h * l;
i.ey.y = n * b + o * l;
return i;
}
static MulTMM(t, s, i) {
const e = t.ex.x, n = t.ex.y;
const h = t.ey.x, o = t.ey.y;
const c = s.ex.x, r = s.ex.y;
const b = s.ey.x, l = s.ey.y;
i.ex.x = e * c + n * r;
i.ex.y = h * c + o * r;
i.ey.x = e * b + n * l;
i.ey.y = h * b + o * l;
return i;
}
}
b2Mat22.IDENTITY = new b2Mat22;
class b2Mat33 {
constructor() {
this.data = new Float32Array([ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]);
this.ex = new b2Vec3(this.data.subarray(0, 3));
this.ey = new b2Vec3(this.data.subarray(3, 6));
this.ez = new b2Vec3(this.data.subarray(6, 9));
}
Clone() {
return (new b2Mat33).Copy(this);
}
SetVVV(t, s, i) {
this.ex.Copy(t);
this.ey.Copy(s);
this.ez.Copy(i);
return this;
}
Copy(t) {
this.ex.Copy(t.ex);
this.ey.Copy(t.ey);
this.ez.Copy(t.ez);
return this;
}
SetIdentity() {
this.ex.SetXYZ(1, 0, 0);
this.ey.SetXYZ(0, 1, 0);
this.ez.SetXYZ(0, 0, 1);
return this;
}
SetZero() {
this.ex.SetZero();
this.ey.SetZero();
this.ez.SetZero();
return this;
}
SelfAddM(t) {
this.ex.SelfAdd(t.ex);
this.ey.SelfAdd(t.ey);
this.ez.SelfAdd(t.ez);
return this;
}
Solve33(t, s, i, e) {
const n = this.ex.x, h = this.ex.y, o = this.ex.z;
const c = this.ey.x, r = this.ey.y, b = this.ey.z;
const l = this.ez.x, a = this.ez.y, u = this.ez.z;
let f = n * (r * u - b * a) + h * (b * l - c * u) + o * (c * a - r * l);
if (f !== 0) {
f = 1 / f;
}
e.x = f * (t * (r * u - b * a) + s * (b * l - c * u) + i * (c * a - r * l));
e.y = f * (n * (s * u - i * a) + h * (i * l - t * u) + o * (t * a - s * l));
e.z = f * (n * (r * i - b * s) + h * (b * t - c * i) + o * (c * s - r * t));
return e;
}
Solve22(t, s, i) {
const e = this.ex.x, n = this.ey.x;
const h = this.ex.y, o = this.ey.y;
let c = e * o - n * h;
if (c !== 0) {
c = 1 / c;
}
i.x = c * (o * t - n * s);
i.y = c * (e * s - h * t);
return i;
}
GetInverse22(t) {
const s = this.ex.x, i = this.ey.x, e = this.ex.y, n = this.ey.y;
let h = s * n - i * e;
if (h !== 0) {
h = 1 / h;
}
t.ex.x = h * n;
t.ey.x = -h * i;
t.ex.z = 0;
t.ex.y = -h * e;
t.ey.y = h * s;
t.ey.z = 0;
t.ez.x = 0;
t.ez.y = 0;
t.ez.z = 0;
}
GetSymInverse33(t) {
let s = b2Vec3.DotV3V3(this.ex, b2Vec3.CrossV3V3(this.ey, this.ez, b2Vec3.s_t0));
if (s !== 0) {
s = 1 / s;
}
const i = this.ex.x, e = this.ey.x, n = this.ez.x;
const h = this.ey.y, o = this.ez.y;
const c = this.ez.z;
t.ex.x = s * (h * c - o * o);
t.ex.y = s * (n * o - e * c);
t.ex.z = s * (e * o - n * h);
t.ey.x = t.ex.y;
t.ey.y = s * (i * c - n * n);
t.ey.z = s * (n * e - i * o);
t.ez.x = t.ex.z;
t.ez.y = t.ey.z;
t.ez.z = s * (i * h - e * e);
}
static MulM33V3(t, s, i) {
const e = s.x, n = s.y, h = s.z;
i.x = t.ex.x * e + t.ey.x * n + t.ez.x * h;
i.y = t.ex.y * e + t.ey.y * n + t.ez.y * h;
i.z = t.ex.z * e + t.ey.z * n + t.ez.z * h;
return i;
}
static MulM33XYZ(t, s, i, e, n) {
n.x = t.ex.x * s + t.ey.x * i + t.ez.x * e;
n.y = t.ex.y * s + t.ey.y * i + t.ez.y * e;
n.z = t.ex.z * s + t.ey.z * i + t.ez.z * e;
return n;
}
static MulM33V2(t, s, i) {
const e = s.x, n = s.y;
i.x = t.ex.x * e + t.ey.x * n;
i.y = t.ex.y * e + t.ey.y * n;
return i;
}
static MulM33XY(t, s, i, e) {
e.x = t.ex.x * s + t.ey.x * i;
e.y = t.ex.y * s + t.ey.y * i;
return e;
}
}
b2Mat33.IDENTITY = new b2Mat33;
class b2Rot {
constructor(t = 0) {
this.s = 0;
this.c = 1;
if (t) {
this.s = Math.sin(t);
this.c = Math.cos(t);
}
}
Clone() {
return (new b2Rot).Copy(this);
}
Copy(t) {
this.s = t.s;
this.c = t.c;
return this;
}
SetAngle(t) {
this.s = Math.sin(t);
this.c = Math.cos(t);
return this;
}
SetIdentity() {
this.s = 0;
this.c = 1;
return this;
}
GetAngle() {
return Math.atan2(this.s, this.c);
}
GetXAxis(t) {
t.x = this.c;
t.y = this.s;
return t;
}
GetYAxis(t) {
t.x = -this.s;
t.y = this.c;
return t;
}
static MulRR(t, s, i) {
const e = t.c, n = t.s;
const h = s.c, o = s.s;
i.s = n * h + e * o;
i.c = e * h - n * o;
return i;
}
static MulTRR(t, s, i) {
const e = t.c, n = t.s;
const h = s.c, o = s.s;
i.s = e * o - n * h;
i.c = e * h + n * o;
return i;
}
static MulRV(t, s, i) {
const e = t.c, n = t.s;
const h = s.x, o = s.y;
i.x = e * h - n * o;
i.y = n * h + e * o;
return i;
}
static MulTRV(t, s, i) {
const e = t.c, n = t.s;
const h = s.x, o = s.y;
i.x = e * h + n * o;
i.y = -n * h + e * o;
return i;
}
}
b2Rot.IDENTITY = new b2Rot;
class b2Transform {
constructor() {
this.p = new b2Vec2;
this.q = new b2Rot;
}
Clone() {
return (new b2Transform).Copy(this);
}
Copy(t) {
this.p.Copy(t.p);
this.q.Copy(t.q);
return this;
}
SetIdentity() {
this.p.SetZero();
this.q.SetIdentity();
return this;
}
SetPositionRotation(t, s) {
this.p.Copy(t);
this.q.Copy(s);
return this;
}
SetPositionAngle(t, s) {
this.p.Copy(t);
this.q.SetAngle(s);
return this;
}
SetPosition(t) {
this.p.Copy(t);
return this;
}
SetPositionXY(t, s) {
this.p.Set(t, s);
return this;
}
SetRotation(t) {
this.q.Copy(t);
return this;
}
SetRotationAngle(t) {
this.q.SetAngle(t);
return this;
}
GetPosition() {
return this.p;
}
GetRotation() {
return this.q;
}
GetRotationAngle() {
return this.q.GetAngle();
}
GetAngle() {
return this.q.GetAngle();
}
static MulXV(t, s, i) {
const e = t.q.c, n = t.q.s;
const h = s.x, o = s.y;
i.x = e * h - n * o + t.p.x;
i.y = n * h + e * o + t.p.y;
return i;
}
static MulTXV(t, s, i) {
const e = t.q.c, n = t.q.s;
const h = s.x - t.p.x;
const o = s.y - t.p.y;
i.x = e * h + n * o;
i.y = -n * h + e * o;
return i;
}
static MulXX(t, s, i) {
b2Rot.MulRR(t.q, s.q, i.q);
b2Vec2.AddVV(b2Rot.MulRV(t.q, s.p, i.p), t.p, i.p);
return i;
}
static MulTXX(t, s, i) {
b2Rot.MulTRR(t.q, s.q, i.q);
b2Rot.MulTRV(t.q, b2Vec2.SubVV(s.p, t.p, i.p), i.p);
return i;
}
}
b2Transform.IDENTITY = new b2Transform;
class b2Sweep {
constructor() {
this.localCenter = new b2Vec2;
this.c0 = new b2Vec2;
this.c = new b2Vec2;
this.a0 = 0;
this.a = 0;
this.alpha0 = 0;
}
Clone() {
return (new b2Sweep).Copy(this);
}
Copy(t) {
this.localCenter.Copy(t.localCenter);
this.c0.Copy(t.c0);
this.c.Copy(t.c);
this.a0 = t.a0;
this.a = t.a;
this.alpha0 = t.alpha0;
return this;
}
GetTransform(t, s) {
t.p.x = (1 - s) * this.c0.x + s * this.c.x;
t.p.y = (1 - s) * this.c0.y + s * this.c.y;
const i = (1 - s) * this.a0 + s * this.a;
t.q.SetAngle(i);
t.p.SelfSub(b2Rot.MulRV(t.q, this.localCenter, b2Vec2.s_t0));
return t;
}
Advance(t) {
const s = (t - this.alpha0) / (1 - this.alpha0);
const i = 1 - s;
this.c0.x = i * this.c0.x + s * this.c.x;
this.c0.y = i * this.c0.y + s * this.c.y;
this.a0 = i * this.a0 + s * this.a;
this.alpha0 = t;
}
Normalize() {
const t = j * Math.floor(this.a0 / j);
this.a0 -= t;
this.a -= t;
}
}
class b2StackAllocator {}
class b2Timer {
constructor() {
this.m_start = Date.now();
}
Reset() {
this.m_start = Date.now();
return this;
}
GetMilliseconds() {
return Date.now() - this.m_start;
}
}
class b2Counter {
constructor() {
this.m_count = 0;
this.m_min_count = 0;
this.m_max_count = 0;
}
GetCount() {
return this.m_count;
}
GetMinCount() {
return this.m_min_count;
}
GetMaxCount() {
return this.m_max_count;
}
ResetCount() {
const t = this.m_count;
this.m_count = 0;
return t;
}
ResetMinCount() {
this.m_min_count = 0;
}
ResetMaxCount() {
this.m_max_count = 0;
}
Increment() {
this.m_count++;
if (this.m_max_count < this.m_count) {
this.m_max_count = this.m_count;
}
}
Decrement() {
this.m_count--;
if (this.m_min_count > this.m_count) {
this.m_min_count = this.m_count;
}
}
}
class b2DistanceProxy {
constructor() {
this.m_buffer = b2Vec2.MakeArray(2);
this.m_vertices = this.m_buffer;
this.m_count = 0;
this.m_radius = 0;
}
Copy(t) {
if (t.m_vertices === t.m_buffer) {
this.m_vertices = this.m_buffer;
this.m_buffer[0].Copy(t.m_buffer[0]);
this.m_buffer[1].Copy(t.m_buffer[1]);
} else {
this.m_vertices = t.m_vertices;
}
this.m_count = t.m_count;
this.m_radius = t.m_radius;
return this;
}
Reset() {
this.m_vertices = this.m_buffer;
this.m_count = 0;
this.m_radius = 0;
return this;
}
SetShape(t, s) {
t.SetupDistanceProxy(this, s);
}
SetVerticesRadius(t, s, i) {
this.m_vertices = t;
this.m_count = s;
this.m_radius = i;
}
GetSupport(t) {
let s = 0;
let i = b2Vec2.DotVV(this.m_vertices[0], t);
for (let e = 1; e < this.m_count; ++e) {
const n = b2Vec2.DotVV(this.m_vertices[e], t);
if (n > i) {
s = e;
i = n;
}
}
return s;
}
GetSupportVertex(t) {
let s = 0;
let i = b2Vec2.DotVV(this.m_vertices[0], t);
for (let e = 1; e < this.m_count; ++e) {
const n = b2Vec2.DotVV(this.m_vertices[e], t);
if (n > i) {
s = e;
i = n;
}
}
return this.m_vertices[s];
}
GetVertexCount() {
return this.m_count;
}
GetVertex(t) {
return this.m_vertices[t];
}
}
class b2SimplexCache {
constructor() {
this.metric = 0;
this.count = 0;
this.indexA = [ 0, 0, 0 ];
this.indexB = [ 0, 0, 0 ];
}
Reset() {
this.metric = 0;
this.count = 0;
return this;
}
}
class b2DistanceInput {
constructor() {
this.proxyA = new b2DistanceProxy;
this.proxyB = new b2DistanceProxy;
this.transformA = new b2Transform;
this.transformB = new b2Transform;
this.useRadii = false;
}
Reset() {
this.proxyA.Reset();
this.proxyB.Reset();
this.transformA.SetIdentity();
this.transformB.SetIdentity();
this.useRadii = false;
return this;
}
}
class b2DistanceOutput {
constructor() {
this.pointA = new b2Vec2;
this.pointB = new b2Vec2;
this.distance = 0;
this.iterations = 0;
}
Reset() {
this.pointA.SetZero();
this.pointB.SetZero();
this.distance = 0;
this.iterations = 0;
return this;
}
}
class b2ShapeCastInput {
constructor() {
this.proxyA = new b2DistanceProxy;
this.proxyB = new b2DistanceProxy;
this.transformA = new b2Transform;
this.transformB = new b2Transform;
this.translationB = new b2Vec2;
}
}
class b2ShapeCastOutput {
constructor() {
this.point = new b2Vec2;
this.normal = new b2Vec2;
this.lambda = 0;
this.iterations = 0;
}
}
t.gjkCalls = 0;
t.gjkIters = 0;
t.gjkMaxIters = 0;
function b2_gjk_reset() {
t.gjkCalls = 0;
t.gjkIters = 0;
t.gjkMaxIters = 0;
}
class b2SimplexVertex {
constructor() {
this.wA = new b2Vec2;
this.wB = new b2Vec2;
this.w = new b2Vec2;
this.a = 0;
this.indexA = 0;
this.indexB = 0;
}
Copy(t) {
this.wA.Copy(t.wA);
this.wB.Copy(t.wB);
this.w.Copy(t.w);
this.a = t.a;
this.indexA = t.indexA;
this.indexB = t.indexB;
return this;
}
}
class b2Simplex {
constructor() {
this.m_v1 = new b2SimplexVertex;
this.m_v2 = new b2SimplexVertex;
this.m_v3 = new b2SimplexVertex;
this.m_vertices = [];
this.m_count = 0;
this.m_vertices[0] = this.m_v1;
this.m_vertices[1] = this.m_v2;
this.m_vertices[2] = this.m_v3;
}
ReadCache(t, s, e, n, h) {
this.m_count = t.count;
const o = this.m_vertices;
for (let i = 0; i < this.m_count; ++i) {
const c = o[i];
c.indexA = t.indexA[i];
c.indexB = t.indexB[i];
const r = s.GetVertex(c.indexA);
const b = n.GetVertex(c.indexB);
b2Transform.MulXV(e, r, c.wA);
b2Transform.MulXV(h, b, c.wB);
b2Vec2.SubVV(c.wB, c.wA, c.w);
c.a = 0;
}
if (this.m_count > 1) {
const s = t.metric;
const e = this.GetMetric();
if (e < .5 * s || 2 * s < e || e < i) {
this.m_count = 0;
}
}
if (this.m_count === 0) {
const t = o[0];
t.indexA = 0;
t.indexB = 0;
const i = s.GetVertex(0);
const c = n.GetVertex(0);
b2Transform.MulXV(e, i, t.wA);
b2Transform.MulXV(h, c, t.wB);
b2Vec2.SubVV(t.wB, t.wA, t.w);
t.a = 1;
this.m_count = 1;
}
}
WriteCache(t) {
t.metric = this.GetMetric();
t.count = this.m_count;
const s = this.m_vertices;
for (let i = 0; i < this.m_count; ++i) {
t.indexA[i] = s[i].indexA;
t.indexB[i] = s[i].indexB;
}
}
GetSearchDirection(t) {
switch (this.m_count) {
case 1:
return b2Vec2.NegV(this.m_v1.w, t);
case 2:
{
const s = b2Vec2.SubVV(this.m_v2.w, this.m_v1.w, t);
const i = b2Vec2.CrossVV(s, b2Vec2.NegV(this.m_v1.w, b2Vec2.s_t0));
if (i > 0) {
return b2Vec2.CrossOneV(s, t);
} else {
return b2Vec2.CrossVOne(s, t);
}
}
default:
return t.SetZero();
}
}
GetClosestPoint(t) {
switch (this.m_count) {
case 0:
return t.SetZero();
case 1:
return t.Copy(this.m_v1.w);
case 2:
return t.Set(this.m_v1.a * this.m_v1.w.x + this.m_v2.a * this.m_v2.w.x, this.m_v1.a * this.m_v1.w.y + this.m_v2.a * this.m_v2.w.y);
case 3:
return t.SetZero();
default:
return t.SetZero();
}
}
GetWitnessPoints(t, s) {
switch (this.m_count) {
case 0:
break;
case 1:
t.Copy(this.m_v1.wA);
s.Copy(this.m_v1.wB);
break;
case 2:
t.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x;
t.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y;
s.x = this.m_v1.a * this.m_v1.wB.x + this.m_v2.a * this.m_v2.wB.x;
s.y = this.m_v1.a * this.m_v1.wB.y + this.m_v2.a * this.m_v2.wB.y;
break;
case 3:
s.x = t.x = this.m_v1.a * this.m_v1.wA.x + this.m_v2.a * this.m_v2.wA.x + this.m_v3.a * this.m_v3.wA.x;
s.y = t.y = this.m_v1.a * this.m_v1.wA.y + this.m_v2.a * this.m_v2.wA.y + this.m_v3.a * this.m_v3.wA.y;
break;
}
}
GetMetric() {
switch (this.m_count) {
case 0:
return 0;
case 1:
return 0;
case 2:
return b2Vec2.DistanceVV(this.m_v1.w, this.m_v2.w);
cas