Skip to content

Commit 5968dd0

Browse files
committed
CollisionGroup and collision masks working. Need to refine a little, but all the essentials are there.
1 parent 7a8c96d commit 5968dd0

8 files changed

Lines changed: 235 additions & 26 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ module.exports = function (grunt) {
140140
'src/physics/Spring.js',
141141
'src/physics/Material.js',
142142
'src/physics/ContactMaterial.js',
143+
'src/physics/CollisionGroup.js',
143144

144145
'src/particles/Particles.js',
145146
'src/particles/arcade/ArcadeParticles.js',

build/config.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
<script src="$path/src/physics/Spring.js"></script>
187187
<script src="$path/src/physics/Material.js"></script>
188188
<script src="$path/src/physics/ContactMaterial.js"></script>
189+
<script src="$path/src/physics/CollisionGroup.js"></script>
189190
190191
<script src="$path/src/particles/Particles.js"></script>
191192
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>

examples/wip/cannon.js

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function preload() {
66
// game.load.image('arrow', 'assets/sprites/arrow.png');
77
game.load.image('arrow', 'assets/sprites/thrust_ship2.png');
88
game.load.image('chunk', 'assets/sprites/chunk.png');
9+
game.load.image('box', 'assets/sprites/block.png');
910
game.load.spritesheet('bullets', 'assets/sprites/balls.png', 17, 17);
1011

1112
}
@@ -16,6 +17,13 @@ var angle = 0;
1617
var fireRate = 100;
1718
var nextFire = 0;
1819
var cursors;
20+
// var PLAYER;
21+
// var BULLET;
22+
// var WORLD;
23+
var boxes;
24+
var playerGroup;
25+
var bulletGroup;
26+
var boxGroup;
1927

2028
function create() {
2129

@@ -29,9 +37,38 @@ function create() {
2937
bullets.createMultiple(250, 'bullets', 0, false);
3038

3139
cannon = game.add.sprite(50, 500, 'arrow');
40+
cannon.name = 'ship';
3241
cannon.physicsEnabled = true;
33-
cannon.body.kinematic = true;
34-
cannon.body.mass = 4;
42+
43+
playerGroup = game.physics.createCollisionGroup();
44+
bulletGroup = game.physics.createCollisionGroup();
45+
boxGroup = game.physics.createCollisionGroup();
46+
47+
// cannon.body.data.shapes[0].collisionGroup = PLAYER.mask;
48+
// cannon.body.data.shapes[0].collisionMask = game.physics.WORLD.mask;
49+
50+
cannon.body.setCollisionGroup(playerGroup);
51+
cannon.body.collides(boxGroup);
52+
53+
54+
boxes = game.add.group();
55+
56+
for (var i = 0; i < 10; i++)
57+
{
58+
var box = boxes.create(game.rnd.integerInRange(100, 700), game.rnd.integerInRange(100, 500), 'box');
59+
box.name = 'box' + i;
60+
box.scale.set(0.5);
61+
// box.scale.set(game.rnd.realInRange(0.2, 0.7));
62+
box.physicsEnabled = true;
63+
box.body.setCollisionGroup(boxGroup);
64+
box.body.collides(playerGroup);
65+
box.body.collides(bulletGroup);
66+
// box.body.mass = 10;
67+
// box.body.setMaterial(boxMaterial);
68+
// box.body.fixedRotation = true;
69+
}
70+
71+
3572
cursors = game.input.keyboard.createCursorKeys();
3673

3774
}
@@ -58,6 +95,12 @@ function fire() {
5895

5996
bullet.body.velocity.x = magnitude * Math.cos(angle);
6097
bullet.body.velocity.y = magnitude * Math.sin(angle);
98+
99+
bullet.body.setCollisionGroup(bulletGroup);
100+
bullet.body.collides(boxGroup);
101+
102+
// bullet.body.data.shapes[0].collisionGroup = BULLET;
103+
// bullet.body.data.shapes[0].collisionMask = WORLD | BULLET;
61104
}
62105
}
63106

@@ -80,11 +123,13 @@ function update() {
80123

81124
if (cursors.up.isDown)
82125
{
83-
cannon.body.moveForward(200);
126+
// cannon.body.moveForward(200);
127+
cannon.body.thrust(200);
84128
}
85129
else if (cursors.down.isDown)
86130
{
87-
cannon.body.moveBackward(200);
131+
// cannon.body.moveBackward(200);
132+
cannon.body.reverse(200);
88133
}
89134

90135
var dx = game.input.activePointer.worldX - cannon.x;

examples/wip/platforms.js

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ var facing = 'left';
1414
var jumpTimer = 0;
1515
var cursors;
1616
var jumpButton;
17-
var box1;
18-
var box2;
17+
18+
var boxes;
1919

2020
function create() {
2121

@@ -39,40 +39,53 @@ function create() {
3939

4040
// game.physics.setBoundsToWorld();
4141

42-
game.physics.gravity.y = 20;
42+
// game.physics.gravity.y = 9.78;
43+
game.physics.setBoundsToWorld(true, true, false, true);
44+
45+
game.physics.world.gravity[1] = -20;
4346
game.physics.friction = 0.5;
47+
game.physics.world.solver.stiffness = 1e20;
48+
game.physics.world.solver.relaxation = 3;
4449

4550
// Materials
4651
var groundMaterial = game.physics.createMaterial('ground');
4752
var characterMaterial = game.physics.createMaterial('character');
4853
var boxMaterial = game.physics.createMaterial('box');
4954

50-
player = game.add.sprite(32, 320, 'dude');
55+
player = game.add.sprite(100, -400, 'dude');
5156
player.physicsEnabled = true;
5257
player.body.fixedRotation = true;
5358
player.body.setMaterial(characterMaterial);
59+
player.body.mass = 1;
60+
player.body.damping = 0.5;
5461

5562
player.animations.add('left', [0, 1, 2, 3], 10, true);
5663
player.animations.add('turn', [4], 20, true);
5764
player.animations.add('right', [5, 6, 7, 8], 10, true);
5865

59-
box1 = game.add.sprite(200, 300, 'box');
60-
box1.physicsEnabled = true;
61-
// box1.body.fixedRotation = true;
62-
box1.body.setMaterial(boxMaterial);
66+
boxes = game.add.group();
6367

64-
box2 = game.add.sprite(400, 300, 'box');
65-
box2.physicsEnabled = true;
66-
// box2.body.fixedRotation = true;
67-
box2.body.setMaterial(boxMaterial);
68+
for (var i = 0; i < 50; i++)
69+
{
70+
var box = boxes.create(game.rnd.integerInRange(200, 700), game.rnd.integerInRange(-200, 400), 'box');
71+
// box.scale.set(0.5);
72+
box.scale.set(game.rnd.realInRange(0.2, 0.7));
73+
box.physicsEnabled = true;
74+
box.body.mass = 10;
75+
box.body.setMaterial(boxMaterial);
76+
box.body.fixedRotation = true;
77+
}
6878

6979
// Set the material along the ground
70-
game.physics.setWorldMaterial(groundMaterial, false, false, false, true);
80+
game.physics.setWorldMaterial(groundMaterial);
7181

7282
var groundCharacterCM = game.physics.createContactMaterial(groundMaterial, characterMaterial, { friction: 0.0 }); // no friction between character and ground
7383
var boxCharacterCM = game.physics.createContactMaterial(boxMaterial, characterMaterial, { friction: 0.0 }); // No friction between character and boxes
7484
var boxGroundCM = game.physics.createContactMaterial(boxMaterial, groundMaterial, { friction: 0.6 }); // Between boxes and ground
7585

86+
console.log(groundCharacterCM);
87+
console.log(boxGroundCM);
88+
7689
// game.camera.follow(player);
7790

7891
cursors = game.input.keyboard.createCursorKeys();
@@ -82,11 +95,9 @@ function create() {
8295

8396
function update() {
8497

85-
player.body.velocity.x = 0;
86-
8798
if (cursors.left.isDown)
8899
{
89-
player.body.moveLeft(150);
100+
player.body.moveLeft(200);
90101

91102
if (facing != 'left')
92103
{
@@ -96,7 +107,7 @@ function update() {
96107
}
97108
else if (cursors.right.isDown)
98109
{
99-
player.body.moveRight(150);
110+
player.body.moveRight(200);
100111

101112
if (facing != 'right')
102113
{
@@ -106,6 +117,8 @@ function update() {
106117
}
107118
else
108119
{
120+
player.body.velocity.x = 0;
121+
109122
if (facing != 'idle')
110123
{
111124
player.animations.stop();
@@ -150,7 +163,7 @@ function render () {
150163

151164
// if (player.debug)
152165
// {
153-
// game.debug.renderPhysicsBody(player.body);
166+
game.debug.renderPhysicsBody(player.body);
154167
// game.debug.renderBodyInfo(player, 16, 24);
155168
// }
156169

src/math/Math.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,7 +1292,7 @@ Phaser.Math = {
12921292
},
12931293

12941294
/**
1295-
* Convert p2 physics value to pixel scale.
1295+
* Convert p2 physics value (meters) to pixel scale.
12961296
*
12971297
* @method Phaser.Math#p2px
12981298
* @param {number} v - The value to convert.
@@ -1303,7 +1303,7 @@ Phaser.Math = {
13031303
},
13041304

13051305
/**
1306-
* Convert pixel value to p2 physics scale.
1306+
* Convert pixel value to p2 physics scale (meters).
13071307
*
13081308
* @method Phaser.Math#px2p
13091309
* @param {number} v - The value to convert.

src/physics/Body.js

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
6363
*/
6464
this.gravity = new Phaser.Point();
6565

66+
/**
67+
* A Body can be set to collide against the World bounds automatically if this is set to true. Otherwise it will leave the World.
68+
* Note that this only applies if your World has bounds! The response to the collision should be managed via CollisionMaterials.
69+
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
70+
*/
71+
this.collideWorldBounds = true;
72+
73+
/**
74+
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
75+
* @private
76+
*/
77+
this.collidesWith = [];
78+
6679
// this.onAdded = new Phaser.Signal();
6780
// this.onRemoved = new Phaser.Signal();
6881

@@ -77,6 +90,66 @@ Phaser.Physics.Body = function (game, sprite, x, y, mass) {
7790

7891
Phaser.Physics.Body.prototype = {
7992

93+
setCollisionGroup: function (group, shape) {
94+
95+
if (typeof shape === 'undefined')
96+
{
97+
for (var i = this.data.shapes.length - 1; i >= 0; i--)
98+
{
99+
this.data.shapes[i].collisionGroup = group.mask;
100+
}
101+
}
102+
else
103+
{
104+
shape.collisionGroup = group.mask;
105+
}
106+
107+
},
108+
109+
/**
110+
* Adds the given CollisionGroup to the list of groups that this body will collide with and updates the collision mask.
111+
*
112+
* @method Phaser.Physics.Body#collides
113+
* @param {Phaser.Physics.CollisionGroup} group - The Collision Group that this Bodies shapes will collide with.
114+
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision mask will be added to all Shapes in this Body.
115+
*/
116+
collides: function (group, shape) {
117+
118+
// TODO: group can be an array
119+
120+
if (this.collidesWith.indexOf(group) === -1)
121+
{
122+
this.collidesWith.push(group);
123+
}
124+
125+
var mask = 0;
126+
127+
if (this.collideWorldBounds)
128+
{
129+
mask = this.game.physics.boundsCollisionGroup.mask;
130+
}
131+
132+
for (var i = 0; i < this.collidesWith.length; i++)
133+
{
134+
mask = mask | this.collidesWith[i].mask;
135+
}
136+
137+
if (typeof shape === 'undefined')
138+
{
139+
for (var i = this.data.shapes.length - 1; i >= 0; i--)
140+
{
141+
this.data.shapes[i].collisionMask = mask;
142+
}
143+
}
144+
else
145+
{
146+
shape.collisionMask = mask;
147+
}
148+
149+
console.log('collides', this.sprite.name, group, mask);
150+
151+
},
152+
80153
/**
81154
* Moves the shape offsets so their center of mass becomes the body center of mass.
82155
*
@@ -819,7 +892,7 @@ Phaser.Physics.Body.prototype = {
819892
},
820893

821894
/**
822-
* Convert p2 physics value to pixel scale.
895+
* Convert p2 physics value (meters) to pixel scale.
823896
*
824897
* @method Phaser.Math#p2px
825898
* @param {number} v - The value to convert.
@@ -832,7 +905,7 @@ Phaser.Physics.Body.prototype = {
832905
},
833906

834907
/**
835-
* Convert pixel value to p2 physics scale.
908+
* Convert pixel value to p2 physics scale (meters).
836909
*
837910
* @method Phaser.Math#px2p
838911
* @param {number} v - The value to convert.

src/physics/CollisionGroup.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Collision Group
9+
*
10+
* @class Phaser.Physics.CollisionGroup
11+
* @classdesc Physics Collision Group Constructor
12+
* @constructor
13+
*/
14+
Phaser.Physics.CollisionGroup = function (bitmask) {
15+
16+
/**
17+
* @property {number} mask - The CollisionGroup bitmask.
18+
*/
19+
this.mask = bitmask;
20+
21+
}

0 commit comments

Comments
 (0)