Skip to content

Commit 4fa109d

Browse files
author
Omar Shehata
committed
Added joint debug rendering to Matter Physics postUpdate
1 parent 2cd7da0 commit 4fa109d

1 file changed

Lines changed: 97 additions & 3 deletions

File tree

src/physics/matter-js/World.js

Lines changed: 97 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
@@ -164,7 +166,9 @@ var World = new Class({
164166
debugShowVelocity: GetValue(config, 'debugShowVelocity', true),
165167
bodyDebugColor: GetValue(config, 'debugBodyColor', 0xff00ff),
166168
staticBodyDebugColor: GetValue(config, 'debugBodyColor', 0x0000ff),
167-
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00)
169+
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00),
170+
debugShowJoint: GetValue(config, 'debugShowJoint', true),
171+
jointDebugColor: GetValue(config, 'debugJointColor', 0x000000)
168172
};
169173

170174
if (this.drawDebug)
@@ -630,7 +634,7 @@ var World = new Class({
630634
graphics.lineStyle(1, this.defaults.bodyDebugColor);
631635
graphics.beginPath();
632636

633-
for (var i = 0; i < bodies.length; i++)
637+
for (let i = 0; i < bodies.length; i++)
634638
{
635639
if (!bodies[i].render.visible)
636640
{
@@ -639,7 +643,7 @@ var World = new Class({
639643

640644
// Handle drawing both single bodies and compound bodies. If compound, draw both the
641645
// convex hull (first part) and the rest of the bodies.
642-
for (var j = 0; j < bodies[i].parts.length; j++)
646+
for (let j = 0; j < bodies[i].parts.length; j++)
643647
{
644648
var body = bodies[i].parts[j];
645649

@@ -659,6 +663,96 @@ var World = new Class({
659663
}
660664

661665
graphics.closePath();
666+
667+
if(this.defaults.debugShowJoint)
668+
{
669+
graphics.lineStyle(2, this.defaults.jointDebugColor);
670+
671+
// Render constraints
672+
var constraints = Composite.allConstraints(this.localWorld);
673+
for (let i = 0; i < constraints.length; i++)
674+
{
675+
var constraint = constraints[i];
676+
677+
if (!constraint.render.visible || !constraint.pointA || !constraint.pointB)
678+
{ continue; }
679+
680+
if (constraint.render.lineWidth)
681+
{
682+
graphics.lineStyle(constraint.render.lineWidth, Common.colorToNumber(constraint.render.strokeStyle));
683+
}
684+
685+
var bodyA = constraint.bodyA,
686+
bodyB = constraint.bodyB,
687+
start,
688+
end;
689+
690+
if (bodyA)
691+
{
692+
start = Vector.add(bodyA.position, constraint.pointA);
693+
}
694+
else
695+
{
696+
start = constraint.pointA;
697+
}
698+
699+
if (constraint.render.type === 'pin')
700+
{
701+
graphics.beginPath();
702+
graphics.arc(start.x, start.y, 3, 0, 2 * Math.PI);
703+
graphics.closePath();
704+
}
705+
else
706+
{
707+
if (bodyB)
708+
{
709+
end = Vector.add(bodyB.position, constraint.pointB);
710+
}
711+
else
712+
{
713+
end = constraint.pointB;
714+
}
715+
716+
graphics.beginPath();
717+
graphics.moveTo(start.x, start.y);
718+
719+
if (constraint.render.type === 'spring')
720+
{
721+
var delta = Vector.sub(end, start),
722+
normal = Vector.perp(Vector.normalise(delta)),
723+
coils = Math.ceil(Common.clamp(constraint.length / 5, 12, 20)),
724+
offset;
725+
726+
for (let j = 1; j < coils; j += 1)
727+
{
728+
offset = j % 2 === 0 ? 1 : -1;
729+
730+
graphics.lineTo(
731+
start.x + delta.x * (j / coils) + normal.x * offset * 4,
732+
start.y + delta.y * (j / coils) + normal.y * offset * 4
733+
);
734+
}
735+
}
736+
737+
graphics.lineTo(end.x, end.y);
738+
}
739+
740+
if (constraint.render.lineWidth)
741+
{
742+
graphics.strokePath();
743+
}
744+
745+
if (constraint.render.anchors)
746+
{
747+
graphics.fillStyle(Common.colorToNumber(constraint.render.strokeStyle));
748+
graphics.beginPath();
749+
graphics.arc(start.x, start.y, 6, 0, 2 * Math.PI);
750+
graphics.arc(end.x, end.y, 6, 0, 2 * Math.PI);
751+
graphics.closePath();
752+
graphics.fillPath();
753+
}
754+
}
755+
}
662756
},
663757

664758
/**

0 commit comments

Comments
 (0)