Skip to content

Commit 0a801cb

Browse files
committed
Added World.renderGrid method.
Renamed showBodyPosition to showPositions
1 parent 7aec93d commit 0a801cb

2 files changed

Lines changed: 70 additions & 3 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,26 @@ var World = new Class({
244244
showBody: GetFastValue(debugConfig, 'showBody', true),
245245
showStaticBody: GetFastValue(debugConfig, 'showStaticBody', true),
246246
showSleeping: GetFastValue(debugConfig, 'showSleeping', false),
247-
showBodyPosition: GetFastValue(debugConfig, 'showBodyPosition', true),
247+
showPositions: GetFastValue(debugConfig, 'showPositions', true),
248248
showJoint: GetFastValue(debugConfig, 'showJoint', true),
249249
showInternalEdges: GetFastValue(debugConfig, 'showInternalEdges', false),
250250
showConvexHulls: GetFastValue(debugConfig, 'showConvexHulls', false),
251251
showSensors: GetFastValue(debugConfig, 'showSensors', true),
252252

253+
showBroadphase: GetFastValue(debugConfig, 'showBroadphase', false),
254+
broadphaseColor: GetFastValue(debugConfig, 'broadphaseColor', 0xffb400),
255+
256+
showBounds: true,
257+
showVelocity: true,
258+
showCollisions: true,
259+
showSeparations: true,
260+
showAxes: true,
261+
showAngleIndicator: true,
262+
showIds: true,
263+
showShadows: true,
264+
showVertexNumbers: true,
265+
showMousePosition: true,
266+
253267
renderFill: GetFastValue(debugConfig, 'renderFill', false),
254268
renderLine: GetFastValue(debugConfig, 'renderLine', true),
255269

@@ -1276,6 +1290,8 @@ var World = new Class({
12761290
postUpdate: function ()
12771291
{
12781292
var config = this.debugConfig;
1293+
var engine = this.engine;
1294+
var graphics = this.debugGraphic;
12791295

12801296
var showBody = config.showBody;
12811297
var showStaticBody = config.showStaticBody;
@@ -1288,6 +1304,11 @@ var World = new Class({
12881304

12891305
this.debugGraphic.clear();
12901306

1307+
if (config.showBroadphase && engine.broadphase.controller)
1308+
{
1309+
this.renderGrid(engine.broadphase, graphics, config.broadphaseColor, 0.2);
1310+
}
1311+
12911312
var bodies = Composite.allBodies(this.localWorld);
12921313

12931314
this.renderBodies(bodies);
@@ -1298,6 +1319,50 @@ var World = new Class({
12981319
}
12991320
},
13001321

1322+
/**
1323+
* Renders the Engine Broadphase Controller Grid to the given Graphics instance.
1324+
*
1325+
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
1326+
* you wish to render the Grid to your own Graphics instance.
1327+
*
1328+
* @method Phaser.Physics.Matter.World#renderGrid
1329+
* @since 3.22.0
1330+
*
1331+
* @param {MatterJS.Grid} grid - The Matter Grid to be rendered.
1332+
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
1333+
* @param {number} lineColor - The line color.
1334+
* @param {number} lineOpacity - The line opacity, between 0 and 1.
1335+
*
1336+
* @return {this} This Matter World instance for method chaining.
1337+
*/
1338+
renderGrid: function (grid, graphics, lineColor, lineOpacity)
1339+
{
1340+
graphics.lineStyle(1, lineColor, lineOpacity);
1341+
1342+
var bucketKeys = Common.keys(grid.buckets);
1343+
1344+
for (var i = 0; i < bucketKeys.length; i++)
1345+
{
1346+
var bucketId = bucketKeys[i];
1347+
1348+
if (grid.buckets[bucketId].length < 2)
1349+
{
1350+
continue;
1351+
}
1352+
1353+
var region = bucketId.split(/C|R/);
1354+
1355+
graphics.strokeRect(
1356+
parseInt(region[1], 10) * grid.bucketWidth,
1357+
parseInt(region[2], 10) * grid.bucketHeight,
1358+
grid.bucketWidth,
1359+
grid.bucketHeight
1360+
);
1361+
}
1362+
1363+
return this;
1364+
},
1365+
13011366
/**
13021367
* Renders the given array of Bodies to the debug graphics instance.
13031368
*
@@ -1515,7 +1580,7 @@ var World = new Class({
15151580
}
15161581
}
15171582

1518-
if (config.showBodyPosition && !body.isStatic)
1583+
if (config.showPositions && !body.isStatic)
15191584
{
15201585
var px = body.position.x;
15211586
var py = body.position.y;

src/physics/matter-js/typedefs/MatterDebugConfig.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
* @property {boolean} [showJoint=true] - Render all world constraints to the Graphics object?
99
* @property {boolean} [showInternalEdges=false] - When rendering bodies, render the internal edges as well?
1010
* @property {boolean} [showConvexHulls=false] - When rendering polygon bodies, render the convex hull as well?
11-
* @property {boolean} [showBodyPosition=true] - Render the position of non-static bodies?
11+
* @property {boolean} [showPositions=true] - Render the position of non-static bodies?
12+
* @property {boolean} [showBroadphase=false] - Render the broadphase grid?
13+
* @property {boolean} [broadphaseColor=0xffb400] - The color of the broadphase grid.
1214
* @property {boolean} [renderFill=false] - Render the bodies using a fill color.
1315
* @property {boolean} [renderLine=true] - Render the bodies using a line stroke.
1416
* @property {number} [fillColor=0x106909] - The color value of the fill when rendering dynamic bodies.

0 commit comments

Comments
 (0)