Added ContactMaterials and lots of new World help functions for assigning and creating them.

This commit is contained in:
photonstorm
2014-02-18 16:37:53 +00:00
parent 15be3f86f3
commit 1e29e28333
3 changed files with 94 additions and 3 deletions
+24
View File
@@ -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.
*
+7 -1
View File
@@ -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);
+63 -2
View File
@@ -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<Phaser.Physics.Material>} 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<Phaser.Physics.Body>} 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);
},