CollisionGroup and collision masks working. Need to refine a little, but all the essentials are there.

This commit is contained in:
photonstorm
2014-02-19 01:51:14 +00:00
parent 7a8c96db37
commit 5968dd053b
8 changed files with 235 additions and 26 deletions
+55
View File
@@ -132,6 +132,20 @@ Phaser.Physics.World = function (game) {
this.world.on("beginContact", this.beginContactHandler, this);
this.world.on("endContact", this.endContactHandler, this);
/**
* @property {array} collisionGroups - Internal var.
*/
this.collisionGroups = [];
/**
* @property {number} _collisionGroupID - Internal var.
* @private
*/
this._collisionGroupID = 2;
this.boundsCollisionGroup = new Phaser.Physics.CollisionGroup(2);
this.boundsCollidesWith = [];
this.setBoundsToWorld(true, true, true, true);
};
@@ -310,24 +324,28 @@ Phaser.Physics.World.prototype = {
if (left)
{
this._wallShapes[0] = new p2.Plane();
this._wallShapes[0].collisionGroup = this.boundsCollisionGroup.mask;
this.bounds.addShape(this._wallShapes[0], [this.game.math.px2p(-hw), 0], 1.5707963267948966 );
}
if (right)
{
this._wallShapes[1] = new p2.Plane();
this._wallShapes[1].collisionGroup = this.boundsCollisionGroup.mask;
this.bounds.addShape(this._wallShapes[1], [this.game.math.px2p(hw), 0], -1.5707963267948966 );
}
if (top)
{
this._wallShapes[2] = new p2.Plane();
this._wallShapes[2].collisionGroup = this.boundsCollisionGroup.mask;
this.bounds.addShape(this._wallShapes[2], [0, this.game.math.px2p(-hh)], -3.141592653589793 );
}
if (bottom)
{
this._wallShapes[3] = new p2.Plane();
this._wallShapes[3].collisionGroup = this.boundsCollisionGroup.mask;
this.bounds.addShape(this._wallShapes[3], [0, this.game.math.px2p(hh)] );
}
@@ -662,6 +680,43 @@ Phaser.Physics.World.prototype = {
},
createCollisionGroup: function () {
var bitmask = Math.pow(2, this._collisionGroupID);
// Add it to the bounds mask automatically (they won't collide with it unless it's added back by the other group)
// this.boundsCollisionGroup.mask = this.boundsCollisionGroup.mask | bitmask;
if (this._wallShapes[0])
{
this._wallShapes[0].collisionMask = this._wallShapes[0].collisionMask | bitmask;
}
if (this._wallShapes[1])
{
this._wallShapes[1].collisionMask = this._wallShapes[1].collisionMask | bitmask;
}
if (this._wallShapes[2])
{
this._wallShapes[2].collisionMask = this._wallShapes[2].collisionMask | bitmask;
}
if (this._wallShapes[3])
{
this._wallShapes[3].collisionMask = this._wallShapes[3].collisionMask | bitmask;
}
this._collisionGroupID++;
var group = new Phaser.Physics.CollisionGroup(bitmask);
this.collisionGroups.push(group);
return group;
},
/**
* @method Phaser.Physics.World.prototype.createBody
* @param {number} x - The x coordinate of Body.