Skip to content

Commit

Permalink
You can now hitTest against P2 bodies + example created.
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Mar 12, 2014
1 parent fd46df1 commit 274fd4a
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 13 deletions.
92 changes: 92 additions & 0 deletions examples/p2 physics/body click.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });

function preload() {

game.load.image('contra2', 'assets/pics/contra2.png');
game.load.image('bunny', 'assets/sprites/bunny.png');
game.load.image('block', 'assets/sprites/block.png');
game.load.image('wizball', 'assets/sprites/wizball.png');

game.load.physics('physicsData', 'assets/physics/sprites.json');

}

var contra;
var bunny;
var block;
var wizball;

var result = '';

function create() {

// Enable p2 physics
game.physics.startSystem(Phaser.Physics.P2JS);

contra = game.add.sprite(100, 200, 'contra2');
bunny = game.add.sprite(550, 200, 'bunny');
block = game.add.sprite(300, 400, 'block');
wizball = game.add.sprite(500, 500, 'wizball');

// Enable the physics bodies on all the sprites and turn on the visual debugger
game.physics.p2.enable([ contra, bunny, block, wizball ], true);

// Convex polys
contra.body.clearShapes();
contra.body.loadPolygon('physicsData', 'contra2');

bunny.body.clearShapes();
bunny.body.loadPolygon('physicsData', 'bunny');

// Circle
wizball.body.setCircle(45);

game.input.onDown.add(click, this);

}

function click(pointer) {

// You can hitTest against an array of Sprites, an array of Phaser.Physics.P2.Body objects, or don't give anything
// in which case it will check every Body in the whole world.

var bodies = game.physics.p2.hitTest(pointer.position, [ contra, bunny, block, wizball ]);

if (bodies.length === 0)
{
result = "You didn't click a Body";
}
else
{
result = "You clicked: ";

for (var i = 0; i < bodies.length; i++)
{
// The bodies that come back are p2.Body objects.
// The parent property is a Phaser.Physics.P2.Body which has a property called 'sprite'
// This relates to the sprites we created earlier.
// The 'key' property is just the texture name, which works well for this demo but you probably need something more robust for an actual game.
result = result + bodies[i].parent.sprite.key;

if (i < bodies.length - 1)
{
result = result + ', ';
}
}

}

}

function update() {

bunny.body.rotateLeft(2);

}

function render() {

game.debug.text(result, 32, 32);

}
16 changes: 9 additions & 7 deletions src/physics/p2/Body.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
* @property {p2.Body} data - The p2 Body data.
* @protected
*/
this.data = new p2.Body({ position:[this.world.pxmi(x), this.world.pxmi(y)], mass: mass });
this.data = new p2.Body({ position: [ this.world.pxmi(x), this.world.pxmi(y) ], mass: mass });
this.data.parent = this;

/**
Expand Down Expand Up @@ -686,13 +686,14 @@ Phaser.Physics.P2.Body.prototype = {
*/
clearShapes: function () {

for (var i = this.data.shapes.length - 1; i >= 0; i--)
var i = this.data.shapes.length;

while (i--)
{
var shape = this.data.shapes[i];
this.data.removeShape(shape);
this.data.removeShape(this.data.shapes[i]);
}

this.shapeChanged()
this.shapeChanged();

},

Expand All @@ -714,7 +715,7 @@ Phaser.Physics.P2.Body.prototype = {
if (typeof rotation === 'undefined') { rotation = 0; }

this.data.addShape(shape, [this.world.pxmi(offsetX), this.world.pxmi(offsetY)], rotation);
this.shapeChanged()
this.shapeChanged();

return shape;

Expand Down Expand Up @@ -900,7 +901,8 @@ Phaser.Physics.P2.Body.prototype = {
// console.table(path);

result = this.data.fromPolygon(path, options);
this.shapeChanged()

this.shapeChanged();

return result
},
Expand Down
41 changes: 35 additions & 6 deletions src/physics/p2/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -845,15 +845,44 @@ Phaser.Physics.P2.prototype = {
},

/**
* Test if a world point overlaps bodies.
* Test if a world point overlaps bodies. You will get an array of actual P2 bodies back. You can find out which Sprite a Body belongs to
* (if any) by checking the Body.parent.sprite property. Body.parent is a Phaser.Physics.P2.Body property.
*
* @method Phaser.Physics.P2#hitTest
* @param {Phaser.Point} worldPoint - Point to use for intersection tests.
* @param {Array} bodies - A list of objects to check for intersection.
* @param {number} precision - Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
* @param {Phaser.Point} worldPoint - Point to use for intersection tests. The points values must be in world (pixel) coordinates.
* @param {Array<Phaser.Physics.P2.Body|Phaser.Sprite|p2.Body>} [bodies] - A list of objects to check for intersection. If not given it will check Phaser.Physics.P2.world.bodies (i.e. all world bodies)
* @param {number} [precision=5] - Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
* @param {boolean} [filterStatic=false] - If true all Static objects will be removed from the results array.
* @return {Array} Array of bodies that overlap the point.
*/
hitTest: function (worldPoint, bodies, precision) {
hitTest: function (worldPoint, bodies, precision, filterStatic) {

if (typeof bodies === 'undefined') { bodies = this.world.bodies; }
if (typeof precision === 'undefined') { precision = 5; }
if (typeof filterStatic === 'undefined') { filterStatic = false; }

var physicsPosition = [ this.pxmi(worldPoint.x), this.pxmi(worldPoint.y) ];

var query = [];
var i = bodies.length;

while (i--)
{
if (bodies[i] instanceof Phaser.Physics.P2.Body && !(filterStatic && bodies[i].data.motionState === p2.Body.STATIC))
{
query.push(bodies[i].data);
}
else if (bodies[i] instanceof p2.Body && bodies[i].parent && !(filterStatic && bodies[i].motionState === p2.Body.STATIC))
{
query.push(bodies[i]);
}
else if (bodies[i] instanceof Phaser.Sprite && bodies[i].hasOwnProperty('body') && !(filterStatic && bodies[i].body.data.motionState === p2.Body.STATIC))
{
query.push(bodies[i].body.data);
}
}

return this.world.hitTest(physicsPosition, query, precision);

},

Expand All @@ -865,7 +894,7 @@ Phaser.Physics.P2.prototype = {
*/
toJSON: function () {

this.world.toJSON();
return this.world.toJSON();

},

Expand Down

0 comments on commit 274fd4a

Please sign in to comment.