2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-02-17 17:54:10 +00:00
|
|
|
* The Physics Body is typically linked to a single Sprite and defines properties that determine how the physics body is simulated.
|
2014-02-12 03:22:49 +00:00
|
|
|
* These properties affect how the body reacts to forces, what forces it generates on itself (to simulate friction), and how it reacts to collisions in the scene.
|
|
|
|
* In most cases, the properties are used to simulate physical effects. Each body also has its own property values that determine exactly how it reacts to forces and collisions in the scene.
|
|
|
|
* By default a single Rectangle shape is added to the Body that matches the dimensions of the parent Sprite. See addShape, removeShape, clearShapes to add extra shapes around the Body.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @class Phaser.Physics.Body
|
|
|
|
* @classdesc Physics Body Constructor
|
|
|
|
* @constructor
|
2014-02-17 17:54:10 +00:00
|
|
|
* @param {Phaser.Game} game - Game reference to the currently running game.
|
|
|
|
* @param {Phaser.Sprite} [sprite] - The Sprite object this physics body belongs to.
|
|
|
|
* @param {number} [x=0] - The x coordinate of this Body.
|
|
|
|
* @param {number} [y=0] - The y coordinate of this Body.
|
|
|
|
* @param {number} [mass=1] - The default mass of this Body (0 = static).
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-17 17:54:10 +00:00
|
|
|
Phaser.Physics.Body = function (game, sprite, x, y, mass) {
|
|
|
|
|
|
|
|
sprite = sprite || null;
|
|
|
|
x = x || 0;
|
|
|
|
y = y || 0;
|
|
|
|
if (typeof mass === 'undefined') { mass = 1; }
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-17 17:54:10 +00:00
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-17 17:54:10 +00:00
|
|
|
this.game = game;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-17 17:54:10 +00:00
|
|
|
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-17 17:54:10 +00:00
|
|
|
this.sprite = sprite;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
|
|
|
|
*/
|
|
|
|
this.offset = new Phaser.Point();
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
/**
|
|
|
|
* @property {p2.Body} data - The p2 Body data.
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-02-17 17:54:10 +00:00
|
|
|
this.data = new p2.Body({ position:[this.px2p(x), this.px2p(y)], mass: mass });
|
2014-02-12 19:45:09 +00:00
|
|
|
this.data.parent = this;
|
2014-02-10 22:54:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {Phaser.PointProxy} velocity - The velocity of the body. Set velocity.x to a negative value to move to the left, position to the right. velocity.y negative values move up, positive move down.
|
|
|
|
*/
|
|
|
|
this.velocity = new Phaser.Physics.PointProxy(this.data.velocity);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
/**
|
2014-02-11 01:52:10 +00:00
|
|
|
* @property {Phaser.PointProxy} force - The force applied to the body.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-11 01:52:10 +00:00
|
|
|
this.force = new Phaser.Physics.PointProxy(this.data.force);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-18 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Point} gravity - A locally applied gravity force to the Body. Applied directly before the world step. NOTE: Not currently implemented.
|
|
|
|
*/
|
|
|
|
this.gravity = new Phaser.Point();
|
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
/**
|
|
|
|
* 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?
|
|
|
|
*/
|
|
|
|
this.collideWorldBounds = true;
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onImpact - Dispatched when the shape/s of this Body impact with another. The event will be sent 2 parameters, this Body and the impact Body.
|
|
|
|
*/
|
|
|
|
this.onImpact = new Phaser.Signal();
|
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
/**
|
|
|
|
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this.collidesWith = [];
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _bodyCallbacks - Array of Body callbacks.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._bodyCallbacks = [];
|
2014-02-19 03:51:48 +00:00
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _bodyCallbackContext - Array of Body callback contexts.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._bodyCallbackContext = [];
|
2014-02-19 03:51:48 +00:00
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
/**
|
|
|
|
* @property {array} _groupCallbacks - Array of Group callbacks.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._groupCallbacks = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {array} _bodyCallbackContext - Array of Grouo callback contexts.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._groupCallbackContext = [];
|
2014-02-12 19:45:09 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
// Set-up the default shape
|
2014-02-17 17:54:10 +00:00
|
|
|
if (sprite)
|
|
|
|
{
|
|
|
|
this.setRectangleFromSprite(sprite);
|
2014-02-19 03:51:48 +00:00
|
|
|
|
2014-02-18 15:16:26 +00:00
|
|
|
this.game.physics.addBody(this);
|
2014-02-17 17:54:10 +00:00
|
|
|
}
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.Body.prototype = {
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
/**
|
|
|
|
* Sets a callback to be fired any time this Body impacts with the given Body. The impact test is performed against body.id values.
|
|
|
|
* The callback will be sent 4 parameters: This body, the body that impacted, the Shape in this body and the shape in the impacting body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#createBodyCallback
|
|
|
|
* @param {Phaser.Physics.Body} body - The Body to send impact events for.
|
|
|
|
* @param {function} callback - The callback to fire on impact. Set to null to clear a previously set callback.
|
|
|
|
* @param {object} callbackContext - The context under which the callback will fire.
|
|
|
|
*/
|
|
|
|
createBodyCallback: function (body, callback, callbackContext) {
|
|
|
|
|
|
|
|
this._bodyCallbacks[body.data.id] = callback;
|
|
|
|
this._bodyCallbackContext[body.data.id] = callbackContext;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the given CollisionGroup to be the collision group for all shapes in this Body, unless a shape is specified.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#createGroupCallback
|
|
|
|
* @param {Phaser.Physics.CollisionGroup} group - The Group to send impact events for.
|
|
|
|
* @param {function} callback - The callback to fire on impact. Set to null to clear a previously set callback.
|
|
|
|
* @param {object} callbackContext - The context under which the callback will fire.
|
|
|
|
*/
|
|
|
|
createGroupCallback: function (group, callback, callbackContext) {
|
|
|
|
|
|
|
|
this._groupCallbacks[group.mask] = callback;
|
|
|
|
this._groupCallbackContext[group.mask] = callbackContext;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
getCollisionMask: function () {
|
|
|
|
|
|
|
|
var mask = 0;
|
|
|
|
|
|
|
|
if (this.collideWorldBounds)
|
|
|
|
{
|
|
|
|
mask = this.game.physics.boundsCollisionGroup.mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < this.collidesWith.length; i++)
|
|
|
|
{
|
|
|
|
mask = mask | this.collidesWith[i].mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-19 02:12:27 +00:00
|
|
|
/**
|
|
|
|
* Sets the given CollisionGroup to be the collision group for all shapes in this Body, unless a shape is specified.
|
2014-02-19 05:22:37 +00:00
|
|
|
* This also resets the collisionMask.
|
2014-02-19 02:12:27 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setCollisionGroup
|
|
|
|
* @param {Phaser.Physics.CollisionGroup|array} group - The Collision Group that this Bodies shapes will use.
|
|
|
|
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision group will be added to all Shapes in this Body.
|
|
|
|
*/
|
2014-02-19 01:51:14 +00:00
|
|
|
setCollisionGroup: function (group, shape) {
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
var mask = this.getCollisionMask();
|
|
|
|
|
2014-02-19 01:51:14 +00:00
|
|
|
if (typeof shape === 'undefined')
|
|
|
|
{
|
|
|
|
for (var i = this.data.shapes.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
this.data.shapes[i].collisionGroup = group.mask;
|
2014-02-19 05:22:37 +00:00
|
|
|
this.data.shapes[i].collisionMask = mask;
|
2014-02-19 01:51:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shape.collisionGroup = group.mask;
|
2014-02-19 05:22:37 +00:00
|
|
|
shapes.collisionMask = mask;
|
2014-02-19 01:51:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-19 02:12:27 +00:00
|
|
|
* Clears the collision data from the shapes in this Body. Optionally clears Group and/or Mask.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#clearCollision
|
|
|
|
* @param {boolean} [clearGroup=true] - Clear the collisionGroup value from the shape/s?
|
|
|
|
* @param {boolean} [clearMask=true] - Clear the collisionMask value from the shape/s?
|
|
|
|
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision data will be cleared from all Shapes in this Body.
|
|
|
|
*/
|
|
|
|
clearCollision: function (clearGroup, clearMask, shape) {
|
|
|
|
|
|
|
|
if (typeof shape === 'undefined')
|
|
|
|
{
|
|
|
|
for (var i = this.data.shapes.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (clearGroup)
|
|
|
|
{
|
|
|
|
this.data.shapes[i].collisionGroup = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearMask)
|
|
|
|
{
|
|
|
|
this.data.shapes[i].collisionMask = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (clearGroup)
|
|
|
|
{
|
|
|
|
shapes.collisionGroup = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clearMask)
|
|
|
|
{
|
|
|
|
shapes.collisionMask = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
if (clearGroup)
|
|
|
|
{
|
|
|
|
this.collidesWith.length = 0;
|
|
|
|
}
|
|
|
|
|
2014-02-19 02:12:27 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds the given CollisionGroup, or array of CollisionGroups, to the list of groups that this body will collide with and updates the collision masks.
|
2014-02-19 01:51:14 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#collides
|
2014-02-19 02:12:27 +00:00
|
|
|
* @param {Phaser.Physics.CollisionGroup|array} group - The Collision Group or Array of Collision Groups that this Bodies shapes will collide with.
|
2014-02-19 05:22:37 +00:00
|
|
|
* @param {function} [callback] - Optional callback that will be triggered when this Body impacts with the given Group.
|
|
|
|
* @param {object} [callbackContext] - The context under which the callback will be called.
|
2014-02-19 01:51:14 +00:00
|
|
|
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision mask will be added to all Shapes in this Body.
|
|
|
|
*/
|
2014-02-19 05:22:37 +00:00
|
|
|
collides: function (group, callback, callbackContext, shape) {
|
2014-02-19 01:51:14 +00:00
|
|
|
|
2014-02-19 02:12:27 +00:00
|
|
|
if (Array.isArray(group))
|
2014-02-19 01:51:14 +00:00
|
|
|
{
|
2014-02-19 02:12:27 +00:00
|
|
|
for (var i = 0; i < group.length; i++)
|
|
|
|
{
|
|
|
|
if (this.collidesWith.indexOf(group[i]) === -1)
|
|
|
|
{
|
|
|
|
this.collidesWith.push(group[i]);
|
2014-02-19 05:22:37 +00:00
|
|
|
|
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
this.createGroupCallback(group[i], callback, callbackContext);
|
|
|
|
}
|
2014-02-19 02:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (this.collidesWith.indexOf(group) === -1)
|
|
|
|
{
|
|
|
|
this.collidesWith.push(group);
|
2014-02-19 01:51:14 +00:00
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
if (callback)
|
|
|
|
{
|
|
|
|
this.createGroupCallback(group, callback, callbackContext);
|
|
|
|
}
|
|
|
|
}
|
2014-02-19 01:51:14 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 05:22:37 +00:00
|
|
|
var mask = this.getCollisionMask();
|
2014-02-19 01:51:14 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
/**
|
|
|
|
* Moves the shape offsets so their center of mass becomes the body center of mass.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
2014-02-12 03:22:49 +00:00
|
|
|
* @method Phaser.Physics.Body#adjustCenterOfMass
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-12 03:22:49 +00:00
|
|
|
adjustCenterOfMass: function () {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.data.adjustCenterOfMass();
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 03:22:49 +00:00
|
|
|
* Apply damping, see http://code.google.com/p/bullet/issues/detail?id=74 for details.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
2014-02-12 03:22:49 +00:00
|
|
|
* @method Phaser.Physics.Body#applyDamping
|
|
|
|
* @param {number} dt - Current time step.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-12 03:22:49 +00:00
|
|
|
applyDamping: function (dt) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.data.applyDamping(dt);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 03:22:49 +00:00
|
|
|
* Apply force to a world point. This could for example be a point on the RigidBody surface. Applying force this way will add to Body.force and Body.angularForce.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
2014-02-12 03:22:49 +00:00
|
|
|
* @method Phaser.Physics.Body#applyForce
|
|
|
|
* @param {number} force - The force to add.
|
|
|
|
* @param {number} worldX - The world x point to apply the force on.
|
|
|
|
* @param {number} worldY - The world y point to apply the force on.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-12 03:22:49 +00:00
|
|
|
applyForce: function (force, worldX, worldY) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.data.applyForce(force, [this.px2p(worldX), this.px2p(worldY)]);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the force on the body to zero.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setZeroForce
|
|
|
|
*/
|
|
|
|
setZeroForce: function () {
|
|
|
|
|
|
|
|
this.data.setZeroForce();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-11 01:52:10 +00:00
|
|
|
/**
|
2014-02-12 03:22:49 +00:00
|
|
|
* If this Body is dynamic then this will zero its angular velocity.
|
2014-02-11 01:52:10 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setZeroRotation
|
|
|
|
*/
|
|
|
|
setZeroRotation: function () {
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.data.angularVelocity = 0;
|
2014-02-11 01:52:10 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-10 22:54:56 +00:00
|
|
|
/**
|
|
|
|
* If this Body is dynamic then this will zero its velocity on both axis.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setZeroVelocity
|
|
|
|
*/
|
|
|
|
setZeroVelocity: function () {
|
|
|
|
|
|
|
|
this.data.velocity[0] = 0;
|
|
|
|
this.data.velocity[1] = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the Body damping and angularDamping to zero.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setZeroDamping
|
|
|
|
*/
|
|
|
|
setZeroDamping: function () {
|
|
|
|
|
|
|
|
this.data.damping = 0;
|
|
|
|
this.data.angularDamping = 0;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
/**
|
|
|
|
* Transform a world point to local body frame.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#toLocalFrame
|
|
|
|
* @param {Float32Array|Array} out - The vector to store the result in.
|
|
|
|
* @param {Float32Array|Array} worldPoint - The input world vector.
|
|
|
|
*/
|
|
|
|
toLocalFrame: function (out, worldPoint) {
|
|
|
|
|
|
|
|
return this.data.toLocalFrame(out, worldPoint);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a local point to world frame.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#toWorldFrame
|
|
|
|
* @param {Array} out - The vector to store the result in.
|
|
|
|
* @param {Array} localPoint - The input local vector.
|
|
|
|
*/
|
|
|
|
toWorldFrame: function (out, localPoint) {
|
|
|
|
|
|
|
|
return this.data.toWorldFrame(out, localPoint);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-11 01:52:10 +00:00
|
|
|
/**
|
|
|
|
* This will rotate the Body by the given speed to the left (counter-clockwise).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#rotateLeft
|
|
|
|
* @param {number} speed - The speed at which it should rotate.
|
|
|
|
*/
|
|
|
|
rotateLeft: function (speed) {
|
|
|
|
|
|
|
|
this.data.angularVelocity = this.px2p(speed);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This will rotate the Body by the given speed to the left (clockwise).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#rotateRight
|
|
|
|
* @param {number} speed - The speed at which it should rotate.
|
|
|
|
*/
|
|
|
|
rotateRight: function (speed) {
|
|
|
|
|
|
|
|
this.data.angularVelocity = this.px2p(-speed);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 04:49:03 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-11 01:52:10 +00:00
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#thrust
|
|
|
|
* @param {number} speed - The speed at which it should thrust.
|
|
|
|
*/
|
|
|
|
thrust: function (speed) {
|
|
|
|
|
|
|
|
var magnitude = this.px2p(-speed);
|
|
|
|
var angle = this.data.angle + Math.PI / 2;
|
|
|
|
|
|
|
|
this.data.force[0] += magnitude * Math.cos(angle);
|
|
|
|
this.data.force[1] += magnitude * Math.sin(angle);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
/**
|
|
|
|
* Applies a force to the Body that causes it to 'thrust' backwards (in reverse), 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#rever
|
|
|
|
* @param {number} speed - The speed at which it should reverse.
|
|
|
|
*/
|
|
|
|
reverse: function (speed) {
|
|
|
|
|
|
|
|
var magnitude = this.px2p(-speed);
|
|
|
|
var angle = this.data.angle + Math.PI / 2;
|
|
|
|
|
|
|
|
this.data.force[0] -= magnitude * Math.cos(angle);
|
|
|
|
this.data.force[1] -= magnitude * Math.sin(angle);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-10 22:54:56 +00:00
|
|
|
/**
|
|
|
|
* If this Body is dynamic then this will move it to the left by setting its x velocity to 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#moveLeft
|
|
|
|
* @param {number} speed - The speed at which it should move to the left, in pixels per second.
|
|
|
|
*/
|
|
|
|
moveLeft: function (speed) {
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
this.data.velocity[0] = this.px2p(-speed);
|
2014-02-10 22:54:56 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this Body is dynamic then this will move it to the right by setting its x velocity to 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#moveRight
|
|
|
|
* @param {number} speed - The speed at which it should move to the right, in pixels per second.
|
|
|
|
*/
|
|
|
|
moveRight: function (speed) {
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
this.data.velocity[0] = this.px2p(speed);
|
2014-02-10 22:54:56 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this Body is dynamic then this will move it up by setting its y velocity to 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#moveUp
|
|
|
|
* @param {number} speed - The speed at which it should move up, in pixels per second.
|
|
|
|
*/
|
|
|
|
moveUp: function (speed) {
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
this.data.velocity[1] = this.px2p(-speed);
|
2014-02-10 22:54:56 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If this Body is dynamic then this will move it down by setting its y velocity to 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#moveDown
|
|
|
|
* @param {number} speed - The speed at which it should move down, in pixels per second.
|
|
|
|
*/
|
|
|
|
moveDown: function (speed) {
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
this.data.velocity[1] = this.px2p(speed);
|
2014-02-10 22:54:56 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 12:32:26 +00:00
|
|
|
/**
|
|
|
|
* Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#preUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
preUpdate: function () {
|
|
|
|
},
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#postUpdate
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
postUpdate: function () {
|
|
|
|
|
|
|
|
this.sprite.x = this.p2px(this.data.position[0]);
|
|
|
|
this.sprite.y = this.p2px(this.data.position[1]);
|
2014-02-11 01:52:10 +00:00
|
|
|
|
|
|
|
if (!this.fixedRotation)
|
|
|
|
{
|
|
|
|
this.sprite.rotation = this.data.angle;
|
|
|
|
}
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 03:22:49 +00:00
|
|
|
* Resets the Body force, velocity (linear and angular) and rotation. Optionally resets damping and mass.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#reset
|
2014-02-13 12:26:39 +00:00
|
|
|
* @param {number} x - The new x position of the Body.
|
|
|
|
* @param {number} y - The new x position of the Body.
|
2014-02-12 03:22:49 +00:00
|
|
|
* @param {boolean} [resetDamping=false] - Resets the linear and angular damping.
|
|
|
|
* @param {boolean} [resetMass=false] - Sets the Body mass back to 1.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-13 12:26:39 +00:00
|
|
|
reset: function (x, y, resetDamping, resetMass) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
if (typeof resetDamping === 'undefined') { resetDamping = false; }
|
|
|
|
if (typeof resetMass === 'undefined') { resetMass = false; }
|
|
|
|
|
|
|
|
this.setZeroForce();
|
|
|
|
this.setZeroVelocity();
|
|
|
|
this.setZeroRotation();
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
if (resetDamping)
|
2014-02-10 16:01:30 +00:00
|
|
|
{
|
2014-02-12 03:22:49 +00:00
|
|
|
this.setZeroDamping();
|
2014-02-10 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
if (resetMass)
|
|
|
|
{
|
|
|
|
this.mass = 1;
|
|
|
|
}
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-13 12:26:39 +00:00
|
|
|
this.x = x;
|
|
|
|
this.y = y;
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
},
|
|
|
|
|
2014-02-13 09:55:46 +00:00
|
|
|
/**
|
|
|
|
* Adds this physics body to the world.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addToWorld
|
|
|
|
*/
|
|
|
|
addToWorld: function () {
|
|
|
|
|
2014-02-19 04:05:12 +00:00
|
|
|
if (this.data.world !== this.game.physics.world)
|
2014-02-13 09:55:46 +00:00
|
|
|
{
|
2014-02-18 15:16:26 +00:00
|
|
|
this.game.physics.addBody(this);
|
2014-02-13 09:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes this physics body from the world.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#removeFromWorld
|
|
|
|
*/
|
|
|
|
removeFromWorld: function () {
|
|
|
|
|
2014-02-19 04:05:12 +00:00
|
|
|
if (this.data.world === this.game.physics.world)
|
2014-02-13 09:55:46 +00:00
|
|
|
{
|
2014-02-19 04:05:12 +00:00
|
|
|
this.game.physics.removeBody(this);
|
2014-02-13 09:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* Destroys this Body and all references it holds to other objects.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#destroy
|
|
|
|
*/
|
|
|
|
destroy: function () {
|
|
|
|
|
2014-02-13 12:26:39 +00:00
|
|
|
this.removeFromWorld();
|
2014-02-12 03:22:49 +00:00
|
|
|
|
|
|
|
this.clearShapes();
|
|
|
|
|
2014-02-13 12:26:39 +00:00
|
|
|
this.sprite = null;
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/*
|
|
|
|
this.collideCallback = null;
|
|
|
|
this.collideCallbackContext = null;
|
|
|
|
*/
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 03:22:49 +00:00
|
|
|
* Removes all Shapes from this Body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#clearShapes
|
|
|
|
*/
|
|
|
|
clearShapes: function () {
|
|
|
|
|
|
|
|
for (var i = this.data.shapes.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
var shape = this.data.shapes[i];
|
|
|
|
this.data.removeShape(shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 14:50:32 +00:00
|
|
|
* Add a shape to the body. You can pass a local transform when adding a shape, so that the shape gets an offset and an angle relative to the body center of mass.
|
2014-02-12 03:22:49 +00:00
|
|
|
* Will automatically update the mass properties and bounding radius.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addShape
|
|
|
|
* @param {*} shape - The shape to add to the body.
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Circle|p2.Rectangle|p2.Plane|p2.Line|p2.Particle} The shape that was added to the body.
|
|
|
|
*/
|
|
|
|
addShape: function (shape, offsetX, offsetY, rotation) {
|
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
if (typeof offsetX === 'undefined') { offsetX = 0; }
|
|
|
|
if (typeof offsetY === 'undefined') { offsetY = 0; }
|
|
|
|
if (typeof rotation === 'undefined') { rotation = 0; }
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.data.addShape(shape, [this.px2p(offsetX), this.px2p(offsetY)], rotation);
|
|
|
|
|
|
|
|
return shape;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
/**
|
|
|
|
* Adds a Circle shape to this Body. You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addCircle
|
|
|
|
* @param {number} radius - The radius of this circle (in pixels)
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Circle} The Circle shape that was added to the Body.
|
|
|
|
*/
|
|
|
|
addCircle: function (radius, offsetX, offsetY, rotation) {
|
|
|
|
|
|
|
|
var shape = new p2.Circle(this.px2p(radius));
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Rectangle shape to this Body. You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addRectangle
|
|
|
|
* @param {number} width - The width of the rectangle in pixels.
|
|
|
|
* @param {number} height - The height of the rectangle in pixels.
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
|
|
|
|
*/
|
|
|
|
addRectangle: function (width, height, offsetX, offsetY, rotation) {
|
|
|
|
|
|
|
|
var shape = new p2.Rectangle(this.px2p(width), this.px2p(height));
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Plane shape to this Body. The plane is facing in the Y direction. You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addPlane
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Plane} The Plane shape that was added to the Body.
|
|
|
|
*/
|
2014-02-18 12:32:26 +00:00
|
|
|
addPlane: function (offsetX, offsetY, rotation) {
|
2014-02-12 14:50:32 +00:00
|
|
|
|
|
|
|
var shape = new p2.Plane();
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Particle shape to this Body. You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addParticle
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Particle} The Particle shape that was added to the Body.
|
|
|
|
*/
|
2014-02-18 12:32:26 +00:00
|
|
|
addParticle: function (offsetX, offsetY, rotation) {
|
2014-02-12 14:50:32 +00:00
|
|
|
|
|
|
|
var shape = new p2.Particle();
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Line shape to this Body.
|
|
|
|
* The line shape is along the x direction, and stretches from [-length/2, 0] to [length/2,0].
|
|
|
|
* You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addLine
|
|
|
|
* @param {number} length - The length of this line (in pixels)
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Line} The Line shape that was added to the Body.
|
|
|
|
*/
|
|
|
|
addLine: function (length, offsetX, offsetY, rotation) {
|
|
|
|
|
|
|
|
var shape = new p2.Line(this.px2p(length));
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a Capsule shape to this Body.
|
|
|
|
* You can control the offset from the center of the body and the rotation.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addCapsule
|
|
|
|
* @param {number} length - The distance between the end points in pixels.
|
|
|
|
* @param {number} radius - Radius of the capsule in radians.
|
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
|
|
|
* @return {p2.Capsule} The Capsule shape that was added to the Body.
|
|
|
|
*/
|
|
|
|
addCapsule: function (length, radius, offsetX, offsetY, rotation) {
|
|
|
|
|
|
|
|
var shape = new p2.Capsule(this.px2p(length), radius);
|
|
|
|
|
|
|
|
return this.addShape(shape, offsetX, offsetY, rotation);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points. The shape must be simple and without holes.
|
|
|
|
* This function expects the x.y values to be given in pixels. If you want to provide them at p2 world scales then call Body.data.fromPolygon directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#addPolygon
|
|
|
|
* @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.
|
|
|
|
* @return {boolean} True on success, else false.
|
|
|
|
*/
|
|
|
|
addPolygon: function (options, points) {
|
|
|
|
|
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
points = Array.prototype.slice.call(arguments, 1);
|
|
|
|
|
2014-02-17 11:27:31 +00:00
|
|
|
var path = [];
|
2014-02-12 14:50:32 +00:00
|
|
|
|
|
|
|
// Did they pass in a single array of points?
|
2014-02-14 23:51:49 +00:00
|
|
|
if (points.length === 1 && Array.isArray(points[0]))
|
2014-02-12 14:50:32 +00:00
|
|
|
{
|
2014-02-18 03:01:51 +00:00
|
|
|
path = points[0].slice(0);
|
2014-02-12 14:50:32 +00:00
|
|
|
}
|
|
|
|
else if (Array.isArray(points[0]))
|
|
|
|
{
|
2014-02-18 03:01:51 +00:00
|
|
|
path = points[0].slice(0);
|
|
|
|
// for (var i = 0, len = points[0].length; i < len; i += 2)
|
|
|
|
// {
|
|
|
|
// path.push([points[0][i], points[0][i + 1]]);
|
|
|
|
// }
|
2014-02-12 14:50:32 +00:00
|
|
|
}
|
|
|
|
else if (typeof points[0] === 'number')
|
|
|
|
{
|
2014-02-18 03:01:51 +00:00
|
|
|
// console.log('addPolygon --- We\'ve a list of numbers');
|
2014-02-12 14:50:32 +00:00
|
|
|
// We've a list of numbers
|
|
|
|
for (var i = 0, len = points.length; i < len; i += 2)
|
|
|
|
{
|
2014-02-17 11:27:31 +00:00
|
|
|
path.push([points[i], points[i + 1]]);
|
2014-02-12 14:50:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
// console.log('addPolygon PATH pre');
|
|
|
|
// console.log(path[1]);
|
|
|
|
// console.table(path);
|
|
|
|
|
|
|
|
// top and tail
|
|
|
|
var idx = path.length - 1;
|
|
|
|
|
|
|
|
if ( path[idx][0] === path[0][0] && path[idx][1] === path[0][1] )
|
|
|
|
{
|
|
|
|
path.pop();
|
|
|
|
}
|
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
// Now process them into p2 values
|
|
|
|
for (var p = 0; p < path.length; p++)
|
|
|
|
{
|
|
|
|
path[p][0] = this.px2p(path[p][0]);
|
|
|
|
path[p][1] = this.px2p(path[p][1]);
|
|
|
|
}
|
|
|
|
|
2014-02-18 03:01:51 +00:00
|
|
|
// console.log('addPolygon PATH POST');
|
|
|
|
// console.log(path[1]);
|
|
|
|
// console.table(path);
|
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
return this.data.fromPolygon(path, options);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
/**
|
|
|
|
* Remove a shape from the body. Will automatically update the mass properties and bounding radius.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#removeShape
|
|
|
|
* @param {p2.Circle|p2.Rectangle|p2.Plane|p2.Line|p2.Particle} shape - The shape to remove from the body.
|
|
|
|
* @return {boolean} True if the shape was found and removed, else false.
|
|
|
|
*/
|
|
|
|
removeShape: function (shape) {
|
|
|
|
|
|
|
|
return this.data.removeShape(shape);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 14:50:32 +00:00
|
|
|
* Clears any previously set shapes. Then creates a new Circle shape and adds it to this Body.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setCircle
|
|
|
|
* @param {number} radius - The radius of this circle (in pixels)
|
2014-02-12 03:22:49 +00:00
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-12 03:22:49 +00:00
|
|
|
setCircle: function (radius, offsetX, offsetY, rotation) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.clearShapes();
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
this.addCircle(radius, offsetX, offsetY, rotation);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-12 14:50:32 +00:00
|
|
|
* Clears any previously set shapes. The creates a new Rectangle shape at the given size and offset, and adds it to this Body.
|
|
|
|
* If you wish to create a Rectangle to match the size of a Sprite or Image see Body.setRectangleFromSprite.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setRectangle
|
2014-02-12 14:50:32 +00:00
|
|
|
* @param {number} [width=16] - The width of the rectangle in pixels.
|
|
|
|
* @param {number} [height=16] - The height of the rectangle in pixels.
|
2014-02-12 03:22:49 +00:00
|
|
|
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
|
|
|
|
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
|
2014-02-12 14:50:32 +00:00
|
|
|
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
|
2014-02-10 16:01:30 +00:00
|
|
|
*/
|
2014-02-12 03:22:49 +00:00
|
|
|
setRectangle: function (width, height, offsetX, offsetY, rotation) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
if (typeof width === 'undefined') { width = 16; }
|
|
|
|
if (typeof height === 'undefined') { height = 16; }
|
|
|
|
|
|
|
|
this.clearShapes();
|
|
|
|
|
|
|
|
return this.addRectangle(width, height, offsetX, offsetY, rotation);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 14:50:32 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears any previously set shapes.
|
|
|
|
* Then creates a Rectangle shape sized to match the dimensions and orientation of the Sprite given.
|
|
|
|
* If no Sprite is given it defaults to using the parent of this Body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setRectangleFromSprite
|
|
|
|
* @param {Phaser.Sprite|Phaser.Image} [sprite] - The Sprite on which the Rectangle will get its dimensions.
|
|
|
|
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
|
|
|
|
*/
|
|
|
|
setRectangleFromSprite: function (sprite) {
|
|
|
|
|
|
|
|
if (typeof sprite === 'undefined') { sprite = this.sprite; }
|
|
|
|
|
2014-02-14 18:06:00 +00:00
|
|
|
// because Sprite.phyicsEnabled = true now sets anchor to 0.5
|
|
|
|
// var px = (sprite.width / 2) + (-sprite.width * sprite.anchor.x);
|
|
|
|
// var py = (sprite.height / 2) + (-sprite.height * sprite.anchor.y);
|
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
this.clearShapes();
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-14 18:06:00 +00:00
|
|
|
return this.addRectangle(sprite.width, sprite.height, 0, 0, sprite.rotation);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-18 16:37:53 +00:00
|
|
|
/**
|
|
|
|
* Adds the given Material to all Shapes that belong to this Body.
|
|
|
|
* If you only wish to apply it to a specific Shape in this Body then provide that as the 2nd parameter.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#setMaterial
|
|
|
|
* @param {Phaser.Physics.Material} material - The Material that will be applied.
|
|
|
|
* @param {p2.Shape} [shape] - An optional Shape. If not provided the Material will be added to all Shapes in this Body.
|
|
|
|
*/
|
|
|
|
setMaterial: function (material, shape) {
|
|
|
|
|
|
|
|
if (typeof shape === 'undefined')
|
|
|
|
{
|
|
|
|
for (var i = this.data.shapes.length - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
this.data.shapes[i].material = material;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shape.material = material;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
2014-02-14 23:51:49 +00:00
|
|
|
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
2014-02-14 23:51:49 +00:00
|
|
|
* @method Phaser.Physics.Body#loadPolygon
|
|
|
|
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
|
|
|
|
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
|
2014-02-12 03:22:49 +00:00
|
|
|
* @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.
|
|
|
|
* @return {boolean} True on success, else false.
|
2014-02-14 23:51:49 +00:00
|
|
|
*/
|
|
|
|
loadPolygon: function (key, object, options) {
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
var data = game.cache.getPhysicsData(key, object);
|
2014-02-12 03:22:49 +00:00
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
if (data && data.shape)
|
2014-02-10 16:01:30 +00:00
|
|
|
{
|
2014-02-12 03:22:49 +00:00
|
|
|
var temp = [];
|
2014-02-10 16:01:30 +00:00
|
|
|
|
2014-02-12 03:22:49 +00:00
|
|
|
// We've a list of numbers
|
2014-02-14 23:51:49 +00:00
|
|
|
for (var i = 0, len = data.shape.length; i < len; i += 2)
|
2014-02-10 16:01:30 +00:00
|
|
|
{
|
2014-02-14 23:51:49 +00:00
|
|
|
temp.push([data.shape[i], data.shape[i + 1]]);
|
2014-02-10 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
return this.addPolygon(options, temp);
|
2014-02-12 03:22:49 +00:00
|
|
|
}
|
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
return false;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-15 01:27:42 +00:00
|
|
|
/**
|
|
|
|
* Reads the physics data from a physics data file stored in the Game.Cache.
|
|
|
|
* It will add the shape data to this Body, as well as set the density (mass), friction and bounce (restitution) values.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Body#loadPolygon
|
|
|
|
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
|
|
|
|
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
|
|
|
|
* @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.
|
|
|
|
* @return {boolean} True on success, else false.
|
|
|
|
*/
|
|
|
|
loadData: function (key, object, options) {
|
|
|
|
|
|
|
|
var data = game.cache.getPhysicsData(key, object);
|
|
|
|
|
|
|
|
if (data && data.shape)
|
|
|
|
{
|
|
|
|
this.mass = data.density;
|
|
|
|
// set friction + bounce here
|
|
|
|
this.loadPolygon(key, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
2014-02-19 01:51:14 +00:00
|
|
|
* Convert p2 physics value (meters) to pixel scale.
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Math#p2px
|
|
|
|
* @param {number} v - The value to convert.
|
|
|
|
* @return {number} The scaled value.
|
|
|
|
*/
|
|
|
|
p2px: function (v) {
|
2014-02-11 01:52:10 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
return v *= -20;
|
2014-02-11 01:52:10 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-02-19 01:51:14 +00:00
|
|
|
* Convert pixel value to p2 physics scale (meters).
|
2014-02-10 16:01:30 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Math#px2p
|
|
|
|
* @param {number} v - The value to convert.
|
|
|
|
* @return {number} The scaled value.
|
|
|
|
*/
|
|
|
|
px2p: function (v) {
|
2014-02-11 01:52:10 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
return v * -0.05;
|
2014-02-11 01:52:10 +00:00
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Phaser.Physics.Body.prototype.constructor = Phaser.Physics.Body;
|
|
|
|
|
2014-02-10 22:54:56 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#static
|
|
|
|
* @property {boolean} static - Returns true if the Body is static. Setting Body.static to 'false' will make it dynamic.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "static", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return (this.data.motionState === Phaser.STATIC);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value && this.data.motionState !== Phaser.STATIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.STATIC;
|
2014-02-17 17:54:10 +00:00
|
|
|
this.mass = 0;
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
else if (!value && this.data.motionState === Phaser.STATIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.DYNAMIC;
|
2014-02-17 17:54:10 +00:00
|
|
|
|
|
|
|
if (this.mass === 0)
|
|
|
|
{
|
|
|
|
this.mass = 1;
|
|
|
|
}
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#dynamic
|
|
|
|
* @property {boolean} dynamic - Returns true if the Body is dynamic. Setting Body.dynamic to 'false' will make it static.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "dynamic", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return (this.data.motionState === Phaser.DYNAMIC);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value && this.data.motionState !== Phaser.DYNAMIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.DYNAMIC;
|
2014-02-17 17:54:10 +00:00
|
|
|
|
|
|
|
if (this.mass === 0)
|
|
|
|
{
|
|
|
|
this.mass = 1;
|
|
|
|
}
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
else if (!value && this.data.motionState === Phaser.DYNAMIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.STATIC;
|
2014-02-17 17:54:10 +00:00
|
|
|
this.mass = 0;
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#kinematic
|
|
|
|
* @property {boolean} kinematic - Returns true if the Body is kinematic. Setting Body.kinematic to 'false' will make it static.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "kinematic", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return (this.data.motionState === Phaser.KINEMATIC);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value && this.data.motionState !== Phaser.KINEMATIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.KINEMATIC;
|
2014-02-18 04:49:03 +00:00
|
|
|
this.mass = 4;
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
else if (!value && this.data.motionState === Phaser.KINEMATIC)
|
|
|
|
{
|
|
|
|
this.data.motionState = Phaser.STATIC;
|
2014-02-17 17:54:10 +00:00
|
|
|
this.mass = 0;
|
2014-02-10 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-02-10 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#allowSleep
|
|
|
|
* @property {boolean} allowSleep -
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "allowSleep", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.data.allowSleep;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value !== this.data.allowSleep)
|
|
|
|
{
|
|
|
|
this.data.allowSleep = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The angle of the Body in degrees from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
|
|
|
|
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement Body.angle = 450 is the same as Body.angle = 90.
|
|
|
|
* If you wish to work in radians instead of degrees use the property Body.rotation instead. Working in radians is faster as it doesn't have to convert values.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Body#angle
|
|
|
|
* @property {number} angle - The angle of this Body in degrees.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "angle", {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.data.angle));
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.data.angle = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Damping is specified as a value between 0 and 1, which is the proportion of velocity lost per second.
|
|
|
|
* @name Phaser.Physics.Body#angularDamping
|
|
|
|
* @property {number} angularDamping - The angular damping acting acting on the body.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "angularDamping", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.angularDamping;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.angularDamping = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#angularForce
|
|
|
|
* @property {number} angularForce - The angular force acting on the body.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "angularForce", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.angularForce;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.angularForce = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#angularVelocity
|
|
|
|
* @property {number} angularVelocity - The angular velocity of the body.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "angularVelocity", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.angularVelocity;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.angularVelocity = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Damping is specified as a value between 0 and 1, which is the proportion of velocity lost per second.
|
|
|
|
* @name Phaser.Physics.Body#damping
|
|
|
|
* @property {number} damping - The linear damping acting on the body in the velocity direction.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "damping", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.damping;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.damping = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#fixedRotation
|
|
|
|
* @property {boolean} fixedRotation -
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "fixedRotation", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.data.fixedRotation;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value !== this.data.fixedRotation)
|
|
|
|
{
|
|
|
|
this.data.fixedRotation = value;
|
|
|
|
// update anything?
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#inertia
|
|
|
|
* @property {number} inertia - The inertia of the body around the Z axis..
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "inertia", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.inertia;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.inertia = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#mass
|
|
|
|
* @property {number} mass -
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "mass", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.data.mass;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value !== this.data.mass)
|
|
|
|
{
|
|
|
|
this.data.mass = value;
|
|
|
|
this.data.updateMassProperties();
|
2014-02-17 17:54:10 +00:00
|
|
|
|
|
|
|
if (value === 0)
|
|
|
|
{
|
|
|
|
// this.static = true;
|
|
|
|
}
|
2014-02-10 16:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#motionState
|
|
|
|
* @property {number} motionState - The type of motion this body has. Should be one of: Body.STATIC (the body does not move), Body.DYNAMIC (body can move and respond to collisions) and Body.KINEMATIC (only moves according to its .velocity).
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "motionState", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
|
|
|
return this.data.motionState;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
if (value !== this.data.motionState)
|
|
|
|
{
|
|
|
|
this.data.motionState = value;
|
|
|
|
// update?
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The angle of the Body in radians.
|
|
|
|
* If you wish to work in degrees instead of radians use the Body.angle property instead. Working in radians is faster as it doesn't have to convert values.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Body#rotation
|
|
|
|
* @property {number} rotation - The angle of this Body in radians.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "rotation", {
|
|
|
|
|
|
|
|
get: function() {
|
|
|
|
|
|
|
|
return this.data.angle;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function(value) {
|
|
|
|
|
|
|
|
this.data.angle = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#sleepSpeedLimit
|
|
|
|
* @property {number} sleepSpeedLimit - .
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "sleepSpeedLimit", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.data.sleepSpeedLimit;
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.sleepSpeedLimit = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#x
|
|
|
|
* @property {number} x - The x coordinate of this Body.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "x", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.p2px(this.data.position[0]);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.position[0] = this.px2p(value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @name Phaser.Physics.Body#y
|
|
|
|
* @property {number} y - The y coordinate of this Body.
|
|
|
|
*/
|
|
|
|
Object.defineProperty(Phaser.Physics.Body.prototype, "y", {
|
|
|
|
|
|
|
|
get: function () {
|
|
|
|
|
2014-02-10 19:33:27 +00:00
|
|
|
return this.p2px(this.data.position[1]);
|
2014-02-10 16:01:30 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
set: function (value) {
|
|
|
|
|
|
|
|
this.data.position[1] = this.px2p(value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|