diff --git a/build/config.php b/build/config.php index 24854326ed..9a7f019653 100644 --- a/build/config.php +++ b/build/config.php @@ -156,6 +156,8 @@ + + @@ -170,9 +172,6 @@ /* - - - */ ?> \ No newline at end of file diff --git a/examples/p2 physics/distance constraint.js b/examples/p2 physics/distance constraint.js new file mode 100644 index 0000000000..8cb0f9fd75 --- /dev/null +++ b/examples/p2 physics/distance constraint.js @@ -0,0 +1,60 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); + +function preload() { + + game.load.image('atari', 'assets/sprites/cokecan.png'); + game.load.image('ball', 'assets/sprites/red_ball.png'); + game.load.image('sky', 'assets/skies/cavern2.png'); + +} + +var sprite1; +var sprite2; +var cursors; + +function create() { + + game.add.image(0, 0, 'sky'); + + // Enable p2 physics + game.physics.startSystem(Phaser.Physics.P2JS); + + // Add 2 sprites which we'll join with a spring + sprite1 = game.add.sprite(400, 300, 'ball'); + sprite2 = game.add.sprite(400, 400, 'atari'); + + game.physics.p2.enable([sprite1, sprite2]); + + var constraint = game.physics.p2.createDistanceConstraint(sprite1, sprite2, 150); + + text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' }); + + cursors = game.input.keyboard.createCursorKeys(); + +} + +function update() { + + sprite1.body.setZeroVelocity(); + + if (cursors.left.isDown) + { + sprite1.body.moveLeft(400); + } + else if (cursors.right.isDown) + { + sprite1.body.moveRight(400); + } + + if (cursors.up.isDown) + { + sprite1.body.moveUp(400); + } + else if (cursors.down.isDown) + { + sprite1.body.moveDown(400); + } + +} + diff --git a/examples/p2 physics/gear constraint.js b/examples/p2 physics/gear constraint.js new file mode 100644 index 0000000000..1b1e6ca98a --- /dev/null +++ b/examples/p2 physics/gear constraint.js @@ -0,0 +1,53 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update }); + +function preload() { + + game.load.image('sonic', 'assets/sprites/sonic_havok_sanity.png'); + game.load.image('ball', 'assets/sprites/arrow.png'); + game.load.image('sky', 'assets/skies/cavern2.png'); + +} + +var sprite; +var cursors; + +function create() { + + game.add.image(0, 0, 'sky'); + + // Enable p2 physics + game.physics.startSystem(Phaser.Physics.P2JS); + + // Add 2 sprites which we'll join with a constraint + sprite = game.add.sprite(400, 200, 'ball'); + + var sonic1 = game.add.sprite(200, 400, 'sonic'); + var sonic2 = game.add.sprite(600, 400, 'sonic'); + + game.physics.p2.enable([sprite, sonic1, sonic2]); + + // This constraint will make sure that as sprite rotates, sonic1 matches that rotation + var constraint1 = game.physics.p2.createGearConstraint(sprite, sonic1, 0, 1); + + // This constraint will make sure that as sprite rotates, sonic2 matches that rotation at half the ratio + var constraint2 = game.physics.p2.createGearConstraint(sprite, sonic2, 0, 0.5); + + text = game.add.text(20, 20, 'rotate with arrow keys', { fill: '#ffffff' }); + + cursors = game.input.keyboard.createCursorKeys(); + +} + +function update() { + + if (cursors.left.isDown) + { + sprite.body.rotateLeft(50); + } + else if (cursors.right.isDown) + { + sprite.body.rotateRight(50); + } + +} diff --git a/license.txt b/license.txt index fa6f41455b..c3d5a22806 100644 --- a/license.txt +++ b/license.txt @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2013 Richard Davey, Photon Storm Ltd. +Copyright (c) 2014 Richard Davey, Photon Storm Ltd. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in diff --git a/src/physics/p2/DistanceConstraint.js b/src/physics/p2/DistanceConstraint.js new file mode 100644 index 0000000000..f555e87f97 --- /dev/null +++ b/src/physics/p2/DistanceConstraint.js @@ -0,0 +1,40 @@ +/** +* @author Richard Davey +* @copyright 2014 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +/** +* A constraint that tries to keep the distance between two bodies constant. +* +* @class Phaser.Physics.P2.DistanceConstraint +* @classdesc Physics DistanceConstraint Constructor +* @constructor +* @param {Phaser.Physics.P2} world - A reference to the P2 World. +* @param {p2.Body} bodyA - First connected body. +* @param {p2.Body} bodyB - Second connected body. +* @param {number} distance - The distance to keep between the bodies. +* @param {number} [maxForce] - The maximum force to apply to the constraint +*/ +Phaser.Physics.P2.DistanceConstraint = function (world, bodyA, bodyB, distance, maxForce) { + + /** + * @property {Phaser.Game} game - Local reference to game. + */ + this.game = world.game; + + /** + * @property {Phaser.Physics.P2} world - Local reference to P2 World. + */ + this.world = world; + + if (typeof distance === 'undefined') { distance = 100; } + + distance = world.pxm(distance); + + p2.DistanceConstraint.call(this, bodyA, bodyB, distance, maxForce); + +} + +Phaser.Physics.P2.DistanceConstraint.prototype = Object.create(p2.DistanceConstraint.prototype); +Phaser.Physics.P2.DistanceConstraint.prototype.constructor = Phaser.Physics.P2.DistanceConstraint; diff --git a/src/physics/p2/GearConstraint.js b/src/physics/p2/GearConstraint.js new file mode 100644 index 0000000000..b13fa5a7a6 --- /dev/null +++ b/src/physics/p2/GearConstraint.js @@ -0,0 +1,41 @@ +/** +* @author Richard Davey +* @copyright 2014 Photon Storm Ltd. +* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} +*/ + +/** +* Connects two bodies at given offset points, letting them rotate relative to each other around this point. +* +* @class Phaser.Physics.P2.GearConstraint +* @classdesc Physics GearConstraint Constructor +* @constructor +* @param {Phaser.Physics.P2} world - A reference to the P2 World. +* @param {p2.Body} bodyA - First connected body. +* @param {p2.Body} bodyB - Second connected body. +* @param {number} [angle=0] - The relative angle +* @param {number} [ratio=1] - The gear ratio. +*/ +Phaser.Physics.P2.GearConstraint = function (world, bodyA, bodyB, angle, ratio) { + + if (typeof angle === 'undefined') { angle = 0; } + if (typeof ratio === 'undefined') { ratio = 1; } + + /** + * @property {Phaser.Game} game - Local reference to game. + */ + this.game = world.game; + + /** + * @property {Phaser.Physics.P2} world - Local reference to P2 World. + */ + this.world = world; + + var options = { angle: angle, ratio: ratio }; + + p2.GearConstraint.call(this, bodyA, bodyB, options); + +} + +Phaser.Physics.P2.GearConstraint.prototype = Object.create(p2.GearConstraint.prototype); +Phaser.Physics.P2.GearConstraint.prototype.constructor = Phaser.Physics.P2.GearConstraint; diff --git a/src/physics/p2/World.js b/src/physics/p2/World.js index 7aee6cace8..2a03d92358 100644 --- a/src/physics/p2/World.js +++ b/src/physics/p2/World.js @@ -695,6 +695,58 @@ Phaser.Physics.P2.prototype = { }, + /** + * Creates a constraint that tries to keep the distance between two bodies constant. + * + * @method Phaser.Physics.P2#createDistanceConstraint + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body. + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body. + * @param {number} distance - The distance to keep between the bodies. + * @param {number} [maxForce] - The maximum force to apply to the constraint + * @return {Phaser.Physics.P2.DistanceConstraint} The constraint + */ + createDistanceConstraint: function (bodyA, bodyB, distance, maxForce) { + + bodyA = this.getBody(bodyA); + bodyB = this.getBody(bodyB); + + if (!bodyA || !bodyB) + { + console.warn('Cannot create Constraint, invalid body objects given'); + } + else + { + return this.addConstraint(new Phaser.Physics.P2.DistanceConstraint(this, bodyA, bodyB, distance, maxForce)); + } + + }, + + /** + * Creates a constraint that tries to keep the distance between two bodies constant. + * + * @method Phaser.Physics.P2#createGearConstraint + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body. + * @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body. + * @param {number} [angle=0] - The relative angle + * @param {number} [ratio=1] - The gear ratio. + * @return {Phaser.Physics.P2.GearConstraint} The constraint + */ + createGearConstraint: function (bodyA, bodyB, angle, ratio) { + + bodyA = this.getBody(bodyA); + bodyB = this.getBody(bodyB); + + if (!bodyA || !bodyB) + { + console.warn('Cannot create Constraint, invalid body objects given'); + } + else + { + return this.addConstraint(new Phaser.Physics.P2.GearConstraint(this, bodyA, bodyB, angle, ratio)); + } + + }, + /** * Adds a Constraint to the world. *