Skip to content

Commit

Permalink
Implemented corner collision pulling for issue #3. Updated readme for…
Browse files Browse the repository at this point in the history
… issue #6.
  • Loading branch information
hexus committed Jun 26, 2016
1 parent 86c6886 commit 6207f71
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 11 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[Phaser](http://phaser.io)'s [Arcade
Physics](http://phaser.io/examples/v2/category/arcade-physics) engine.

## Demo
## [Demo](http://hexus.github.io/phaser-arcade-slopes)

Check out the **[demo](http://hexus.github.io/phaser-arcade-slopes)**!

Expand All @@ -17,6 +17,16 @@ Check out the **[demo](http://hexus.github.io/phaser-arcade-slopes)**!
- Works with sprites :rocket:, groups :busts_in_silhouette: and particle
emitters :sparkles:

## Compatibility

This is a simple compatibility chart for different versions of the plugin. It
also conveniently provides links to each version.

| Phaser Version | Arcade Slopes Version |
|-----------------|---------------------------------------------------------------------|
| v2.4.1 - v2.4.8 | [v0.1.0](https://github.com/hexus/phaser-arcade-slopes/tree/v0.1.0) |
| v2.5.0 | [v0.1.1](https://github.com/hexus/phaser-arcade-slopes/tree/v0.1.1) |

## Usage

Grab a copy of the latest release from the **dist** directory in this repository
Expand Down Expand Up @@ -78,11 +88,14 @@ game.slopes.convertTilemapLayer(ground, {
```

Now you need to enable slopes for any game entities you want to collide against
the tilemap.
the tilemap. *For sprites, make sure you call `game.slopes.enable(sprite)` after
any changes to the **size** of the physics body.*

```js
game.slopes.enable(player);
game.slopes.enable(emitter); // Call this after emitter.makeParticles()!
game.physics.arcade.enable(player);

game.slopes.enable(player); // Call this after any changes to the body's size
game.slopes.enable(emitter); // Call this after emitter.makeParticles()
```

Now you can collide your player against the tilemap in the
Expand Down Expand Up @@ -125,6 +138,9 @@ learn than to build something yourself?
- [x] Friction
- [x] `body.slope` properties for friction, sticky slopes, preferred
separation axis and last overlap response
- [x] v0.1.1
- [x] Phaser 2.4.9/2.5.0 compatibility
- [x] Corner collision pulling
- [ ] v0.2.0
- [ ] Arcade Slopes tilesheet
- [ ] Premade tilesheets
Expand All @@ -138,6 +154,7 @@ learn than to build something yourself?
- [ ] Tile face properties
- [ ] Tile polygons
- [ ] [Metroid collision solver](https://github.com/geselle-jan/Metroid/commit/9c213e9f5779df1dcd6f7d2bed2a9b676a9e3c6b#diff-467b4e6069f6692511fc5e60f3c426cc)
- [ ] Clearer yet more in-depth readme
- [ ] v0.3.0
- [ ] Custom SAT.js implementation that can prevent internal edge collisions
([like this](http://www.wildbunny.co.uk/blog/2012/10/31/2d-polygonal-collision-detection-and-internal-edges/comment-page-1/#comment-1978))
Expand Down
45 changes: 38 additions & 7 deletions src/ArcadeSlopes/SatSolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.snap = function (body, tiles) {
* @return {boolean} - Whether the body was pulled.
*/
Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.pull = function (body, response) {
if (!body.slopes.pullUp && !body.slopes.pullDown && !body.slopes.pullLeft && !body.slopes.pullRight) {
if (!body.slopes.pullUp && !body.slopes.pullDown && !body.slopes.pullLeft && !body.slopes.pullRight &&
!body.slopes.pullTopLeft && !body.slopes.pullTopRight && !body.slopes.pullBottomLeft && !body.slopes.pullBottomRight) {
return false;
}

Expand All @@ -373,32 +374,62 @@ Phaser.Plugin.ArcadeSlopes.SatSolver.prototype.pull = function (body, response)
}

if (body.slopes.pullDown && overlapN.y > 0) {
// Scale it by the configured amount
pullDown = overlapN.clone().scale(body.slopes.pullDown);

// Apply it to the body velocity
body.velocity.x += pullDown.x;
body.velocity.y += pullDown.y;

return true;
}

if (body.slopes.pullLeft && overlapN.x < 0) {
// Scale it by the configured amount
pullLeft = overlapN.clone().scale(body.slopes.pullLeft);

// Apply it to the body velocity
body.velocity.x += pullLeft.x;
body.velocity.y += pullLeft.y;

return true;
}

if (body.slopes.pullRight && overlapN.x > 0) {
// Scale it by the configured amount
pullRight = overlapN.clone().scale(body.slopes.pullRight);

// Apply it to the body velocity
body.velocity.x += pullRight.x;
body.velocity.y += pullRight.y;

return true;
}

if (body.slopes.pullTopLeft && overlapN.x < 0 && overlapN.y < 0) {
pullUp = overlapN.clone().scale(body.slopes.pullTopLeft);

body.velocity.x += pullUp.x;
body.velocity.y += pullUp.y;

return true;
}

if (body.slopes.pullTopRight && overlapN.x > 0 && overlapN.y < 0) {
pullDown = overlapN.clone().scale(body.slopes.pullTopRight);

body.velocity.x += pullDown.x;
body.velocity.y += pullDown.y;

return true;
}

if (body.slopes.pullBottomLeft && overlapN.x < 0 && overlapN.y > 0) {
pullLeft = overlapN.clone().scale(body.slopes.pullBottomLeft);

body.velocity.x += pullLeft.x;
body.velocity.y += pullLeft.y;

return true;
}

if (body.slopes.pullBottomRight && overlapN.x > 0 && overlapN.y > 0) {
pullRight = overlapN.clone().scale(body.slopes.pullBottomRight);

body.velocity.x += pullRight.x;
body.velocity.y += pullRight.y;

Expand Down

0 comments on commit 6207f71

Please sign in to comment.