Skip to content

Commit 7f1723c

Browse files
authored
Merge pull request phaserjs#3458 from OmarShehata/master
Added joint debug rendering to Matter Physics postUpdate
2 parents c1e5aa7 + b185307 commit 7f1723c

1 file changed

Lines changed: 99 additions & 3 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var MatterBody = require('./lib/body/Body');
1717
var MatterEvents = require('./lib/core/Events');
1818
var MatterWorld = require('./lib/body/World');
1919
var MatterTileBody = require('./MatterTileBody');
20+
var Vector = require('./lib/geometry/Vector');
21+
var Common = require('./lib/core/Common');
2022

2123
/**
2224
* @classdesc
@@ -206,7 +208,9 @@ var World = new Class({
206208
debugShowVelocity: GetValue(config, 'debugShowVelocity', true),
207209
bodyDebugColor: GetValue(config, 'debugBodyColor', 0xff00ff),
208210
staticBodyDebugColor: GetValue(config, 'debugBodyColor', 0x0000ff),
209-
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00)
211+
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00),
212+
debugShowJoint: GetValue(config, 'debugShowJoint', true),
213+
jointDebugColor: GetValue(config, 'debugJointColor', 0x000000)
210214
};
211215

212216
if (this.drawDebug)
@@ -730,7 +734,9 @@ var World = new Class({
730734
graphics.lineStyle(1, this.defaults.bodyDebugColor);
731735
graphics.beginPath();
732736

733-
for (var i = 0; i < bodies.length; i++)
737+
var i,j;
738+
739+
for (i = 0; i < bodies.length; i++)
734740
{
735741
if (!bodies[i].render.visible)
736742
{
@@ -739,7 +745,7 @@ var World = new Class({
739745

740746
// Handle drawing both single bodies and compound bodies. If compound, draw both the
741747
// convex hull (first part) and the rest of the bodies.
742-
for (var j = 0; j < bodies[i].parts.length; j++)
748+
for (j = 0; j < bodies[i].parts.length; j++)
743749
{
744750
var body = bodies[i].parts[j];
745751

@@ -759,6 +765,96 @@ var World = new Class({
759765
}
760766

761767
graphics.closePath();
768+
769+
if(this.defaults.debugShowJoint)
770+
{
771+
graphics.lineStyle(2, this.defaults.jointDebugColor);
772+
773+
// Render constraints
774+
var constraints = Composite.allConstraints(this.localWorld);
775+
for (i = 0; i < constraints.length; i++)
776+
{
777+
var constraint = constraints[i];
778+
779+
if (!constraint.render.visible || !constraint.pointA || !constraint.pointB)
780+
{ continue; }
781+
782+
if (constraint.render.lineWidth)
783+
{
784+
graphics.lineStyle(constraint.render.lineWidth, Common.colorToNumber(constraint.render.strokeStyle));
785+
}
786+
787+
var bodyA = constraint.bodyA,
788+
bodyB = constraint.bodyB,
789+
start,
790+
end;
791+
792+
if (bodyA)
793+
{
794+
start = Vector.add(bodyA.position, constraint.pointA);
795+
}
796+
else
797+
{
798+
start = constraint.pointA;
799+
}
800+
801+
if (constraint.render.type === 'pin')
802+
{
803+
graphics.beginPath();
804+
graphics.arc(start.x, start.y, 3, 0, 2 * Math.PI);
805+
graphics.closePath();
806+
}
807+
else
808+
{
809+
if (bodyB)
810+
{
811+
end = Vector.add(bodyB.position, constraint.pointB);
812+
}
813+
else
814+
{
815+
end = constraint.pointB;
816+
}
817+
818+
graphics.beginPath();
819+
graphics.moveTo(start.x, start.y);
820+
821+
if (constraint.render.type === 'spring')
822+
{
823+
var delta = Vector.sub(end, start),
824+
normal = Vector.perp(Vector.normalise(delta)),
825+
coils = Math.ceil(Common.clamp(constraint.length / 5, 12, 20)),
826+
offset;
827+
828+
for (j = 1; j < coils; j += 1)
829+
{
830+
offset = j % 2 === 0 ? 1 : -1;
831+
832+
graphics.lineTo(
833+
start.x + delta.x * (j / coils) + normal.x * offset * 4,
834+
start.y + delta.y * (j / coils) + normal.y * offset * 4
835+
);
836+
}
837+
}
838+
839+
graphics.lineTo(end.x, end.y);
840+
}
841+
842+
if (constraint.render.lineWidth)
843+
{
844+
graphics.strokePath();
845+
}
846+
847+
if (constraint.render.anchors)
848+
{
849+
graphics.fillStyle(Common.colorToNumber(constraint.render.strokeStyle));
850+
graphics.beginPath();
851+
graphics.arc(start.x, start.y, 6, 0, 2 * Math.PI);
852+
graphics.arc(end.x, end.y, 6, 0, 2 * Math.PI);
853+
graphics.closePath();
854+
graphics.fillPath();
855+
}
856+
}
857+
}
762858
},
763859

764860
/**

0 commit comments

Comments
 (0)