From 1e29e28333b860ee97bf8f3ce5e9d5cad51a8368 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 18 Feb 2014 16:37:53 +0000 Subject: [PATCH] Added ContactMaterials and lots of new World help functions for assigning and creating them. --- src/physics/Body.js | 24 +++++++++++++++ src/physics/Material.js | 8 ++++- src/physics/World.js | 65 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/physics/Body.js b/src/physics/Body.js index 556ff5db..8fc62f6c 100644 --- a/src/physics/Body.js +++ b/src/physics/Body.js @@ -735,6 +735,30 @@ Phaser.Physics.Body.prototype = { }, + /** + * Adds the given Material to all Shapes that belong to this Body. + * If you only wish to apply it to a specific Shape in this Body then provide that as the 2nd parameter. + * + * @method Phaser.Physics.Body#setMaterial + * @param {Phaser.Physics.Material} material - The Material that will be applied. + * @param {p2.Shape} [shape] - An optional Shape. If not provided the Material will be added to all Shapes in this Body. + */ + setMaterial: function (material, shape) { + + if (typeof shape === 'undefined') + { + for (var i = this.data.shapes.length - 1; i >= 0; i--) + { + this.data.shapes[i].material = material; + } + } + else + { + shape.material = material; + } + + }, + /** * Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body. * diff --git a/src/physics/Material.js b/src/physics/Material.js index 6560e7b2..884ac525 100644 --- a/src/physics/Material.js +++ b/src/physics/Material.js @@ -11,7 +11,13 @@ * @classdesc Physics Material Constructor * @constructor */ -Phaser.Physics.Material = function () { +Phaser.Physics.Material = function (name) { + + /** + * @property {string} name - The user defined name given to this Material. + * @default + */ + this.name = name; p2.Material.call(this); diff --git a/src/physics/World.js b/src/physics/World.js index aee999f4..f0372b36 100644 --- a/src/physics/World.js +++ b/src/physics/World.js @@ -32,8 +32,18 @@ Phaser.Physics.World = function (game) { */ this.game = game; + /** + * @property {p2.World} game - The p2 World in which the simulation is run. + * @protected + */ this.world = new p2.World( { gravity: [0, 0] }); + /** + * @property {array} materials - A local array of all created Materials. + * @protected + */ + this.materials = []; + /** * @property {Phaser.InversePointProxy} gravity - The gravity applied to all bodies each step. */ @@ -453,16 +463,67 @@ Phaser.Physics.World.prototype = { }, + /** + * Sets the given Material against all Shapes owned by all the Bodies in the given array. + * + * @method Phaser.Physics.World#setMaterial + * @param {Phaser.Physics.Material} material - The Material to be applied to the given Bodies. + * @param {array} bodies - An Array of Body objects that the given Material will be set on. + */ + setMaterial: function (material, bodies) { + + var i = bodies.length; + + while (i--) + { + bodies.setMaterial(material); + } + + }, + + /** + * Creates a Material. Materials are applied to Shapes owned by a Body and can be set with Body.setMaterial(). + * Materials are a way to control what happens when Shapes collide. Combine unique Materials together to create Contact Materials. + * Contact Materials have properties such as friction and restitution that allow for fine-grained collision control between different Materials. + * + * @method Phaser.Physics.World#createMaterial + * @param {string} [name] - Optional name of the Material. Each Material has a unique ID but string names are handy for debugging. + * @param {Phaser.Physics.Body} [body] - Optional Body. If given it will assign the newly created Material to the Body shapes. + * @return {Phaser.Physics.Material} The Material that was created. This is also stored in Phaser.Physics.World.materials. + */ + createMaterial: function (name, body) { + + name = name || ''; + + var material = new Phaser.Physics.Material(name); + + this.materials.push(material); + + if (typeof body !== 'undefined') + { + body.setMaterial(material); + } + + return material; + + }, + /** * Creates a Contact Material from the two given Materials. You can then edit the properties of the Contact Material directly. * * @method Phaser.Physics.World#createContactMaterial - * @param {Phaser.Physics.Material} materialA - The first Material to create the ContactMaterial from. - * @param {Phaser.Physics.Material} materialB - The second Material to create the ContactMaterial from. + * @param {Phaser.Physics.Material} [materialA] - The first Material to create the ContactMaterial from. If undefined it will create a new Material object first. + * @param {Phaser.Physics.Material} [materialB] - The second Material to create the ContactMaterial from. If undefined it will create a new Material object first. * @return {Phaser.Physics.ContactMaterial} The Contact Material that was created. */ createContactMaterial: function (materialA, materialB) { + if (typeof materialA === 'undefined') { materialA = this.createMaterial(); } + if (typeof materialB === 'undefined') { materialB = this.createMaterial(); } + + var contact = new Phaser.Physics.ContactMaterial(materialA, materialB, options); + + return this.addContactMaterial(contact); },