New labs demo. Added moveForward and moveBackward to Body.

This commit is contained in:
photonstorm
2014-02-18 04:49:03 +00:00
parent 5d5c64d22f
commit 63145d7735
6 changed files with 439 additions and 0 deletions
+35
View File
@@ -205,6 +205,40 @@ Phaser.Physics.Body.prototype = {
},
/**
* Moves the Body forwards based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.Body#moveForward
* @param {number} speed - The speed at which it should move forwards.
*/
moveForward: function (speed) {
var magnitude = this.px2p(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.velocity[0] = magnitude * Math.cos(angle);
this.data.velocity[1] = magnitude * Math.sin(angle);
},
/**
* Moves the Body backwards based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.Body#moveBackward
* @param {number} speed - The speed at which it should move backwards.
*/
moveBackward: function (speed) {
var magnitude = this.px2p(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.velocity[0] = -(magnitude * Math.cos(angle));
this.data.velocity[1] = -(magnitude * Math.sin(angle));
},
/**
* Applies a force to the Body that causes it to 'thrust' forwards, based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
@@ -859,6 +893,7 @@ Object.defineProperty(Phaser.Physics.Body.prototype, "kinematic", {
if (value && this.data.motionState !== Phaser.KINEMATIC)
{
this.data.motionState = Phaser.KINEMATIC;
this.mass = 4;
}
else if (!value && this.data.motionState === Phaser.KINEMATIC)
{
+44
View File
@@ -30,6 +30,11 @@ Phaser.Physics.World = function (game) {
*/
this.game = game;
/**
* @property {Phaser.PointProxy} force - The force applied to the body.
*/
// this.gravity = new Phaser.Physics.PointProxy(this.data.force);
this.onBodyAdded = new Phaser.Signal();
this.onBodyRemoved = new Phaser.Signal();
@@ -271,3 +276,42 @@ Phaser.Physics.World.prototype.createBody = function (x, y, mass, addToWorld, op
return body;
};
/**
* @method Phaser.Physics.World.prototype.createBody
* @param {number} x - The x coordinate of Body.
* @param {number} y - The y coordinate of Body.
* @param {number} mass - The mass of the Body. A mass of 0 means a 'static' Body is created.
* @param {boolean} [addToWorld=false] - Automatically add this Body to the world? (usually false as it won't have any shapes on construction).
* @param {object} options - An object containing the build options:
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
*/
Phaser.Physics.World.prototype.createParticle = function (x, y, mass, addToWorld, options, data) {
if (typeof addToWorld === 'undefined') { addToWorld = false; }
var body = new Phaser.Physics.Body(this.game, null, x, y, mass);
if (data)
{
var result = body.addPolygon(options, data);
if (!result)
{
return false;
}
}
if (addToWorld)
{
this.addBody(body.data);
}
return body;
};