Skip to content

Commit 8527e10

Browse files
committed
Added renderCollisions and renderSeparations
1 parent d4b6c54 commit 8527e10

1 file changed

Lines changed: 183 additions & 12 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 183 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,6 @@ var World = new Class({
241241
* @since 3.22.0
242242
*/
243243
this.debugConfig = {
244-
245-
showCollisions: true,
246-
showSeparations: true,
247-
showIds: true,
248-
showShadows: true,
249-
showVertexNumbers: true,
250-
showMousePosition: true,
251-
252244
showAxes: GetFastValue(debugConfig, 'showAxes', false),
253245
showAngleIndicator: GetFastValue(debugConfig, 'showAngleIndicator', false),
254246
angleColor: GetFastValue(debugConfig, 'angleColor', 0xe81153),
@@ -262,6 +254,12 @@ var World = new Class({
262254
showVelocity: GetFastValue(debugConfig, 'showVelocity', false),
263255
velocityColor: GetFastValue(debugConfig, 'velocityColor', 0x00aeef),
264256

257+
showCollisions: GetFastValue(debugConfig, 'showCollisions', false),
258+
collisionColor: GetFastValue(debugConfig, 'collisionColor', 0xf5950c),
259+
260+
showSeparations: GetFastValue(debugConfig, 'showSeparations', false),
261+
separationColor: GetFastValue(debugConfig, 'separationColor', 0xffa500),
262+
265263
showBody: GetFastValue(debugConfig, 'showBody', true),
266264
showStaticBody: GetFastValue(debugConfig, 'showStaticBody', true),
267265
showInternalEdges: GetFastValue(debugConfig, 'showInternalEdges', false),
@@ -1319,6 +1317,16 @@ var World = new Class({
13191317
this.renderBodyBounds(bodies, graphics, config.boundsColor, 0.2);
13201318
}
13211319

1320+
if (config.showBody || config.showStaticBody)
1321+
{
1322+
this.renderBodies(bodies);
1323+
}
1324+
1325+
if (config.showJoint)
1326+
{
1327+
this.renderJoints();
1328+
}
1329+
13221330
if (config.showAxes || config.showAngleIndicator)
13231331
{
13241332
this.renderBodyAxes(bodies, graphics, config.showAxes, config.angleColor, 0.5);
@@ -1329,14 +1337,14 @@ var World = new Class({
13291337
this.renderBodyVelocity(bodies, graphics, config.velocityColor, 1, 2);
13301338
}
13311339

1332-
if (config.showBody || config.showStaticBody)
1340+
if (config.showSeparations)
13331341
{
1334-
this.renderBodies(bodies);
1342+
this.renderSeparations(engine.pairs.list, graphics, config.separationColor);
13351343
}
13361344

1337-
if (config.showJoint)
1345+
if (config.showCollisions)
13381346
{
1339-
this.renderJoints();
1347+
this.renderCollisions(engine.pairs.list, graphics, config.collisionColor);
13401348
}
13411349
},
13421350

@@ -1386,6 +1394,169 @@ var World = new Class({
13861394
return this;
13871395
},
13881396

1397+
/**
1398+
* Renders the list of Pair separations to the given Graphics instance.
1399+
*
1400+
* The debug renderer calls this method if the `showSeparations` config value is set.
1401+
*
1402+
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
1403+
* you wish to render the Grid to your own Graphics instance.
1404+
*
1405+
* @method Phaser.Physics.Matter.World#renderSeparations
1406+
* @since 3.22.0
1407+
*
1408+
* @param {MatterJS.Pair[]} pairs - An array of Matter Pairs to be rendered.
1409+
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
1410+
* @param {number} lineColor - The line color.
1411+
*
1412+
* @return {this} This Matter World instance for method chaining.
1413+
*/
1414+
renderSeparations: function (pairs, graphics, lineColor)
1415+
{
1416+
graphics.lineStyle(1, lineColor, 1);
1417+
1418+
for (var i = 0; i < pairs.length; i++)
1419+
{
1420+
var pair = pairs[i];
1421+
1422+
if (!pair.isActive)
1423+
{
1424+
continue;
1425+
}
1426+
1427+
var collision = pair.collision;
1428+
var bodyA = collision.bodyA;
1429+
var bodyB = collision.bodyB;
1430+
var posA = bodyA.position;
1431+
var posB = bodyB.position;
1432+
var penetration = collision.penetration;
1433+
1434+
var k = (!bodyA.isStatic && !bodyB.isStatic) ? 4 : 1;
1435+
1436+
if (bodyB.isStatic)
1437+
{
1438+
k = 0;
1439+
}
1440+
1441+
graphics.lineBetween(
1442+
posB.x,
1443+
posB.y,
1444+
posB.x - (penetration.x * k),
1445+
posB.y - (penetration.y * k)
1446+
);
1447+
1448+
k = (!bodyA.isStatic && !bodyB.isStatic) ? 4 : 1;
1449+
1450+
if (bodyA.isStatic)
1451+
{
1452+
k = 0;
1453+
}
1454+
1455+
graphics.lineBetween(
1456+
posA.x,
1457+
posA.y,
1458+
posA.x - (penetration.x * k),
1459+
posA.y - (penetration.y * k)
1460+
);
1461+
}
1462+
1463+
return this;
1464+
},
1465+
1466+
/**
1467+
* Renders the list of collision points and normals to the given Graphics instance.
1468+
*
1469+
* The debug renderer calls this method if the `showCollisions` config value is set.
1470+
*
1471+
* This method is used internally by the Matter Debug Renderer, but is also exposed publically should
1472+
* you wish to render the Grid to your own Graphics instance.
1473+
*
1474+
* @method Phaser.Physics.Matter.World#renderCollisions
1475+
* @since 3.22.0
1476+
*
1477+
* @param {MatterJS.Pair[]} pairs - An array of Matter Pairs to be rendered.
1478+
* @param {Phaser.GameObjects.Graphics} graphics - The Graphics object to render to.
1479+
* @param {number} lineColor - The line color.
1480+
*
1481+
* @return {this} This Matter World instance for method chaining.
1482+
*/
1483+
renderCollisions: function (pairs, graphics, lineColor)
1484+
{
1485+
graphics.lineStyle(1, lineColor, 0.5);
1486+
graphics.fillStyle(lineColor, 1);
1487+
1488+
var i;
1489+
var pair;
1490+
1491+
// Collision Positions
1492+
1493+
for (i = 0; i < pairs.length; i++)
1494+
{
1495+
pair = pairs[i];
1496+
1497+
if (!pair.isActive)
1498+
{
1499+
continue;
1500+
}
1501+
1502+
for (var j = 0; j < pair.activeContacts.length; j++)
1503+
{
1504+
var contact = pair.activeContacts[j];
1505+
var vertex = contact.vertex;
1506+
1507+
graphics.fillRect(vertex.x - 2, vertex.y - 2, 5, 5);
1508+
}
1509+
}
1510+
1511+
// Collision Normals
1512+
1513+
for (i = 0; i < pairs.length; i++)
1514+
{
1515+
pair = pairs[i];
1516+
1517+
if (!pair.isActive)
1518+
{
1519+
continue;
1520+
}
1521+
1522+
var collision = pair.collision;
1523+
var contacts = pair.activeContacts;
1524+
1525+
if (contacts.length > 0)
1526+
{
1527+
var normalPosX = contacts[0].vertex.x;
1528+
var normalPosY = contacts[0].vertex.y;
1529+
1530+
if (contacts.length === 2)
1531+
{
1532+
normalPosX = (contacts[0].vertex.x + contacts[1].vertex.x) / 2;
1533+
normalPosY = (contacts[0].vertex.y + contacts[1].vertex.y) / 2;
1534+
}
1535+
1536+
if (collision.bodyB === collision.supports[0].body || collision.bodyA.isStatic)
1537+
{
1538+
graphics.lineBetween(
1539+
normalPosX - collision.normal.x * 8,
1540+
normalPosY - collision.normal.y * 8,
1541+
normalPosX,
1542+
normalPosY
1543+
);
1544+
}
1545+
else
1546+
{
1547+
graphics.lineBetween(
1548+
normalPosX + collision.normal.x * 8,
1549+
normalPosY + collision.normal.y * 8,
1550+
normalPosX,
1551+
normalPosY
1552+
);
1553+
}
1554+
}
1555+
}
1556+
1557+
return this;
1558+
},
1559+
13891560
/**
13901561
* Renders the bounds of an array of Bodies to the given Graphics instance.
13911562
*

0 commit comments

Comments
 (0)