@box2d/debug-draw
Version:
Debug drawing helper for @box2d
119 lines (118 loc) • 4.79 kB
JavaScript
;
// MIT License
Object.defineProperty(exports, "__esModule", { value: true });
exports.DrawCenterOfMasses = exports.DrawAABBs = exports.DrawPairs = exports.DrawJoints = exports.DrawShapes = exports.GetShapeColor = void 0;
// Copyright (c) 2019 Erin Catto
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
const b2_math_1 = require("./b2_math");
const b2_draw_1 = require("./b2_draw");
const b2_body_1 = require("../dynamics/b2_body");
const b2_common_1 = require("./b2_common");
const temp = {
cA: new b2_math_1.b2Vec2(),
cB: new b2_math_1.b2Vec2(),
vs: (0, b2_common_1.b2MakeArray)(4, b2_math_1.b2Vec2),
xf: new b2_math_1.b2Transform(),
};
function GetShapeColor(b) {
if (b.GetType() === b2_body_1.b2BodyType.b2_dynamicBody && b.m_mass === 0) {
return b2_draw_1.debugColors.badBody;
}
if (!b.IsEnabled()) {
return b2_draw_1.debugColors.disabledBody;
}
if (b.GetType() === b2_body_1.b2BodyType.b2_staticBody) {
return b2_draw_1.debugColors.staticBody;
}
if (b.GetType() === b2_body_1.b2BodyType.b2_kinematicBody) {
return b2_draw_1.debugColors.kinematicBody;
}
if (!b.IsAwake()) {
return b2_draw_1.debugColors.sleepingBody;
}
return b2_draw_1.debugColors.body;
}
exports.GetShapeColor = GetShapeColor;
function testOverlap(fixture, aabb) {
for (let i = 0; i < fixture.m_proxyCount; i++) {
if (aabb.TestOverlap(fixture.GetAABB(i))) {
return true;
}
}
return false;
}
function DrawShapes(draw, world, within) {
for (let b = world.GetBodyList(); b; b = b.m_next) {
const xf = b.m_xf;
draw.PushTransform(xf);
for (let f = b.GetFixtureList(); f; f = f.m_next) {
if (within && !testOverlap(f, within))
continue;
f.GetShape().Draw(draw, GetShapeColor(b));
}
draw.PopTransform(xf);
}
}
exports.DrawShapes = DrawShapes;
function DrawJoints(draw, world) {
for (let j = world.GetJointList(); j; j = j.m_next) {
j.Draw(draw);
}
}
exports.DrawJoints = DrawJoints;
function DrawPairs(draw, world) {
for (let contact = world.GetContactList(); contact; contact = contact.m_next) {
const fixtureA = contact.GetFixtureA();
const fixtureB = contact.GetFixtureB();
const indexA = contact.GetChildIndexA();
const indexB = contact.GetChildIndexB();
const cA = fixtureA.GetAABB(indexA).GetCenter(temp.cA);
const cB = fixtureB.GetAABB(indexB).GetCenter(temp.cB);
draw.DrawSegment(cA, cB, b2_draw_1.debugColors.pair);
}
}
exports.DrawPairs = DrawPairs;
function DrawAABBs(draw, world, within) {
const { vs } = temp;
for (let b = world.GetBodyList(); b; b = b.m_next) {
if (!b.IsEnabled()) {
continue;
}
for (let f = b.GetFixtureList(); f; f = f.m_next) {
for (let i = 0; i < f.m_proxyCount; ++i) {
const { aabb } = f.m_proxies[i].treeNode;
if (within && !within.TestOverlap(aabb))
continue;
vs[0].Set(aabb.lowerBound.x, aabb.lowerBound.y);
vs[1].Set(aabb.upperBound.x, aabb.lowerBound.y);
vs[2].Set(aabb.upperBound.x, aabb.upperBound.y);
vs[3].Set(aabb.lowerBound.x, aabb.upperBound.y);
draw.DrawPolygon(vs, 4, b2_draw_1.debugColors.aabb);
}
}
}
}
exports.DrawAABBs = DrawAABBs;
function DrawCenterOfMasses(draw, world) {
const { xf } = temp;
for (let b = world.GetBodyList(); b; b = b.m_next) {
xf.q.Copy(b.m_xf.q);
xf.p.Copy(b.GetWorldCenter());
draw.DrawTransform(xf);
}
}
exports.DrawCenterOfMasses = DrawCenterOfMasses;