Skip to content

Commit d009b8d

Browse files
committed
Circle vs. Circle rebound code started.
1 parent cc5361d commit d009b8d

1 file changed

Lines changed: 59 additions & 0 deletions

File tree

src/physics/arcade/World.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,12 @@ Phaser.Physics.Arcade.prototype = {
947947
return false;
948948
}
949949

950+
// Circle vs. Circle quick bail out
951+
if (body1.isCircle && body2.isCircle)
952+
{
953+
return this.separateCircle(body1, body2, overlapOnly);
954+
}
955+
950956
var resultX = false;
951957
var resultY = false;
952958

@@ -1055,6 +1061,59 @@ Phaser.Physics.Arcade.prototype = {
10551061

10561062
},
10571063

1064+
separateCircle: function (body1, body2, overlapOnly) {
1065+
1066+
// Get the angle between the two circles
1067+
var dx = body2.center.x - body1.center.x;
1068+
var dy = body2.center.y - body1.center.y;
1069+
1070+
var angle = Math.atan2(dy, dx);
1071+
var sin = Math.sin(angle);
1072+
var cos = Math.cos(angle);
1073+
1074+
var x0 = 0;
1075+
var y0 = 0;
1076+
1077+
var x1 = dx * cos + dy * sin;
1078+
var y1 = dy * cos - dx * sin;
1079+
1080+
var vx0 = body1.velocity.x * cos + body1.velocity.y * sin;
1081+
var vy0 = body1.velocity.y * cos - body1.velocity.x * sin;
1082+
1083+
var vx1 = body2.velocity.x * cos + body2.velocity.y * sin;
1084+
var vy1 = body2.velocity.y * cos - body2.velocity.x * sin;
1085+
1086+
var vxTotal = vx0 - vx1;
1087+
1088+
vx0 = ((body1.mass - body2.mass) * vx0 + 2 * body2.mass * vx1) / (body1.mass + body2.mass);
1089+
vx1 = vxTotal + vx0;
1090+
1091+
x0 += vx0;
1092+
x1 += vx1;
1093+
1094+
var x0Final = x0 * cos - y0 * sin;
1095+
var y0Final = y0 * cos + x0 * sin;
1096+
var x1Final = x1 * cos - y1 * sin;
1097+
var y1Final = y1 * cos + x1 * sin;
1098+
1099+
body2.x = body1.x + x1Final;
1100+
body2.y = body1.y + y1Final;
1101+
1102+
body1.x = body1.x + x0Final;
1103+
body1.y = body1.y + y0Final;
1104+
1105+
console.log('final', x0Final, y0Final, x1Final, y1Final);
1106+
1107+
body1.velocity.x = vx0 * cos - vy0 * sin;
1108+
body1.velocity.y = vy0 * cos + vx0 * sin;
1109+
1110+
body2.velocity.x = vx1 * cos - vy1 * sin;
1111+
body2.velocity.y = vy1 * cos + vx1 * sin;
1112+
1113+
return true;
1114+
1115+
},
1116+
10581117
/**
10591118
* The core separation function to separate two physics bodies on the x axis.
10601119
*

0 commit comments

Comments
 (0)