the-world-engine
Version:
three.js based, unity like game engine for browser
160 lines (145 loc) • 5.17 kB
JavaScript
import { b2_polygonRadius } from "../common/b2_settings.js";
import { b2Vec2, b2Rot, b2Transform } from "../common/b2_math.js";
import { b2Shape, b2ShapeType } from "./b2_shape.js";
export class b2EdgeShape extends b2Shape {
constructor() {
super(b2ShapeType.e_edgeShape, b2_polygonRadius);
this.m_vertex1 = new b2Vec2;
this.m_vertex2 = new b2Vec2;
this.m_vertex0 = new b2Vec2;
this.m_vertex3 = new b2Vec2;
this.m_oneSided = false;
}
SetOneSided(e, t, s, h) {
this.m_vertex0.Copy(e);
this.m_vertex1.Copy(t);
this.m_vertex2.Copy(s);
this.m_vertex3.Copy(h);
this.m_oneSided = true;
return this;
}
SetTwoSided(e, t) {
this.m_vertex1.Copy(e);
this.m_vertex2.Copy(t);
this.m_oneSided = false;
return this;
}
Clone() {
return (new b2EdgeShape).Copy(this);
}
Copy(e) {
super.Copy(e);
this.m_vertex1.Copy(e.m_vertex1);
this.m_vertex2.Copy(e.m_vertex2);
this.m_vertex0.Copy(e.m_vertex0);
this.m_vertex3.Copy(e.m_vertex3);
this.m_oneSided = e.m_oneSided;
return this;
}
GetChildCount() {
return 1;
}
TestPoint(e, t) {
return false;
}
ComputeDistance(e, t, s, h) {
const b = b2Transform.MulXV(e, this.m_vertex1, b2EdgeShape.ComputeDistance_s_v1);
const n = b2Transform.MulXV(e, this.m_vertex2, b2EdgeShape.ComputeDistance_s_v2);
const c = b2Vec2.SubVV(t, b, b2EdgeShape.ComputeDistance_s_d);
const i = b2Vec2.SubVV(n, b, b2EdgeShape.ComputeDistance_s_s);
const a = b2Vec2.DotVV(c, i);
if (a > 0) {
const e = b2Vec2.DotVV(i, i);
if (a > e) {
b2Vec2.SubVV(t, n, c);
} else {
c.SelfMulSub(a / e, i);
}
}
s.Copy(c);
return s.Normalize();
}
RayCast(e, t, s, h) {
const b = b2Transform.MulTXV(s, t.p1, b2EdgeShape.RayCast_s_p1);
const n = b2Transform.MulTXV(s, t.p2, b2EdgeShape.RayCast_s_p2);
const c = b2Vec2.SubVV(n, b, b2EdgeShape.RayCast_s_d);
const i = this.m_vertex1;
const a = this.m_vertex2;
const r = b2Vec2.SubVV(a, i, b2EdgeShape.RayCast_s_e);
const o = e.normal.Set(r.y, -r.x).SelfNormalize();
const p = b2Vec2.DotVV(o, b2Vec2.SubVV(i, b, b2Vec2.s_t0));
if (this.m_oneSided && p > 0) {
return false;
}
const S = b2Vec2.DotVV(o, c);
if (S === 0) {
return false;
}
const d = p / S;
if (d < 0 || t.maxFraction < d) {
return false;
}
const V = b2Vec2.AddVMulSV(b, d, c, b2EdgeShape.RayCast_s_q);
const f = b2Vec2.SubVV(a, i, b2EdgeShape.RayCast_s_r);
const g = b2Vec2.DotVV(f, f);
if (g === 0) {
return false;
}
const m = b2Vec2.DotVV(b2Vec2.SubVV(V, i, b2Vec2.s_t0), f) / g;
if (m < 0 || 1 < m) {
return false;
}
e.fraction = d;
b2Rot.MulRV(s.q, e.normal, e.normal);
if (p > 0) {
e.normal.SelfNeg();
}
return true;
}
ComputeAABB(e, t, s) {
const h = b2Transform.MulXV(t, this.m_vertex1, b2EdgeShape.ComputeAABB_s_v1);
const b = b2Transform.MulXV(t, this.m_vertex2, b2EdgeShape.ComputeAABB_s_v2);
b2Vec2.MinV(h, b, e.lowerBound);
b2Vec2.MaxV(h, b, e.upperBound);
const n = this.m_radius;
e.lowerBound.SelfSubXY(n, n);
e.upperBound.SelfAddXY(n, n);
}
ComputeMass(e, t) {
e.mass = 0;
b2Vec2.MidVV(this.m_vertex1, this.m_vertex2, e.center);
e.I = 0;
}
SetupDistanceProxy(e, t) {
e.m_vertices = e.m_buffer;
e.m_vertices[0].Copy(this.m_vertex1);
e.m_vertices[1].Copy(this.m_vertex2);
e.m_count = 2;
e.m_radius = this.m_radius;
}
ComputeSubmergedArea(e, t, s, h) {
h.SetZero();
return 0;
}
Dump(e) {
e(" const shape: b2EdgeShape = new b2EdgeShape();\n");
e(" shape.m_radius = %.15f;\n", this.m_radius);
e(" shape.m_vertex0.Set(%.15f, %.15f);\n", this.m_vertex0.x, this.m_vertex0.y);
e(" shape.m_vertex1.Set(%.15f, %.15f);\n", this.m_vertex1.x, this.m_vertex1.y);
e(" shape.m_vertex2.Set(%.15f, %.15f);\n", this.m_vertex2.x, this.m_vertex2.y);
e(" shape.m_vertex3.Set(%.15f, %.15f);\n", this.m_vertex3.x, this.m_vertex3.y);
e(" shape.m_oneSided = %s;\n", this.m_oneSided);
}
}
b2EdgeShape.ComputeDistance_s_v1 = new b2Vec2;
b2EdgeShape.ComputeDistance_s_v2 = new b2Vec2;
b2EdgeShape.ComputeDistance_s_d = new b2Vec2;
b2EdgeShape.ComputeDistance_s_s = new b2Vec2;
b2EdgeShape.RayCast_s_p1 = new b2Vec2;
b2EdgeShape.RayCast_s_p2 = new b2Vec2;
b2EdgeShape.RayCast_s_d = new b2Vec2;
b2EdgeShape.RayCast_s_e = new b2Vec2;
b2EdgeShape.RayCast_s_q = new b2Vec2;
b2EdgeShape.RayCast_s_r = new b2Vec2;
b2EdgeShape.ComputeAABB_s_v1 = new b2Vec2;
b2EdgeShape.ComputeAABB_s_v2 = new b2Vec2;