Working through AABB vs. AABB tests.

This commit is contained in:
photonstorm 2014-03-07 05:30:56 +00:00
parent 4cac6408d8
commit 480c1819d6
2 changed files with 54 additions and 17 deletions

View file

@ -5,23 +5,34 @@ var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload:
function preload() {
game.load.image('block', 'assets/sprites/block.png');
game.load.spritesheet('ninja-tiles', 'assets/physics/ninja-tiles128.png', 128, 128, 34);
}
var sprite1;
var sprite2;
var tile;
var cursors;
function create() {
game.physics.startSystem(Phaser.Physics.NINJA);
sprite1 = game.add.sprite(100, 100, 'block');
sprite2 = game.add.sprite(400, 100, 'block');
// game.physics.ninja.gravity = 6;
// sprite1 = game.add.sprite(200, 100, 'block');
sprite1 = game.add.sprite(200, 500, 'block');
sprite1.name = 'blockA';
sprite2 = game.add.sprite(600, 100, 'block');
sprite2.name = 'blockB';
// game.physics.ninja.enableAABB([sprite1]);
game.physics.ninja.enableAABB([sprite1, sprite2]);
tile = game.add.sprite(600, 480, 'ninja-tiles', 3);
game.physics.ninja.enableTile(tile, tile.frame);
cursors = game.input.keyboard.createCursorKeys();
}
@ -29,6 +40,10 @@ function create() {
function update() {
game.physics.ninja.collide(sprite1, sprite2);
game.physics.ninja.collide(sprite1, tile);
game.physics.ninja.collide(sprite2, tile);
sprite1.body.moveRight(5);
if (cursors.left.isDown)
{

View file

@ -116,13 +116,9 @@ Phaser.Physics.Ninja.AABB.prototype = {
var py = this.pos.y;
// integrate
// this.pos.x += (this.body.drag * this.pos.x) - (this.body.drag * this.oldpos.x);
// this.pos.y += (this.body.drag * this.pos.y) - (this.body.drag * this.oldpos.y) + (this.system.gravity * this.body.gravityScale);
this.pos.x += (this.body.drag * this.pos.x) - (this.body.drag * this.oldpos.x);
this.pos.y += (this.body.drag * this.pos.y) - (this.body.drag * this.oldpos.y) + (this.system.gravity * this.body.gravityScale);
// store
this.velocity.set(this.pos.x - px, this.pos.y - py);
this.oldpos.set(px, py);
@ -216,6 +212,9 @@ Phaser.Physics.Ninja.AABB.prototype = {
*/
reportCollisionVsBody: function (px, py, dx, dy, obj) {
// Here - we check if obj is immovable, etc and then we canswap the p/o values below depending on which is heavy
// But then still need to work out how to split force
var p = this.pos;
var o = this.oldpos;
@ -251,21 +250,44 @@ Phaser.Physics.Ninja.AABB.prototype = {
bx = by = fx = fy = 0;
}
// Project object out of collision
// working version
// p.x += px;
// p.y += py;
// o.x += px + bx + fx;
// o.y += py + by + fy;
// Project object out of collision (applied to the position value)
p.x += px;
p.y += py;
// Apply bounce+friction impulses which alter velocity
o.x += px + bx + fx;
o.y += py + by + fy;
// obj.pos.x += px;
// obj.pos.y += py;
// apply to obj
if (obj)
{
// obj.oldpos.x =
// console.log('reportCollisionVsBody');
// obj.reportCollisionVsBody(px *= -1, py *= -1, dx *= -1, dy *= -1, null);
}
// Apply bounce+friction impulses which alter velocity (applied to old position, thus setting a sort of velocity up)
var rx = px + bx + fx;
var ry = py + by + fy;
// let's pretend obj is immovable
// rx *= -1;
// ry *= -1;
// Now let's share the load
o.x += rx;
o.y += ry;
// work out objs velocity
// rx *= -1;
// ry *= -1;
// obj.oldpos.x += rx;
// obj.oldpos.y += ry;
// console.log(this.body.sprite.name, 'o.x', rx, ry, obj);
},