UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

125 lines (123 loc) 3.32 kB
import { b2DynamicTree } from "./b2_dynamic_tree.js"; export class b2Pair { constructor(t, s) { this.proxyA = t; this.proxyB = s; } } export class b2BroadPhase { constructor() { this.m_tree = new b2DynamicTree; this.m_proxyCount = 0; this.m_moveCount = 0; this.m_moveBuffer = []; this.m_pairCount = 0; this.m_pairBuffer = []; } CreateProxy(t, s) { const i = this.m_tree.CreateProxy(t, s); ++this.m_proxyCount; this.BufferMove(i); return i; } DestroyProxy(t) { this.UnBufferMove(t); --this.m_proxyCount; this.m_tree.DestroyProxy(t); } MoveProxy(t, s, i) { const e = this.m_tree.MoveProxy(t, s, i); if (e) { this.BufferMove(t); } } TouchProxy(t) { this.BufferMove(t); } GetProxyCount() { return this.m_proxyCount; } UpdatePairs(t) { this.m_pairCount = 0; for (let t = 0; t < this.m_moveCount; ++t) { const s = this.m_moveBuffer[t]; if (s === null) { continue; } const i = s.aabb; this.m_tree.Query(i, (t => { if (t.m_id === s.m_id) { return true; } const i = t.moved; if (i && t.m_id > s.m_id) { return true; } let e; let r; if (t.m_id < s.m_id) { e = t; r = s; } else { e = s; r = t; } if (this.m_pairCount === this.m_pairBuffer.length) { this.m_pairBuffer[this.m_pairCount] = new b2Pair(e, r); } else { const t = this.m_pairBuffer[this.m_pairCount]; t.proxyA = e; t.proxyB = r; } ++this.m_pairCount; return true; })); } for (let s = 0; s < this.m_pairCount; ++s) { const i = this.m_pairBuffer[s]; const e = i.proxyA.userData; const r = i.proxyB.userData; t(e, r); } for (let t = 0; t < this.m_moveCount; ++t) { const s = this.m_moveBuffer[t]; if (s === null) { continue; } s.moved = false; } this.m_moveCount = 0; } Query(t, s) { this.m_tree.Query(t, s); } QueryPoint(t, s) { this.m_tree.QueryPoint(t, s); } RayCast(t, s) { this.m_tree.RayCast(t, s); } GetTreeHeight() { return this.m_tree.GetHeight(); } GetTreeBalance() { return this.m_tree.GetMaxBalance(); } GetTreeQuality() { return this.m_tree.GetAreaRatio(); } ShiftOrigin(t) { this.m_tree.ShiftOrigin(t); } BufferMove(t) { this.m_moveBuffer[this.m_moveCount] = t; ++this.m_moveCount; } UnBufferMove(t) { for (let s = 0; s < this.m_moveCount; ++s) { if (this.m_moveBuffer[s] === t) { this.m_moveBuffer[s] = null; } } } }