Skip to content

Commit

Permalink
Distance and Gear constraints done.
Browse files Browse the repository at this point in the history
  • Loading branch information
photonstorm committed Mar 13, 2014
1 parent ef359e8 commit 6b55fea
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 4 deletions.
5 changes: 2 additions & 3 deletions build/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@
<script src="$path/src/physics/p2/Material.js"></script>
<script src="$path/src/physics/p2/ContactMaterial.js"></script>
<script src="$path/src/physics/p2/CollisionGroup.js"></script>
<script src="$path/src/physics/p2/DistanceConstraint.js"></script>
<script src="$path/src/physics/p2/GearConstraint.js"></script>
<script src="$path/src/particles/Particles.js"></script>
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script>
Expand All @@ -170,9 +172,6 @@

/*
*/

?>
60 changes: 60 additions & 0 deletions examples/p2 physics/distance constraint.js
Original file line number Diff line number Diff line change
@@ -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);
}

}

53 changes: 53 additions & 0 deletions examples/p2 physics/gear constraint.js
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 40 additions & 0 deletions src/physics/p2/DistanceConstraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @author Richard Davey <[email protected]>
* @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;
41 changes: 41 additions & 0 deletions src/physics/p2/GearConstraint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @author Richard Davey <[email protected]>
* @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;
52 changes: 52 additions & 0 deletions src/physics/p2/World.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 6b55fea

Please sign in to comment.