Skip to content

Commit ad1891c

Browse files
committed
Added renderBodyBounds and renderBodyAxes methods
1 parent a938b6d commit ad1891c

1 file changed

Lines changed: 166 additions & 25 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 166 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -241,29 +241,29 @@ var World = new Class({
241241
* @since 3.22.0
242242
*/
243243
this.debugConfig = {
244-
showBody: GetFastValue(debugConfig, 'showBody', true),
245-
showStaticBody: GetFastValue(debugConfig, 'showStaticBody', true),
246-
showSleeping: GetFastValue(debugConfig, 'showSleeping', false),
247-
showPositions: GetFastValue(debugConfig, 'showPositions', true),
248-
showJoint: GetFastValue(debugConfig, 'showJoint', true),
249-
showInternalEdges: GetFastValue(debugConfig, 'showInternalEdges', false),
250-
showConvexHulls: GetFastValue(debugConfig, 'showConvexHulls', false),
251-
showSensors: GetFastValue(debugConfig, 'showSensors', true),
252244

253-
showBroadphase: GetFastValue(debugConfig, 'showBroadphase', false),
254-
broadphaseColor: GetFastValue(debugConfig, 'broadphaseColor', 0xffb400),
255-
256-
showBounds: true,
257245
showVelocity: true,
258246
showCollisions: true,
259247
showSeparations: true,
260-
showAxes: true,
261-
showAngleIndicator: true,
262248
showIds: true,
263249
showShadows: true,
264250
showVertexNumbers: true,
265251
showMousePosition: true,
266252

253+
showAxes: GetFastValue(debugConfig, 'showAxes', false),
254+
showAngleIndicator: GetFastValue(debugConfig, 'showAngleIndicator', false),
255+
angleColor: GetFastValue(debugConfig, 'angleColor', 0xe81153),
256+
257+
showBroadphase: GetFastValue(debugConfig, 'showBroadphase', false),
258+
broadphaseColor: GetFastValue(debugConfig, 'broadphaseColor', 0xffb400),
259+
260+
showBounds: GetFastValue(debugConfig, 'showBounds', false),
261+
boundsColor: GetFastValue(debugConfig, 'boundsColor', 0xffffff),
262+
263+
showBody: GetFastValue(debugConfig, 'showBody', true),
264+
showStaticBody: GetFastValue(debugConfig, 'showStaticBody', true),
265+
showInternalEdges: GetFastValue(debugConfig, 'showInternalEdges', false),
266+
267267
renderFill: GetFastValue(debugConfig, 'renderFill', false),
268268
renderLine: GetFastValue(debugConfig, 'renderLine', true),
269269

@@ -276,16 +276,20 @@ var World = new Class({
276276
staticFillColor: GetFastValue(debugConfig, 'staticFillColor', 0x0d177b),
277277
staticLineColor: GetFastValue(debugConfig, 'staticLineColor', 0x1327e4),
278278

279+
showSleeping: GetFastValue(debugConfig, 'showSleeping', false),
279280
staticBodySleepOpacity: GetFastValue(debugConfig, 'staticBodySleepOpacity', 0.7),
280281
sleepFillColor: GetFastValue(debugConfig, 'sleepFillColor', 0x464646),
281282
sleepLineColor: GetFastValue(debugConfig, 'sleepLineColor', 0x999a99),
282283

284+
showSensors: GetFastValue(debugConfig, 'showSensors', true),
283285
sensorFillColor: GetFastValue(debugConfig, 'sensorFillColor', 0x0d177b),
284286
sensorLineColor: GetFastValue(debugConfig, 'sensorLineColor', 0x1327e4),
285287

288+
showPositions: GetFastValue(debugConfig, 'showPositions', true),
286289
positionSize: GetFastValue(debugConfig, 'positionSize', 4),
287290
positionColor: GetFastValue(debugConfig, 'positionColor', 0xe042da),
288291

292+
showJoint: GetFastValue(debugConfig, 'showJoint', true),
289293
jointColor: GetFastValue(debugConfig, 'jointColor', 0xe0e042),
290294
jointLineOpacity: GetFastValue(debugConfig, 'jointLineOpacity', 1),
291295
jointLineThickness: GetFastValue(debugConfig, 'jointLineThickness', 2),
@@ -298,6 +302,7 @@ var World = new Class({
298302
anchorColor: GetFastValue(debugConfig, 'anchorColor', 0xefefef),
299303
anchorSize: GetFastValue(debugConfig, 'anchorSize', 6),
300304

305+
showConvexHulls: GetFastValue(debugConfig, 'showConvexHulls', false),
301306
hullColor: GetFastValue(debugConfig, 'hullColor', 0xd703d0)
302307
};
303308

@@ -1289,18 +1294,16 @@ var World = new Class({
12891294
*/
12901295
postUpdate: function ()
12911296
{
1297+
if (!this.drawDebug)
1298+
{
1299+
return;
1300+
}
1301+
12921302
var config = this.debugConfig;
12931303
var engine = this.engine;
12941304
var graphics = this.debugGraphic;
12951305

1296-
var showBody = config.showBody;
1297-
var showStaticBody = config.showStaticBody;
1298-
var showJoint = config.showJoint;
1299-
1300-
if (!this.drawDebug || (!showBody && !showStaticBody && !showJoint))
1301-
{
1302-
return;
1303-
}
1306+
var bodies = Composite.allBodies(this.localWorld);
13041307

13051308
this.debugGraphic.clear();
13061309

@@ -1309,11 +1312,22 @@ var World = new Class({
13091312
this.renderGrid(engine.broadphase, graphics, config.broadphaseColor, 0.2);
13101313
}
13111314

1312-
var bodies = Composite.allBodies(this.localWorld);
1315+
if (config.showBounds)
1316+
{
1317+
this.renderBodyBounds(bodies, graphics, config.boundsColor, 0.2);
1318+
}
13131319

1314-
this.renderBodies(bodies);
1320+
if (config.showAxes || config.showAngleIndicator)
1321+
{
1322+
this.renderBodyAxes(bodies, graphics, config.showAxes, config.angleColor, 0.5);
1323+
}
1324+
1325+
if (config.showBody || config.showStaticBody)
1326+
{
1327+
this.renderBodies(bodies);
1328+
}
13151329

1316-
if (showJoint)
1330+
if (config.showJoint)
13171331
{
13181332
this.renderJoints();
13191333
}
@@ -1322,6 +1336,8 @@ var World = new Class({
13221336
/**
13231337
* Renders the Engine Broadphase Controller Grid to the given Graphics instance.
13241338
*
1339+
* The debug renderer calls this method if the `showBroadphase` config value is set.
1340+
*
13251341
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
13261342
* you wish to render the Grid to your own Graphics instance.
13271343
*
@@ -1363,6 +1379,131 @@ var World = new Class({
13631379
return this;
13641380
},
13651381

1382+
/**
1383+
* Renders the bounds of an array of Bodies to the given Graphics instance.
1384+
*
1385+
* The debug renderer calls this method if the `showBounds` config value is set.
1386+
*
1387+
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
1388+
* you wish to render bounds to your own Graphics instance.
1389+
*
1390+
* @method Phaser.Physics.Matter.World#renderBodyBounds
1391+
* @since 3.22.0
1392+
*
1393+
* @param {array} bodies - An array of bodies from the localWorld.
1394+
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
1395+
* @param {number} lineColor - The line color.
1396+
* @param {number} lineOpacity - The line opacity, between 0 and 1.
1397+
*/
1398+
renderBodyBounds: function (bodies, graphics, lineColor, lineOpacity)
1399+
{
1400+
graphics.lineStyle(1, lineColor, lineOpacity);
1401+
1402+
for (var i = 0; i < bodies.length; i++)
1403+
{
1404+
var body = bodies[i];
1405+
1406+
// 1) Don't show invisible bodies
1407+
if (!body.render.visible)
1408+
{
1409+
continue;
1410+
}
1411+
1412+
var parts = body.parts;
1413+
1414+
for (var j = parts.length > 1 ? 1 : 0; j < parts.length; j++)
1415+
{
1416+
var part = parts[j];
1417+
1418+
graphics.strokeRect(
1419+
part.bounds.min.x,
1420+
part.bounds.min.y,
1421+
part.bounds.max.x - part.bounds.min.x,
1422+
part.bounds.max.y - part.bounds.min.y
1423+
);
1424+
}
1425+
}
1426+
1427+
return this;
1428+
},
1429+
1430+
/**
1431+
* Renders either all axes, or a single axis indicator, for an array of Bodies, to the given Graphics instance.
1432+
*
1433+
* The debug renderer calls this method if the `showAxes` or `showAngleIndicator` config values are set.
1434+
*
1435+
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
1436+
* you wish to render bounds to your own Graphics instance.
1437+
*
1438+
* @method Phaser.Physics.Matter.World#renderBodyAxes
1439+
* @since 3.22.0
1440+
*
1441+
* @param {array} bodies - An array of bodies from the localWorld.
1442+
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
1443+
* @param {boolean} showAxes - If `true` it will render all body axes. If `false` it will render a single axis indicator.
1444+
* @param {number} lineColor - The line color.
1445+
* @param {number} lineOpacity - The line opacity, between 0 and 1.
1446+
*/
1447+
renderBodyAxes: function (bodies, graphics, showAxes, lineColor, lineOpacity)
1448+
{
1449+
graphics.lineStyle(1, lineColor, lineOpacity);
1450+
1451+
for (var i = 0; i < bodies.length; i++)
1452+
{
1453+
var body = bodies[i];
1454+
var parts = body.parts;
1455+
1456+
// 1) Don't show invisible bodies
1457+
if (!body.render.visible)
1458+
{
1459+
continue;
1460+
}
1461+
1462+
var part;
1463+
var j;
1464+
var k;
1465+
1466+
if (showAxes)
1467+
{
1468+
for (j = parts.length > 1 ? 1 : 0; j < parts.length; j++)
1469+
{
1470+
part = parts[j];
1471+
1472+
for (k = 0; k < part.axes.length; k++)
1473+
{
1474+
var axis = part.axes[k];
1475+
1476+
graphics.lineBetween(
1477+
part.position.x,
1478+
part.position.y,
1479+
part.position.x + axis.x * 20,
1480+
part.position.y + axis.y * 20
1481+
);
1482+
}
1483+
}
1484+
}
1485+
else
1486+
{
1487+
for (j = parts.length > 1 ? 1 : 0; j < parts.length; j++)
1488+
{
1489+
part = parts[j];
1490+
1491+
for (k = 0; k < part.axes.length; k++)
1492+
{
1493+
graphics.lineBetween(
1494+
part.position.x,
1495+
part.position.y,
1496+
(part.vertices[0].x + part.vertices[part.vertices.length - 1].x) / 2,
1497+
(part.vertices[0].y + part.vertices[part.vertices.length - 1].y) / 2
1498+
);
1499+
}
1500+
}
1501+
}
1502+
}
1503+
1504+
return this;
1505+
},
1506+
13661507
/**
13671508
* Renders the given array of Bodies to the debug graphics instance.
13681509
*

0 commit comments

Comments
 (0)