Skip to content

Commit b7ff2c0

Browse files
committed
FIX phaserjs#4751 - Physics.Arcade.World.separateCircle - weird effects when bounce=0
1 parent 6a40d6f commit b7ff2c0

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

src/physics/arcade/World.js

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ var World = new Class({
323323
*
324324
* If you have a large number of dynamic bodies in your world then it may be best to
325325
* disable the use of the RTree by setting this property to `false` in the physics config.
326-
*
326+
*
327327
* The number it can cope with depends on browser and device, but a conservative estimate
328328
* of around 5,000 bodies should be considered the max before disabling it.
329329
*
@@ -1049,7 +1049,7 @@ var World = new Class({
10491049
for (i = 0; i < len; i++)
10501050
{
10511051
body = bodies[i];
1052-
1052+
10531053
if (body.enable)
10541054
{
10551055
body.postUpdate();
@@ -1500,23 +1500,23 @@ var World = new Class({
15001500
return (overlap !== 0);
15011501
}
15021502

1503-
var dx = body1.position.x - body2.position.x;
1504-
var dy = body1.position.y - body2.position.y;
1503+
var dx = body1.center.x - body2.center.x;
1504+
var dy = body1.center.y - body2.center.y;
15051505
var d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2));
1506-
var nx = ((body2.position.x - body1.position.x) / d) || 0;
1507-
var ny = ((body2.position.y - body1.position.y) / d) || 0;
1506+
var nx = ((body2.center.x - body1.center.x) / d) || 0;
1507+
var ny = ((body2.center.y - body1.center.y) / d) || 0;
15081508
var p = 2 * (body1.velocity.x * nx + body1.velocity.y * ny - body2.velocity.x * nx - body2.velocity.y * ny) / (body1.mass + body2.mass);
15091509

15101510
if (!body1.immovable)
15111511
{
1512-
body1.velocity.x = (body1.velocity.x - p * body1.mass * nx) * body1.bounce.x;
1513-
body1.velocity.y = (body1.velocity.y - p * body1.mass * ny) * body1.bounce.y;
1512+
body1.velocity.x = (body1.velocity.x - p * body1.mass * nx);
1513+
body1.velocity.y = (body1.velocity.y - p * body1.mass * ny);
15141514
}
15151515

15161516
if (!body2.immovable)
15171517
{
1518-
body2.velocity.x = (body2.velocity.x + p * body2.mass * nx) * body2.bounce.x;
1519-
body2.velocity.y = (body2.velocity.y + p * body2.mass * ny) * body2.bounce.y;
1518+
body2.velocity.x = (body2.velocity.x + p * body2.mass * nx);
1519+
body2.velocity.y = (body2.velocity.y + p * body2.mass * ny);
15201520
}
15211521

15221522
var dvx = body2.velocity.x - body1.velocity.x;
@@ -1525,6 +1525,16 @@ var World = new Class({
15251525

15261526
var delta = this._frameTime;
15271527

1528+
body1.velocity.x *= body1.bounce.x;
1529+
body1.velocity.y *= body1.bounce.y;
1530+
body2.velocity.x *= body2.bounce.x;
1531+
body2.velocity.y *= body2.bounce.y;
1532+
1533+
if (!body1.immovable && !body2.immovable)
1534+
{
1535+
overlap /= 2;
1536+
}
1537+
15281538
if (!body1.immovable)
15291539
{
15301540
body1.x += (body1.velocity.x * delta) - overlap * Math.cos(angleCollision);
@@ -1647,7 +1657,7 @@ var World = new Class({
16471657
* If two Groups or arrays are passed, each member of one will be tested against each member of the other.
16481658
*
16491659
* If **only** one Group is passed (as `object1`), each member of the Group will be collided against the other members.
1650-
*
1660+
*
16511661
* If **only** one Array is passed, the array is iterated and every element in it is tested against the others.
16521662
*
16531663
* Two callbacks can be provided. The `collideCallback` is invoked if a collision occurs and the two colliding
@@ -2026,15 +2036,15 @@ var World = new Class({
20262036

20272037
/**
20282038
* This advanced method is specifically for testing for collision between a single Sprite and an array of Tile objects.
2029-
*
2039+
*
20302040
* You should generally use the `collide` method instead, with a Sprite vs. a Tilemap Layer, as that will perform
20312041
* tile filtering and culling for you, as well as handle the interesting face collision automatically.
2032-
*
2042+
*
20332043
* This method is offered for those who would like to check for collision with specific Tiles in a layer, without
20342044
* having to set any collision attributes on the tiles in question. This allows you to perform quick dynamic collisions
20352045
* on small sets of Tiles. As such, no culling or checks are made to the array of Tiles given to this method,
20362046
* you should filter them before passing them to this method.
2037-
*
2047+
*
20382048
* Important: Use of this method skips the `interesting faces` system that Tilemap Layers use. This means if you have
20392049
* say a row or column of tiles, and you jump into, or walk over them, it's possible to get stuck on the edges of the
20402050
* tiles as the interesting face calculations are skipped. However, for quick-fire small collision set tests on
@@ -2066,10 +2076,10 @@ var World = new Class({
20662076

20672077
/**
20682078
* This advanced method is specifically for testing for overlaps between a single Sprite and an array of Tile objects.
2069-
*
2079+
*
20702080
* You should generally use the `overlap` method instead, with a Sprite vs. a Tilemap Layer, as that will perform
20712081
* tile filtering and culling for you, as well as handle the interesting face collision automatically.
2072-
*
2082+
*
20732083
* This method is offered for those who would like to check for overlaps with specific Tiles in a layer, without
20742084
* having to set any collision attributes on the tiles in question. This allows you to perform quick dynamic overlap
20752085
* tests on small sets of Tiles. As such, no culling or checks are made to the array of Tiles given to this method,

0 commit comments

Comments
 (0)