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}
* /
2019-03-18 08:08:46 +00:00
var ArrayAdd = require ( '../../utils/array/Add' ) ;
2017-11-08 17:18:41 +00:00
var CircleContains = require ( '../../geom/circle/Contains' ) ;
2019-03-25 12:55:31 +00:00
var CheckOverlap = require ( './CheckOverlap' ) ;
2017-11-08 17:18:41 +00:00
var Class = require ( '../../utils/Class' ) ;
var CONST = require ( './const' ) ;
2019-01-17 14:04:36 +00:00
var Events = require ( './events' ) ;
2019-03-20 01:28:00 +00:00
var FuzzyEqual = require ( '../../math/fuzzy/Equal' ) ;
2019-03-18 14:12:52 +00:00
var FuzzyGreaterThan = require ( '../../math/fuzzy/GreaterThan' ) ;
var FuzzyLessThan = require ( '../../math/fuzzy/LessThan' ) ;
2018-04-11 12:17:53 +00:00
var RadToDeg = require ( '../../math/RadToDeg' ) ;
2017-11-08 17:18:41 +00:00
var Rectangle = require ( '../../geom/rectangle/Rectangle' ) ;
var RectangleContains = require ( '../../geom/rectangle/Contains' ) ;
var Vector2 = require ( '../../math/Vector2' ) ;
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
* /
2017-11-08 17:18:41 +00:00
var Body = new Class ( {
initialize :
function Body ( world , gameObject )
{
2018-04-11 12:37:38 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . debugShowVelocity = world . defaults . debugShowVelocity ;
2018-02-09 03:44:23 +00:00
2019-03-11 11:05:42 +00:00
/ * *
* Whether the Body ' s blocked faces are drawn to the debug display .
*
* @ name Phaser . Physics . Arcade . Body # debugShowVelocity
* @ type { boolean }
* @ since 3.17 . 0
* /
2019-03-15 12:53:59 +00:00
this . debugShowBlocked = world . defaults . debugShowBlocked ;
2019-03-11 11:05:42 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . prev = new Vector2 ( gameObject . x , gameObject . y ) ;
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . width = width ;
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . height = height ;
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . sourceWidth = width ;
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . sourceHeight = height ;
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . halfWidth = Math . abs ( width / 2 ) ;
2017-11-08 17:18:41 +00:00
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
* /
2018-04-11 12:37:38 +00:00
this . halfHeight = Math . abs ( height / 2 ) ;
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . velocity = new Vector2 ( ) ;
2019-03-20 04:06:53 +00:00
/ * *
* The minimum velocity a body can move before it won ' t rebound and is considered for sleep .
2019-03-21 01:02:38 +00:00
* The default is 15 but you may wish to change this based on game type .
2019-03-20 04:06:53 +00:00
*
* @ name Phaser . Physics . Arcade . Body # minVelocity
* @ type { Phaser . Math . Vector2 }
* @ since 3.17 . 0
* /
this . minVelocity = new Vector2 ( 15 , 15 ) ;
2019-03-15 13:31:57 +00:00
this . prevVelocity = new Vector2 ( ) ;
2018-02-09 03:44:23 +00:00
/ * *
2019-03-15 12:53:59 +00:00
* Is the Body asleep ? .
2018-02-09 03:44:23 +00:00
*
2019-03-15 12:53:59 +00:00
* @ name Phaser . Physics . Arcade . Body # sleeping
* @ type { boolean }
2018-10-09 12:40:00 +00:00
* @ readonly
2019-03-14 14:52:07 +00:00
* @ since 3.17 . 0
2018-02-09 03:44:23 +00:00
* /
2019-03-15 12:53:59 +00:00
this . sleeping = false ;
this . _sleep = 0 ;
2019-03-22 12:18:34 +00:00
this . sleepIterations = 60 * world . positionIterations ;
2019-03-15 13:31:57 +00:00
2019-03-24 23:30:38 +00:00
// 0 = none
// 1 = soft up
// 2 = soft down
// 3 = soft left
// 4 = soft right
// 5 = world bounds
// 6 = hard up
// 7 = hard down
// 8 = hard left
// 9 = hard right
// 10 = riding
2019-03-21 01:02:38 +00:00
this . forcePosition = 0 ;
2017-11-08 17:18:41 +00:00
2019-03-22 11:34:23 +00:00
this . snapTo = null ;
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . worldBounce = null ;
2017-11-09 04:02:59 +00:00
// 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
2019-03-08 20:10:49 +00:00
* @ see Phaser . Physics . Arcade . Events # WORLD _BOUNDS
2018-02-09 03:44:23 +00:00
* /
2017-11-09 04:02:59 +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
2019-03-08 20:10:49 +00:00
* @ see Phaser . Physics . Arcade . Events # COLLIDE
2018-02-09 03:44:23 +00:00
* /
2017-11-09 04:02:59 +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
2019-03-08 20:10:49 +00:00
* @ see Phaser . Physics . Arcade . Events # OVERLAP
2018-02-09 03:44:23 +00:00
* /
2017-11-09 04:02:59 +00:00
this . onOverlap = false ;
2017-11-08 17:18:41 +00:00
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 .
2018-09-24 23:58:00 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
this . maxVelocity = new Vector2 ( 10000 , 10000 ) ;
2019-01-24 00:30:44 +00:00
/ * *
2019-01-24 00:26:29 +00:00
* The maximum speed this Body is allowed to reach .
*
* If not negative it limits the scalar value of speed .
*
* Any negative value means no maximum is being applied .
*
* @ name Phaser . Physics . Arcade . Body # maxSpeed
* @ type { number }
* @ since 3.16 . 0
* /
this . maxSpeed = - 1 ;
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
* /
2017-11-08 17:18:41 +00:00
this . friction = new Vector2 ( 1 , 0 ) ;
2018-06-03 16:08:08 +00:00
/ * *
* 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 .
*
2018-06-03 20:17:33 +00:00
* If you enable this property then you should use far smaller ` drag ` values than with linear , as
2018-06-03 16:08:08 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . moves = true ;
2019-03-22 19:07:06 +00:00
/ * *
* Can this body be ridden like a platform ?
*
* @ name Phaser . Physics . Arcade . Body # rideable
* @ type { boolean }
* @ default false
* @ since 3.17 . 0
* /
this . rideable = false ;
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 .
2018-09-25 20:25:47 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
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 .
2018-09-25 20:25:47 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
this . customSeparateY = false ;
2018-02-09 03:44:23 +00:00
/ * *
2018-11-07 17:43:43 +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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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 .
2018-10-18 22:42:25 +00:00
* You can set ` checkCollision.none = true ` to disable collision checks .
2018-02-09 03:44:23 +00:00
*
* @ name Phaser . Physics . Arcade . Body # checkCollision
2019-02-13 14:45:36 +00:00
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
2018-02-09 03:44:23 +00:00
* @ since 3.0 . 0
* /
2017-11-08 17:18:41 +00:00
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
2019-02-13 14:45:36 +00:00
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
2018-02-09 03:44:23 +00:00
* @ since 3.0 . 0
* /
2017-11-08 17:18:41 +00:00
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
2019-02-13 14:45:36 +00:00
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
2018-02-09 03:44:23 +00:00
* @ since 3.0 . 0
* /
2017-11-08 17:18:41 +00:00
this . wasTouching = { none : true , up : false , down : false , left : false , right : false } ;
2018-02-09 03:44:23 +00:00
/ * *
2019-03-11 09:19:41 +00:00
* Whether this Body is blocked from moving in a given direction .
2018-02-09 03:44:23 +00:00
*
* @ name Phaser . Physics . Arcade . Body # blocked
2019-02-13 14:45:36 +00:00
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
2018-02-09 03:44:23 +00:00
* @ since 3.0 . 0
* /
2019-03-19 23:21:48 +00:00
this . blocked = { none : true , up : false , down : false , left : false , right : false } ;
2019-03-11 09:19:41 +00:00
/ * *
* Whether this Body is colliding with a tile or the world boundary .
*
* @ name Phaser . Physics . Arcade . Body # worldBlocked
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
* @ since 3.17 . 0
* /
2019-03-11 11:05:42 +00:00
this . worldBlocked = { none : true , up : false , down : false , left : false , right : false } ;
2017-11-08 17:18:41 +00:00
2019-03-20 04:06:53 +00:00
/ * *
2019-03-22 01:17:32 +00:00
*
2019-03-20 04:06:53 +00:00
*
2019-03-22 01:17:32 +00:00
* @ name Phaser . Physics . Arcade . Body # hardBlocked
2019-03-20 04:06:53 +00:00
* @ type { Phaser . Physics . Arcade . Types . ArcadeBodyCollision }
* @ since 3.17 . 0
* /
2019-03-22 01:17:32 +00:00
this . hardBlocked = { none : true , up : false , down : false , left : false , right : false } ;
2019-03-20 04:06:53 +00:00
2019-03-22 01:17:32 +00:00
this . blockers = { up : [ ] , down : [ ] , left : [ ] , right : [ ] } ;
2019-03-18 08:08:46 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . syncBounds = false ;
2017-11-09 04:02:59 +00:00
// 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
2018-11-07 17:43:43 +00:00
* @ default Phaser . Physics . Arcade . DYNAMIC _BODY
2018-02-09 03:44:23 +00:00
* @ since 3.0 . 0
* /
2017-11-09 15:32:08 +00:00
this . physicsType = CONST . DYNAMIC _BODY ;
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
this . _bounds = new Rectangle ( ) ;
2019-03-14 14:52:07 +00:00
/ * *
2019-03-18 14:12:52 +00:00
* The amount of gravity that was applied to the body in the current frame .
2019-03-14 14:52:07 +00:00
*
2019-03-18 14:12:52 +00:00
* @ name Phaser . Physics . Arcade . Body # _gx
* @ type { number }
2019-03-14 14:52:07 +00:00
* @ private
* @ default 0
* @ since 3.17 . 0
* /
2019-03-18 14:12:52 +00:00
this . _gx = 0 ;
2019-03-14 14:52:07 +00:00
/ * *
2019-03-18 14:12:52 +00:00
* The amount of gravity that was applied to the body in the current frame .
2019-03-14 14:52:07 +00:00
*
2019-03-18 14:12:52 +00:00
* @ name Phaser . Physics . Arcade . Body # _gy
* @ type { number }
2019-03-14 14:52:07 +00:00
* @ private
* @ default 0
* @ since 3.17 . 0
* /
2019-03-18 14:12:52 +00:00
this . _gy = 0 ;
2019-03-20 01:28:00 +00:00
this . _sleepX = 0 ;
this . _sleepY = 0 ;
2017-11-08 17:18:41 +00:00
} ,
2018-02-09 03:44:23 +00:00
/ * *
2018-11-07 17:43:43 +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
* /
2017-11-08 17:18:41 +00:00
updateBounds : function ( )
{
var sprite = this . gameObject ;
2018-04-11 12:17:53 +00:00
// Container?
var transform = this . transform ;
if ( sprite . parentContainer )
{
2018-08-29 15:06:13 +00:00
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 ;
2018-09-26 16:15:22 +00:00
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 ;
2018-09-26 16:15:22 +00:00
transform . displayOriginX = sprite . displayOriginX ;
transform . displayOriginY = sprite . displayOriginY ;
2018-04-11 12:17:53 +00:00
}
2018-06-01 18:18:40 +00:00
var recalc = false ;
2017-11-08 17:18:41 +00:00
if ( this . syncBounds )
{
var b = sprite . getBounds ( this . _bounds ) ;
2018-06-01 18:18:40 +00:00
this . width = b . width ;
this . height = b . height ;
recalc = true ;
2017-11-08 17:18:41 +00:00
}
else
{
2018-04-11 12:17:53 +00:00
var asx = Math . abs ( transform . scaleX ) ;
var asy = Math . abs ( transform . scaleY ) ;
2017-11-08 17:18:41 +00:00
2018-06-01 18:18:40 +00:00
if ( this . _sx !== asx || this . _sy !== asy )
2017-11-08 17:18:41 +00:00
{
this . width = this . sourceWidth * asx ;
this . height = this . sourceHeight * asy ;
this . _sx = asx ;
this . _sy = asy ;
2018-06-01 18:18:40 +00:00
recalc = true ;
2017-11-08 17:18:41 +00:00
}
}
2018-06-01 18:18:40 +00:00
if ( recalc )
2017-11-08 17:18:41 +00:00
{
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
* /
2017-11-08 17:18:41 +00:00
updateCenter : function ( )
{
this . center . set ( this . position . x + this . halfWidth , this . position . y + this . halfHeight ) ;
} ,
2018-02-09 03:44:23 +00:00
/ * *
2019-03-05 14:17:57 +00:00
* Prepares the Body for a physics step by resetting all the states and syncing the position
* with the parent Game Object .
*
* This method is only ever called once per game step .
2018-02-09 03:44:23 +00:00
*
2019-03-05 14:17:57 +00:00
* @ method Phaser . Physics . Arcade . Body # preUpdate
2019-03-07 12:31:06 +00:00
* @ since 3.17 . 0
2018-02-09 03:44:23 +00:00
* /
2019-03-05 14:17:57 +00:00
preUpdate : function ( )
2017-11-08 17:18:41 +00:00
{
2019-03-11 09:19:41 +00:00
var touching = this . touching ;
var blocked = this . blocked ;
var worldBlocked = this . worldBlocked ;
2019-03-22 01:17:32 +00:00
var hardBlocked = this . hardBlocked ;
2019-03-11 09:19:41 +00:00
touching . none = true ;
touching . up = false ;
touching . down = false ;
touching . left = false ;
touching . right = false ;
2019-03-13 17:27:11 +00:00
blocked . none = true ;
blocked . up = false ;
blocked . down = false ;
blocked . left = false ;
blocked . right = false ;
2019-03-11 11:05:42 +00:00
worldBlocked . none = true ;
2019-03-11 09:19:41 +00:00
worldBlocked . left = false ;
worldBlocked . right = false ;
worldBlocked . up = false ;
worldBlocked . down = false ;
2017-11-08 17:18:41 +00:00
2019-03-22 01:17:32 +00:00
hardBlocked . none = true ;
hardBlocked . left = false ;
hardBlocked . right = false ;
hardBlocked . up = false ;
hardBlocked . down = false ;
2019-03-22 12:50:28 +00:00
this . snapTo = null ;
2017-11-08 17:18:41 +00:00
this . embedded = false ;
2019-03-21 01:02:38 +00:00
this . forcePosition = 0 ;
2017-11-08 17:18:41 +00:00
2018-04-11 12:17:53 +00:00
// Updates the transform values
2017-11-08 17:18:41 +00:00
this . updateBounds ( ) ;
2019-03-19 23:21:48 +00:00
var parent = this . transform ;
2017-11-08 17:18:41 +00:00
2019-03-19 23:21:48 +00:00
this . x = parent . x + parent . scaleX * ( this . offset . x - parent . displayOriginX ) ;
this . y = parent . y + parent . scaleY * ( this . offset . y - parent . displayOriginY ) ;
2017-11-08 17:18:41 +00:00
2019-03-19 23:21:48 +00:00
this . rotation = parent . rotation ;
2019-03-18 08:08:46 +00:00
2019-03-26 15:29:58 +00:00
// Reset deltas (world bounds checks have no effect on this)
this . prev . x = this . x ;
this . prev . y = this . y ;
this . preRotation = this . rotation ;
2019-03-18 08:08:46 +00:00
if ( this . collideWorldBounds )
2019-03-11 09:19:41 +00:00
{
2019-03-18 08:08:46 +00:00
this . checkWorldBounds ( ) ;
2019-03-11 09:19:41 +00:00
}
2019-03-21 11:31:05 +00:00
this . updateCenter ( ) ;
2019-03-11 09:19:41 +00:00
2019-03-22 14:48:00 +00:00
this . prevVelocity . x = this . velocity . x ;
this . prevVelocity . y = this . velocity . y ;
2019-03-05 14:17:57 +00:00
} ,
2017-11-08 17:18:41 +00:00
2019-03-21 17:24:34 +00:00
/ * *
* Performs a single physics step and updates the body velocity , angle , speed and other
* properties .
*
* This method can be called multiple times per game step .
*
* The results are synced back to the Game Object in ` postUpdate ` .
*
* @ method Phaser . Physics . Arcade . Body # update
* @ fires Phaser . Physics . Arcade . Events # WORLD _BOUNDS
* @ since 3.0 . 0
*
* @ param { number } delta - The delta time , in seconds , elapsed since the last frame .
* /
update : function ( delta )
{
this . checkBlockers ( ) ;
var velocity = this . velocity ;
var position = this . position ;
if ( this . moves )
{
this . world . updateMotion ( this , delta ) ;
2019-03-22 14:48:00 +00:00
// Has it been woken up?
if ( this . sleeping && ! this . checkWake ( ) )
{
return ;
}
2019-03-21 23:21:02 +00:00
if ( this . collideWorldBounds && ! this . worldBlocked . none )
2019-03-21 17:24:34 +00:00
{
this . checkWorldRebound ( ) ;
}
2019-03-22 12:18:34 +00:00
if ( this . forcePosition < 5 )
2019-03-21 17:24:34 +00:00
{
2019-03-26 13:07:57 +00:00
this . moveX ( velocity . x * delta ) ;
this . moveY ( velocity . y * delta ) ;
2019-03-21 17:24:34 +00:00
}
}
// Calculate the delta
this . _dx = position . x - this . prev . x ;
this . _dy = position . y - this . prev . y ;
this . updateCenter ( ) ;
var vx = velocity . x ;
var vy = velocity . y ;
this . angle = Math . atan2 ( vy , vx ) ;
this . speed = Math . sqrt ( vx * vx + vy * vy ) ;
// Now the update will throw collision checks at the Body
// And finally we'll integrate the new position back to the Sprite in postUpdate
} ,
/ * *
* Feeds the Body results back into the parent Game Object .
*
* This method is only ever called once per game step .
*
* @ method Phaser . Physics . Arcade . Body # postUpdate
* @ since 3.0 . 0
* /
postUpdate : function ( )
{
var dx = this . position . x - this . prev . x ;
var dy = this . position . y - this . prev . y ;
var gameObject = this . gameObject ;
if ( this . moves )
{
var mx = this . deltaMax . x ;
var my = this . deltaMax . y ;
if ( mx !== 0 && dx !== 0 )
{
if ( dx < 0 && dx < - mx )
{
dx = - mx ;
}
else if ( dx > 0 && dx > mx )
{
dx = mx ;
}
}
if ( my !== 0 && dy !== 0 )
{
if ( dy < 0 && dy < - my )
{
dy = - my ;
}
else if ( dy > 0 && dy > my )
{
dy = my ;
}
}
if ( dx < 0 )
{
this . facing = CONST . FACING _LEFT ;
}
else if ( dx > 0 )
{
this . facing = CONST . FACING _RIGHT ;
}
if ( dy < 0 )
{
this . facing = CONST . FACING _UP ;
}
else if ( dy > 0 )
{
this . facing = CONST . FACING _DOWN ;
}
2019-03-22 11:34:23 +00:00
if ( this . forcePosition > 0 )
2019-03-21 17:24:34 +00:00
{
2019-03-22 11:34:23 +00:00
var snapX = this . x ;
var snapY = this . y ;
switch ( this . forcePosition )
{
case 1 :
snapY = this . snapTo . bottom ;
break ;
2019-03-21 17:24:34 +00:00
2019-03-22 11:34:23 +00:00
case 2 :
snapY = this . snapTo . y - this . height ;
break ;
2019-03-25 12:55:31 +00:00
case 3 :
snapX = this . snapTo . right ;
break ;
case 4 :
snapX = this . snapTo . x - this . width ;
break ;
2019-03-22 11:34:23 +00:00
}
2019-03-26 15:29:58 +00:00
gameObject . x += ( snapX - this . prev . x ) ;
gameObject . y += ( snapY - this . prev . y ) ;
2019-03-21 17:24:34 +00:00
dx = 0 ;
dy = 0 ;
}
else if ( ! this . sleeping )
{
2019-03-22 12:18:34 +00:00
gameObject . x += dx / this . world . positionIterations ;
gameObject . y += dy / this . world . positionIterations ;
2019-03-21 17:24:34 +00:00
if ( this . allowRotation )
{
gameObject . angle += this . deltaZ ( ) ;
}
}
}
this . _dx = dx ;
this . _dy = dy ;
2019-03-26 13:07:57 +00:00
this . checkSleep ( dx , dy ) ;
this . _sleepX = this . x ;
this . _sleepY = this . y ;
2019-03-21 17:24:34 +00:00
// Store collision flags
var wasTouching = this . wasTouching ;
var touching = this . touching ;
wasTouching . none = touching . none ;
wasTouching . up = touching . up ;
wasTouching . down = touching . down ;
wasTouching . left = touching . left ;
wasTouching . right = touching . right ;
} ,
2019-03-25 17:25:39 +00:00
sleep : function ( forceY )
{
if ( ! this . sleeping )
{
this . sleeping = true ;
2019-03-26 13:07:57 +00:00
// console.log(this.gameObject.name, 'put to sleep on frame', this.world._frame, 'force?', forceY, 'at', this.x);
2019-03-25 17:25:39 +00:00
this . velocity . set ( 0 ) ;
this . prevVelocity . set ( 0 ) ;
this . speed = 0 ;
if ( forceY )
{
// this.snapToBlocker();
}
}
} ,
2019-03-22 11:34:23 +00:00
snapToBlocker : function ( )
2019-03-19 23:21:48 +00:00
{
2019-03-25 17:25:39 +00:00
if ( ! this . velocity . equals ( 0 ) )
2019-03-19 23:21:48 +00:00
{
2019-03-22 11:34:23 +00:00
return ;
}
2019-03-19 23:21:48 +00:00
2019-03-22 11:34:23 +00:00
var blocked = this . blocked ;
var worldBlocked = this . worldBlocked ;
2019-03-20 04:06:53 +00:00
2019-03-22 11:34:23 +00:00
if ( ! worldBlocked . none )
{
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'snapped to world bounds');
2019-03-20 14:59:53 +00:00
2019-03-22 11:34:23 +00:00
var worldBounds = this . world . bounds ;
2019-03-20 14:59:53 +00:00
2019-03-25 17:25:39 +00:00
if ( worldBlocked . up )
{
this . y = worldBounds . y ;
}
else if ( worldBlocked . down )
2019-03-22 11:34:23 +00:00
{
this . bottom = worldBounds . bottom ;
}
2019-03-26 15:29:58 +00:00
if ( worldBlocked . left )
2019-03-20 14:59:53 +00:00
{
2019-03-25 17:25:39 +00:00
this . x = worldBounds . x ;
}
else if ( worldBlocked . right )
{
this . right = worldBounds . right ;
2019-03-22 11:34:23 +00:00
}
2019-03-26 15:29:58 +00:00
this . forcePosition = 5 ;
2019-03-22 11:34:23 +00:00
}
else if ( ! blocked . none )
{
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'snapped to blocker bounds scanning ...');
2019-03-21 11:31:05 +00:00
2019-03-22 11:34:23 +00:00
var body2 ;
2019-03-20 14:59:53 +00:00
2019-03-25 17:25:39 +00:00
if ( blocked . up )
{
body2 = this . getBlocker ( this . blockers . up ) ;
if ( body2 )
{
// console.log('blocker bounds found', body2.y);
this . y = body2 . bottom ;
this . forcePosition = 5 ;
}
}
else if ( blocked . down )
2019-03-22 11:34:23 +00:00
{
body2 = this . getBlocker ( this . blockers . down ) ;
if ( body2 )
2019-03-20 14:59:53 +00:00
{
2019-03-22 19:07:06 +00:00
// console.log('blocker bounds found', body2.y);
2019-03-22 11:34:23 +00:00
this . bottom = body2 . y ;
this . forcePosition = 5 ;
2019-03-20 14:59:53 +00:00
}
}
2019-03-25 17:25:39 +00:00
else if ( blocked . left )
2019-03-21 11:31:05 +00:00
{
2019-03-25 17:25:39 +00:00
body2 = this . getBlocker ( this . blockers . left ) ;
2019-03-21 11:31:05 +00:00
2019-03-22 11:34:23 +00:00
if ( body2 )
2019-03-21 11:31:05 +00:00
{
2019-03-22 19:07:06 +00:00
// console.log('blocker bounds found', body2.y);
2019-03-22 01:17:32 +00:00
2019-03-25 17:25:39 +00:00
this . x = body2 . right ;
2019-03-22 01:17:32 +00:00
2019-03-22 11:34:23 +00:00
this . forcePosition = 5 ;
2019-03-21 11:31:05 +00:00
}
}
2019-03-25 17:25:39 +00:00
else if ( blocked . right )
{
body2 = this . getBlocker ( this . blockers . right ) ;
2019-03-19 23:21:48 +00:00
2019-03-25 17:25:39 +00:00
if ( body2 )
{
// console.log('blocker bounds found', body2.y);
2019-03-22 11:34:23 +00:00
2019-03-25 17:25:39 +00:00
this . right = body2 . x ;
2019-03-22 11:34:23 +00:00
2019-03-25 17:25:39 +00:00
this . forcePosition = 5 ;
}
2019-03-22 11:34:23 +00:00
}
}
} ,
2019-03-22 01:17:32 +00:00
getBlocker : function ( blockers )
{
for ( var i = 0 ; i < blockers . length ; i ++ )
{
var collisionInfo = blockers [ i ] ;
2019-03-22 02:29:48 +00:00
// console.log('CI', collisionInfo.body1.gameObject.name, collisionInfo.body2.gameObject.name);
2019-03-22 01:17:32 +00:00
if ( collisionInfo . body1 === this )
{
return collisionInfo . body2 ;
}
else if ( collisionInfo . body2 === this )
{
return collisionInfo . body1 ;
}
}
2019-03-22 02:29:48 +00:00
return null ;
2019-03-22 01:17:32 +00:00
} ,
2019-03-22 14:48:00 +00:00
checkWake : function ( )
{
if ( ! this . sleeping )
{
return false ;
}
var velocity = this . velocity ;
2019-03-25 12:55:31 +00:00
if ( ( velocity . x < 0 && ! this . isBlockedLeft ( ) ) || ( velocity . y > 0 && ! this . isBlockedRight ( ) ) || ( velocity . y < 0 && ! this . isBlockedUp ( ) ) || ( velocity . y > 0 && ! this . isBlockedDown ( ) ) )
2019-03-22 14:48:00 +00:00
{
2019-03-22 19:07:06 +00:00
// console.log('%c' + this.gameObject.name + ' has woken ', 'background-color: lime');
2019-03-22 14:48:00 +00:00
this . sleeping = false ;
this . _sleep = 0 ;
return true ;
}
return false ;
} ,
2019-03-15 13:31:57 +00:00
wake : function ( )
{
if ( this . sleeping )
{
2019-03-22 19:07:06 +00:00
// console.log('%c' + this.gameObject.name + ' has woken ', 'background-color: lime');
2019-03-15 13:31:57 +00:00
this . sleeping = false ;
this . _sleep = 0 ;
}
} ,
2019-03-20 14:59:53 +00:00
checkBlockers : function ( )
2019-03-18 08:08:46 +00:00
{
2019-03-19 23:21:48 +00:00
// Iterate through the list of previous frame blockers and see if they are still there
2019-03-18 08:08:46 +00:00
2019-03-22 01:17:32 +00:00
for ( var face in this . blockers )
2019-03-18 08:08:46 +00:00
{
2019-03-22 01:17:32 +00:00
var currentBlockers = [ ] ;
var prevBlockers = this . blockers [ face ] ;
2019-03-18 08:08:46 +00:00
2019-03-22 01:17:32 +00:00
for ( var i = 0 ; i < prevBlockers . length ; i ++ )
2019-03-18 08:08:46 +00:00
{
2019-03-22 01:17:32 +00:00
var data = prevBlockers [ i ] ;
2019-03-25 12:55:31 +00:00
if ( CheckOverlap ( data ) )
2019-03-22 01:17:32 +00:00
{
currentBlockers . push ( data ) ;
}
2019-03-18 08:08:46 +00:00
}
2019-03-22 01:17:32 +00:00
this . blockers [ face ] = currentBlockers ;
2019-03-18 08:08:46 +00:00
}
} ,
2019-03-26 13:07:57 +00:00
// Return false if this body is:
// Static or Immovable
// Under direct movement control
// Has bounce and is moving left but is blocked right, or is moving right and is blocked left
// Otherwise, return true
canMoveX : function ( )
2019-03-25 12:55:31 +00:00
{
if ( this . physicsType === CONST . STATIC _BODY || this . immovable || ! this . moves )
{
// Static bodies don't move
return false ;
}
2019-03-26 13:07:57 +00:00
var bounceX = this . bounce . x ;
2019-03-25 12:55:31 +00:00
var velocityX = this . velocity . x ;
2019-03-26 13:07:57 +00:00
if ( bounceX && ( ( velocityX < 0 && this . isWorldBlockedRight ( ) ) || ( velocityX > 0 && this . isWorldBlockedLeft ( ) ) ) )
2019-03-25 12:55:31 +00:00
{
return false ;
}
2019-03-26 15:29:58 +00:00
return true ;
2019-03-25 12:55:31 +00:00
} ,
2019-03-26 13:07:57 +00:00
// Return false if this body is:
// Static or Immovable
// Under direct movement control
// Has bounce and is moving up but is blocked down, or is moving down and is blocked up
// Otherwise, return true
canMoveY : function ( )
2019-03-20 14:59:53 +00:00
{
2019-03-22 19:07:06 +00:00
if ( this . physicsType === CONST . STATIC _BODY || this . immovable || ! this . moves )
2019-03-20 14:59:53 +00:00
{
2019-03-20 17:14:44 +00:00
// Static bodies don't move
2019-03-20 14:59:53 +00:00
return false ;
}
2019-03-26 13:07:57 +00:00
var bounceY = this . bounce . y ;
2019-03-20 14:59:53 +00:00
var velocityY = this . velocity . y ;
2019-03-26 13:07:57 +00:00
if ( bounceY && ( ( velocityY < 0 && this . isWorldBlockedDown ( ) ) || ( velocityY > 0 && this . isWorldBlockedUp ( ) ) ) )
2019-03-20 14:59:53 +00:00
{
return false ;
}
2019-03-26 15:29:58 +00:00
return true ;
2019-03-20 14:59:53 +00:00
} ,
// Return true if body can be repositioned after this call, otherwise return false to stop positioning in the update
2019-03-20 04:06:53 +00:00
checkWorldRebound : function ( )
2019-03-18 14:12:52 +00:00
{
2019-03-19 23:21:48 +00:00
var blocked = this . blocked ;
2019-03-18 14:12:52 +00:00
var velocity = this . velocity ;
2019-03-19 23:21:48 +00:00
var worldBlocked = this . worldBlocked ;
2019-03-20 04:06:53 +00:00
var worldCollision = this . world . checkCollision ;
2019-03-18 14:12:52 +00:00
var bx = ( this . worldBounce ) ? this . worldBounce . x : this . bounce . x ;
var by = ( this . worldBounce ) ? this . worldBounce . y : this . bounce . y ;
2019-03-19 23:21:48 +00:00
if ( ! this . collideWorldBounds || worldBlocked . none || velocity . equals ( 0 ) || ( bx === 0 && by === 0 ) )
2019-03-18 14:12:52 +00:00
{
2019-03-19 23:21:48 +00:00
// Nothing to do
2019-03-22 01:17:32 +00:00
// console.log('CWB abort', this.collideWorldBounds, worldBlocked.none, velocity.equals(0));
2019-03-21 23:21:02 +00:00
2019-03-19 23:21:48 +00:00
return true ;
}
2019-03-25 12:55:31 +00:00
var testX = ( bx !== 0 ) && ( ( worldCollision . right && worldBlocked . right && ! blocked . left && velocity . x > 0 ) || ( worldCollision . left && worldBlocked . left && ! blocked . right && velocity . x < 0 ) ) ;
var testY = ( by !== 0 ) && ( ( worldCollision . down && worldBlocked . down && ! blocked . up && velocity . y > 0 ) || ( worldCollision . up && worldBlocked . up && ! blocked . down && velocity . y < 0 ) ) ;
if ( this . sleeping || ( ! testX && ! testY ) )
2019-03-20 14:59:53 +00:00
{
return false ;
}
2019-03-25 12:55:31 +00:00
var sleepX = false ;
var sleepY = false ;
var emitBoundsEvent = false ;
if ( testX )
{
var gravityX = this . _gx ;
var newVelocityX = velocity . x * bx ;
if ( gravityX > 0 )
{
// Gravity is pulling them down
if ( newVelocityX > 0 && ( newVelocityX < gravityX || FuzzyLessThan ( newVelocityX , gravityX , this . minVelocity . x ) ) )
{
// console.log('frame', this.world._frame, this.gameObject.name, 'rebound up too small, sending to sleep', newVelocityY, gravityY);
sleepX = true ;
}
else
{
velocity . x *= - bx ;
// console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY, 'frame', this.world._frame);
if ( this . forcePosition === 5 )
{
this . forcePosition = 0 ;
}
emitBoundsEvent = true ;
}
}
else if ( gravityX < 0 )
{
// Gravity is pulling them up
if ( newVelocityX < 0 && ( newVelocityX > gravityX || FuzzyGreaterThan ( newVelocityX , gravityX , this . minVelocity . x ) ) )
{
// console.log(this.gameObject.name, 'rebound down too small, sending to sleep', newVelocityY, gravityY);
sleepX = true ;
}
else
{
velocity . x *= - bx ;
// console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY, 'frame', this.world._frame);
if ( this . forcePosition === 5 )
{
this . forcePosition = 0 ;
}
emitBoundsEvent = true ;
}
}
else if ( gravityX === 0 )
{
if ( FuzzyEqual ( newVelocityX , 0 , this . minVelocity . x ) )
{
// Gravity is zero, so rebound must have been from velocity alone
// console.log(this.gameObject.name, 'rebound zero g too small, sending to sleep', newVelocityY, gravityY, 'y pos', this.bottom);
sleepX = true ;
}
else
{
velocity . x *= - bx ;
// console.log(this.gameObject.name, 'rebounded zero-g', newVelocityY, velocity.y);
if ( this . forcePosition === 5 )
{
this . forcePosition = 0 ;
}
emitBoundsEvent = true ;
}
}
}
else
{
sleepX = true ;
}
2019-03-19 23:21:48 +00:00
// Reverse the velocity for the world bounce?
2019-03-25 12:55:31 +00:00
if ( testY )
2019-03-19 23:21:48 +00:00
{
2019-03-20 04:06:53 +00:00
var gravityY = this . _gy ;
var newVelocityY = velocity . y * by ;
if ( gravityY > 0 )
2019-03-18 14:12:52 +00:00
{
2019-03-20 04:06:53 +00:00
// Gravity is pulling them down
if ( newVelocityY > 0 && ( newVelocityY < gravityY || FuzzyLessThan ( newVelocityY , gravityY , this . minVelocity . y ) ) )
{
2019-03-22 19:07:06 +00:00
// console.log('frame', this.world._frame, this.gameObject.name, 'rebound up too small, sending to sleep', newVelocityY, gravityY);
2019-03-20 14:59:53 +00:00
2019-03-25 12:55:31 +00:00
sleepY = true ;
2019-03-20 04:06:53 +00:00
}
else
2019-03-18 14:12:52 +00:00
{
2019-03-20 04:06:53 +00:00
velocity . y *= - by ;
2019-03-21 11:31:05 +00:00
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'rebounded up', newVelocityY, gravityY, 'frame', this.world._frame);
2019-03-18 14:12:52 +00:00
2019-03-22 11:34:23 +00:00
if ( this . forcePosition === 5 )
2019-03-21 11:31:05 +00:00
{
this . forcePosition = 0 ;
}
2019-03-25 12:55:31 +00:00
emitBoundsEvent = true ;
2019-03-20 04:06:53 +00:00
}
}
else if ( gravityY < 0 )
{
// Gravity is pulling them up
if ( newVelocityY < 0 && ( newVelocityY > gravityY || FuzzyGreaterThan ( newVelocityY , gravityY , this . minVelocity . y ) ) )
{
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'rebound down too small, sending to sleep', newVelocityY, gravityY);
2019-03-20 14:59:53 +00:00
2019-03-25 12:55:31 +00:00
sleepY = true ;
2019-03-19 23:21:48 +00:00
}
2019-03-20 04:06:53 +00:00
else
2019-03-19 23:21:48 +00:00
{
2019-03-20 04:06:53 +00:00
velocity . y *= - by ;
2019-03-21 11:31:05 +00:00
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'rebounded down', newVelocityY, gravityY, 'frame', this.world._frame);
2019-03-20 04:06:53 +00:00
2019-03-22 11:34:23 +00:00
if ( this . forcePosition === 5 )
2019-03-21 11:31:05 +00:00
{
this . forcePosition = 0 ;
}
2019-03-25 12:55:31 +00:00
emitBoundsEvent = true ;
2019-03-20 04:06:53 +00:00
}
}
2019-03-25 12:55:31 +00:00
else if ( gravityY === 0 )
2019-03-20 04:06:53 +00:00
{
if ( FuzzyEqual ( newVelocityY , 0 , this . minVelocity . y ) )
{
2019-03-25 12:55:31 +00:00
// Gravity is zero, so rebound must have been from velocity alone
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'rebound zero g too small, sending to sleep', newVelocityY, gravityY, 'y pos', this.bottom);
2019-03-25 12:55:31 +00:00
sleepY = true ;
2019-03-20 04:06:53 +00:00
}
else
{
velocity . y *= - by ;
2019-03-25 12:55:31 +00:00
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'rebounded zero-g', newVelocityY, velocity.y);
2019-03-25 12:55:31 +00:00
2019-03-22 11:34:23 +00:00
if ( this . forcePosition === 5 )
2019-03-21 11:31:05 +00:00
{
this . forcePosition = 0 ;
}
2019-03-25 12:55:31 +00:00
emitBoundsEvent = true ;
2019-03-18 14:12:52 +00:00
}
}
}
2019-03-25 12:55:31 +00:00
else
{
sleepY = true ;
}
if ( sleepX && sleepY )
{
2019-03-26 13:07:57 +00:00
// console.log('world rebound');
2019-03-25 12:55:31 +00:00
this . sleep ( true ) ;
}
else if ( emitBoundsEvent && this . onWorldBounds )
{
this . world . emit ( Events . WORLD _BOUNDS , this , worldBlocked . up , worldBlocked . down , worldBlocked . left , worldBlocked . right ) ;
}
2019-03-18 14:12:52 +00:00
} ,
2019-03-25 17:25:39 +00:00
// return true if gravity is pulling up and body blocked up,
// or gravity is pulling down and body blocked down
isGravityBlockedX : function ( )
{
var gx = this . _gx ;
return ( gx === 0 || ( gx < 0 && this . isBlockedLeft ( ) ) || ( gx > 0 && this . isBlockedRight ( ) ) ) ;
} ,
2019-03-20 01:28:00 +00:00
// return true if gravity is pulling up and body blocked up,
// or gravity is pulling down and body blocked down
isGravityBlockedY : function ( )
{
var gy = this . _gy ;
2019-03-20 04:06:53 +00:00
return ( gy === 0 || ( gy < 0 && this . isBlockedUp ( ) ) || ( gy > 0 && this . isBlockedDown ( ) ) ) ;
2019-03-20 01:28:00 +00:00
} ,
2019-03-26 00:05:09 +00:00
// Check for sleeping state
2019-03-20 01:28:00 +00:00
checkSleep : function ( dx , dy )
{
2019-03-22 19:07:06 +00:00
if ( ! this . moves )
{
return ;
}
2019-03-20 01:28:00 +00:00
// Can't sleep if not blocked in the opposite direction somehow
dx = Math . abs ( dx ) ;
dy = Math . abs ( dy ) ;
2019-03-25 17:25:39 +00:00
if ( ! this . sleeping && this . isGravityBlockedX ( ) && this . isGravityBlockedY ( ) )
2019-03-20 01:28:00 +00:00
{
// Falling asleep?
2019-03-25 12:55:31 +00:00
if ( dx < 1 && dy < 1 && FuzzyEqual ( this . x , this . _sleepX , 0.01 ) && FuzzyEqual ( this . y , this . _sleepY , 0.01 ) )
2019-03-20 01:28:00 +00:00
{
if ( this . _sleep < this . sleepIterations )
{
this . _sleep ++ ;
2019-03-20 14:59:53 +00:00
2019-03-26 00:05:09 +00:00
// console.log(this.world._frame, 'sleeping ...', this._sleepY, this.y);
2019-03-20 14:59:53 +00:00
// console.log(this.gameObject.name, 'sleep y', this.y);
2019-03-20 01:28:00 +00:00
if ( this . _sleep >= this . sleepIterations )
{
2019-03-26 00:05:09 +00:00
// console.log(this.world._frame, 'checkSleep sending ...', dx, this._dx, this._sleepX, this.x, 'dy', dy, this._dy, this._sleepY, this.y);
2019-03-22 01:17:32 +00:00
this . sleep ( true ) ;
2019-03-22 19:07:06 +00:00
// console.log(this.world._frame, 'slept by checkSleep');
2019-03-22 02:29:48 +00:00
2019-03-26 15:29:58 +00:00
// var gameObject = this.gameObject;
2019-03-22 01:17:32 +00:00
2019-03-26 15:29:58 +00:00
// gameObject.x = this.x;
// gameObject.y = this.y;
2019-03-20 01:28:00 +00:00
}
}
}
}
2019-03-25 17:25:39 +00:00
else if ( this . sleeping && ( ! this . isGravityBlockedX ( ) || ! this . isGravityBlockedY ( ) ) )
2019-03-20 01:28:00 +00:00
{
// Waking up?
2019-03-25 17:25:39 +00:00
// console.log('waking???');
2019-03-20 01:28:00 +00:00
if ( this . _sleep > 0 )
{
// Do it progressively, not instantly, to ensure it isn't just a step fluctuation
this . _sleep -= ( this . sleepIterations * 0.1 ) ;
if ( this . _sleep <= 0 )
{
2019-03-26 15:29:58 +00:00
// console.log('body woken from postUpdate', dy);
2019-03-20 01:28:00 +00:00
this . wake ( ) ;
}
}
}
2019-03-22 15:01:47 +00:00
else if ( this . sleeping && ! this . velocity . equals ( this . prevVelocity ) )
{
2019-03-25 17:25:39 +00:00
// console.log('body woken from significant change in velocity =', this.velocity.x);
2019-03-22 15:01:47 +00:00
this . wake ( ) ;
}
2019-03-20 01:28:00 +00:00
} ,
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
*
2019-03-21 11:31:05 +00:00
* @ return { boolean } True if this Body is touching over intersecting with the world boundary .
2018-02-09 03:44:23 +00:00
* /
2017-11-08 17:18:41 +00:00
checkWorldBounds : function ( )
{
2019-03-21 23:21:02 +00:00
var velocity = this . velocity ;
2019-03-21 11:31:05 +00:00
var worldBounds = this . world . bounds ;
var worldCollision = this . world . checkCollision ;
2019-03-20 04:06:53 +00:00
2019-03-21 23:21:02 +00:00
if ( worldCollision . up && this . y <= ( worldBounds . y + 1 ) && velocity . y <= 0 )
2017-11-08 17:18:41 +00:00
{
2019-03-21 11:31:05 +00:00
this . setWorldBlockedUp ( true ) ;
2019-03-08 20:10:49 +00:00
}
2019-03-21 23:21:02 +00:00
else if ( worldCollision . down && this . bottom >= ( worldBounds . bottom - 1 ) && velocity . y >= 0 )
2019-03-08 20:10:49 +00:00
{
2019-03-21 23:21:02 +00:00
// console.log(this.world._frame, 'via check world bounds');
2019-03-21 11:31:05 +00:00
this . setWorldBlockedDown ( true ) ;
2017-11-08 17:18:41 +00:00
}
2019-03-25 12:55:31 +00:00
if ( worldCollision . left && this . x <= ( worldBounds . x + 1 ) && velocity . x <= 0 )
{
this . setWorldBlockedLeft ( true ) ;
}
else if ( worldCollision . right && this . right >= ( worldBounds . right - 1 ) && velocity . x >= 0 )
{
// console.log(this.world._frame, 'via check world bounds');
this . setWorldBlockedRight ( true ) ;
}
2017-11-08 17:18:41 +00:00
} ,
2019-03-11 17:28:43 +00:00
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 .
* /
2017-11-11 03:52:23 +00:00
setOffset : function ( x , y )
2017-11-08 17:18:41 +00:00
{
2017-11-11 03:52:23 +00:00
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 .
* /
2017-11-11 03:52:23 +00:00
setSize : function ( width , height , center )
{
if ( center === undefined ) { center = true ; }
2017-11-08 17:18:41 +00:00
2018-02-27 01:08:55 +00:00
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
2017-11-08 17:18:41 +00:00
this . sourceWidth = width ;
this . sourceHeight = height ;
2017-11-11 03:52:23 +00:00
2017-11-08 17:18:41 +00:00
this . width = this . sourceWidth * this . _sx ;
this . height = this . sourceHeight * this . _sy ;
2017-11-11 03:52:23 +00:00
2017-11-08 17:18:41 +00:00
this . halfWidth = Math . floor ( this . width / 2 ) ;
this . halfHeight = Math . floor ( this . height / 2 ) ;
this . updateCenter ( ) ;
2018-02-27 01:08:55 +00:00
if ( center && gameObject . getCenter )
2017-11-11 03:52:23 +00:00
{
var ox = gameObject . displayWidth / 2 ;
var oy = gameObject . displayHeight / 2 ;
this . offset . set ( ox - this . halfWidth , oy - this . halfHeight ) ;
}
2017-11-08 17:18:41 +00:00
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 .
* /
2017-11-08 17:18:41 +00:00
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
/ * *
2018-02-15 01:49:55 +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
*
2018-02-15 01:49:55 +00:00
* @ 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
* /
2017-11-08 17:18:41 +00:00
reset : function ( x , y )
{
this . stop ( ) ;
2018-02-15 01:49:55 +00:00
var gameObject = this . gameObject ;
2017-11-08 17:18:41 +00:00
2018-02-15 01:49:55 +00:00
gameObject . setPosition ( x , y ) ;
2017-11-08 17:18:41 +00:00
2018-02-15 01:49:55 +00:00
gameObject . getTopLeft ( this . position ) ;
2017-11-08 17:18:41 +00:00
2018-02-15 01:49:55 +00:00
this . prev . copy ( this . position ) ;
this . rotation = gameObject . angle ;
this . preRotation = gameObject . angle ;
2017-11-08 17:18:41 +00:00
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 .
* /
2017-11-08 17:18:41 +00:00
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
*
2019-02-13 14:45:36 +00:00
* @ param { Phaser . Physics . Arcade . Types . ArcadeBodyBounds } obj - An object to copy the values into .
2018-02-09 03:44:23 +00:00
*
2019-02-13 14:45:36 +00:00
* @ return { Phaser . Physics . Arcade . Types . ArcadeBodyBounds } - An object with { x , y , right , bottom } .
2018-02-09 03:44:23 +00:00
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +00:00
onWall : function ( )
{
return ( this . blocked . left || this . blocked . right ) ;
} ,
2018-02-09 03:44:23 +00:00
/ * *
2018-06-01 18:18:40 +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
* /
2017-11-08 17:18:41 +00:00
deltaAbsX : function ( )
{
2019-03-08 20:10:49 +00:00
return Math . abs ( this . _dx ) ;
2017-11-08 17:18:41 +00:00
} ,
2018-02-09 03:44:23 +00:00
/ * *
2018-06-01 18:18:40 +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
* /
2017-11-08 17:18:41 +00:00
deltaAbsY : function ( )
{
2019-03-08 20:10:49 +00:00
return Math . abs ( this . _dy ) ;
2017-11-08 17:18:41 +00:00
} ,
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 .
2018-06-01 18:18:40 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
deltaX : function ( )
{
2018-09-04 11:16:24 +00:00
return this . _dx ;
2017-11-08 17:18:41 +00:00
} ,
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 .
2018-06-01 18:18:40 +00:00
* 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
* /
2017-11-08 17:18:41 +00:00
deltaY : function ( )
{
2018-09-04 11:16:24 +00:00
return this . _dy ;
2017-11-08 17:18:41 +00:00
} ,
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
* /
2017-11-08 17:18:41 +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
* /
2017-11-08 17:18:41 +00:00
destroy : function ( )
{
2018-02-15 01:49:55 +00:00
this . enable = false ;
2018-02-14 19:36:34 +00:00
2018-12-06 14:47:06 +00:00
if ( this . world )
{
this . world . pendingDestroy . set ( this ) ;
}
2017-11-08 17:18:41 +00:00
} ,
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
* /
2017-11-08 17:18:41 +00:00
drawDebug : function ( graphic )
{
var pos = this . position ;
2018-09-26 16:15:22 +00:00
2017-11-11 03:52:23 +00:00
var x = pos . x + this . halfWidth ;
var y = pos . y + this . halfHeight ;
2017-11-08 17:18:41 +00:00
2019-03-20 00:08:23 +00:00
var worldBlockedColor = this . world . defaults . worldBlockedDebugColor ;
2019-03-15 12:53:59 +00:00
var blockedColor = this . world . defaults . blockedDebugColor ;
var sleepColor = this . world . defaults . sleepDebugColor ;
2017-11-11 03:52:23 +00:00
2019-03-15 19:22:32 +00:00
// var thickness = graphic.defaultStrokeWidth;
var thickness = 2 ;
2019-03-15 12:53:59 +00:00
var halfThickness = thickness / 2 ;
2017-11-08 17:18:41 +00:00
2019-03-15 12:53:59 +00:00
// Top Left
var x1 = pos . x ;
var y1 = pos . y ;
// Top Right
var x2 = this . right ;
var y2 = y1 ;
2019-03-11 11:05:42 +00:00
2019-03-15 12:53:59 +00:00
// Bottom Left
var x3 = x1 ;
var y3 = this . bottom ;
2019-03-11 11:05:42 +00:00
2019-03-15 12:53:59 +00:00
// Bottom Right
var x4 = x2 ;
var y4 = y3 ;
2019-03-11 11:05:42 +00:00
2019-03-19 23:21:48 +00:00
var blocked = this . blocked ;
var worldBlocked = this . worldBlocked ;
2019-03-22 01:17:32 +00:00
var hardBlocked = this . hardBlocked ;
2019-03-11 11:05:42 +00:00
2019-03-15 12:53:59 +00:00
var color ;
2019-03-11 11:05:42 +00:00
2019-03-15 12:53:59 +00:00
if ( this . debugShowBody )
{
// Top
color = ( this . sleeping ) ? sleepColor : this . debugBodyColor ;
2019-03-11 11:05:42 +00:00
2019-03-22 01:17:32 +00:00
if ( blocked . up || worldBlocked . up || hardBlocked . up )
2019-03-11 11:05:42 +00:00
{
2019-03-22 01:17:32 +00:00
color = ( worldBlocked . up || hardBlocked . up ) ? worldBlockedColor : blockedColor ;
2019-03-11 11:05:42 +00:00
}
2019-03-15 12:53:59 +00:00
graphic . lineStyle ( thickness , color ) . lineBetween ( x1 , y1 + halfThickness , x2 , y2 + halfThickness ) ;
// Bottom
color = ( this . sleeping ) ? sleepColor : this . debugBodyColor ;
2019-03-22 01:17:32 +00:00
if ( blocked . down || worldBlocked . down || hardBlocked . down )
2019-03-11 11:05:42 +00:00
{
2019-03-22 01:17:32 +00:00
color = ( worldBlocked . down || hardBlocked . down ) ? worldBlockedColor : blockedColor ;
2019-03-11 11:05:42 +00:00
}
2019-03-11 17:28:43 +00:00
2019-03-15 12:53:59 +00:00
graphic . lineStyle ( thickness , color ) . lineBetween ( x3 , y3 - halfThickness , x4 , y4 - halfThickness ) ;
// Left
color = ( this . sleeping ) ? sleepColor : this . debugBodyColor ;
2019-03-19 23:21:48 +00:00
if ( blocked . left || worldBlocked . left )
2019-03-11 17:28:43 +00:00
{
2019-03-19 23:21:48 +00:00
color = ( worldBlocked . left ) ? worldBlockedColor : blockedColor ;
2019-03-11 17:28:43 +00:00
}
2019-03-15 12:53:59 +00:00
graphic . lineStyle ( thickness , color ) . lineBetween ( x1 + halfThickness , y1 , x3 + halfThickness , y3 ) ;
// Right
color = ( this . sleeping ) ? sleepColor : this . debugBodyColor ;
2019-03-19 23:21:48 +00:00
if ( blocked . right || worldBlocked . right )
2019-03-11 17:28:43 +00:00
{
2019-03-19 23:21:48 +00:00
color = ( worldBlocked . right ) ? worldBlockedColor : blockedColor ;
2019-03-11 17:28:43 +00:00
}
2019-03-15 12:53:59 +00:00
graphic . lineStyle ( thickness , color ) . lineBetween ( x2 - halfThickness , y2 , x4 - halfThickness , y4 ) ;
2019-03-11 11:05:42 +00:00
}
2019-03-15 12:53:59 +00:00
// if (this.isCircle)
// {
// graphic.strokeCircle(x, y, this.width / 2);
// }
2017-11-08 17:18:41 +00:00
if ( this . debugShowVelocity )
{
2018-09-26 16:50:48 +00:00
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 ) ;
2017-11-08 17:18:41 +00:00
}
} ,
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
* /
2017-11-08 17:18:41 +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
*
2018-05-22 15:19:18 +00:00
* @ 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 .
* /
2017-11-09 04:02:59 +00:00
setCollideWorldBounds : function ( value )
{
2018-05-22 15:19:18 +00:00
if ( value === undefined ) { value = true ; }
2017-11-09 04:02:59 +00:00
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 .
2018-06-01 18:18:40 +00:00
* @ 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 .
* /
2017-11-09 04:02:59 +00:00
setVelocity : function ( x , y )
{
this . velocity . set ( x , y ) ;
2019-03-22 13:16:37 +00:00
x = this . velocity . x ;
y = this . velocity . y ;
2018-08-08 16:31:22 +00:00
this . speed = Math . sqrt ( x * x + y * y ) ;
2019-03-22 14:48:00 +00:00
if ( this . speed > 0 )
{
this . wake ( ) ;
}
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setVelocityX : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setVelocity ( value , this . velocity . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setVelocityY : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setVelocity ( this . velocity . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
2018-06-01 18:18:40 +00:00
/ * *
* 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 ;
} ,
2019-01-24 00:26:29 +00:00
/ * *
* Sets the maximum speed the Body can move .
*
* @ method Phaser . Physics . Arcade . Body # setMaxSpeed
* @ since 3.16 . 0
*
* @ param { number } value - The maximum speed value , in pixels per second . Set to a negative value to disable .
*
* @ return { Phaser . Physics . Arcade . Body } This Body object .
* /
setMaxSpeed : function ( value )
{
this . maxSpeed = value ;
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setBounceX : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setBounce ( value , this . bounce . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setBounceY : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setBounce ( this . bounce . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
2019-03-25 12:55:31 +00:00
setRiding : function ( body2 , face )
{
this . snapTo = body2 ;
this . forcePosition = 10 ;
if ( face === CONST . FACING _UP )
{
this . y = body2 . bottom ;
}
else if ( face === CONST . FACING _DOWN )
{
this . bottom = body2 . y ;
}
return this ;
} ,
2019-03-08 20:10:49 +00:00
setTouchingUp : function ( )
{
var touching = this . touching ;
touching . up = true ;
touching . none = false ;
return this ;
} ,
setTouchingDown : function ( )
{
var touching = this . touching ;
touching . down = true ;
touching . none = false ;
return this ;
} ,
setTouchingLeft : function ( )
{
var touching = this . touching ;
touching . left = true ;
touching . none = false ;
return this ;
} ,
setTouchingRight : function ( )
{
var touching = this . touching ;
touching . right = true ;
touching . none = false ;
return this ;
} ,
2019-03-22 01:17:32 +00:00
setHardBlockedUp : function ( )
{
var hardBlocked = this . hardBlocked ;
hardBlocked . none = false ;
hardBlocked . up = true ;
return this ;
} ,
setHardBlockedDown : function ( )
{
var hardBlocked = this . hardBlocked ;
hardBlocked . none = false ;
hardBlocked . down = true ;
return this ;
} ,
2019-03-25 12:55:31 +00:00
setHardBlockedLeft : function ( )
{
var hardBlocked = this . hardBlocked ;
hardBlocked . none = false ;
hardBlocked . left = true ;
return this ;
} ,
setHardBlockedRight : function ( )
{
var hardBlocked = this . hardBlocked ;
hardBlocked . none = false ;
hardBlocked . right = true ;
return this ;
} ,
2019-03-21 17:24:34 +00:00
setBlockedUp : function ( collisionInfo , body2 )
2019-03-08 20:10:49 +00:00
{
var blocked = this . blocked ;
blocked . up = true ;
blocked . none = false ;
2019-03-21 17:24:34 +00:00
if ( collisionInfo )
2019-03-20 00:02:16 +00:00
{
2019-03-21 17:24:34 +00:00
if ( ! body2 )
{
2019-03-22 01:17:32 +00:00
ArrayAdd ( this . blockers . up , collisionInfo ) ;
}
2019-03-24 23:30:38 +00:00
else if ( body2 . rideable )
{
return this . setRiding ( body2 , CONST . FACING _UP ) ;
}
2019-03-22 12:18:34 +00:00
else if ( body2 . isWorldBlockedUp ( ) )
2019-03-22 01:17:32 +00:00
{
2019-03-22 12:18:34 +00:00
this . setHardBlockedUp ( ) ;
this . forcePosition = 6 ;
this . y = body2 . bottom ;
2019-03-21 17:24:34 +00:00
}
2019-03-21 11:31:05 +00:00
2019-03-21 17:24:34 +00:00
// We don't reposition this body if it's already blocked on a face
2019-03-22 12:18:34 +00:00
if ( this . forcePosition === 5 || this . isWorldBlockedUp ( ) || this . isWorldBlockedDown ( ) )
2019-03-21 17:24:34 +00:00
{
return this ;
}
2019-03-21 11:31:05 +00:00
2019-03-22 12:50:28 +00:00
if ( body2 && ! collisionInfo . set && ! this . snapTo )
2019-03-21 17:24:34 +00:00
{
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'setBlockedUp', body2.bottom);
2019-03-21 17:24:34 +00:00
2019-03-22 11:34:23 +00:00
this . snapTo = body2 ;
2019-03-21 17:24:34 +00:00
this . y = body2 . bottom ;
2019-03-22 11:34:23 +00:00
2019-03-21 17:24:34 +00:00
this . forcePosition = 1 ;
2019-03-22 12:18:34 +00:00
collisionInfo . set = true ;
2019-03-21 17:24:34 +00:00
}
2019-03-20 00:02:16 +00:00
}
2019-03-08 20:10:49 +00:00
return this ;
} ,
2019-03-21 17:24:34 +00:00
setBlockedDown : function ( collisionInfo , body2 )
2019-03-08 20:10:49 +00:00
{
var blocked = this . blocked ;
blocked . down = true ;
blocked . none = false ;
2019-03-21 17:24:34 +00:00
if ( collisionInfo )
{
if ( ! body2 )
{
2019-03-22 01:17:32 +00:00
ArrayAdd ( this . blockers . down , collisionInfo ) ;
}
2019-03-22 19:07:06 +00:00
else if ( body2 . rideable )
{
2019-03-24 23:30:38 +00:00
return this . setRiding ( body2 , CONST . FACING _DOWN ) ;
2019-03-22 19:07:06 +00:00
}
2019-03-22 12:18:34 +00:00
else if ( body2 . isWorldBlockedDown ( ) )
2019-03-22 01:17:32 +00:00
{
2019-03-22 12:18:34 +00:00
this . setHardBlockedDown ( ) ;
this . forcePosition = 7 ;
this . bottom = body2 . y ;
2019-03-21 17:24:34 +00:00
}
2019-03-20 17:14:44 +00:00
2019-03-21 17:24:34 +00:00
// We don't reposition this body if it's already blocked on a face
2019-03-22 12:18:34 +00:00
if ( this . forcePosition === 5 || this . isWorldBlockedUp ( ) || this . isWorldBlockedDown ( ) )
2019-03-21 17:24:34 +00:00
{
return this ;
}
2019-03-20 17:14:44 +00:00
2019-03-22 12:50:28 +00:00
if ( body2 && ! collisionInfo . set && ! this . snapTo )
2019-03-21 17:24:34 +00:00
{
2019-03-22 19:07:06 +00:00
// console.log(this.gameObject.name, 'setBlockedDown', body2.y);
2019-03-21 11:31:05 +00:00
2019-03-22 11:34:23 +00:00
this . snapTo = body2 ;
2019-03-21 17:24:34 +00:00
this . bottom = body2 . y ;
2019-03-21 11:31:05 +00:00
2019-03-22 11:34:23 +00:00
this . forcePosition = 2 ;
2019-03-22 12:18:34 +00:00
collisionInfo . set = true ;
2019-03-21 17:24:34 +00:00
}
2019-03-20 00:02:16 +00:00
}
2019-03-08 20:10:49 +00:00
return this ;
} ,
2019-03-25 12:55:31 +00:00
setBlockedLeft : function ( collisionInfo , body2 )
{
var blocked = this . blocked ;
blocked . left = true ;
blocked . none = false ;
if ( collisionInfo )
{
if ( ! body2 )
{
ArrayAdd ( this . blockers . left , collisionInfo ) ;
}
else if ( body2 . rideable )
{
return this . setRiding ( body2 , CONST . FACING _LEFT ) ;
}
else if ( body2 . isWorldBlockedLeft ( ) )
{
this . setHardBlockedLeft ( ) ;
this . forcePosition = 8 ;
this . x = body2 . right ;
}
// We don't reposition this body if it's already blocked on a face
if ( this . forcePosition === 5 || this . isWorldBlockedLeft ( ) || this . isWorldBlockedRight ( ) )
{
return this ;
}
if ( body2 && ! collisionInfo . set && ! this . snapTo )
{
// console.log(this.gameObject.name, 'setBlockedUp', body2.bottom);
this . snapTo = body2 ;
this . x = body2 . right ;
this . forcePosition = 3 ;
collisionInfo . set = true ;
}
}
return this ;
} ,
setBlockedRight : function ( collisionInfo , body2 )
{
var blocked = this . blocked ;
blocked . right = true ;
blocked . none = false ;
if ( collisionInfo )
{
if ( ! body2 )
{
ArrayAdd ( this . blockers . right , collisionInfo ) ;
}
else if ( body2 . rideable )
{
return this . setRiding ( body2 , CONST . FACING _RIGHT ) ;
}
else if ( body2 . isWorldBlockedRight ( ) )
{
this . setHardBlockedRight ( ) ;
this . forcePosition = 9 ;
this . right = body2 . x ;
}
// We don't reposition this body if it's already blocked on a face
if ( this . forcePosition === 5 || this . isWorldBlockedLeft ( ) || this . isWorldBlockedRight ( ) )
{
return this ;
}
if ( body2 && ! collisionInfo . set && ! this . snapTo )
{
// console.log(this.gameObject.name, 'setBlockedDown', body2.y);
this . snapTo = body2 ;
this . right = body2 . x ;
this . forcePosition = 4 ;
collisionInfo . set = true ;
}
}
return this ;
} ,
2019-03-15 19:22:32 +00:00
setWorldBlockedUp : function ( forceY )
{
2019-03-21 11:31:05 +00:00
var worldBounds = this . world . bounds ;
2019-03-15 19:22:32 +00:00
var worldBlocked = this . worldBlocked ;
2019-03-21 11:31:05 +00:00
var worldCollision = this . world . checkCollision ;
if ( ! worldCollision . up )
{
return ;
}
2019-03-15 19:22:32 +00:00
worldBlocked . up = true ;
worldBlocked . none = false ;
2019-03-21 11:31:05 +00:00
if ( forceY && this . y !== worldBounds . y )
2019-03-15 19:22:32 +00:00
{
2019-03-21 11:31:05 +00:00
this . y = worldBounds . y ;
2019-03-22 11:34:23 +00:00
this . forcePosition = 5 ;
2019-03-15 19:22:32 +00:00
}
return this ;
} ,
setWorldBlockedDown : function ( forceY )
{
2019-03-21 11:31:05 +00:00
var worldBounds = this . world . bounds ;
2019-03-15 19:22:32 +00:00
var worldBlocked = this . worldBlocked ;
2019-03-21 11:31:05 +00:00
var worldCollision = this . world . checkCollision ;
if ( ! worldCollision . down )
{
return ;
}
2019-03-15 19:22:32 +00:00
worldBlocked . down = true ;
worldBlocked . none = false ;
2019-03-21 11:31:05 +00:00
if ( forceY && this . bottom !== worldBounds . bottom )
2019-03-15 19:22:32 +00:00
{
2019-03-21 11:31:05 +00:00
this . bottom = worldBounds . bottom ;
2019-03-21 23:21:02 +00:00
2019-03-22 11:34:23 +00:00
this . forcePosition = 5 ;
2019-03-15 19:22:32 +00:00
}
return this ;
} ,
2019-03-25 12:55:31 +00:00
setWorldBlockedLeft : function ( forceX )
{
var worldBounds = this . world . bounds ;
var worldBlocked = this . worldBlocked ;
var worldCollision = this . world . checkCollision ;
if ( ! worldCollision . left )
{
return ;
}
worldBlocked . left = true ;
worldBlocked . none = false ;
if ( forceX && this . x !== worldBounds . x )
{
this . x = worldBounds . x ;
this . forcePosition = 5 ;
}
return this ;
} ,
setWorldBlockedRight : function ( forceX )
{
var worldBounds = this . world . bounds ;
var worldBlocked = this . worldBlocked ;
var worldCollision = this . world . checkCollision ;
if ( ! worldCollision . right )
{
return ;
}
worldBlocked . right = true ;
worldBlocked . none = false ;
if ( forceX && this . right !== worldBounds . right )
{
this . right = worldBounds . right ;
this . forcePosition = 5 ;
}
return this ;
} ,
2019-03-20 00:02:16 +00:00
isBlocked : function ( )
{
2019-03-22 01:17:32 +00:00
return ( ! this . blocked . none || ! this . worldBlocked . none || ! this . hardBlocked . none ) ;
2019-03-20 00:02:16 +00:00
} ,
2019-03-19 23:21:48 +00:00
isBlockedUp : function ( )
{
2019-03-22 19:07:06 +00:00
return ( this . immovable || this . blocked . up || this . worldBlocked . up || this . hardBlocked . up ) ;
2019-03-19 23:21:48 +00:00
} ,
isBlockedDown : function ( )
{
2019-03-22 19:07:06 +00:00
return ( this . immovable || this . blocked . down || this . worldBlocked . down || this . hardBlocked . down ) ;
2019-03-19 23:21:48 +00:00
} ,
2019-03-25 12:55:31 +00:00
isBlockedLeft : function ( )
2019-03-20 04:06:53 +00:00
{
2019-03-25 12:55:31 +00:00
return ( this . immovable || this . blocked . left || this . worldBlocked . left || this . hardBlocked . left ) ;
} ,
isBlockedRight : function ( )
{
return ( this . immovable || this . blocked . right || this . worldBlocked . right || this . hardBlocked . right ) ;
2019-03-20 04:06:53 +00:00
} ,
isWorldBlockedUp : function ( )
{
2019-03-22 19:07:06 +00:00
return ( this . immovable || this . worldBlocked . up || this . hardBlocked . up ) ;
2019-03-20 04:06:53 +00:00
} ,
2019-03-25 12:55:31 +00:00
isWorldBlockedDown : function ( )
{
return ( this . immovable || this . worldBlocked . down || this . hardBlocked . down ) ;
} ,
isWorldBlockedLeft : function ( )
{
return ( this . immovable || this . worldBlocked . left || this . hardBlocked . left ) ;
} ,
isWorldBlockedRight : function ( )
{
return ( this . immovable || this . worldBlocked . right || this . hardBlocked . right ) ;
} ,
2019-03-20 01:28:00 +00:00
// Is this body world blocked AND blocked on the opposite face?
2019-03-25 12:55:31 +00:00
isBlockedX : function ( )
{
var blocked = this . blocked ;
var worldBlocked = this . worldBlocked ;
var hardBlocked = this . hardBlocked ;
return (
( ( worldBlocked . right || hardBlocked . right ) && blocked . left ) ||
( ( worldBlocked . left || hardBlocked . left ) && blocked . right )
) ;
} ,
2019-03-19 23:21:48 +00:00
isBlockedY : function ( )
2019-03-08 20:10:49 +00:00
{
var blocked = this . blocked ;
2019-03-15 19:22:32 +00:00
var worldBlocked = this . worldBlocked ;
2019-03-22 01:17:32 +00:00
var hardBlocked = this . hardBlocked ;
2019-03-08 20:10:49 +00:00
2019-03-19 23:21:48 +00:00
return (
2019-03-22 01:17:32 +00:00
( ( worldBlocked . down || hardBlocked . down ) && blocked . up ) ||
( ( worldBlocked . up || hardBlocked . up ) && blocked . down )
2019-03-19 23:21:48 +00:00
) ;
} ,
2019-03-26 13:07:57 +00:00
getShareX : function ( amount )
2019-03-22 19:07:06 +00:00
{
2019-03-25 12:55:31 +00:00
var diff = amount ;
var bounds = this . world . bounds ;
if ( amount === 0 )
{
return diff ;
}
else
{
if ( this . collideWorldBounds )
{
var worldCollision = this . world . checkCollision ;
if ( amount < 0 && worldCollision . left && this . x + amount < bounds . x )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . x + amount ) - bounds . x ) ;
2019-03-25 12:55:31 +00:00
}
else if ( amount > 0 && worldCollision . right && this . right + amount > bounds . right )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . right + amount ) - bounds . right ) ;
2019-03-25 12:55:31 +00:00
}
}
if ( amount < 0 && this . isBlockedLeft ( ) )
{
bounds = this . getBlocker ( this . blockers . left ) ;
if ( bounds && this . x + amount < bounds . x )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . x + amount ) - bounds . x ) ;
2019-03-25 12:55:31 +00:00
}
}
else if ( amount > 0 && this . isBlockedRight ( ) )
{
bounds = this . getBlocker ( this . blockers . right ) ;
if ( bounds && this . right + amount > bounds . right )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . right + amount ) - bounds . right ) ;
2019-03-25 12:55:31 +00:00
}
}
}
return diff ;
2019-03-22 19:07:06 +00:00
} ,
2019-03-26 13:07:57 +00:00
getShareY : function ( amount )
2019-03-19 23:21:48 +00:00
{
2019-03-22 02:29:48 +00:00
var diff = amount ;
var bounds = this . world . bounds ;
if ( amount === 0 )
2019-03-08 20:10:49 +00:00
{
2019-03-22 02:29:48 +00:00
return diff ;
2019-03-08 20:10:49 +00:00
}
2019-03-22 02:39:10 +00:00
else
2019-03-22 02:29:48 +00:00
{
2019-03-22 02:39:10 +00:00
if ( this . collideWorldBounds )
2019-03-22 02:29:48 +00:00
{
2019-03-22 02:39:10 +00:00
var worldCollision = this . world . checkCollision ;
if ( amount < 0 && worldCollision . up && this . y + amount < bounds . y )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . y + amount ) - bounds . y ) ;
2019-03-22 02:39:10 +00:00
}
else if ( amount > 0 && worldCollision . down && this . bottom + amount > bounds . bottom )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . bottom + amount ) - bounds . bottom ) ;
2019-03-22 02:39:10 +00:00
}
2019-03-22 02:29:48 +00:00
}
2019-03-08 20:10:49 +00:00
2019-03-22 02:39:10 +00:00
if ( amount < 0 && this . isBlockedUp ( ) )
2019-03-08 20:10:49 +00:00
{
2019-03-22 02:39:10 +00:00
bounds = this . getBlocker ( this . blockers . up ) ;
2019-03-21 23:21:02 +00:00
2019-03-22 02:39:10 +00:00
if ( bounds && this . y + amount < bounds . y )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . y + amount ) - bounds . y ) ;
2019-03-22 02:39:10 +00:00
}
2019-03-08 20:10:49 +00:00
}
2019-03-22 02:39:10 +00:00
else if ( amount > 0 && this . isBlockedDown ( ) )
2019-03-08 20:10:49 +00:00
{
2019-03-22 02:39:10 +00:00
bounds = this . getBlocker ( this . blockers . down ) ;
if ( bounds && this . bottom + amount > bounds . bottom )
{
2019-03-26 13:07:57 +00:00
return amount - ( ( this . bottom + amount ) - bounds . bottom ) ;
2019-03-22 02:39:10 +00:00
}
2019-03-08 20:10:49 +00:00
}
}
2019-03-26 13:07:57 +00:00
return diff ;
} ,
moveX : function ( amount )
{
var diff = amount ;
var bounds = this . world . bounds ;
if ( this . collideWorldBounds )
{
var worldCollision = this . world . checkCollision ;
if ( amount < 0 && worldCollision . left && this . x + amount < bounds . x )
{
diff = amount - ( ( this . x + amount ) - bounds . x ) ;
if ( diff !== 0 )
{
this . wake ( ) ;
}
this . setWorldBlockedLeft ( true ) ;
this . x += diff ;
return diff ;
}
else if ( amount > 0 && worldCollision . right && this . right + amount > bounds . right )
{
diff = amount - ( ( this . right + amount ) - bounds . right ) ;
if ( diff !== 0 )
{
this . wake ( ) ;
}
this . setWorldBlockedRight ( true ) ;
this . x += diff ;
return diff ;
}
}
if ( amount < 0 && this . isBlockedLeft ( ) )
{
bounds = this . getBlocker ( this . blockers . left ) ;
if ( bounds && this . x + amount < bounds . x )
{
diff = amount - ( ( this . x + amount ) - bounds . x ) ;
}
}
else if ( amount > 0 && this . isBlockedRight ( ) )
{
bounds = this . getBlocker ( this . blockers . right ) ;
if ( bounds && this . right + amount > bounds . right )
{
diff = amount - ( ( this . right + amount ) - bounds . right ) ;
}
}
if ( diff !== 0 )
{
this . wake ( ) ;
this . x += diff ;
}
} ,
moveY : function ( amount )
{
var diff = amount ;
var bounds = this . world . bounds ;
if ( this . collideWorldBounds )
{
var worldCollision = this . world . checkCollision ;
if ( amount < 0 && worldCollision . up && this . y + amount < bounds . y )
{
diff = amount - ( ( this . y + amount ) - bounds . y ) ;
if ( diff !== 0 )
{
this . wake ( ) ;
}
this . setWorldBlockedUp ( true ) ;
this . y += diff ;
return diff ;
}
else if ( amount > 0 && worldCollision . down && this . bottom + amount > bounds . bottom )
{
diff = amount - ( ( this . bottom + amount ) - bounds . bottom ) ;
if ( diff !== 0 )
{
this . wake ( ) ;
}
this . setWorldBlockedDown ( true ) ;
this . y += diff ;
return diff ;
}
}
if ( amount < 0 && this . isBlockedUp ( ) )
{
bounds = this . getBlocker ( this . blockers . up ) ;
if ( bounds && this . y + amount < bounds . y )
{
diff = amount - ( ( this . y + amount ) - bounds . y ) ;
}
}
else if ( amount > 0 && this . isBlockedDown ( ) )
{
bounds = this . getBlocker ( this . blockers . down ) ;
if ( bounds && this . bottom + amount > bounds . bottom )
{
diff = amount - ( ( this . bottom + amount ) - bounds . bottom ) ;
}
}
if ( diff !== 0 )
{
this . wake ( ) ;
this . y += diff ;
}
2019-03-22 14:48:00 +00:00
2019-03-22 02:29:48 +00:00
return diff ;
2019-03-08 20:10:49 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setAccelerationX : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setAcceleration ( value , this . acceleration . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setAccelerationY : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setAcceleration ( this . acceleration . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
2018-05-15 14:59:02 +00:00
/ * *
* Enables or disables drag .
*
* @ method Phaser . Physics . Arcade . Body # setAllowDrag
* @ since 3.9 . 0
* @ see Phaser . Physics . Arcade . Body # allowDrag
*
2018-05-22 15:19:18 +00:00
* @ param { boolean } [ value = true ] - ` true ` to allow drag on this body , or ` false ` to disable it .
2018-05-15 14:59:02 +00:00
*
* @ return { Phaser . Physics . Arcade . Body } This Body object .
* /
2018-05-22 15:19:18 +00:00
setAllowDrag : function ( value )
2018-05-15 14:59:02 +00:00
{
2018-05-22 15:19:18 +00:00
if ( value === undefined ) { value = true ; }
this . allowDrag = value ;
2018-05-15 14:59:02 +00:00
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
*
2018-05-22 15:19:18 +00:00
* @ param { boolean } [ value = true ] - ` true ` to allow gravity on this body , or ` false ` to disable it .
2018-05-15 14:59:02 +00:00
*
* @ return { Phaser . Physics . Arcade . Body } This Body object .
* /
2018-05-22 15:19:18 +00:00
setAllowGravity : function ( value )
2018-05-15 14:59:02 +00:00
{
2018-05-22 15:19:18 +00:00
if ( value === undefined ) { value = true ; }
this . allowGravity = value ;
2018-05-15 14:59:02 +00:00
return this ;
} ,
/ * *
* Enables or disables rotation .
*
* @ method Phaser . Physics . Arcade . Body # setAllowRotation
* @ since 3.9 . 0
* @ see Phaser . Physics . Arcade . Body # allowRotation
*
2018-05-22 15:19:18 +00:00
* @ param { boolean } [ value = true ] - ` true ` to allow rotation on this body , or ` false ` to disable it .
2018-05-15 14:59:02 +00:00
*
* @ return { Phaser . Physics . Arcade . Body } This Body object .
* /
2018-05-22 15:19:18 +00:00
setAllowRotation : function ( value )
2018-05-15 14:59:02 +00:00
{
2018-05-22 15:19:18 +00:00
if ( value === undefined ) { value = true ; }
this . allowRotation = value ;
2018-05-15 14:59:02 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setDragX : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setDrag ( value , this . drag . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setDragY : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setDrag ( this . drag . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setGravityX : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setGravity ( value , this . gravity . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setGravityY : function ( value )
{
2019-03-22 19:07:06 +00:00
return this . setGravity ( this . gravity . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
setFrictionX : function ( value )
{
2019-03-22 19:07:06 +00:00
this . setFriction ( value , this . friction . y ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
setFrictionY : function ( value )
{
2019-03-22 19:07:06 +00:00
this . setFriction ( this . friction . x , value ) ;
2017-11-09 04:02:59 +00:00
} ,
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
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 .
* /
2017-11-09 04:02:59 +00:00
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
*
2018-05-22 15:19:18 +00:00
* @ 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 .
* /
2017-11-09 04:02:59 +00:00
setImmovable : function ( value )
{
2018-05-22 15:19:18 +00:00
if ( value === undefined ) { value = true ; }
2017-11-09 04:02:59 +00:00
this . immovable = value ;
return this ;
} ,
2019-03-22 19:07:06 +00:00
setDirectControl : function ( )
{
this . immovable = true ;
this . moves = false ;
return this ;
} ,
setMovingPlatform : function ( )
{
this . immovable = true ;
this . rideable = true ;
this . moves = false ;
return this ;
} ,
2018-09-26 17:25:45 +00:00
/ * *
* Sets the Body ' s ` enable ` property .
*
* @ method Phaser . Physics . Arcade . Body # setEnable
2018-10-12 17:32:52 +00:00
* @ since 3.15 . 0
2018-09-26 17:25:45 +00:00
*
* @ 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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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
* /
2017-11-08 17:18:41 +00:00
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 }
* @ since 3.0 . 0
* /
2017-11-08 17:18:41 +00:00
right : {
get : function ( )
{
return this . position . x + this . width ;
2019-03-11 09:19:41 +00:00
} ,
set : function ( value )
{
this . position . x = value - this . width ;
2017-11-08 17:18:41 +00:00
}
} ,
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
* /
2017-11-08 17:18:41 +00:00
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 }
* @ since 3.0 . 0
* /
2017-11-08 17:18:41 +00:00
bottom : {
get : function ( )
{
return this . position . y + this . height ;
2019-03-11 09:19:41 +00:00
} ,
set : function ( value )
{
this . position . y = value - this . height ;
2017-11-08 17:18:41 +00:00
}
}
} ) ;
module . exports = Body ;