Skip to content

Commit 6531552

Browse files
committed
Merge pull request phaserjs#1905 from standardgaussian/aabbninja
AABB vs. AABB collision in Ninja
2 parents 2d3b1ee + b8710d7 commit 6531552

1 file changed

Lines changed: 29 additions & 36 deletions

File tree

src/physics/ninja/AABB.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,17 @@ Phaser.Physics.Ninja.AABB.prototype = {
124124
this.oldpos.set(px, py);
125125

126126
},
127-
127+
128128
/**
129-
* Process a world collision and apply the resulting forces.
130-
*
131-
* @method Phaser.Physics.Ninja.AABB#reportCollisionVsWorld
132-
* @param {number} px - The tangent velocity
133-
* @param {number} py - The tangent velocity
134-
* @param {number} dx - Collision normal
135-
* @param {number} dy - Collision normal
136-
* @param {number} obj - Object this AABB collided with
137-
*/
138-
reportCollisionVsWorld: function (px, py, dx, dy) {
129+
* Process a collision partner-agnostic collision response and apply the resulting forces.
130+
*
131+
* @method Phaser.Phyiscs.Ninja.AABB#reportCollision
132+
* @param {number} px - The tangent velocity
133+
* @param {number} py - The tangent velocity
134+
* @param {number} dx - Collision normal
135+
* @param {number} dy - Collision normal
136+
*/
137+
reportCollision: function(px, py, dx, dy) {
139138

140139
var p = this.pos;
141140
var o = this.oldpos;
@@ -150,7 +149,7 @@ Phaser.Physics.Ninja.AABB.prototype = {
150149

151150
var ny = dp * dy; //nx,ny is normal velocity
152151

153-
var tx = vx - nx; //px,py is tangent velocity
152+
var tx = vx - nx; //tx,ty is tangent velocity
154153
var ty = vy - ny;
155154

156155
// We only want to apply collision response forces if the object is travelling into, and not out of, the collision
@@ -200,6 +199,20 @@ Phaser.Physics.Ninja.AABB.prototype = {
200199

201200
},
202201

202+
/**
203+
* Process a world collision and apply the resulting forces.
204+
*
205+
* @method Phaser.Physics.Ninja.AABB#reportCollisionVsWorld
206+
* @param {number} px - The tangent velocity
207+
* @param {number} py - The tangent velocity
208+
* @param {number} dx - Collision normal
209+
* @param {number} dy - Collision normal
210+
*/
211+
reportCollisionVsWorld: function (px, py, dx, dy) {
212+
213+
this.reportCollision(px,py,dx,dy);
214+
},
215+
203216
reverse: function () {
204217

205218
var vx = this.pos.x - this.oldpos.x;
@@ -241,10 +254,6 @@ Phaser.Physics.Ninja.AABB.prototype = {
241254
*/
242255
reportCollisionVsBody: function (px, py, dx, dy, obj) {
243256

244-
var vx1 = this.pos.x - this.oldpos.x; // Calc velocity of this object
245-
var vy1 = this.pos.y - this.oldpos.y;
246-
var dp1 = (vx1 * dx + vy1 * dy); // Find component of velocity parallel to collision normal
247-
248257
// We only want to apply collision response forces if the object is travelling into, and not out of, the collision
249258
if (this.body.immovable && obj.body.immovable)
250259
{
@@ -266,32 +275,16 @@ Phaser.Physics.Ninja.AABB.prototype = {
266275
px *= 0.5;
267276
py *= 0.5;
268277

269-
this.pos.add(px, py);
270-
obj.pos.subtract(px, py);
271-
272-
if (dp1 < 0)
273-
{
274-
this.reverse();
275-
obj.reverse();
276-
}
278+
this.reportCollision(px, py, dx, dy);
279+
obj.reportCollision(-px, -py, -dx, -dy);
277280
}
278281
else if (!this.body.immovable)
279282
{
280-
this.pos.subtract(px, py);
281-
282-
if (dp1 < 0)
283-
{
284-
this.reverse();
285-
}
283+
this.reportCollision(px,py,dx,dy);
286284
}
287285
else if (!obj.body.immovable)
288286
{
289-
obj.pos.subtract(px, py);
290-
291-
if (dp1 < 0)
292-
{
293-
obj.reverse();
294-
}
287+
obj.reportCollision(-px, -py, -dx, -dy);
295288
}
296289

297290
},

0 commit comments

Comments
 (0)