Skip to content

Commit 876b9a9

Browse files
authored
Merge pull request #17 from hexus/circle
Initial circular physics body support
2 parents 4d9c905 + 28297aa commit 876b9a9

File tree

3 files changed

+81
-57
lines changed

3 files changed

+81
-57
lines changed

src/ArcadeSlopes/Facade.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,22 @@ Phaser.Plugin.ArcadeSlopes.Facade.prototype.enable = function (object) {
7777
*/
7878
Phaser.Plugin.ArcadeSlopes.Facade.prototype.enableBody = function (body) {
7979
// Create an SAT polygon from the body's bounding box
80-
body.polygon = new SAT.Box(
81-
new SAT.Vector(body.x, body.y),
82-
body.width,
83-
body.height
84-
).toPolygon();
80+
// TODO: Rename body.polygon to body.shape or body.slopes.shape
81+
if (body.isCircle) {
82+
body.polygon = new SAT.Circle(
83+
new SAT.Vector(
84+
body.x + body.halfWidth,
85+
body.y + body.halfHeight
86+
),
87+
body.radius
88+
);
89+
} else {
90+
body.polygon = new SAT.Box(
91+
new SAT.Vector(body.x, body.y),
92+
body.width,
93+
body.height
94+
).toPolygon();
95+
}
8596

8697
// Attach a new set of properties that configure the body's interaction
8798
// with sloped tiles (TODO: Formalize as a class?)
@@ -92,6 +103,10 @@ Phaser.Plugin.ArcadeSlopes.Facade.prototype.enableBody = function (body) {
92103
pullDown: 0,
93104
pullLeft: 0,
94105
pullRight: 0,
106+
pullTopLeft: 0,
107+
pullTopRight: 0,
108+
pullBottomLeft: 0,
109+
pullBottomRight: 0,
95110
sat: {
96111
response: null,
97112
},

src/ArcadeSlopes/SatSolver.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.snap = function (body, tiles) {
348348
/**
349349
* Pull the body into a collision response based on its slopes options.
350350
*
351-
* TODO: Refactor; don't return after any condition is met, accumulate values
352-
* into a single SAT.Vector and apply at the end.
351+
* TODO: Don't return after any condition is met, accumulate values into a
352+
* single SAT.Vector and apply at the end.
353353
*
354354
* @method Phaser.Plugin.ArcadeSlopes.SatSolver#pull
355355
* @param {Phaser.Physics.Arcade.Body} body - The physics body.
@@ -500,14 +500,19 @@ Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.collide = function (i, body, tile
500500
body.polygon.pos.x = body.x;
501501
body.polygon.pos.y = body.y;
502502

503+
if (body.isCircle) {
504+
body.polygon.pos.x += body.halfWidth;
505+
body.polygon.pos.y += body.halfHeight;
506+
}
507+
503508
// Update the tile polygon position
504509
tile.slope.polygon.pos.x = tile.worldX;
505510
tile.slope.polygon.pos.y = tile.worldY;
506511

507512
var response = new SAT.Response();
508513

509-
// Nothing more to do here if there isn't an overlap
510-
if (!SAT.testPolygonPolygon(body.polygon, tile.slope.polygon, response)) {
514+
// Test for an overlap and bail if there isn't one
515+
if ((body.isCircle && !SAT.testCirclePolygon(body.polygon, tile.slope.polygon, response)) || (!body.isCircle && !SAT.testPolygonPolygon(body.polygon, tile.slope.polygon, response))) {
511516
return false;
512517
}
513518

@@ -611,7 +616,7 @@ Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.shouldSeparate = function (i, bod
611616
return false;
612617
}
613618

614-
if (!this.options.restrain) {
619+
if (!this.options.restrain || body.isCircle) {
615620
return true;
616621
}
617622

src/ArcadeSlopes/TileSlopeFactory.js

+51-47
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,30 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.prototype.calculateEdges = function
182182

183183
if (above && above.hasOwnProperty('slope')) {
184184
tile.slope.edges.top = this.compareEdges(tile.slope.edges.top, above.slope.edges.bottom);
185+
tile.collideUp = tile.slope.edges.top !== Phaser.Plugin.ArcadeSlopes.TileSlope.EMPTY;
185186
}
186187

187188
if (below && below.hasOwnProperty('slope')) {
188189
tile.slope.edges.bottom = this.compareEdges(tile.slope.edges.bottom, below.slope.edges.top);
190+
tile.collideDown = tile.slope.edges.bottom !== Phaser.Plugin.ArcadeSlopes.TileSlope.EMPTY;
189191
}
190192

191193
if (left && left.hasOwnProperty('slope')) {
192194
tile.slope.edges.left = this.compareEdges(tile.slope.edges.left, left.slope.edges.right);
195+
tile.collideLeft = tile.slope.edges.left !== Phaser.Plugin.ArcadeSlopes.TileSlope.EMPTY;
193196
}
194197

195198
if (right && right.hasOwnProperty('slope')) {
196199
tile.slope.edges.right = this.compareEdges(tile.slope.edges.right, right.slope.edges.left);
200+
tile.collideRight = tile.slope.edges.right !== Phaser.Plugin.ArcadeSlopes.TileSlope.EMPTY;
197201
}
198202
}
199203
}
200204
}
201205
};
202206

203207
/**
204-
* Resolve the given flags of two shared edges.
208+
* Resolve the given flags of two shared tile edges.
205209
*
206210
* Returns the new flag to use for the first edge after comparing it with the
207211
* second edge.
@@ -572,10 +576,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterBottomRightLow = functi
572576
*/
573577
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterBottomRightHigh = function (type, tile) {
574578
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
575-
new SAT.Vector(tile.width, 0), // Top right
576-
new SAT.Vector(0, tile.height / 2), // Center left
577-
new SAT.Vector(0, tile.height), // Bottom left
578-
new SAT.Vector(tile.width, tile.height) // Bottom right
579+
new SAT.Vector(0, tile.height / 2), // Center left
580+
new SAT.Vector(tile.width, 0), // Top right
581+
new SAT.Vector(tile.width, tile.height), // Bottom right
582+
new SAT.Vector(0, tile.height) // Bottom left
579583
]);
580584

581585
var line = new Phaser.Line(tile.left, tile.bottom, tile.right, tile.top + tile.height / 2);
@@ -602,10 +606,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterBottomRightHigh = funct
602606
*/
603607
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftBottomLow = function (type, tile) {
604608
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
605-
new SAT.Vector(0, 0),
606-
new SAT.Vector(tile.width / 2, 0),
607-
new SAT.Vector(tile.width, tile.height),
608-
new SAT.Vector(0, tile.height)
609+
new SAT.Vector(0, 0), // Top left
610+
new SAT.Vector(tile.width / 2, 0), // Top center
611+
new SAT.Vector(tile.width, tile.height), // Bottom right
612+
new SAT.Vector(0, tile.height) // Bottom left
609613
]);
610614

611615
var line = new Phaser.Line(tile.left + tile.width / 2, tile.top, tile.right, tile.bottom);
@@ -631,9 +635,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftBottomLow = functio
631635
*/
632636
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftBottomHigh = function (type, tile) {
633637
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
634-
new SAT.Vector(0, 0),
635-
new SAT.Vector(tile.width / 2, tile.height),
636-
new SAT.Vector(0, tile.height)
638+
new SAT.Vector(0, 0), // Top left
639+
new SAT.Vector(tile.width / 2, tile.height), // Bottom center
640+
new SAT.Vector(0, tile.height) // Bottom left
637641
]);
638642

639643
var line = new Phaser.Line(tile.left, tile.top, tile.left + tile.width / 2, tile.bottom);
@@ -661,10 +665,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftBottomHigh = functi
661665
*/
662666
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightBottomLow = function (type, tile) {
663667
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
664-
new SAT.Vector(tile.width / 2, 0),
665-
new SAT.Vector(tile.width, 0),
666-
new SAT.Vector(tile.width, tile.height),
667-
new SAT.Vector(0, tile.height)
668+
new SAT.Vector(tile.width / 2, 0), // Top center
669+
new SAT.Vector(tile.width, 0), // Top right
670+
new SAT.Vector(tile.width, tile.height), // Bottom right
671+
new SAT.Vector(0, tile.height) // Bottom left
668672
]);
669673

670674
var line = new Phaser.Line(tile.left, tile.bottom, tile.left + tile.width / 2, tile.top);
@@ -691,9 +695,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightBottomLow = functi
691695
*/
692696
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightBottomHigh = function (type, tile) {
693697
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
694-
new SAT.Vector(tile.width, 0),
695-
new SAT.Vector(tile.width, tile.height),
696-
new SAT.Vector(tile.width / 2, tile.height)
698+
new SAT.Vector(tile.width, 0), // Top right
699+
new SAT.Vector(tile.width, tile.height), // Bottom right
700+
new SAT.Vector(tile.width / 2, tile.height) // Bottom center
697701
]);
698702

699703
var line = new Phaser.Line(tile.left + tile.width / 2, tile.bottom, tile.right, tile.top);
@@ -720,9 +724,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightBottomHigh = funct
720724
*/
721725
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftTopLow = function (type, tile) {
722726
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
723-
new SAT.Vector(0, 0),
724-
new SAT.Vector(tile.width / 2, 0),
725-
new SAT.Vector(0, tile.height)
727+
new SAT.Vector(0, 0), // Top left
728+
new SAT.Vector(tile.width / 2, 0), // Top center
729+
new SAT.Vector(0, tile.height) // Bottom left
726730
]);
727731

728732
var line = new Phaser.Line(0, tile.height, tile.width / 2, 0);
@@ -749,10 +753,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftTopLow = function (
749753
*/
750754
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftTopHigh = function (type, tile) {
751755
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
752-
new SAT.Vector(0, 0),
753-
new SAT.Vector(tile.width, 0),
754-
new SAT.Vector(tile.width / 2, tile.height),
755-
new SAT.Vector(0, tile.height)
756+
new SAT.Vector(0, 0), // Top left
757+
new SAT.Vector(tile.width, 0), // Top right
758+
new SAT.Vector(tile.width / 2, tile.height), // Bottom center
759+
new SAT.Vector(0, tile.height) // Bottom left
756760
]);
757761

758762
var line = new Phaser.Line(tile.left + tile.width / 2, tile.bottom, tile.right, tile.bottom);
@@ -778,9 +782,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterLeftTopHigh = function
778782
*/
779783
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightTopLow = function (type, tile) {
780784
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
781-
new SAT.Vector(tile.width / 2, 0),
782-
new SAT.Vector(tile.width, 0),
783-
new SAT.Vector(tile.width, tile.height)
785+
new SAT.Vector(tile.width / 2, 0), // Top center
786+
new SAT.Vector(tile.width, 0), // Top right
787+
new SAT.Vector(tile.width, tile.height) // Bottom right
784788
]);
785789

786790
var line = new Phaser.Line(tile.left + tile.width / 2, tile.top, tile.right, tile.bottom);
@@ -807,10 +811,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightTopLow = function
807811
*/
808812
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightTopHigh = function (type, tile) {
809813
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
810-
new SAT.Vector(0, 0),
811-
new SAT.Vector(tile.width, 0),
812-
new SAT.Vector(tile.width, tile.height),
813-
new SAT.Vector(tile.width / 2, tile.height)
814+
new SAT.Vector(0, 0), // Top left
815+
new SAT.Vector(tile.width, 0), // Top right
816+
new SAT.Vector(tile.width, tile.height), // Bottom right
817+
new SAT.Vector(tile.width / 2, tile.height) // Center right
814818
]);
815819

816820
var line = new Phaser.Line(tile.left, tile.top, tile.left + tile.width / 2, tile.bottom);
@@ -836,9 +840,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterRightTopHigh = function
836840
*/
837841
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopLeftLow = function (type, tile) {
838842
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
839-
new SAT.Vector(0, 0),
840-
new SAT.Vector(tile.width, 0),
841-
new SAT.Vector(0, tile.height / 2)
843+
new SAT.Vector(0, 0), // Top left
844+
new SAT.Vector(tile.width, 0), // Top right
845+
new SAT.Vector(0, tile.height / 2) // Center left
842846
]);
843847

844848
var line = new Phaser.Line(tile.left, tile.top + tile.height / 2, tile.right, tile.top);
@@ -865,10 +869,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopLeftLow = function (
865869
*/
866870
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopLeftHigh = function (type, tile) {
867871
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
868-
new SAT.Vector(0, 0),
869-
new SAT.Vector(tile.width, 0),
870-
new SAT.Vector(tile.width, tile.height / 2),
871-
new SAT.Vector(0, tile.height)
872+
new SAT.Vector(0, 0), // Top left
873+
new SAT.Vector(tile.width, 0), // Top right
874+
new SAT.Vector(tile.width, tile.height / 2), // Right center
875+
new SAT.Vector(0, tile.height) // Bottom left
872876
]);
873877

874878
var line = new Phaser.Line(tile.left, tile.bottom, tile.right, tile.top + tile.height / 2);
@@ -894,9 +898,9 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopLeftHigh = function
894898
*/
895899
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopRightLow = function (type, tile) {
896900
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
897-
new SAT.Vector(0, 0),
898-
new SAT.Vector(tile.width, 0),
899-
new SAT.Vector(tile.width, tile.height / 2)
901+
new SAT.Vector(0, 0), // Top left
902+
new SAT.Vector(tile.width, 0), // Top right
903+
new SAT.Vector(tile.width, tile.height / 2) // Right center
900904
]);
901905

902906
var line = new Phaser.Line(tile.left, tile.top, tile.right, tile.top + tile.height / 2);
@@ -923,10 +927,10 @@ Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopRightLow = function
923927
*/
924928
Phaser.Plugin.ArcadeSlopes.TileSlopeFactory.createQuarterTopRightHigh = function (type, tile) {
925929
var polygon = new SAT.Polygon(new SAT.Vector(tile.worldX, tile.worldY), [
926-
new SAT.Vector(0, 0),
927-
new SAT.Vector(tile.width, 0),
928-
new SAT.Vector(tile.width, tile.height),
929-
new SAT.Vector(0, tile.height / 2)
930+
new SAT.Vector(0, 0), // Top left
931+
new SAT.Vector(tile.width, 0), // Top right
932+
new SAT.Vector(tile.width, tile.height), // Bottom right
933+
new SAT.Vector(0, tile.height / 2) // Left center
930934
]);
931935

932936
var line = new Phaser.Line(tile.left, tile.top + tile.height / 2, tile.right, tile.top + tile.height);

0 commit comments

Comments
 (0)