@@ -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