mirror of
https://github.com/photonstorm/phaser
synced 2024-11-10 23:24:41 +00:00
Prismatic Constraint done. That's all of them! jshint time.
This commit is contained in:
parent
87684bb15f
commit
c3f687eda9
6 changed files with 220 additions and 18 deletions
59
examples/p2 physics/lock constraint.js
Normal file
59
examples/p2 physics/lock constraint.js
Normal file
|
@ -0,0 +1,59 @@
|
|||
|
||||
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
|
||||
|
||||
function preload() {
|
||||
|
||||
game.load.image('vu', 'assets/sprites/vu.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 vu1 = game.add.sprite(400, 300, 'vu');
|
||||
|
||||
game.physics.p2.enable([sprite, vu1]);
|
||||
|
||||
// Lock the two bodies together. The [0, 50] sets the distance apart (y: 80)
|
||||
var constraint = game.physics.p2.createLockConstraint(sprite, vu1, [0, 80], 0);
|
||||
|
||||
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite.body.moveLeft(100);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
sprite.body.moveRight(100);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
sprite.body.moveUp(100);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
sprite.body.moveDown(100);
|
||||
}
|
||||
|
||||
|
||||
}
|
73
examples/p2 physics/prismatic constraint.js
Normal file
73
examples/p2 physics/prismatic constraint.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
|
||||
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/atari800xl.png');
|
||||
game.load.image('lift', 'assets/sprites/flectrum.png');
|
||||
game.load.image('sky', 'assets/skies/cavern2.png');
|
||||
|
||||
}
|
||||
|
||||
var sprite;
|
||||
var vu1;
|
||||
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(200, 400, 'atari');
|
||||
|
||||
vu1 = game.add.sprite(400, 400, 'lift');
|
||||
|
||||
game.physics.p2.enable([sprite, vu1]);
|
||||
|
||||
sprite.body.fixedRotation = true;
|
||||
vu1.body.fixedRotation = true;
|
||||
|
||||
var constraint = game.physics.p2.createPrismaticConstraint(sprite, vu1, false, [150, 0], [-15, 0], [0, 1]);
|
||||
|
||||
// You can also set limits:
|
||||
/*
|
||||
constraint.upperLimitEnabled = true;
|
||||
constraint.upperLimit = game.physics.p2.pxm(0.5);
|
||||
constraint.lowerLimitEnabled = true;
|
||||
constraint.lowerLimit = game.physics.p2.pxm(-0.5);
|
||||
*/
|
||||
|
||||
text = game.add.text(20, 20, 'move with arrow keys', { fill: '#ffffff' });
|
||||
|
||||
cursors = game.input.keyboard.createCursorKeys();
|
||||
|
||||
}
|
||||
|
||||
function update() {
|
||||
|
||||
sprite.body.setZeroVelocity();
|
||||
vu1.body.setZeroVelocity();
|
||||
|
||||
if (cursors.left.isDown)
|
||||
{
|
||||
sprite.body.moveLeft(200);
|
||||
}
|
||||
else if (cursors.right.isDown)
|
||||
{
|
||||
sprite.body.moveRight(200);
|
||||
}
|
||||
|
||||
if (cursors.up.isDown)
|
||||
{
|
||||
vu1.body.moveUp(200);
|
||||
}
|
||||
else if (cursors.down.isDown)
|
||||
{
|
||||
vu1.body.moveDown(200);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -18,6 +18,8 @@
|
|||
*/
|
||||
Phaser.Physics.P2.DistanceConstraint = function (world, bodyA, bodyB, distance, maxForce) {
|
||||
|
||||
if (typeof distance === 'undefined') { distance = 100; }
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
*/
|
||||
|
@ -28,8 +30,6 @@ Phaser.Physics.P2.DistanceConstraint = function (world, bodyA, bodyB, distance,
|
|||
*/
|
||||
this.world = world;
|
||||
|
||||
if (typeof distance === 'undefined') { distance = 100; }
|
||||
|
||||
distance = world.pxm(distance);
|
||||
|
||||
p2.DistanceConstraint.call(this, bodyA, bodyB, distance, maxForce);
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* Connects two bodies at given offset points, letting them rotate relative to each other around this point.
|
||||
* Locks the relative position between two bodies.
|
||||
*
|
||||
* @class Phaser.Physics.P2.LockConstraint
|
||||
* @classdesc Physics LockConstraint Constructor
|
||||
|
@ -13,13 +13,15 @@
|
|||
* @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.
|
||||
* @param {Array} [offset] - The offset of bodyB in bodyA's frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {number} [angle=0] - The angle of bodyB in bodyA's frame.
|
||||
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
|
||||
*/
|
||||
Phaser.Physics.P2.LockConstraint = function (world, bodyA, bodyB, angle, ratio) {
|
||||
Phaser.Physics.P2.LockConstraint = function (world, bodyA, bodyB, offset, angle, maxForce) {
|
||||
|
||||
if (typeof offset === 'undefined') { offset = [0, 0]; }
|
||||
if (typeof angle === 'undefined') { angle = 0; }
|
||||
if (typeof ratio === 'undefined') { ratio = 1; }
|
||||
if (typeof maxForce === 'undefined') { maxForce = Number.MAX_VALUE; }
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
|
@ -31,7 +33,9 @@ Phaser.Physics.P2.LockConstraint = function (world, bodyA, bodyB, angle, ratio)
|
|||
*/
|
||||
this.world = world;
|
||||
|
||||
var options = { angle: angle, ratio: ratio };
|
||||
offset = [ world.pxm(offset[0]), world.pxm(offset[1]) ];
|
||||
|
||||
var options = { localOffsetB: offset, localAngleB: angle, maxForce: maxForce };
|
||||
|
||||
p2.LockConstraint.call(this, bodyA, bodyB, options);
|
||||
|
||||
|
|
|
@ -13,13 +13,19 @@
|
|||
* @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.
|
||||
* @param {boolean} [lock=false] - If set to true, bodyB will be free to rotate around its anchor point.
|
||||
* @param {Array} [anchorA] - Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Array} [anchorB] - Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Array} [axis] - An axis, defined in body A frame, that body B's anchor point may slide along. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
|
||||
*/
|
||||
Phaser.Physics.P2.PrismaticConstraint = function (world, bodyA, bodyB, angle, ratio) {
|
||||
Phaser.Physics.P2.PrismaticConstraint = function (world, bodyA, bodyB, lock, anchorA, anchorB, axis, maxForce) {
|
||||
|
||||
if (typeof angle === 'undefined') { angle = 0; }
|
||||
if (typeof ratio === 'undefined') { ratio = 1; }
|
||||
if (typeof lock === 'undefined') { lock = false; }
|
||||
if (typeof anchorA === 'undefined') { anchorA = [0, 0]; }
|
||||
if (typeof anchorB === 'undefined') { anchorB = [0, 0]; }
|
||||
if (typeof axis === 'undefined') { axis = [0, 0]; }
|
||||
if (typeof maxForce === 'undefined') { maxForce = Number.MAX_VALUE; }
|
||||
|
||||
/**
|
||||
* @property {Phaser.Game} game - Local reference to game.
|
||||
|
@ -31,7 +37,10 @@ Phaser.Physics.P2.PrismaticConstraint = function (world, bodyA, bodyB, angle, ra
|
|||
*/
|
||||
this.world = world;
|
||||
|
||||
var options = { angle: angle, ratio: ratio };
|
||||
anchorA = [ world.pxmi(anchorA[0]), world.pxmi(anchorA[1]) ];
|
||||
anchorB = [ world.pxmi(anchorB[0]), world.pxmi(anchorB[1]) ];
|
||||
|
||||
var options = { localAnchorA: anchorA, localAnchorB: anchorB, localAxisA: axis, maxForce: maxForce, disableRotationalLock: lock };
|
||||
|
||||
p2.PrismaticConstraint.call(this, bodyA, bodyB, options);
|
||||
|
||||
|
|
|
@ -752,10 +752,10 @@ Phaser.Physics.P2.prototype = {
|
|||
* The pivot points are given in world (pixel) coordinates.
|
||||
*
|
||||
* @method Phaser.Physics.P2#createRevoluteConstraint
|
||||
* @param {p2.Body} bodyA - First connected body.
|
||||
* @param {Float32Array} pivotA - The point relative to the center of mass of bodyA which bodyA is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {p2.Body} bodyB - Second connected body.
|
||||
* @param {Float32Array} pivotB - The point relative to the center of mass of bodyB which bodyB is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body.
|
||||
* @param {Array} pivotA - The point relative to the center of mass of bodyA which bodyA is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body.
|
||||
* @param {Array} pivotB - The point relative to the center of mass of bodyB which bodyB is constrained to. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {number} [maxForce=0] - The maximum force that should be applied to constrain the bodies.
|
||||
* @return {Phaser.Physics.P2.RevoluteConstraint} The constraint
|
||||
*/
|
||||
|
@ -775,6 +775,63 @@ Phaser.Physics.P2.prototype = {
|
|||
|
||||
},
|
||||
|
||||
/**
|
||||
* Locks the relative position between two bodies.
|
||||
*
|
||||
* @method Phaser.Physics.P2#createLockConstraint
|
||||
* @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 {Array} [offset] - The offset of bodyB in bodyA's frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {number} [angle=0] - The angle of bodyB in bodyA's frame.
|
||||
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
|
||||
* @return {Phaser.Physics.P2.LockConstraint} The constraint
|
||||
*/
|
||||
createLockConstraint: function (bodyA, bodyB, offset, angle, 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.LockConstraint(this, bodyA, bodyB, offset, angle, maxForce));
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Constraint that only allows bodies to move along a line, relative to each other.
|
||||
* See http://www.iforce2d.net/b2dtut/joints-prismatic
|
||||
*
|
||||
* @method Phaser.Physics.P2#createPrismaticConstraint
|
||||
* @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 {boolean} [lock=false] - If set to true, bodyB will be free to rotate around its anchor point.
|
||||
* @param {Array} [anchorA] - Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Array} [anchorB] - Body A's anchor point, defined in its own local frame. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {Array} [axis] - An axis, defined in body A frame, that body B's anchor point may slide along. The value is an array with 2 elements matching x and y, i.e: [32, 32].
|
||||
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
|
||||
* @return {Phaser.Physics.P2.PrismaticConstraint} The constraint
|
||||
*/
|
||||
createPrismaticConstraint: function (bodyA, bodyB, lock, anchorA, anchorB, axis, 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.PrismaticConstraint(this, bodyA, bodyB, lock, anchorA, anchorB, axis, maxForce));
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Adds a Constraint to the world.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue