phaser/src/physics/arcade/Body.js

2095 lines
57 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2019-01-15 16:20:22 +00:00
* @copyright 2019 Photon Storm Ltd.
2018-02-12 16:01:20 +00:00
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
var CircleContains = require('../../geom/circle/Contains');
var Class = require('../../utils/Class');
var CONST = require('./const');
2018-04-11 12:17:53 +00:00
var RadToDeg = require('../../math/RadToDeg');
var Rectangle = require('../../geom/rectangle/Rectangle');
var RectangleContains = require('../../geom/rectangle/Contains');
var Vector2 = require('../../math/Vector2');
2018-03-21 13:15:25 +00:00
/**
* @typedef {object} ArcadeBodyBounds
*
2018-04-24 00:48:15 +00:00
* @property {number} x - The left edge.
* @property {number} y - The upper edge.
* @property {number} right - The right edge.
* @property {number} bottom - The lower edge.
2018-03-21 13:15:25 +00:00
*/
/**
* @typedef {object} ArcadeBodyCollision
*
2018-04-24 00:48:15 +00:00
* @property {boolean} none - True if the Body is not colliding.
* @property {boolean} up - True if the Body is colliding on its upper edge.
* @property {boolean} down - True if the Body is colliding on its lower edge.
* @property {boolean} left - True if the Body is colliding on its left edge.
* @property {boolean} right - True if the Body is colliding on its right edge.
2018-03-21 13:15:25 +00:00
*/
2018-02-09 03:44:23 +00:00
/**
* @classdesc
2018-04-24 00:48:15 +00:00
* A Dynamic Arcade Body.
2018-02-09 03:44:23 +00:00
*
2018-06-13 16:41:50 +00:00
* Its static counterpart is {@link Phaser.Physics.Arcade.StaticBody}.
*
2018-02-09 03:44:23 +00:00
* @class Body
2018-10-10 09:49:13 +00:00
* @memberof Phaser.Physics.Arcade
2018-02-09 03:44:23 +00:00
* @constructor
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {Phaser.Physics.Arcade.World} world - The Arcade Physics simulation this Body belongs to.
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object this Body belongs to.
2018-02-09 03:44:23 +00:00
*/
var Body = new Class({
initialize:
function Body (world, gameObject)
{
var width = (gameObject.width) ? gameObject.width : 64;
var height = (gameObject.height) ? gameObject.height : 64;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Arcade Physics simulation this Body belongs to.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#world
* @type {Phaser.Physics.Arcade.World}
* @since 3.0.0
*/
this.world = world;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Game Object this Body belongs to.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#gameObject
* @type {Phaser.GameObjects.GameObject}
* @since 3.0.0
*/
this.gameObject = gameObject;
2018-04-11 12:17:53 +00:00
/**
2018-04-24 00:48:15 +00:00
* Transformations applied to this Body.
2018-04-11 12:17:53 +00:00
*
* @name Phaser.Physics.Arcade.Body#transform
* @type {object}
* @since 3.4.0
*/
this.transform = {
x: gameObject.x,
y: gameObject.y,
rotation: gameObject.angle,
scaleX: gameObject.scaleX,
scaleY: gameObject.scaleY,
displayOriginX: gameObject.displayOriginX,
displayOriginY: gameObject.displayOriginY
};
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the Body's boundary is drawn to the debug display.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#debugShowBody
* @type {boolean}
* @since 3.0.0
*/
this.debugShowBody = world.defaults.debugShowBody;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the Body's velocity is drawn to the debug display.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#debugShowVelocity
* @type {boolean}
* @since 3.0.0
*/
this.debugShowVelocity = world.defaults.debugShowVelocity;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The color of this Body on the debug display.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#debugBodyColor
* @type {integer}
* @since 3.0.0
*/
this.debugBodyColor = world.defaults.bodyDebugColor;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is updated by the physics simulation.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#enable
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.enable = true;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body's boundary is circular (true) or rectangular (false).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#isCircle
* @type {boolean}
* @default false
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#setCircle
2018-02-09 03:44:23 +00:00
*/
this.isCircle = false;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* If this Body is circular, this is the unscaled radius of the Body's boundary, as set by setCircle(), in source pixels.
* The true radius is equal to `halfWidth`.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#radius
* @type {number}
* @default 0
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#setCircle
2018-02-09 03:44:23 +00:00
*/
this.radius = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The offset of this Body's position from its Game Object's position, in source pixels.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#offset
* @type {Phaser.Math.Vector2}
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#setOffset
2018-02-09 03:44:23 +00:00
*/
this.offset = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The position of this Body within the simulation.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#position
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.position = new Vector2(gameObject.x, gameObject.y);
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The position of this Body during the previous step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#prev
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.prev = new Vector2(gameObject.x, gameObject.y);
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Whether this Body's `rotation` is affected by its angular acceleration and angular velocity.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#allowRotation
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.allowRotation = true;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* This body's rotation, in degrees, based on its angular acceleration and angular velocity.
2018-04-24 00:48:15 +00:00
* The Body's rotation controls the `angle` of its Game Object.
* It doesn't rotate the Body's boundary, which is always an axis-aligned rectangle or a circle.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#rotation
* @type {number}
* @since 3.0.0
*/
this.rotation = gameObject.angle;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's rotation, in degrees, during the previous step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#preRotation
* @type {number}
* @since 3.0.0
*/
this.preRotation = gameObject.angle;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The width of the Body's boundary, in pixels.
* If the Body is circular, this is also the Body's diameter.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#width
* @type {number}
2018-04-24 00:48:15 +00:00
* @default 64
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.width = width;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The height of the Body's boundary, in pixels.
* If the Body is circular, this is also the Body's diameter.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#height
* @type {number}
2018-04-24 00:48:15 +00:00
* @default 64
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.height = height;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The unscaled width of the Body, in source pixels, as set by setSize().
* The default is the width of the Body's Game Object's texture frame.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#sourceWidth
* @type {number}
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#setSize
2018-02-09 03:44:23 +00:00
*/
this.sourceWidth = width;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The unscaled height of the Body, in source pixels, as set by setSize().
* The default is the height of the Body's Game Object's texture frame.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#sourceHeight
* @type {number}
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#setSize
2018-02-09 03:44:23 +00:00
*/
this.sourceHeight = height;
if (gameObject.frame)
{
this.sourceWidth = gameObject.frame.realWidth;
this.sourceHeight = gameObject.frame.realHeight;
}
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Half the Body's width, in pixels.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#halfWidth
* @type {number}
* @since 3.0.0
*/
this.halfWidth = Math.abs(width / 2);
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Half the Body's height, in pixels.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#halfHeight
* @type {number}
* @since 3.0.0
*/
this.halfHeight = Math.abs(height / 2);
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The center of the Body's boundary.
* The midpoint of its `position` (top-left corner) and its bottom-right corner.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#center
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.center = new Vector2(gameObject.x + this.halfWidth, gameObject.y + this.halfHeight);
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The Body's velocity, in pixels per second.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#velocity
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.velocity = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The Body's calculated velocity, in pixels per second, at the last step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#newVelocity
* @type {Phaser.Math.Vector2}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.newVelocity = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's absolute maximum change in position, in pixels per step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#deltaMax
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.deltaMax = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's change in velocity, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#acceleration
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.acceleration = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Whether this Body's velocity is affected by its `drag`.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#allowDrag
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.allowDrag = true;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Absolute loss of velocity due to movement, in pixels per second squared.
2018-06-13 16:41:50 +00:00
* The x and y components are applied separately.
*
* When `useDamping` is true, this is 1 minus the damping factor.
* A value of 1 means the Body loses no velocity.
* A value of 0.95 means the Body loses 5% of its velocity per step.
* A value of 0.5 means the Body loses 50% of its velocity per step.
*
* Drag is applied only when `acceleration` is zero.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#drag
2018-06-13 16:41:50 +00:00
* @type {(Phaser.Math.Vector2|number)}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.drag = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Whether this Body's position is affected by gravity (local or world).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#allowGravity
* @type {boolean}
* @default true
* @since 3.0.0
2018-06-13 16:41:50 +00:00
* @see Phaser.Physics.Arcade.Body#gravity
* @see Phaser.Physics.Arcade.World#gravity
2018-02-09 03:44:23 +00:00
*/
this.allowGravity = true;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Acceleration due to gravity (specific to this Body), in pixels per second squared.
* Total gravity is the sum of this vector and the simulation's `gravity`.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#gravity
* @type {Phaser.Math.Vector2}
* @since 3.0.0
2018-06-13 16:41:50 +00:00
* @see Phaser.Physics.Arcade.World#gravity
2018-02-09 03:44:23 +00:00
*/
this.gravity = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Rebound following a collision, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#bounce
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.bounce = new Vector2();
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Rebound following a collision with the world boundary, relative to 1.
2018-06-13 16:41:50 +00:00
* If null, `bounce` is used instead.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#worldBounce
2018-03-18 23:29:46 +00:00
* @type {?Phaser.Math.Vector2}
2018-02-09 03:44:23 +00:00
* @default null
* @since 3.0.0
*/
this.worldBounce = null;
// If true this Body will dispatch events
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the simulation emits a `worldbounds` event when this Body collides with the world boundary (and `collideWorldBounds` is also true).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#onWorldBounds
* @type {boolean}
* @default false
* @since 3.0.0
2018-10-24 19:14:44 +00:00
* @see Phaser.Physics.Arcade.World#worldboundsEvent
2018-02-09 03:44:23 +00:00
*/
this.onWorldBounds = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the simulation emits a `collide` event when this Body collides with another.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#onCollide
* @type {boolean}
* @default false
* @since 3.0.0
2018-10-24 19:14:44 +00:00
* @see Phaser.Physics.Arcade.World#collideEvent
2018-02-09 03:44:23 +00:00
*/
this.onCollide = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the simulation emits an `overlap` event when this Body overlaps with another.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#onOverlap
* @type {boolean}
* @default false
* @since 3.0.0
2018-10-24 19:14:44 +00:00
* @see Phaser.Physics.Arcade.World#overlapEvent
2018-02-09 03:44:23 +00:00
*/
this.onOverlap = false;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The Body's absolute maximum velocity, in pixels per second.
* The horizontal and vertical components are applied separately.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#maxVelocity
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.maxVelocity = new Vector2(10000, 10000);
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* If this Body is `immovable` and in motion, `friction` is the proportion of this Body's motion received by the riding Body on each axis, relative to 1.
* The default value (1, 0) moves the riding Body horizontally in equal proportion to this Body and vertically not at all.
* The horizontal component (x) is applied only when two colliding Bodies are separated vertically.
* The vertical component (y) is applied only when two colliding Bodies are separated horizontally.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#friction
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.friction = new Vector2(1, 0);
/**
* If this Body is using `drag` for deceleration this property controls how the drag is applied.
* If set to `true` drag will use a damping effect rather than a linear approach. If you are
* creating a game where the Body moves freely at any angle (i.e. like the way the ship moves in
* the game Asteroids) then you will get a far smoother and more visually correct deceleration
* by using damping, avoiding the axis-drift that is prone with linear deceleration.
*
* If you enable this property then you should use far smaller `drag` values than with linear, as
* they are used as a multiplier on the velocity. Values such as 0.95 will give a nice slow
* deceleration, where-as smaller values, such as 0.5 will stop an object almost immediately.
*
* @name Phaser.Physics.Arcade.Body#useDamping
* @type {boolean}
* @default false
* @since 3.10.0
*/
this.useDamping = false;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The rate of change of this Body's `rotation`, in degrees per second.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#angularVelocity
* @type {number}
* @default 0
* @since 3.0.0
*/
this.angularVelocity = 0;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The Body's angular acceleration (change in angular velocity), in degrees per second squared.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#angularAcceleration
* @type {number}
* @default 0
* @since 3.0.0
*/
this.angularAcceleration = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Loss of angular velocity due to angular movement, in degrees per second.
2018-02-09 03:44:23 +00:00
*
2018-06-13 16:41:50 +00:00
* Angular drag is applied only when angular acceleration is zero.
*
2018-02-09 03:44:23 +00:00
* @name Phaser.Physics.Arcade.Body#angularDrag
* @type {number}
* @default 0
* @since 3.0.0
*/
this.angularDrag = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's maximum angular velocity, in degrees per second.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#maxAngular
* @type {number}
* @default 1000
* @since 3.0.0
*/
this.maxAngular = 1000;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's inertia, relative to a default unit (1).
* With `bounce`, this affects the exchange of momentum (velocities) during collisions.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#mass
* @type {number}
* @default 1
* @since 3.0.0
*/
this.mass = 1;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The calculated angle of this Body's velocity vector, in degrees, during the last step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#angle
* @type {number}
* @default 0
* @since 3.0.0
*/
this.angle = 0;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The calculated magnitude of the Body's velocity, in pixels per second, during the last step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#speed
* @type {number}
* @default 0
* @since 3.0.0
*/
this.speed = 0;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The direction of the Body's velocity, as calculated during the last step.
* If the Body is moving on both axes (diagonally), this describes motion on the vertical axis only.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#facing
* @type {integer}
* @since 3.0.0
*/
this.facing = CONST.FACING_NONE;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Whether this Body can be moved by collisions with another Body.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#immovable
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.immovable = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the Body's position and rotation are affected by its velocity, acceleration, drag, and gravity.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#moves
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.moves = true;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* A flag disabling the default horizontal separation of colliding bodies.
* Pass your own `collideCallback` to the collider.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#customSeparateX
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.customSeparateX = false;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* A flag disabling the default vertical separation of colliding bodies.
* Pass your own `collideCallback` to the collider.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#customSeparateY
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.customSeparateY = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The amount of horizontal overlap (before separation), if this Body is colliding with another.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#overlapX
* @type {number}
* @default 0
* @since 3.0.0
*/
this.overlapX = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The amount of vertical overlap (before separation), if this Body is colliding with another.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#overlapY
* @type {number}
* @default 0
* @since 3.0.0
*/
this.overlapY = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The amount of overlap (before separation), if this Body is circular and colliding with another circular body.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#overlapR
* @type {number}
* @default 0
* @since 3.0.0
*/
this.overlapR = 0;
2018-02-09 03:44:23 +00:00
/**
* Whether this Body is overlapped with another and both are not moving.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#embedded
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.embedded = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body interacts with the world boundary.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#collideWorldBounds
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.collideWorldBounds = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is checked for collisions and for which directions.
* You can set `checkCollision.none = true` to disable collision checks.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#checkCollision
2018-03-21 13:15:25 +00:00
* @type {ArcadeBodyCollision}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.checkCollision = { none: false, up: true, down: true, left: true, right: true };
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is colliding with another and in which direction.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#touching
2018-03-21 13:15:25 +00:00
* @type {ArcadeBodyCollision}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.touching = { none: true, up: false, down: false, left: false, right: false };
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body was colliding with another during the last step, and in which direction.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#wasTouching
2018-03-21 13:15:25 +00:00
* @type {ArcadeBodyCollision}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is colliding with a tile or the world boundary.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#blocked
2018-03-21 13:15:25 +00:00
* @type {ArcadeBodyCollision}
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.blocked = { none: true, up: false, down: false, left: false, right: false };
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether to automatically synchronize this Body's dimensions to the dimensions of its Game Object's visual bounds.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#syncBounds
* @type {boolean}
* @default false
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.GameObjects.Components.GetBounds#getBounds
2018-02-09 03:44:23 +00:00
*/
this.syncBounds = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is being moved by the `moveTo` or `moveFrom` methods.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#isMoving
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.isMoving = false;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body's movement by `moveTo` or `moveFrom` will be stopped by collisions with other bodies.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#stopVelocityOnCollide
* @type {boolean}
* @default true
* @since 3.0.0
*/
this.stopVelocityOnCollide = true;
// read-only
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's physics type (dynamic or static).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#physicsType
* @type {integer}
2018-10-09 12:40:00 +00:00
* @readonly
* @default Phaser.Physics.Arcade.DYNAMIC_BODY
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
this.physicsType = CONST.DYNAMIC_BODY;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether the Body's position needs updating from its Game Object.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_reset
* @type {boolean}
* @private
* @default true
* @since 3.0.0
*/
this._reset = true;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Cached horizontal scale of the Body's Game Object.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_sx
* @type {number}
* @private
* @since 3.0.0
*/
this._sx = gameObject.scaleX;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Cached vertical scale of the Body's Game Object.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_sy
* @type {number}
* @private
* @since 3.0.0
*/
this._sy = gameObject.scaleY;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The calculated change in the Body's horizontal position during the last step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_dx
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._dx = 0;
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* The calculated change in the Body's vertical position during the last step.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_dy
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._dy = 0;
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Stores the Game Object's bounds.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#_bounds
* @type {Phaser.Geom.Rectangle}
* @private
* @since 3.0.0
*/
this._bounds = new Rectangle();
},
2018-02-09 03:44:23 +00:00
/**
* Updates the Body's `transform`, `width`, `height`, and `center` from its Game Object.
* The Body's `position` isn't changed.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#updateBounds
* @since 3.0.0
*/
updateBounds: function ()
{
var sprite = this.gameObject;
2018-04-11 12:17:53 +00:00
// Container?
var transform = this.transform;
if (sprite.parentContainer)
{
var matrix = sprite.getWorldTransformMatrix(this.world._tempMatrix, this.world._tempMatrix2);
2018-04-11 12:17:53 +00:00
transform.x = matrix.tx;
transform.y = matrix.ty;
transform.rotation = RadToDeg(matrix.rotation);
transform.scaleX = matrix.scaleX;
transform.scaleY = matrix.scaleY;
transform.displayOriginX = sprite.displayOriginX;
transform.displayOriginY = sprite.displayOriginY;
2018-04-11 12:17:53 +00:00
}
else
{
transform.x = sprite.x;
transform.y = sprite.y;
transform.rotation = sprite.angle;
transform.scaleX = sprite.scaleX;
transform.scaleY = sprite.scaleY;
transform.displayOriginX = sprite.displayOriginX;
transform.displayOriginY = sprite.displayOriginY;
2018-04-11 12:17:53 +00:00
}
var recalc = false;
if (this.syncBounds)
{
var b = sprite.getBounds(this._bounds);
this.width = b.width;
this.height = b.height;
recalc = true;
}
else
{
2018-04-11 12:17:53 +00:00
var asx = Math.abs(transform.scaleX);
var asy = Math.abs(transform.scaleY);
if (this._sx !== asx || this._sy !== asy)
{
this.width = this.sourceWidth * asx;
this.height = this.sourceHeight * asy;
this._sx = asx;
this._sy = asy;
recalc = true;
}
}
if (recalc)
{
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.updateCenter();
}
},
2018-02-09 03:44:23 +00:00
/**
2018-06-13 16:41:50 +00:00
* Updates the Body's `center` from its `position`, `width`, and `height`.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#updateCenter
* @since 3.0.0
*/
updateCenter: function ()
{
this.center.set(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Updates the Body.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#update
2018-04-24 00:48:15 +00:00
* @fires Phaser.Physics.Arcade.World#worldbounds
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*
* @param {number} delta - The delta time, in seconds, elapsed since the last frame.
2018-02-09 03:44:23 +00:00
*/
update: function (delta)
{
// Store and reset collision flags
this.wasTouching.none = this.touching.none;
this.wasTouching.up = this.touching.up;
this.wasTouching.down = this.touching.down;
this.wasTouching.left = this.touching.left;
this.wasTouching.right = this.touching.right;
this.touching.none = true;
this.touching.up = false;
this.touching.down = false;
this.touching.left = false;
this.touching.right = false;
this.blocked.none = true;
this.blocked.up = false;
this.blocked.down = false;
this.blocked.left = false;
this.blocked.right = false;
this.overlapR = 0;
this.overlapX = 0;
this.overlapY = 0;
this.embedded = false;
2018-04-11 12:17:53 +00:00
// Updates the transform values
this.updateBounds();
2018-04-11 12:17:53 +00:00
var sprite = this.transform;
this.position.x = sprite.x + sprite.scaleX * (this.offset.x - sprite.displayOriginX);
this.position.y = sprite.y + sprite.scaleY * (this.offset.y - sprite.displayOriginY);
this.updateCenter();
2018-04-11 12:17:53 +00:00
this.rotation = sprite.rotation;
this.preRotation = this.rotation;
if (this._reset)
{
this.prev.x = this.position.x;
this.prev.y = this.position.y;
}
if (this.moves)
{
this.world.updateMotion(this, delta);
var vx = this.velocity.x;
var vy = this.velocity.y;
this.newVelocity.set(vx * delta, vy * delta);
this.position.add(this.newVelocity);
this.updateCenter();
this.angle = Math.atan2(vy, vx);
this.speed = Math.sqrt(vx * vx + vy * vy);
// Now the State update will throw collision checks at the Body
// And finally we'll integrate the new position back to the Sprite in postUpdate
2017-11-13 01:04:35 +00:00
if (this.collideWorldBounds && this.checkWorldBounds() && this.onWorldBounds)
{
this.world.emit('worldbounds', this, this.blocked.up, this.blocked.down, this.blocked.left, this.blocked.right);
}
}
this._dx = this.position.x - this.prev.x;
this._dy = this.position.y - this.prev.y;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Feeds the Body results back into the parent Game Object.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#postUpdate
* @since 3.0.0
*
* @param {boolean} resetDelta - Reset the delta properties?
2018-02-09 03:44:23 +00:00
*/
postUpdate: function ()
{
this._dx = this.position.x - this.prev.x;
this._dy = this.position.y - this.prev.y;
if (this.moves)
{
if (this.deltaMax.x !== 0 && this._dx !== 0)
{
if (this._dx < 0 && this._dx < -this.deltaMax.x)
{
this._dx = -this.deltaMax.x;
}
else if (this._dx > 0 && this._dx > this.deltaMax.x)
{
this._dx = this.deltaMax.x;
}
}
if (this.deltaMax.y !== 0 && this._dy !== 0)
{
if (this._dy < 0 && this._dy < -this.deltaMax.y)
{
this._dy = -this.deltaMax.y;
}
else if (this._dy > 0 && this._dy > this.deltaMax.y)
{
this._dy = this.deltaMax.y;
}
}
this.gameObject.x += this._dx;
this.gameObject.y += this._dy;
this._reset = true;
}
if (this._dx < 0)
{
this.facing = CONST.FACING_LEFT;
}
else if (this._dx > 0)
{
this.facing = CONST.FACING_RIGHT;
}
if (this._dy < 0)
{
this.facing = CONST.FACING_UP;
}
else if (this._dy > 0)
{
this.facing = CONST.FACING_DOWN;
}
if (this.allowRotation)
{
this.gameObject.angle += this.deltaZ();
}
this.prev.x = this.position.x;
this.prev.y = this.position.y;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Checks for collisions between this Body and the world boundary and separates them.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#checkWorldBounds
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if this Body is colliding with the world boundary.
2018-02-09 03:44:23 +00:00
*/
checkWorldBounds: function ()
{
var pos = this.position;
var bounds = this.world.bounds;
var check = this.world.checkCollision;
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
if (pos.x < bounds.x && check.left)
{
pos.x = bounds.x;
this.velocity.x *= bx;
this.blocked.left = true;
this.blocked.none = false;
}
else if (this.right > bounds.right && check.right)
{
pos.x = bounds.right - this.width;
this.velocity.x *= bx;
this.blocked.right = true;
this.blocked.none = false;
}
if (pos.y < bounds.y && check.up)
{
pos.y = bounds.y;
this.velocity.y *= by;
this.blocked.up = true;
this.blocked.none = false;
}
else if (this.bottom > bounds.bottom && check.down)
{
pos.y = bounds.bottom - this.height;
this.velocity.y *= by;
this.blocked.down = true;
this.blocked.none = false;
}
return !this.blocked.none;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the offset of the Body's position from its Game Object's position.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setOffset
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal offset, in source pixels.
* @param {number} [y=x] - The vertical offset, in source pixels.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setOffset: function (x, y)
{
if (y === undefined) { y = x; }
this.offset.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sizes and positions this Body's boundary, as a rectangle.
2018-07-31 08:39:22 +00:00
* Modifies the Body `offset` if `center` is true (the default).
* Resets the width and height to match current frame, if no width and height provided and a frame is found.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setSize
* @since 3.0.0
*
2018-07-31 08:39:22 +00:00
* @param {integer} [width] - The width of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame width.
* @param {integer} [height] - The height of the Body in pixels. Cannot be zero. If not given, and the parent Game Object has a frame, it will use the frame height.
2018-09-01 09:14:22 +00:00
* @param {boolean} [center=true] - Modify the Body's `offset`, placing the Body's center on its Game Object's center. Only works if the Game Object has the `getCenter` method.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setSize: function (width, height, center)
{
if (center === undefined) { center = true; }
var gameObject = this.gameObject;
2018-07-31 08:39:22 +00:00
if (!width && gameObject.frame)
{
width = gameObject.frame.realWidth;
}
if (!height && gameObject.frame)
{
height = gameObject.frame.realHeight;
}
2018-06-13 16:41:50 +00:00
this.sourceWidth = width;
this.sourceHeight = height;
this.width = this.sourceWidth * this._sx;
this.height = this.sourceHeight * this._sy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.updateCenter();
if (center && gameObject.getCenter)
{
var ox = gameObject.displayWidth / 2;
var oy = gameObject.displayHeight / 2;
this.offset.set(ox - this.halfWidth, oy - this.halfHeight);
}
this.isCircle = false;
this.radius = 0;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sizes and positions this Body's boundary, as a circle.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setCircle
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} radius - The radius of the Body, in source pixels.
* @param {number} [offsetX] - The horizontal offset of the Body from its Game Object, in source pixels.
* @param {number} [offsetY] - The vertical offset of the Body from its Game Object, in source pixels.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setCircle: function (radius, offsetX, offsetY)
{
if (offsetX === undefined) { offsetX = this.offset.x; }
if (offsetY === undefined) { offsetY = this.offset.y; }
if (radius > 0)
{
this.isCircle = true;
this.radius = radius;
this.sourceWidth = radius * 2;
this.sourceHeight = radius * 2;
this.width = this.sourceWidth * this._sx;
this.height = this.sourceHeight * this._sy;
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.offset.set(offsetX, offsetY);
this.updateCenter();
}
else
{
this.isCircle = false;
}
return this;
},
2018-02-09 03:44:23 +00:00
/**
* Resets this Body to the given coordinates. Also positions its parent Game Object to the same coordinates.
2018-04-24 00:48:15 +00:00
* If the Body had any velocity or acceleration it is lost as a result of calling this.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#reset
* @since 3.0.0
*
* @param {number} x - The horizontal position to place the Game Object and Body.
* @param {number} y - The vertical position to place the Game Object and Body.
2018-02-09 03:44:23 +00:00
*/
reset: function (x, y)
{
this.stop();
var gameObject = this.gameObject;
gameObject.setPosition(x, y);
gameObject.getTopLeft(this.position);
this.prev.copy(this.position);
this.rotation = gameObject.angle;
this.preRotation = gameObject.angle;
this.updateBounds();
this.updateCenter();
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets acceleration, velocity, and speed to zero.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#stop
* @since 3.0.0
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
stop: function ()
{
this.velocity.set(0);
this.acceleration.set(0);
this.speed = 0;
this.angularVelocity = 0;
this.angularAcceleration = 0;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Copies the coordinates of this Body's edges into an object.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#getBounds
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {ArcadeBodyBounds} obj - An object to copy the values into.
2018-02-09 03:44:23 +00:00
*
2018-04-24 00:48:15 +00:00
* @return {ArcadeBodyBounds} - An object with {x, y, right, bottom}.
2018-02-09 03:44:23 +00:00
*/
getBounds: function (obj)
{
obj.x = this.x;
obj.y = this.y;
obj.right = this.right;
obj.bottom = this.bottom;
return obj;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Tests if the coordinates are within this Body's boundary.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#hitTest
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal coordinate.
* @param {number} y - The vertical coordinate.
2018-02-09 03:44:23 +00:00
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if (x, y) is within this Body.
2018-02-09 03:44:23 +00:00
*/
hitTest: function (x, y)
{
return (this.isCircle) ? CircleContains(this, x, y) : RectangleContains(this, x, y);
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is touching a tile or the world boundary while moving down.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#onFloor
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#blocked
2018-02-09 03:44:23 +00:00
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if touching.
2018-02-09 03:44:23 +00:00
*/
onFloor: function ()
{
return this.blocked.down;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is touching a tile or the world boundary while moving up.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#onCeiling
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#blocked
2018-02-09 03:44:23 +00:00
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if touching.
2018-02-09 03:44:23 +00:00
*/
onCeiling: function ()
{
return this.blocked.up;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body is touching a tile or the world boundary while moving left or right.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#onWall
* @since 3.0.0
2018-04-24 00:48:15 +00:00
* @see Phaser.Physics.Arcade.Body#blocked
2018-02-09 03:44:23 +00:00
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if touching.
2018-02-09 03:44:23 +00:00
*/
onWall: function ()
{
return (this.blocked.left || this.blocked.right);
},
2018-02-09 03:44:23 +00:00
/**
* The absolute (non-negative) change in this Body's horizontal position from the previous step.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaAbsX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {number} The delta value.
2018-02-09 03:44:23 +00:00
*/
deltaAbsX: function ()
{
return (this._dx > 0) ? this._dx : -this._dx;
},
2018-02-09 03:44:23 +00:00
/**
* The absolute (non-negative) change in this Body's vertical position from the previous step.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaAbsY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {number} The delta value.
2018-02-09 03:44:23 +00:00
*/
deltaAbsY: function ()
{
return (this._dy > 0) ? this._dy : -this._dy;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The change in this Body's horizontal position from the previous step.
* This value is set during the Body's update phase.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {number} The delta value.
2018-02-09 03:44:23 +00:00
*/
deltaX: function ()
{
return this._dx;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The change in this Body's vertical position from the previous step.
* This value is set during the Body's update phase.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {number} The delta value.
2018-02-09 03:44:23 +00:00
*/
deltaY: function ()
{
return this._dy;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The change in this Body's rotation from the previous step, in degrees.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#deltaZ
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {number} The delta value.
2018-02-09 03:44:23 +00:00
*/
deltaZ: function ()
{
return this.rotation - this.preRotation;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Disables this Body and marks it for deletion by the simulation.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#destroy
* @since 3.0.0
*/
destroy: function ()
{
this.enable = false;
if (this.world)
{
this.world.pendingDestroy.set(this);
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Draws this Body's boundary and velocity, if enabled.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#drawDebug
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {Phaser.GameObjects.Graphics} graphic - The Graphics object to draw on.
2018-02-09 03:44:23 +00:00
*/
drawDebug: function (graphic)
{
var pos = this.position;
var x = pos.x + this.halfWidth;
var y = pos.y + this.halfHeight;
if (this.debugShowBody)
{
graphic.lineStyle(graphic.defaultStrokeWidth, this.debugBodyColor);
if (this.isCircle)
{
2018-03-13 00:02:58 +00:00
graphic.strokeCircle(x, y, this.width / 2);
}
else
{
graphic.strokeRect(pos.x, pos.y, this.width, this.height);
}
}
if (this.debugShowVelocity)
{
graphic.lineStyle(graphic.defaultStrokeWidth, this.world.defaults.velocityDebugColor, 1);
2017-11-09 23:56:12 +00:00
graphic.lineBetween(x, y, x + this.velocity.x / 2, y + this.velocity.y / 2);
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Whether this Body will be drawn to the debug display.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#willDrawDebug
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @return {boolean} True if either `debugShowBody` or `debugShowVelocity` are enabled.
2018-02-09 03:44:23 +00:00
*/
willDrawDebug: function ()
{
return (this.debugShowBody || this.debugShowVelocity);
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets whether this Body collides with the world boundary.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setCollideWorldBounds
* @since 3.0.0
*
* @param {boolean} [value=true] - True (collisions) or false (no collisions).
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setCollideWorldBounds: function (value)
{
if (value === undefined) { value = true; }
this.collideWorldBounds = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's velocity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setVelocity
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal velocity, in pixels per second.
* @param {number} [y=x] - The vertical velocity, in pixels per second.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setVelocity: function (x, y)
{
this.velocity.set(x, y);
this.speed = Math.sqrt(x * x + y * y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal velocity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setVelocityX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The velocity, in pixels per second.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setVelocityX: function (value)
{
this.velocity.x = value;
var vx = value;
var vy = this.velocity.y;
this.speed = Math.sqrt(vx * vx + vy * vy);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical velocity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setVelocityY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The velocity, in pixels per second.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setVelocityY: function (value)
{
this.velocity.y = value;
var vx = this.velocity.x;
var vy = value;
this.speed = Math.sqrt(vx * vx + vy * vy);
return this;
},
/**
* Sets the Body's maximum velocity.
*
* @method Phaser.Physics.Arcade.Body#setMaxVelocity
* @since 3.10.0
*
* @param {number} x - The horizontal velocity, in pixels per second.
* @param {number} [y=x] - The vertical velocity, in pixels per second.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setMaxVelocity: function (x, y)
{
this.maxVelocity.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's bounce.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setBounce
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal bounce, relative to 1.
* @param {number} y - The vertical bounce, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setBounce: function (x, y)
{
this.bounce.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal bounce.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setBounceX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The bounce, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setBounceX: function (value)
{
this.bounce.x = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical bounce.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setBounceY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The bounce, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setBounceY: function (value)
{
this.bounce.y = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's acceleration.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAcceleration
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal component, in pixels per second squared.
* @param {number} y - The vertical component, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAcceleration: function (x, y)
{
this.acceleration.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal acceleration.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAccelerationX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The acceleration, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAccelerationX: function (value)
{
this.acceleration.x = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical acceleration.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAccelerationY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The acceleration, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAccelerationY: function (value)
{
this.acceleration.y = value;
return this;
},
/**
* Enables or disables drag.
*
* @method Phaser.Physics.Arcade.Body#setAllowDrag
* @since 3.9.0
* @see Phaser.Physics.Arcade.Body#allowDrag
*
* @param {boolean} [value=true] - `true` to allow drag on this body, or `false` to disable it.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAllowDrag: function (value)
{
if (value === undefined) { value = true; }
this.allowDrag = value;
return this;
},
/**
* Enables or disables gravity's effect on this Body.
*
* @method Phaser.Physics.Arcade.Body#setAllowGravity
* @since 3.9.0
* @see Phaser.Physics.Arcade.Body#allowGravity
*
* @param {boolean} [value=true] - `true` to allow gravity on this body, or `false` to disable it.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAllowGravity: function (value)
{
if (value === undefined) { value = true; }
this.allowGravity = value;
return this;
},
/**
* Enables or disables rotation.
*
* @method Phaser.Physics.Arcade.Body#setAllowRotation
* @since 3.9.0
* @see Phaser.Physics.Arcade.Body#allowRotation
*
* @param {boolean} [value=true] - `true` to allow rotation on this body, or `false` to disable it.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAllowRotation: function (value)
{
if (value === undefined) { value = true; }
this.allowRotation = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's drag.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setDrag
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal component, in pixels per second squared.
* @param {number} y - The vertical component, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setDrag: function (x, y)
{
this.drag.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal drag.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setDragX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The drag, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setDragX: function (value)
{
this.drag.x = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical drag.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setDragY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The drag, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setDragY: function (value)
{
this.drag.y = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's gravity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setGravity
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal component, in pixels per second squared.
* @param {number} y - The vertical component, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setGravity: function (x, y)
{
this.gravity.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal gravity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setGravityX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The gravity, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setGravityX: function (value)
{
this.gravity.x = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical gravity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setGravityY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The gravity, in pixels per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setGravityY: function (value)
{
this.gravity.y = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's friction.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setFriction
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} x - The horizontal component, relative to 1.
* @param {number} y - The vertical component, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setFriction: function (x, y)
{
this.friction.set(x, y);
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's horizontal friction.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setFrictionX
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The friction value, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setFrictionX: function (value)
{
this.friction.x = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's vertical friction.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setFrictionY
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The friction value, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setFrictionY: function (value)
{
this.friction.y = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's angular velocity.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAngularVelocity
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The velocity, in degrees per second.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAngularVelocity: function (value)
{
this.angularVelocity = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's angular acceleration.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAngularAcceleration
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The acceleration, in degrees per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAngularAcceleration: function (value)
{
this.angularAcceleration = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's angular drag.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setAngularDrag
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The drag, in degrees per second squared.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setAngularDrag: function (value)
{
this.angularDrag = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's mass.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setMass
* @since 3.0.0
*
2018-04-24 00:48:15 +00:00
* @param {number} value - The mass value, relative to 1.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setMass: function (value)
{
this.mass = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* Sets the Body's `immovable` property.
2018-02-09 03:44:23 +00:00
*
* @method Phaser.Physics.Arcade.Body#setImmovable
* @since 3.0.0
*
* @param {boolean} [value=true] - The value to assign to `immovable`.
2018-02-09 03:44:23 +00:00
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setImmovable: function (value)
{
if (value === undefined) { value = true; }
this.immovable = value;
return this;
},
/**
* Sets the Body's `enable` property.
*
* @method Phaser.Physics.Arcade.Body#setEnable
2018-10-12 17:32:52 +00:00
* @since 3.15.0
*
* @param {boolean} [value=true] - The value to assign to `enable`.
*
* @return {Phaser.Physics.Arcade.Body} This Body object.
*/
setEnable: function (value)
{
if (value === undefined) { value = true; }
this.enable = value;
return this;
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's horizontal position (left edge).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#x
* @type {number}
* @since 3.0.0
*/
x: {
get: function ()
{
return this.position.x;
},
set: function (value)
{
this.position.x = value;
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The Body's vertical position (top edge).
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#y
* @type {number}
* @since 3.0.0
*/
y: {
get: function ()
{
return this.position.y;
},
set: function (value)
{
this.position.y = value;
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The left edge of the Body's boundary. Identical to x.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#left
* @type {number}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
left: {
get: function ()
{
return this.position.x;
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The right edge of the Body's boundary.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#right
* @type {number}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
right: {
get: function ()
{
return this.position.x + this.width;
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The top edge of the Body's boundary. Identical to y.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#top
* @type {number}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
top: {
get: function ()
{
return this.position.y;
}
},
2018-02-09 03:44:23 +00:00
/**
2018-04-24 00:48:15 +00:00
* The bottom edge of this Body's boundary.
2018-02-09 03:44:23 +00:00
*
* @name Phaser.Physics.Arcade.Body#bottom
* @type {number}
2018-10-09 12:40:00 +00:00
* @readonly
2018-02-09 03:44:23 +00:00
* @since 3.0.0
*/
bottom: {
get: function ()
{
return this.position.y + this.height;
}
}
});
module.exports = Body;