Skip to content

Commit 1e29e28

Browse files
committed
Added ContactMaterials and lots of new World help functions for assigning and creating them.
1 parent 15be3f8 commit 1e29e28

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

src/physics/Body.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,30 @@ Phaser.Physics.Body.prototype = {
735735

736736
},
737737

738+
/**
739+
* Adds the given Material to all Shapes that belong to this Body.
740+
* If you only wish to apply it to a specific Shape in this Body then provide that as the 2nd parameter.
741+
*
742+
* @method Phaser.Physics.Body#setMaterial
743+
* @param {Phaser.Physics.Material} material - The Material that will be applied.
744+
* @param {p2.Shape} [shape] - An optional Shape. If not provided the Material will be added to all Shapes in this Body.
745+
*/
746+
setMaterial: function (material, shape) {
747+
748+
if (typeof shape === 'undefined')
749+
{
750+
for (var i = this.data.shapes.length - 1; i >= 0; i--)
751+
{
752+
this.data.shapes[i].material = material;
753+
}
754+
}
755+
else
756+
{
757+
shape.material = material;
758+
}
759+
760+
},
761+
738762
/**
739763
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
740764
*

src/physics/Material.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@
1111
* @classdesc Physics Material Constructor
1212
* @constructor
1313
*/
14-
Phaser.Physics.Material = function () {
14+
Phaser.Physics.Material = function (name) {
15+
16+
/**
17+
* @property {string} name - The user defined name given to this Material.
18+
* @default
19+
*/
20+
this.name = name;
1521

1622
p2.Material.call(this);
1723

src/physics/World.js

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ Phaser.Physics.World = function (game) {
3232
*/
3333
this.game = game;
3434

35+
/**
36+
* @property {p2.World} game - The p2 World in which the simulation is run.
37+
* @protected
38+
*/
3539
this.world = new p2.World( { gravity: [0, 0] });
3640

41+
/**
42+
* @property {array<Phaser.Physics.Material>} materials - A local array of all created Materials.
43+
* @protected
44+
*/
45+
this.materials = [];
46+
3747
/**
3848
* @property {Phaser.InversePointProxy} gravity - The gravity applied to all bodies each step.
3949
*/
@@ -453,16 +463,67 @@ Phaser.Physics.World.prototype = {
453463

454464
},
455465

466+
/**
467+
* Sets the given Material against all Shapes owned by all the Bodies in the given array.
468+
*
469+
* @method Phaser.Physics.World#setMaterial
470+
* @param {Phaser.Physics.Material} material - The Material to be applied to the given Bodies.
471+
* @param {array<Phaser.Physics.Body>} bodies - An Array of Body objects that the given Material will be set on.
472+
*/
473+
setMaterial: function (material, bodies) {
474+
475+
var i = bodies.length;
476+
477+
while (i--)
478+
{
479+
bodies.setMaterial(material);
480+
}
481+
482+
},
483+
484+
/**
485+
* Creates a Material. Materials are applied to Shapes owned by a Body and can be set with Body.setMaterial().
486+
* Materials are a way to control what happens when Shapes collide. Combine unique Materials together to create Contact Materials.
487+
* Contact Materials have properties such as friction and restitution that allow for fine-grained collision control between different Materials.
488+
*
489+
* @method Phaser.Physics.World#createMaterial
490+
* @param {string} [name] - Optional name of the Material. Each Material has a unique ID but string names are handy for debugging.
491+
* @param {Phaser.Physics.Body} [body] - Optional Body. If given it will assign the newly created Material to the Body shapes.
492+
* @return {Phaser.Physics.Material} The Material that was created. This is also stored in Phaser.Physics.World.materials.
493+
*/
494+
createMaterial: function (name, body) {
495+
496+
name = name || '';
497+
498+
var material = new Phaser.Physics.Material(name);
499+
500+
this.materials.push(material);
501+
502+
if (typeof body !== 'undefined')
503+
{
504+
body.setMaterial(material);
505+
}
506+
507+
return material;
508+
509+
},
510+
456511
/**
457512
* Creates a Contact Material from the two given Materials. You can then edit the properties of the Contact Material directly.
458513
*
459514
* @method Phaser.Physics.World#createContactMaterial
460-
* @param {Phaser.Physics.Material} materialA - The first Material to create the ContactMaterial from.
461-
* @param {Phaser.Physics.Material} materialB - The second Material to create the ContactMaterial from.
515+
* @param {Phaser.Physics.Material} [materialA] - The first Material to create the ContactMaterial from. If undefined it will create a new Material object first.
516+
* @param {Phaser.Physics.Material} [materialB] - The second Material to create the ContactMaterial from. If undefined it will create a new Material object first.
462517
* @return {Phaser.Physics.ContactMaterial} The Contact Material that was created.
463518
*/
464519
createContactMaterial: function (materialA, materialB) {
465520

521+
if (typeof materialA === 'undefined') { materialA = this.createMaterial(); }
522+
if (typeof materialB === 'undefined') { materialB = this.createMaterial(); }
523+
524+
var contact = new Phaser.Physics.ContactMaterial(materialA, materialB, options);
525+
526+
return this.addContactMaterial(contact);
466527

467528
},
468529

0 commit comments

Comments
 (0)