Skip to content

Commit ac89d1a

Browse files
committed
Arcade Physics Body has a new property worldBounce. This controls the elasticity of the Body specifically when colliding with the World bounds. By default this property is null, in which case Body.bounce is used instead. Set this property to a Phaser.Point object in order to enable a World bounds specific bounce value (thanks @VitaZheltyakov phaserjs#2465)
1 parent f9994fa commit ac89d1a

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ You can read all about the philosophy behind Lazer [here](http://phaser.io/news/
341341

342342
* BitmapData.copy, and by extension any method that uses it, including BitmapData.draw, drawGroup and drawFull, now all support drawing RenderTexture objects. These can either be passed directly, or be the textures of Sprites, such as from a call to generateTexture.
343343
* Arcade Physics has had a new `world` argument added to the following functions: `distanceBetween`, `distanceToXY`, `distanceToPointer`, `angleBetween`, `angleToXY` and `angleToPointer`. The argument (which is false by default), when enabled will calculate the angles or distances based on the Game Objects `world` property, instead of its `x` and `y` properties. This allows it to work for objects that are placed in offset Groups, or are children of other display objects (thanks @Skeptron for the thread #2463)
344+
* Arcade Physics Body has a new property `worldBounce`. This controls the elasticity of the Body specifically when colliding with the World bounds. By default this property is `null`, in which case Body.bounce is used instead. Set this property to a Phaser.Point object in order to enable a World bounds specific bounce value (thanks @VitaZheltyakov #2465)
344345

345346
### Updates
346347

src/physics/arcade/Body.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ Phaser.Physics.Arcade.Body = function (sprite) {
161161
*/
162162
this.bounce = new Phaser.Point();
163163

164+
/**
165+
* The elasticity of the Body when colliding with the World bounds.
166+
* By default this property is `null`, in which case `Body.bounce` is used instead. Set this property
167+
* to a Phaser.Point object in order to enable a World bounds specific bounce value.
168+
* @property {Phaser.Point} worldBounce
169+
*/
170+
this.worldBounce = null;
171+
164172
/**
165173
* @property {Phaser.Point} maxVelocity - The maximum velocity in pixels per second sq. that the Body can reach.
166174
* @default
@@ -581,29 +589,32 @@ Phaser.Physics.Arcade.Body.prototype = {
581589
var bounds = this.game.physics.arcade.bounds;
582590
var check = this.game.physics.arcade.checkCollision;
583591

592+
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
593+
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
594+
584595
if (pos.x < bounds.x && check.left)
585596
{
586597
pos.x = bounds.x;
587-
this.velocity.x *= -this.bounce.x;
598+
this.velocity.x *= bx;
588599
this.blocked.left = true;
589600
}
590601
else if (this.right > bounds.right && check.right)
591602
{
592603
pos.x = bounds.right - this.width;
593-
this.velocity.x *= -this.bounce.x;
604+
this.velocity.x *= bx;
594605
this.blocked.right = true;
595606
}
596607

597608
if (pos.y < bounds.y && check.up)
598609
{
599610
pos.y = bounds.y;
600-
this.velocity.y *= -this.bounce.y;
611+
this.velocity.y *= by;
601612
this.blocked.up = true;
602613
}
603614
else if (this.bottom > bounds.bottom && check.down)
604615
{
605616
pos.y = bounds.bottom - this.height;
606-
this.velocity.y *= -this.bounce.y;
617+
this.velocity.y *= by;
607618
this.blocked.down = true;
608619
}
609620

typescript/phaser.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,6 +2937,7 @@ declare module Phaser {
29372937
type: number;
29382938
wasTouching: FaceChoices;
29392939
width: number;
2940+
worldBounce: Phaser.Point;
29402941
velocity: Phaser.Point;
29412942
x: number;
29422943
y: number;

0 commit comments

Comments
 (0)