Skip to content

Commit d2d77f3

Browse files
committed
Finished Ninja Physics updates.
1 parent 170776a commit d2d77f3

4 files changed

Lines changed: 130 additions & 187 deletions

File tree

examples/wip/ninja aabb vs aabb2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function create() {
4141

4242
function t() {
4343

44-
sprite1.body.shape.oldpos.x = sprite1.body.shape.pos.x - 20;
44+
sprite1.body.shape.oldpos.x = sprite1.body.shape.pos.x - 30;
4545
sprite2.body.shape.oldpos.x = sprite2.body.shape.pos.x + 20;
4646

4747
}

src/physics/ninja/AABB.js

Lines changed: 56 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,37 @@ Phaser.Physics.Ninja.AABB.prototype = {
200200

201201
},
202202

203+
reverse: function () {
204+
205+
var vx = this.pos.x - this.oldpos.x;
206+
var vy = this.pos.y - this.oldpos.y;
207+
208+
if (this.oldpos.x < this.pos.x)
209+
{
210+
this.oldpos.x = this.pos.x + vx;
211+
// this.oldpos.x = this.pos.x + (vx + 1 + this.body.bounce);
212+
}
213+
else if (this.oldpos.x > this.pos.x)
214+
{
215+
this.oldpos.x = this.pos.x - vx;
216+
// this.oldpos.x = this.pos.x - (vx + 1 + this.body.bounce);
217+
}
218+
219+
if (this.oldpos.y < this.pos.y)
220+
{
221+
this.oldpos.y = this.pos.y + vy;
222+
// this.oldpos.y = this.pos.y + (vy + 1 + this.body.bounce);
223+
}
224+
else if (this.oldpos.y > this.pos.y)
225+
{
226+
this.oldpos.y = this.pos.y - vy;
227+
// this.oldpos.y = this.pos.y - (vy + 1 + this.body.bounce);
228+
}
229+
230+
},
231+
203232
/**
204-
* Process a body collision and apply the resulting forces.
233+
* Process a body collision and apply the resulting forces. Still very much WIP and doesn't work fully. Feel free to fix!
205234
*
206235
* @method Phaser.Physics.Ninja.AABB#reportCollisionVsBody
207236
* @param {number} px - The tangent velocity
@@ -226,13 +255,7 @@ Phaser.Physics.Ninja.AABB.prototype = {
226255
var nx2 = dp2 * dx2; // Project velocity onto collision normal
227256
var ny2 = dp2 * dy2; // nx, ny is normal velocity
228257

229-
console.log(this.body.sprite.name, 'hit', obj.body.sprite.name, 'at', px, py, 'dx', dx, dy, 'dx2', dx2, dy2);
230-
console.log(this.body.sprite.name, 'x', (this.pos.x + this.xw), obj.body.sprite.name, 'x', (obj.pos.x - obj.xw));
231-
console.log('vx1', vx1, vy1, 'dp1', dp1, 'nx1', nx1, ny1);
232-
console.log('vx2', vx2, vy2, 'dp2', dp2, 'nx2', nx2, ny2);
233-
234258
// We only want to apply collision response forces if the object is travelling into, and not out of, the collision
235-
236259
if (this.body.immovable && obj.body.immovable)
237260
{
238261
// Split the separation then return, no forces applied as they come to a stand-still
@@ -249,104 +272,38 @@ Phaser.Physics.Ninja.AABB.prototype = {
249272
}
250273
else if (!this.body.immovable && !obj.body.immovable)
251274
{
275+
// separate
252276
px *= 0.5;
253277
py *= 0.5;
254278

255279
this.pos.add(px, py);
256-
this.oldpos.set(this.pos.x, this.pos.y);
257-
258280
obj.pos.subtract(px, py);
259-
obj.oldpos.set(obj.pos.x, obj.pos.y);
260281

261-
// x velocity
262-
var nv1 = Math.sqrt((vx2 * vx2 * 1) / 1) * ((vx2 > 0) ? 1 : -1);
263-
var nv2 = Math.sqrt((vx1 * vx1 * 1) / 1) * ((vx1 > 0) ? 1 : -1);
264-
var avg = (nv1 + nv2) * 0.5;
265-
nv1 -= avg;
266-
nv2 -= avg;
267-
268-
this.pos.x += avg + nv1 * this.body.bounce;
269-
obj.pos.x += avg + nv2 * obj.body.bounce;
270-
271-
// y velocity
272-
var nv1 = Math.sqrt((vy2 * vy2 * 1) / 1) * ((vy2 > 0) ? 1 : -1);
273-
var nv2 = Math.sqrt((vy1 * vy1 * 1) / 1) * ((vy1 > 0) ? 1 : -1);
274-
var avg = (nv1 + nv2) * 0.5;
275-
nv1 -= avg;
276-
nv2 -= avg;
277-
278-
this.pos.y += avg + nv1 * this.body.bounce;
279-
obj.pos.y += avg + nv2 * obj.body.bounce;
282+
if (dp1 < 0)
283+
{
284+
this.reverse();
285+
obj.reverse();
286+
}
280287
}
281288
else if (!this.body.immovable)
282289
{
283-
/*
290+
this.pos.subtract(px, py);
291+
284292
if (dp1 < 0)
285293
{
286-
this.pos.add(px, py);
287-
// px + bounce + friction
288-
this.oldpos.x += px + (nx1 * (1 + this.body.bounce)) + ((vx2 - nx1) * this.body.friction);
289-
this.oldpos.y += py + (ny1 * (1 + this.body.bounce)) + ((vy2 - ny1) * this.body.friction);
290-
}
291-
else
292-
{
293-
// Moving out of collision, do not apply forces
294-
this.pos.add(px, py);
295-
this.oldpos.add(px, py);
294+
this.reverse();
296295
}
297-
*/
298296
}
299297
else if (!obj.body.immovable)
300298
{
301-
/*
302-
if (dp2 < 0)
303-
{
304-
obj.pos.add(px, py);
305-
// px + bounce + friction
306-
obj.oldpos.x += px + (nx2 * (1 + obj.body.bounce)) + ((vx2 - nx2) * obj.body.friction);
307-
obj.oldpos.y += py + (ny2 * (1 + obj.body.bounce)) + ((vy2 - ny2) * obj.body.friction);
308-
}
309-
else
299+
obj.pos.subtract(px, py);
300+
301+
if (dp1 < 0)
310302
{
311-
// Moving out of collision, do not apply forces
312-
obj.pos.add(px, py);
313-
obj.oldpos.add(px, py);
303+
obj.reverse();
314304
}
315-
*/
316305
}
317306

318-
319-
320-
/*
321-
if (!body1.immovable && !body2.immovable)
322-
{
323-
this._overlap *= 0.5;
324-
325-
body1.x = body1.x - this._overlap;
326-
body2.x += this._overlap;
327-
328-
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
329-
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
330-
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
331-
this._newVelocity1 -= this._average;
332-
this._newVelocity2 -= this._average;
333-
334-
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
335-
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
336-
}
337-
else if (!body1.immovable)
338-
{
339-
body1.x = body1.x - this._overlap;
340-
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
341-
}
342-
else if (!body2.immovable)
343-
{
344-
body2.x += this._overlap;
345-
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
346-
}
347-
348-
*/
349-
350307
},
351308

352309
/**
@@ -450,10 +407,7 @@ Phaser.Physics.Ninja.AABB.prototype = {
450407
}
451408
}
452409

453-
// return this.aabbTileProjections[1](px, py, this, c);
454-
455410
var l = Math.sqrt(px * px + py * py);
456-
// this.reportCollisionVsWorld(px, py, px / l, py / l, c);
457411
this.reportCollisionVsBody(px, py, px / l, py / l, c);
458412

459413
return Phaser.Physics.Ninja.AABB.COL_AXIS;
@@ -473,59 +427,50 @@ Phaser.Physics.Ninja.AABB.prototype = {
473427
*/
474428
collideAABBVsTile: function (tile) {
475429

476-
var pos = this.pos;
477-
var c = tile;
478-
479-
var tx = c.pos.x;
480-
var ty = c.pos.y;
481-
var txw = c.xw;
482-
var tyw = c.yw;
483-
484-
var dx = pos.x - tx;//tile->obj delta
485-
var px = (txw + this.xw) - Math.abs(dx);//penetration depth in x
430+
var dx = this.pos.x - tile.pos.x; // tile->obj delta
431+
var px = (tile.xw + this.xw) - Math.abs(dx); // penetration depth in x
486432

487433
if (0 < px)
488434
{
489-
var dy = pos.y - ty;//tile->obj delta
490-
var py = (tyw + this.yw) - Math.abs(dy);//pen depth in y
435+
var dy = this.pos.y - tile.pos.y; // tile->obj delta
436+
var py = (tile.yw + this.yw) - Math.abs(dy); // pen depth in y
491437

492438
if (0 < py)
493439
{
494-
//object may be colliding with tile; call tile-specific collision function
495-
496-
//calculate projection vectors
440+
// Calculate projection vectors
497441
if (px < py)
498442
{
499-
//project in x
443+
// Project in x
500444
if (dx < 0)
501445
{
502-
//project to the left
446+
// Project to the left
503447
px *= -1;
504448
py = 0;
505449
}
506450
else
507451
{
508-
//proj to right
452+
// Project to the right
509453
py = 0;
510454
}
511455
}
512456
else
513457
{
514-
//project in y
458+
// Project in y
515459
if (dy < 0)
516460
{
517-
//project up
461+
// Project up
518462
px = 0;
519463
py *= -1;
520464
}
521465
else
522466
{
523-
//project down
467+
// Project down
524468
px = 0;
525469
}
526470
}
527471

528-
return this.resolveTile(px, py, this, c);
472+
// Object may be colliding with tile; call tile-specific collision function
473+
return this.resolveTile(px, py, this, tile);
529474
}
530475
}
531476

0 commit comments

Comments
 (0)