Skip to content

Commit 87684bb

Browse files
committed
Revolute Constraint done.
1 parent 1501eb9 commit 87684bb

9 files changed

Lines changed: 220 additions & 7 deletions

File tree

Gruntfile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ module.exports = function (grunt) {
207207
'src/physics/p2/Spring.js',
208208
'src/physics/p2/Material.js',
209209
'src/physics/p2/ContactMaterial.js',
210-
'src/physics/p2/CollisionGroup.js'
210+
'src/physics/p2/CollisionGroup.js',
211+
'/src/physics/p2/DistanceConstraint.js',
212+
'/src/physics/p2/GearConstraint.js',
213+
'/src/physics/p2/LockConstraint.js',
214+
'/src/physics/p2/PrismaticConstraint.js',
215+
'/src/physics/p2/RevoluteConstraint.js'
211216
],
212217

213218
ninja: [

build/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
<script src="$path/src/physics/p2/CollisionGroup.js"></script>
159159
<script src="$path/src/physics/p2/DistanceConstraint.js"></script>
160160
<script src="$path/src/physics/p2/GearConstraint.js"></script>
161+
<script src="$path/src/physics/p2/LockConstraint.js"></script>
162+
<script src="$path/src/physics/p2/PrismaticConstraint.js"></script>
163+
<script src="$path/src/physics/p2/RevoluteConstraint.js"></script>
161164
162165
<script src="$path/src/particles/Particles.js"></script>
163166
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
game.load.image('vu', 'assets/sprites/vu.png');
7+
game.load.image('ball', 'assets/sprites/arrow.png');
8+
game.load.image('sky', 'assets/skies/cavern2.png');
9+
10+
}
11+
12+
var sprite;
13+
var cursors;
14+
15+
function create() {
16+
17+
game.add.image(0, 0, 'sky');
18+
19+
// Enable p2 physics
20+
game.physics.startSystem(Phaser.Physics.P2JS);
21+
22+
// Add 2 sprites which we'll join with a constraint
23+
sprite = game.add.sprite(400, 300, 'ball');
24+
25+
var vu1 = game.add.sprite(400, 300, 'vu');
26+
27+
game.physics.p2.enable([sprite, vu1]);
28+
29+
// So they don't collide with each other
30+
sprite.body.clearCollision(true, true);
31+
vu1.body.clearCollision(true, true);
32+
33+
var constraint = game.physics.p2.createRevoluteConstraint(sprite, [ 50, 100 ], vu1, [ 0, 0 ]);
34+
35+
text = game.add.text(20, 20, 'rotate with arrow keys', { fill: '#ffffff' });
36+
37+
cursors = game.input.keyboard.createCursorKeys();
38+
39+
}
40+
41+
function update() {
42+
43+
if (cursors.left.isDown)
44+
{
45+
sprite.body.rotateLeft(50);
46+
}
47+
else if (cursors.right.isDown)
48+
{
49+
sprite.body.rotateRight(50);
50+
}
51+
52+
}

src/physics/p2/DistanceConstraint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @param {p2.Body} bodyA - First connected body.
1515
* @param {p2.Body} bodyB - Second connected body.
1616
* @param {number} distance - The distance to keep between the bodies.
17-
* @param {number} [maxForce] - The maximum force to apply to the constraint
17+
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
1818
*/
1919
Phaser.Physics.P2.DistanceConstraint = function (world, bodyA, bodyB, distance, maxForce) {
2020

src/physics/p2/LockConstraint.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Connects two bodies at given offset points, letting them rotate relative to each other around this point.
9+
*
10+
* @class Phaser.Physics.P2.LockConstraint
11+
* @classdesc Physics LockConstraint Constructor
12+
* @constructor
13+
* @param {Phaser.Physics.P2} world - A reference to the P2 World.
14+
* @param {p2.Body} bodyA - First connected body.
15+
* @param {p2.Body} bodyB - Second connected body.
16+
* @param {number} [angle=0] - The relative angle
17+
* @param {number} [ratio=1] - The gear ratio.
18+
*/
19+
Phaser.Physics.P2.LockConstraint = function (world, bodyA, bodyB, angle, ratio) {
20+
21+
if (typeof angle === 'undefined') { angle = 0; }
22+
if (typeof ratio === 'undefined') { ratio = 1; }
23+
24+
/**
25+
* @property {Phaser.Game} game - Local reference to game.
26+
*/
27+
this.game = world.game;
28+
29+
/**
30+
* @property {Phaser.Physics.P2} world - Local reference to P2 World.
31+
*/
32+
this.world = world;
33+
34+
var options = { angle: angle, ratio: ratio };
35+
36+
p2.LockConstraint.call(this, bodyA, bodyB, options);
37+
38+
}
39+
40+
Phaser.Physics.P2.LockConstraint.prototype = Object.create(p2.LockConstraint.prototype);
41+
Phaser.Physics.P2.LockConstraint.prototype.constructor = Phaser.Physics.P2.LockConstraint;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Connects two bodies at given offset points, letting them rotate relative to each other around this point.
9+
*
10+
* @class Phaser.Physics.P2.PrismaticConstraint
11+
* @classdesc Physics PrismaticConstraint Constructor
12+
* @constructor
13+
* @param {Phaser.Physics.P2} world - A reference to the P2 World.
14+
* @param {p2.Body} bodyA - First connected body.
15+
* @param {p2.Body} bodyB - Second connected body.
16+
* @param {number} [angle=0] - The relative angle
17+
* @param {number} [ratio=1] - The gear ratio.
18+
*/
19+
Phaser.Physics.P2.PrismaticConstraint = function (world, bodyA, bodyB, angle, ratio) {
20+
21+
if (typeof angle === 'undefined') { angle = 0; }
22+
if (typeof ratio === 'undefined') { ratio = 1; }
23+
24+
/**
25+
* @property {Phaser.Game} game - Local reference to game.
26+
*/
27+
this.game = world.game;
28+
29+
/**
30+
* @property {Phaser.Physics.P2} world - Local reference to P2 World.
31+
*/
32+
this.world = world;
33+
34+
var options = { angle: angle, ratio: ratio };
35+
36+
p2.PrismaticConstraint.call(this, bodyA, bodyB, options);
37+
38+
}
39+
40+
Phaser.Physics.P2.PrismaticConstraint.prototype = Object.create(p2.PrismaticConstraint.prototype);
41+
Phaser.Physics.P2.PrismaticConstraint.prototype.constructor = Phaser.Physics.P2.PrismaticConstraint;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2014 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
/**
8+
* Connects two bodies at given offset points, letting them rotate relative to each other around this point.
9+
* The pivot points are given in world (pixel) coordinates.
10+
*
11+
* @class Phaser.Physics.P2.RevoluteConstraint
12+
* @classdesc Physics RevoluteConstraint Constructor
13+
* @constructor
14+
* @param {Phaser.Physics.P2} world - A reference to the P2 World.
15+
* @param {p2.Body} bodyA - First connected body.
16+
* @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].
17+
* @param {p2.Body} bodyB - Second connected body.
18+
* @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].
19+
* @param {number} [maxForce=0] - The maximum force that should be applied to constrain the bodies.
20+
*/
21+
Phaser.Physics.P2.RevoluteConstraint = function (world, bodyA, pivotA, bodyB, pivotB, maxForce) {
22+
23+
if (typeof maxForce === 'undefined') { maxForce = Number.MAX_VALUE; }
24+
25+
/**
26+
* @property {Phaser.Game} game - Local reference to game.
27+
*/
28+
this.game = world.game;
29+
30+
/**
31+
* @property {Phaser.Physics.P2} world - Local reference to P2 World.
32+
*/
33+
this.world = world;
34+
35+
pivotA = [ world.pxm(pivotA[0]), world.pxm(pivotA[1]) ];
36+
pivotB = [ world.pxm(pivotB[0]), world.pxm(pivotB[1]) ];
37+
38+
p2.RevoluteConstraint.call(this, bodyA, pivotA, bodyB, pivotB, maxForce);
39+
40+
}
41+
42+
Phaser.Physics.P2.RevoluteConstraint.prototype = Object.create(p2.RevoluteConstraint.prototype);
43+
Phaser.Physics.P2.RevoluteConstraint.prototype.constructor = Phaser.Physics.P2.RevoluteConstraint;

src/physics/p2/Spring.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
* @param {number} [restLength=1] - Rest length of the spring. A number > 0.
1717
* @param {number} [stiffness=100] - Stiffness of the spring. A number >= 0.
1818
* @param {number} [damping=1] - Damping of the spring. A number >= 0.
19-
* @param {Array} [worldA] - Where to hook the spring to body A in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32].
20-
* @param {Array} [worldB] - Where to hook the spring to body B in world coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32].
21-
* @param {Array} [localA] - Where to hook the spring to body A in local body coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32].
22-
* @param {Array} [localB] - Where to hook the spring to body B in local body coordinates. This value is an array by 2 elements, x and y, i.e: [32, 32].
19+
* @param {Array} [worldA] - Where to hook the spring to body A in world coordinates. This value is an array with 2 elements matching x and y, i.e: [32, 32].
20+
* @param {Array} [worldB] - Where to hook the spring to body B in world coordinates. This value is an array with 2 elements matching x and y, i.e: [32, 32].
21+
* @param {Array} [localA] - Where to hook the spring to body A in local body coordinates. This value is an array with 2 elements matching x and y, i.e: [32, 32].
22+
* @param {Array} [localB] - Where to hook the spring to body B in local body coordinates. This value is an array with 2 elements matching x and y, i.e: [32, 32].
2323
*/
2424
Phaser.Physics.P2.Spring = function (world, bodyA, bodyB, restLength, stiffness, damping, worldA, worldB, localA, localB) {
2525

src/physics/p2/World.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ Phaser.Physics.P2.prototype = {
702702
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyA - First connected body.
703703
* @param {Phaser.Sprite|Phaser.Physics.P2.Body|p2.Body} bodyB - Second connected body.
704704
* @param {number} distance - The distance to keep between the bodies.
705-
* @param {number} [maxForce] - The maximum force to apply to the constraint
705+
* @param {number} [maxForce] - The maximum force that should be applied to constrain the bodies.
706706
* @return {Phaser.Physics.P2.DistanceConstraint} The constraint
707707
*/
708708
createDistanceConstraint: function (bodyA, bodyB, distance, maxForce) {
@@ -747,6 +747,34 @@ Phaser.Physics.P2.prototype = {
747747

748748
},
749749

750+
/**
751+
* Connects two bodies at given offset points, letting them rotate relative to each other around this point.
752+
* The pivot points are given in world (pixel) coordinates.
753+
*
754+
* @method Phaser.Physics.P2#createRevoluteConstraint
755+
* @param {p2.Body} bodyA - First connected body.
756+
* @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].
757+
* @param {p2.Body} bodyB - Second connected body.
758+
* @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].
759+
* @param {number} [maxForce=0] - The maximum force that should be applied to constrain the bodies.
760+
* @return {Phaser.Physics.P2.RevoluteConstraint} The constraint
761+
*/
762+
createRevoluteConstraint: function (bodyA, pivotA, bodyB, pivotB, maxForce) {
763+
764+
bodyA = this.getBody(bodyA);
765+
bodyB = this.getBody(bodyB);
766+
767+
if (!bodyA || !bodyB)
768+
{
769+
console.warn('Cannot create Constraint, invalid body objects given');
770+
}
771+
else
772+
{
773+
return this.addConstraint(new Phaser.Physics.P2.RevoluteConstraint(this, bodyA, pivotA, bodyB, pivotB, maxForce));
774+
}
775+
776+
},
777+
750778
/**
751779
* Adds a Constraint to the world.
752780
*

0 commit comments

Comments
 (0)