UNPKG

the-world-engine

Version:

three.js based, unity like game engine for browser

79 lines (76 loc) 2.18 kB
import { Vector2 } from "three/src/Three"; import { Vec2, WorldManifold } from "../../../box2d.ts/build/index"; import { ContactPoint2D } from "./ContactPoint2D"; export class Collision2D { di=null; fi=new WorldManifold; pi=null; Ci=null; vi=null; bi=null; Di=0; Vi=new Vector2; setData(t) { this.di = t; const i = t.GetFixtureA(); const e = t.GetFixtureB(); const o = i.GetUserData(); const r = e.GetUserData(); const n = i.GetBody(); const s = e.GetBody(); this.pi = o; this.Ci = n.GetUserData().rigidbody; this.vi = r; this.bi = s.GetUserData().rigidbody; this.Di = t.GetManifold().pointCount; const l = Collision2D._tempVec.Copy(n.GetLinearVelocity()); const h = l.SelfSub(s.GetLinearVelocity()); this.Vi.set(h.x, h.y); } static _tempVec=new Vec2; get collider() { return this.pi; } get rigidbody() { return this.Ci; } get otherCollider() { return this.vi; } get otherRigidbody() { return this.bi; } get contactCount() { return this.Di; } getContacts(t) { if (!this.di) return 0; let i = 0; const e = this.di.GetManifold(); for (let o = 0; o < e.pointCount; ++o) { if (!t[i]) t[i] = new ContactPoint2D; this.di.GetWorldManifold(this.fi); t[i].setData(this.di, e.points[o], this.fi.normal, this.fi.points[o], this.fi.separations[o], this.Vi); i += 1; } return i; } getContact(t, i) { if (!this.di) return null; const e = this.di.GetManifold(); if (t < 0 || e.pointCount <= t) return null; if (!i) i = new ContactPoint2D; this.di.GetWorldManifold(this.fi); i.setData(this.di, e.points[t], this.fi.normal, this.fi.points[t], this.fi.separations[t], this.Vi); return i; } get enabled() { return this.di?.IsEnabled() ?? false; } set enabled(t) { this.di?.SetEnabled(t); } get relativeVelocity() { return this.Vi; } }