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
+2 -2
View File
@@ -1292,7 +1292,7 @@ Phaser.Math = {
},
/**
* Convert p2 physics value to pixel scale.
* Convert p2 physics value (meters) to pixel scale.
*
* @method Phaser.Math#p2px
* @param {number} v - The value to convert.
@@ -1303,7 +1303,7 @@ Phaser.Math = {
},
/**
* Convert pixel value to p2 physics scale.
* Convert pixel value to p2 physics scale (meters).
*
* @method Phaser.Math#px2p
* @param {number} v - The value to convert.
+75 -2
View File
@@ -63,6 +63,19 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
*/
this.gravity = new Phaser.Point();
/**
* A Body can be set to collide against the World bounds automatically if this is set to true. Otherwise it will leave the World.
* Note that this only applies if your World has bounds! The response to the collision should be managed via CollisionMaterials.
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
*/
this.collideWorldBounds = true;
/**
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
* @private
*/
this.collidesWith = [];
// this.onAdded = new Phaser.Signal();
// this.onRemoved = new Phaser.Signal();
@@ -77,6 +90,66 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
Phaser.Physics.Body.prototype = {
setCollisionGroup: function (group, shape) {
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionGroup = group.mask;
}
}
else
{
shape.collisionGroup = group.mask;
}
},
/**
* Adds the given CollisionGroup to the list of groups that this body will collide with and updates the collision mask.
*
* @method Phaser.Physics.Body#collides
* @param {Phaser.Physics.CollisionGroup} group - The Collision Group that this Bodies shapes will collide with.
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision mask will be added to all Shapes in this Body.
*/
collides: function (group, shape) {
// TODO: group can be an array
if (this.collidesWith.indexOf(group) === -1)
{
this.collidesWith.push(group);
}
var mask = 0;
if (this.collideWorldBounds)
{
mask = this.game.physics.boundsCollisionGroup.mask;
}
for (var i = 0; i < this.collidesWith.length; i++)
{
mask = mask | this.collidesWith[i].mask;
}
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionMask = mask;
}
}
else
{
shape.collisionMask = mask;
}
console.log('collides', this.sprite.name, group, mask);
},
/**
* Moves the shape offsets so their center of mass becomes the body center of mass.
*
@@ -819,7 +892,7 @@ Phaser.Physics.Body.prototype = {
},
/**
* Convert p2 physics value to pixel scale.
* Convert p2 physics value (meters) to pixel scale.
*
* @method Phaser.Math#p2px
* @param {number} v - The value to convert.
@@ -832,7 +905,7 @@ Phaser.Physics.Body.prototype = {
},
/**
* Convert pixel value to p2 physics scale.
* Convert pixel value to p2 physics scale (meters).
*
* @method Phaser.Math#px2p
* @param {number} v - The value to convert.
+21
View File
@@ -0,0 +1,21 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* Collision Group
*
* @class Phaser.Physics.CollisionGroup
* @classdesc Physics Collision Group Constructor
* @constructor
*/
Phaser.Physics.CollisionGroup = function (bitmask) {
/**
* @property {number} mask - The CollisionGroup bitmask.
*/
this.mask = bitmask;
}
+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.