Skip to content

Commit 274fd4a

Browse files
committed
You can now hitTest against P2 bodies + example created.
1 parent fd46df1 commit 274fd4a

3 files changed

Lines changed: 136 additions & 13 deletions

File tree

examples/p2 physics/body click.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
3+
4+
function preload() {
5+
6+
game.load.image('contra2', 'assets/pics/contra2.png');
7+
game.load.image('bunny', 'assets/sprites/bunny.png');
8+
game.load.image('block', 'assets/sprites/block.png');
9+
game.load.image('wizball', 'assets/sprites/wizball.png');
10+
11+
game.load.physics('physicsData', 'assets/physics/sprites.json');
12+
13+
}
14+
15+
var contra;
16+
var bunny;
17+
var block;
18+
var wizball;
19+
20+
var result = '';
21+
22+
function create() {
23+
24+
// Enable p2 physics
25+
game.physics.startSystem(Phaser.Physics.P2JS);
26+
27+
contra = game.add.sprite(100, 200, 'contra2');
28+
bunny = game.add.sprite(550, 200, 'bunny');
29+
block = game.add.sprite(300, 400, 'block');
30+
wizball = game.add.sprite(500, 500, 'wizball');
31+
32+
// Enable the physics bodies on all the sprites and turn on the visual debugger
33+
game.physics.p2.enable([ contra, bunny, block, wizball ], true);
34+
35+
// Convex polys
36+
contra.body.clearShapes();
37+
contra.body.loadPolygon('physicsData', 'contra2');
38+
39+
bunny.body.clearShapes();
40+
bunny.body.loadPolygon('physicsData', 'bunny');
41+
42+
// Circle
43+
wizball.body.setCircle(45);
44+
45+
game.input.onDown.add(click, this);
46+
47+
}
48+
49+
function click(pointer) {
50+
51+
// You can hitTest against an array of Sprites, an array of Phaser.Physics.P2.Body objects, or don't give anything
52+
// in which case it will check every Body in the whole world.
53+
54+
var bodies = game.physics.p2.hitTest(pointer.position, [ contra, bunny, block, wizball ]);
55+
56+
if (bodies.length === 0)
57+
{
58+
result = "You didn't click a Body";
59+
}
60+
else
61+
{
62+
result = "You clicked: ";
63+
64+
for (var i = 0; i < bodies.length; i++)
65+
{
66+
// The bodies that come back are p2.Body objects.
67+
// The parent property is a Phaser.Physics.P2.Body which has a property called 'sprite'
68+
// This relates to the sprites we created earlier.
69+
// 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.
70+
result = result + bodies[i].parent.sprite.key;
71+
72+
if (i < bodies.length - 1)
73+
{
74+
result = result + ', ';
75+
}
76+
}
77+
78+
}
79+
80+
}
81+
82+
function update() {
83+
84+
bunny.body.rotateLeft(2);
85+
86+
}
87+
88+
function render() {
89+
90+
game.debug.text(result, 32, 32);
91+
92+
}

src/physics/p2/Body.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
5656
* @property {p2.Body} data - The p2 Body data.
5757
* @protected
5858
*/
59-
this.data = new p2.Body({ position:[this.world.pxmi(x), this.world.pxmi(y)], mass: mass });
59+
this.data = new p2.Body({ position: [ this.world.pxmi(x), this.world.pxmi(y) ], mass: mass });
6060
this.data.parent = this;
6161

6262
/**
@@ -686,13 +686,14 @@ Phaser.Physics.P2.Body.prototype = {
686686
*/
687687
clearShapes: function () {
688688

689-
for (var i = this.data.shapes.length - 1; i >= 0; i--)
689+
var i = this.data.shapes.length;
690+
691+
while (i--)
690692
{
691-
var shape = this.data.shapes[i];
692-
this.data.removeShape(shape);
693+
this.data.removeShape(this.data.shapes[i]);
693694
}
694695

695-
this.shapeChanged()
696+
this.shapeChanged();
696697

697698
},
698699

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

716717
this.data.addShape(shape, [this.world.pxmi(offsetX), this.world.pxmi(offsetY)], rotation);
717-
this.shapeChanged()
718+
this.shapeChanged();
718719

719720
return shape;
720721

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

902903
result = this.data.fromPolygon(path, options);
903-
this.shapeChanged()
904+
905+
this.shapeChanged();
904906

905907
return result
906908
},

src/physics/p2/World.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -845,15 +845,44 @@ Phaser.Physics.P2.prototype = {
845845
},
846846

847847
/**
848-
* Test if a world point overlaps bodies.
848+
* 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
849+
* (if any) by checking the Body.parent.sprite property. Body.parent is a Phaser.Physics.P2.Body property.
849850
*
850851
* @method Phaser.Physics.P2#hitTest
851-
* @param {Phaser.Point} worldPoint - Point to use for intersection tests.
852-
* @param {Array} bodies - A list of objects to check for intersection.
853-
* @param {number} precision - Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
852+
* @param {Phaser.Point} worldPoint - Point to use for intersection tests. The points values must be in world (pixel) coordinates.
853+
* @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)
854+
* @param {number} [precision=5] - Used for matching against particles and lines. Adds some margin to these infinitesimal objects.
855+
* @param {boolean} [filterStatic=false] - If true all Static objects will be removed from the results array.
854856
* @return {Array} Array of bodies that overlap the point.
855857
*/
856-
hitTest: function (worldPoint, bodies, precision) {
858+
hitTest: function (worldPoint, bodies, precision, filterStatic) {
859+
860+
if (typeof bodies === 'undefined') { bodies = this.world.bodies; }
861+
if (typeof precision === 'undefined') { precision = 5; }
862+
if (typeof filterStatic === 'undefined') { filterStatic = false; }
863+
864+
var physicsPosition = [ this.pxmi(worldPoint.x), this.pxmi(worldPoint.y) ];
865+
866+
var query = [];
867+
var i = bodies.length;
868+
869+
while (i--)
870+
{
871+
if (bodies[i] instanceof Phaser.Physics.P2.Body && !(filterStatic && bodies[i].data.motionState === p2.Body.STATIC))
872+
{
873+
query.push(bodies[i].data);
874+
}
875+
else if (bodies[i] instanceof p2.Body && bodies[i].parent && !(filterStatic && bodies[i].motionState === p2.Body.STATIC))
876+
{
877+
query.push(bodies[i]);
878+
}
879+
else if (bodies[i] instanceof Phaser.Sprite && bodies[i].hasOwnProperty('body') && !(filterStatic && bodies[i].body.data.motionState === p2.Body.STATIC))
880+
{
881+
query.push(bodies[i].body.data);
882+
}
883+
}
884+
885+
return this.world.hitTest(physicsPosition, query, precision);
857886

858887
},
859888

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

868-
this.world.toJSON();
897+
return this.world.toJSON();
869898

870899
},
871900

0 commit comments

Comments
 (0)