P2 Springs example done and working nicely.

This commit is contained in:
photonstorm 2014-03-13 10:32:34 +00:00
parent aa6b1821bd
commit ef359e8992
4 changed files with 80 additions and 36 deletions

View file

@ -21,16 +21,17 @@ function create() {
game.physics.startSystem(Phaser.Physics.P2JS);
// Add 2 sprites which we'll join with a spring
sprite1 = game.add.sprite(200, 100, 'ball');
sprite2 = game.add.sprite(200, 300, 'atari');
sprite1 = game.add.sprite(400, 300, 'ball');
sprite2 = game.add.sprite(400, 400, 'atari');
game.physics.p2.enable([sprite1, sprite2]);
sprite2.body.collideWorldBounds = false;
// Create our spring
// var spring = game.physics.p2.createSpring(sprite1, sprite2, restLength, stiffness, damping, worldA, worldB, localA, localB);
// var spring = game.physics.p2.createSpring(sprite1, sprite2);
// The parameters are: createSpring(sprite1, sprite2, restLength, stiffness, damping, worldA, worldB, localA, localB)
// See the docs for more details
var spring = game.physics.p2.createSpring(sprite1, sprite2, 150, 5, 1);
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });
@ -40,24 +41,16 @@ function create() {
function update() {
// sprite.body.setZeroVelocity();
sprite1.body.setZeroVelocity();
// if (cursors.left.isDown)
// {
// sprite.body.moveLeft(400);
// }
// else if (cursors.right.isDown)
// {
// sprite.body.moveRight(400);
// }
// if (cursors.up.isDown)
// {
// sprite.body.moveUp(400);
// }
// else if (cursors.down.isDown)
// {
// sprite.body.moveDown(400);
// }
if (cursors.left.isDown)
{
sprite1.body.moveLeft(400);
}
else if (cursors.right.isDown)
{
sprite1.body.moveRight(400);
}
}

View file

@ -74,14 +74,6 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
*/
this.gravity = new Phaser.Point();
/**
* A Body can be set to collide against the World bounds automatically if this is set to true. Otherwise it will leave the World.
* Note that this only applies if your World has bounds! The response to the collision should be managed via CollisionMaterials.
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
* @default
*/
this.collideWorldBounds = true;
/**
* Dispatched when the shape/s of this Body impact with another. The event will be sent 2 parameters, this Body and the impact Body.
* @property {Phaser.Signal} onImpact
@ -113,6 +105,8 @@ Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
*/
this.safeRemove = false;
this._collideWorldBounds = true;
/**
* @property {object} _bodyCallbacks - Array of Body callbacks.
* @private
@ -231,7 +225,7 @@ Phaser.Physics.P2.Body.prototype = {
var mask = 0;
if (this.collideWorldBounds)
if (this._collideWorldBounds)
{
mask = this.game.physics.p2.boundsCollisionGroup.mask;
}
@ -245,6 +239,30 @@ Phaser.Physics.P2.Body.prototype = {
},
/**
* Updates the collisionMask.
*
* @method Phaser.Physics.P2.Body#updateCollisionMask
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision group will be added to all Shapes in this Body.
*/
updateCollisionMask: function (shape) {
var mask = this.getCollisionMask();
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionMask = mask;
}
}
else
{
shape.collisionMask = mask;
}
},
/**
* Sets the given CollisionGroup to be the collision group for all shapes in this Body, unless a shape is specified.
* This also resets the collisionMask.
@ -1616,4 +1634,35 @@ Object.defineProperty(Phaser.Physics.P2.Body.prototype, "debug", {
}
});
});
/**
* A Body can be set to collide against the World bounds automatically if this is set to true. Otherwise it will leave the World.
* Note that this only applies if your World has bounds! The response to the collision should be managed via CollisionMaterials.
* @name Phaser.Physics.P2.Body#collideWorldBounds
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "collideWorldBounds", {
get: function () {
return this._collideWorldBounds;
},
set: function (value) {
if (value && !this._collideWorldBounds)
{
this._collideWorldBounds = true;
this.updateCollisionMask();
}
else if (!value && this._collideWorldBounds)
{
this._collideWorldBounds = false;
this.updateCollisionMask();
}
}
});

View file

@ -37,6 +37,8 @@ Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness,
if (typeof stiffness === 'undefined') { stiffness = 100; }
if (typeof damping === 'undefined') { damping = 1; }
restLength = world.pxm(restLength);
var options = {
restLength: restLength,
stiffness: stiffness,

View file

@ -871,7 +871,7 @@ Phaser.Physics.P2.prototype = {
*/
getBody: function (object) {
if (object['world'])
if (object instanceof p2.Body)
{
// Native p2 body
return object;
@ -1088,7 +1088,7 @@ Phaser.Physics.P2.prototype = {
}
else
{
return new Phaser.Physics.P2.Spring(this, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB);
return this.addSpring(new Phaser.Physics.P2.Spring(this, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB));
}
},