mirror of
https://github.com/photonstorm/phaser
synced 2024-11-30 16:39:34 +00:00
2406 lines
90 KiB
HTML
2406 lines
90 KiB
HTML
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Phaser Source: physics/arcade/World.js</title>
|
|
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">
|
|
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/site.cerulean.css">
|
|
|
|
</head>
|
|
|
|
<body>
|
|
<div class="container-fluid">
|
|
<div class="navbar navbar-fixed-top navbar-inverse">
|
|
<div class="navbar-inner">
|
|
<a class="brand" href="index.html">Phaser</a>
|
|
<ul class="nav">
|
|
|
|
<li class="dropdown">
|
|
<a href="namespaces.list.html" class="dropdown-toggle" data-toggle="dropdown">Namespaces<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li class="class-depth-0">
|
|
<a href="Phaser.html">Phaser</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
<li class="dropdown">
|
|
<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b
|
|
class="caret"></b></a>
|
|
|
|
<ul class="dropdown-menu ">
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Animation.html">Animation</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.AnimationManager.html">AnimationManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.AnimationParser.html">AnimationParser</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.ArrayList.html">ArrayList</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.BitmapData.html">BitmapData</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.BitmapText.html">BitmapText</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Button.html">Button</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Cache.html">Cache</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Camera.html">Camera</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Canvas.html">Canvas</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Circle.html">Circle</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Color.html">Color</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Device.html">Device</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Easing.html">Easing</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Back.html">Back</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Bounce.html">Bounce</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Circular.html">Circular</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Cubic.html">Cubic</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Elastic.html">Elastic</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Exponential.html">Exponential</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Linear.html">Linear</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Quadratic.html">Quadratic</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Quartic.html">Quartic</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Quintic.html">Quintic</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Ellipse.html">Ellipse</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Events.html">Events</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Filter.html">Filter</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.FlexGrid.html">FlexGrid</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.FlexLayer.html">FlexLayer</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Frame.html">Frame</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.FrameData.html">FrameData</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Game.html">Game</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Gamepad.html">Gamepad</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.GamepadButton.html">GamepadButton</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Graphics.html">Graphics</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Group.html">Group</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Image.html">Image</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Input.html">Input</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.InputHandler.html">InputHandler</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Key.html">Key</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Keyboard.html">Keyboard</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Line.html">Line</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.LinkedList.html">LinkedList</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Loader.html">Loader</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.LoaderParser.html">LoaderParser</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Math.html">Math</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Mouse.html">Mouse</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.MSPointer.html">MSPointer</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Net.html">Net</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Particle.html">Particle</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Particles.html">Particles</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Physics.html">Physics</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Physics.Arcade.html">Arcade</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.Arcade.Body.html">Body</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Physics.Ninja.html">Ninja</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.Ninja.Body.html">Body</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Physics.P2.html">P2</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.Body.html">Body</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.DistanceConstraint.html">DistanceConstraint</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.FixtureList.html">FixtureList</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.Material.html">Material</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.RotationalSpring.html">RotationalSpring</a>
|
|
</li>
|
|
|
|
<li class="class-depth-3">
|
|
<a href="Phaser.Physics.P2.Spring.html">Spring</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Plugin.html">Plugin</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.PluginManager.html">PluginManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Point.html">Point</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Pointer.html">Pointer</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Polygon.html">Polygon</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.QuadTree.html">QuadTree</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Rectangle.html">Rectangle</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.RenderTexture.html">RenderTexture</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.RetroFont.html">RetroFont</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Rope.html">Rope</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.ScaleManager.html">ScaleManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Signal.html">Signal</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.SignalBinding.html">SignalBinding</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.SinglePad.html">SinglePad</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Sound.html">Sound</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.SoundManager.html">SoundManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Sprite.html">Sprite</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.SpriteBatch.html">SpriteBatch</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Stage.html">Stage</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.State.html">State</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.StateManager.html">StateManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Text.html">Text</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Tile.html">Tile</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Tilemap.html">Tilemap</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.TilemapLayer.html">TilemapLayer</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.TilemapParser.html">TilemapParser</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Tileset.html">Tileset</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.TileSprite.html">TileSprite</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Time.html">Time</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Timer.html">Timer</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.TimerEvent.html">TimerEvent</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Touch.html">Touch</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Tween.html">Tween</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.TweenManager.html">TweenManager</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.Utils.html">Utils</a>
|
|
</li>
|
|
|
|
<li class="class-depth-2">
|
|
<a href="Phaser.Utils.Debug.html">Debug</a>
|
|
</li>
|
|
|
|
<li class="class-depth-1">
|
|
<a href="Phaser.World.html">World</a>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</li>
|
|
|
|
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="row-fluid">
|
|
|
|
|
|
<div class="span12">
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: physics/arcade/World.js</h1>
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="sunlight-highlight-javascript linenums">/**
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
* @copyright 2014 Photon Storm Ltd.
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
*/
|
|
|
|
/**
|
|
* Arcade Physics constructor.
|
|
*
|
|
* @class Phaser.Physics.Arcade
|
|
* @classdesc Arcade Physics Constructor
|
|
* @constructor
|
|
* @param {Phaser.Game} game reference to the current game instance.
|
|
*/
|
|
Phaser.Physics.Arcade = function (game) {
|
|
|
|
/**
|
|
* @property {Phaser.Game} game - Local reference to game.
|
|
*/
|
|
this.game = game;
|
|
|
|
/**
|
|
* @property {Phaser.Point} gravity - The World gravity setting. Defaults to x: 0, y: 0, or no gravity.
|
|
*/
|
|
this.gravity = new Phaser.Point();
|
|
|
|
/**
|
|
* @property {Phaser.Rectangle} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds.
|
|
*/
|
|
this.bounds = new Phaser.Rectangle(0, 0, game.world.width, game.world.height);
|
|
|
|
/**
|
|
* Set the checkCollision properties to control for which bounds collision is processed.
|
|
* For example checkCollision.down = false means Bodies cannot collide with the World.bounds.bottom.
|
|
* @property {object} checkCollision - An object containing allowed collision flags.
|
|
*/
|
|
this.checkCollision = { up: true, down: true, left: true, right: true };
|
|
|
|
/**
|
|
* @property {number} maxObjects - Used by the QuadTree to set the maximum number of objects per quad.
|
|
*/
|
|
this.maxObjects = 10;
|
|
|
|
/**
|
|
* @property {number} maxLevels - Used by the QuadTree to set the maximum number of iteration levels.
|
|
*/
|
|
this.maxLevels = 4;
|
|
|
|
/**
|
|
* @property {number} OVERLAP_BIAS - A value added to the delta values during collision checks.
|
|
*/
|
|
this.OVERLAP_BIAS = 4;
|
|
|
|
/**
|
|
* @property {number} TILE_BIAS - A value added to the delta values during collision with tiles. Adjust this if you get tunnelling.
|
|
*/
|
|
this.TILE_BIAS = 16;
|
|
|
|
/**
|
|
* @property {boolean} forceX - If true World.separate will always separate on the X axis before Y. Otherwise it will check gravity totals first.
|
|
*/
|
|
this.forceX = false;
|
|
|
|
/**
|
|
* @property {boolean} skipQuadTree - If true a QuadTree will never be used for any collision. Handy for tightly packed games. See also Body.skipQuadTree.
|
|
*/
|
|
this.skipQuadTree = false;
|
|
|
|
/**
|
|
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
|
|
*/
|
|
this.quadTree = new Phaser.QuadTree(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
|
|
|
// Avoid gc spikes by caching these values for re-use
|
|
|
|
/**
|
|
* @property {number} _overlap - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._overlap = 0;
|
|
|
|
/**
|
|
* @property {number} _maxOverlap - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._maxOverlap = 0;
|
|
|
|
/**
|
|
* @property {number} _velocity1 - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._velocity1 = 0;
|
|
|
|
/**
|
|
* @property {number} _velocity2 - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._velocity2 = 0;
|
|
|
|
/**
|
|
* @property {number} _newVelocity1 - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._newVelocity1 = 0;
|
|
|
|
/**
|
|
* @property {number} _newVelocity2 - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._newVelocity2 = 0;
|
|
|
|
/**
|
|
* @property {number} _average - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._average = 0;
|
|
|
|
/**
|
|
* @property {Array} _mapData - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._mapData = [];
|
|
|
|
/**
|
|
* @property {boolean} _result - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._result = false;
|
|
|
|
/**
|
|
* @property {number} _total - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._total = 0;
|
|
|
|
/**
|
|
* @property {number} _angle - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._angle = 0;
|
|
|
|
/**
|
|
* @property {number} _dx - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._dx = 0;
|
|
|
|
/**
|
|
* @property {number} _dy - Internal cache var.
|
|
* @private
|
|
*/
|
|
this._dy = 0;
|
|
|
|
// By default we want the bounds the same size as the world bounds
|
|
this.setBoundsToWorld();
|
|
|
|
};
|
|
|
|
Phaser.Physics.Arcade.prototype.constructor = Phaser.Physics.Arcade;
|
|
|
|
Phaser.Physics.Arcade.prototype = {
|
|
|
|
/**
|
|
* Updates the size of this physics world.
|
|
*
|
|
* @method Phaser.Physics.Arcade#setBounds
|
|
* @param {number} x - Top left most corner of the world.
|
|
* @param {number} y - Top left most corner of the world.
|
|
* @param {number} width - New width of the world. Can never be smaller than the Game.width.
|
|
* @param {number} height - New height of the world. Can never be smaller than the Game.height.
|
|
*/
|
|
setBounds: function (x, y, width, height) {
|
|
|
|
this.bounds.setTo(x, y, width, height);
|
|
|
|
},
|
|
|
|
/**
|
|
* Updates the size of this physics world to match the size of the game world.
|
|
*
|
|
* @method Phaser.Physics.Arcade#setBoundsToWorld
|
|
*/
|
|
setBoundsToWorld: function () {
|
|
|
|
this.bounds.setTo(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height);
|
|
|
|
},
|
|
|
|
/**
|
|
* This will create an Arcade Physics body on the given game object or array of game objects.
|
|
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
|
|
*
|
|
* @method Phaser.Physics.Arcade#enable
|
|
* @param {object|array|Phaser.Group} object - The game object to create the physics body on. Can also be an array or Group of objects, a body will be created on every child that has a `body` property.
|
|
* @param {boolean} [children=true] - Should a body be created on all children of this object? If true it will recurse down the display list as far as it can go.
|
|
*/
|
|
enable: function (object, children) {
|
|
|
|
if (typeof children === 'undefined') { children = true; }
|
|
|
|
var i = 1;
|
|
|
|
if (Array.isArray(object))
|
|
{
|
|
i = object.length;
|
|
|
|
while (i--)
|
|
{
|
|
if (object[i] instanceof Phaser.Group)
|
|
{
|
|
// If it's a Group then we do it on the children regardless
|
|
this.enable(object[i].children, children);
|
|
}
|
|
else
|
|
{
|
|
this.enableBody(object[i]);
|
|
|
|
if (children && object[i].hasOwnProperty('children') && object[i].children.length > 0)
|
|
{
|
|
this.enable(object[i], true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (object instanceof Phaser.Group)
|
|
{
|
|
// If it's a Group then we do it on the children regardless
|
|
this.enable(object.children, children);
|
|
}
|
|
else
|
|
{
|
|
this.enableBody(object);
|
|
|
|
if (children && object.hasOwnProperty('children') && object.children.length > 0)
|
|
{
|
|
this.enable(object.children, true);
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* Creates an Arcade Physics body on the given game object.
|
|
* A game object can only have 1 physics body active at any one time, and it can't be changed until the body is nulled.
|
|
*
|
|
* @method Phaser.Physics.Arcade#enableBody
|
|
* @param {object} object - The game object to create the physics body on. A body will only be created if this object has a null `body` property.
|
|
*/
|
|
enableBody: function (object) {
|
|
|
|
if (object.hasOwnProperty('body') && object.body === null)
|
|
{
|
|
object.body = new Phaser.Physics.Arcade.Body(object);
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* Called automatically by a Physics body, it updates all motion related values on the Body.
|
|
*
|
|
* @method Phaser.Physics.Arcade#updateMotion
|
|
* @param {Phaser.Physics.Arcade.Body} The Body object to be updated.
|
|
*/
|
|
updateMotion: function (body) {
|
|
|
|
this._velocityDelta = this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity;
|
|
body.angularVelocity += this._velocityDelta;
|
|
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
|
|
|
|
body.velocity.x = this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x);
|
|
body.velocity.y = this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y);
|
|
|
|
},
|
|
|
|
/**
|
|
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
|
|
* Based on a function in Flixel by @ADAMATOMIC
|
|
*
|
|
* @method Phaser.Physics.Arcade#computeVelocity
|
|
* @param {number} axis - 0 for nothing, 1 for horizontal, 2 for vertical.
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to be updated.
|
|
* @param {number} velocity - Any component of velocity (e.g. 20).
|
|
* @param {number} acceleration - Rate at which the velocity is changing.
|
|
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
|
|
* @param {number} [max=10000] - An absolute value cap for the velocity.
|
|
* @return {number} The altered Velocity value.
|
|
*/
|
|
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
|
|
|
|
max = max || 10000;
|
|
|
|
if (axis == 1 && body.allowGravity)
|
|
{
|
|
velocity += (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
|
|
}
|
|
else if (axis == 2 && body.allowGravity)
|
|
{
|
|
velocity += (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
|
|
}
|
|
|
|
if (acceleration)
|
|
{
|
|
velocity += acceleration * this.game.time.physicsElapsed;
|
|
}
|
|
else if (drag)
|
|
{
|
|
this._drag = drag * this.game.time.physicsElapsed;
|
|
|
|
if (velocity - this._drag > 0)
|
|
{
|
|
velocity -= this._drag;
|
|
}
|
|
else if (velocity + this._drag < 0)
|
|
{
|
|
velocity += this._drag;
|
|
}
|
|
else
|
|
{
|
|
velocity = 0;
|
|
}
|
|
}
|
|
|
|
if (velocity > max)
|
|
{
|
|
velocity = max;
|
|
}
|
|
else if (velocity < -max)
|
|
{
|
|
velocity = -max;
|
|
}
|
|
|
|
return velocity;
|
|
|
|
},
|
|
|
|
/**
|
|
* Checks for overlaps between two game objects. The objects can be Sprites, Groups or Emitters.
|
|
* You can perform Sprite vs. Sprite, Sprite vs. Group and Group vs. Group overlap checks.
|
|
* Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
|
|
* Both the first and second parameter can be arrays of objects, of differing types.
|
|
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
|
|
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups).
|
|
*
|
|
* @method Phaser.Physics.Arcade#overlap
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group or Phaser.Particles.Emitter.
|
|
* @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true.
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
|
* @return {boolean} True if an overlap occurred otherwise false.
|
|
*/
|
|
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
|
|
|
|
overlapCallback = overlapCallback || null;
|
|
processCallback = processCallback || null;
|
|
callbackContext = callbackContext || overlapCallback;
|
|
|
|
this._result = false;
|
|
this._total = 0;
|
|
|
|
if (!Array.isArray(object1) && Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len = object2.length; i < len; i++)
|
|
{
|
|
this.collideHandler(object1, object2[i], overlapCallback, processCallback, callbackContext, true);
|
|
}
|
|
}
|
|
else if (Array.isArray(object1) && !Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len = object1.length; i < len; i++)
|
|
{
|
|
this.collideHandler(object1[i], object2, overlapCallback, processCallback, callbackContext, true);
|
|
}
|
|
}
|
|
else if (Array.isArray(object1) && Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len = object1.length; i < len; i++)
|
|
{
|
|
for (var j = 0, len2 = object2.length; j < len2; j++)
|
|
{
|
|
this.collideHandler(object1[i], object2[j], overlapCallback, processCallback, callbackContext, true);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.collideHandler(object1, object2, overlapCallback, processCallback, callbackContext, true);
|
|
}
|
|
|
|
return (this._total > 0);
|
|
|
|
},
|
|
|
|
/**
|
|
* Checks for collision between two game objects. You can perform Sprite vs. Sprite, Sprite vs. Group, Group vs. Group, Sprite vs. Tilemap Layer or Group vs. Tilemap Layer collisions.
|
|
* Both the first and second parameter can be arrays of objects, of differing types.
|
|
* If two arrays are passed, the contents of the first parameter will be tested against all contents of the 2nd parameter.
|
|
* The objects are also automatically separated. If you don't require separation then use ArcadePhysics.overlap instead.
|
|
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
|
|
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
|
|
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
|
|
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups or Tilemaps within other Groups).
|
|
*
|
|
* @method Phaser.Physics.Arcade#collide
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap|array} object1 - The first object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap|array} object2 - The second object or array of objects to check. Can be Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap.
|
|
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} [callbackContext] - The context in which to run the callbacks.
|
|
* @return {boolean} True if a collision occured otherwise false.
|
|
*/
|
|
collide: function (object1, object2, collideCallback, processCallback, callbackContext) {
|
|
|
|
collideCallback = collideCallback || null;
|
|
processCallback = processCallback || null;
|
|
callbackContext = callbackContext || collideCallback;
|
|
|
|
this._result = false;
|
|
this._total = 0;
|
|
|
|
if (!Array.isArray(object1) && Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len = object2.length; i < len; i++)
|
|
{
|
|
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, false);
|
|
}
|
|
}
|
|
else if (Array.isArray(object1) && !Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len = object1.length; i < len; i++)
|
|
{
|
|
this.collideHandler(object1[i], object2, collideCallback, processCallback, callbackContext, false);
|
|
}
|
|
}
|
|
else if (Array.isArray(object1) && Array.isArray(object2))
|
|
{
|
|
for (var i = 0, len1 = object1.length; i < len1; i++)
|
|
{
|
|
for (var j = 0, len2 = object2.length; j < len2; j++)
|
|
{
|
|
this.collideHandler(object1[i], object2[j], collideCallback, processCallback, callbackContext, false);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, false);
|
|
}
|
|
|
|
return (this._total > 0);
|
|
|
|
},
|
|
|
|
/**
|
|
* Internal collision handler.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideHandler
|
|
* @private
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object1 - The first object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter, or Phaser.Tilemap.
|
|
* @param {Phaser.Sprite|Phaser.Group|Phaser.Particles.Emitter|Phaser.Tilemap} object2 - The second object to check. Can be an instance of Phaser.Sprite, Phaser.Group, Phaser.Particles.Emitter or Phaser.Tilemap. Can also be an array of objects to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
*/
|
|
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly) {
|
|
|
|
// Only collide valid objects
|
|
if (typeof object2 === 'undefined' && (object1.type === Phaser.GROUP || object1.type === Phaser.EMITTER))
|
|
{
|
|
this.collideGroupVsSelf(object1, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
return;
|
|
}
|
|
|
|
if (object1 && object2 && object1.exists && object2.exists)
|
|
{
|
|
// SPRITES
|
|
if (object1.type == Phaser.SPRITE || object1.type == Phaser.TILESPRITE)
|
|
{
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
|
{
|
|
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
{
|
|
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
|
{
|
|
this.collideSpriteVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
|
}
|
|
}
|
|
// GROUPS
|
|
else if (object1.type == Phaser.GROUP)
|
|
{
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
|
{
|
|
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
{
|
|
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
|
{
|
|
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
|
}
|
|
}
|
|
// TILEMAP LAYERS
|
|
else if (object1.type == Phaser.TILEMAPLAYER)
|
|
{
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
|
{
|
|
this.collideSpriteVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
|
|
}
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
{
|
|
this.collideGroupVsTilemapLayer(object2, object1, collideCallback, processCallback, callbackContext);
|
|
}
|
|
}
|
|
// EMITTER
|
|
else if (object1.type == Phaser.EMITTER)
|
|
{
|
|
if (object2.type == Phaser.SPRITE || object2.type == Phaser.TILESPRITE)
|
|
{
|
|
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.GROUP || object2.type == Phaser.EMITTER)
|
|
{
|
|
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else if (object2.type == Phaser.TILEMAPLAYER)
|
|
{
|
|
this.collideGroupVsTilemapLayer(object1, object2, collideCallback, processCallback, callbackContext);
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsSprite
|
|
* @private
|
|
* @param {Phaser.Sprite} sprite1 - The first sprite to check.
|
|
* @param {Phaser.Sprite} sprite2 - The second sprite to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
* @return {boolean} True if there was a collision, otherwise false.
|
|
*/
|
|
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly) {
|
|
|
|
if (!sprite1.body || !sprite2.body)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly))
|
|
{
|
|
if (collideCallback)
|
|
{
|
|
collideCallback.call(callbackContext, sprite1, sprite2);
|
|
}
|
|
|
|
this._total++;
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsGroup
|
|
* @private
|
|
* @param {Phaser.Sprite} sprite - The sprite to check.
|
|
* @param {Phaser.Group} group - The Group to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
*/
|
|
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly) {
|
|
|
|
if (group.length === 0 || !sprite.body)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (sprite.body.skipQuadTree || this.skipQuadTree)
|
|
{
|
|
for (var i = 0, len = group.children.length; i < len; i++)
|
|
{
|
|
if (group.children[i] && group.children[i].exists)
|
|
{
|
|
this.collideSpriteVsSprite(sprite, group.children[i], collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// What is the sprite colliding with in the quadtree?
|
|
this.quadTree.clear();
|
|
|
|
this.quadTree.reset(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
|
|
|
this.quadTree.populate(group);
|
|
|
|
this._potentials = this.quadTree.retrieve(sprite);
|
|
|
|
for (var i = 0, len = this._potentials.length; i < len; i++)
|
|
{
|
|
// We have our potential suspects, are they in this group?
|
|
if (this.separate(sprite.body, this._potentials[i], processCallback, callbackContext, overlapOnly))
|
|
{
|
|
if (collideCallback)
|
|
{
|
|
collideCallback.call(callbackContext, sprite, this._potentials[i].sprite);
|
|
}
|
|
|
|
this._total++;
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideGroupVsSelf
|
|
* @private
|
|
* @param {Phaser.Group} group - The Group to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
* @return {boolean} True if there was a collision, otherwise false.
|
|
*/
|
|
collideGroupVsSelf: function (group, collideCallback, processCallback, callbackContext, overlapOnly) {
|
|
|
|
if (group.length === 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var len = group.children.length;
|
|
|
|
for (var i = 0; i < len; i++)
|
|
{
|
|
for (var j = i + 1; j <= len; j++)
|
|
{
|
|
if (group.children[i] && group.children[j] && group.children[i].exists && group.children[j].exists)
|
|
{
|
|
this.collideSpriteVsSprite(group.children[i], group.children[j], collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideGroupVsGroup
|
|
* @private
|
|
* @param {Phaser.Group} group1 - The first Group to check.
|
|
* @param {Phaser.Group} group2 - The second Group to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
*/
|
|
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly) {
|
|
|
|
if (group1.length === 0 || group2.length === 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (var i = 0, len = group1.children.length; i < len; i++)
|
|
{
|
|
if (group1.children[i].exists)
|
|
{
|
|
if (group1.children[i].type === Phaser.GROUP)
|
|
{
|
|
this.collideGroupVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
else
|
|
{
|
|
this.collideSpriteVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly);
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @method Phaser.Physics.Arcade#collideSpriteVsTilemapLayer
|
|
* @private
|
|
* @param {Phaser.Sprite} sprite - The sprite to check.
|
|
* @param {Phaser.TilemapLayer} tilemapLayer - The layer to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
*/
|
|
collideSpriteVsTilemapLayer: function (sprite, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
|
|
|
if (!sprite.body)
|
|
{
|
|
return;
|
|
}
|
|
|
|
this._mapData = tilemapLayer.getTiles(
|
|
sprite.body.position.x - sprite.body.tilePadding.x,
|
|
sprite.body.position.y - sprite.body.tilePadding.y,
|
|
sprite.body.width + sprite.body.tilePadding.x,
|
|
sprite.body.height + sprite.body.tilePadding.y,
|
|
false, false);
|
|
|
|
if (this._mapData.length === 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (var i = 0; i < this._mapData.length; i++)
|
|
{
|
|
if (processCallback)
|
|
{
|
|
if (processCallback.call(callbackContext, sprite, this._mapData[i]))
|
|
{
|
|
if (this.separateTile(i, sprite.body, this._mapData[i]))
|
|
{
|
|
this._total++;
|
|
|
|
if (collideCallback)
|
|
{
|
|
collideCallback.call(callbackContext, sprite, this._mapData[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (this.separateTile(i, sprite.body, this._mapData[i]))
|
|
{
|
|
this._total++;
|
|
|
|
if (collideCallback)
|
|
{
|
|
collideCallback.call(callbackContext, sprite, this._mapData[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* An internal function. Use Phaser.Physics.Arcade.collide instead.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#collideGroupVsTilemapLayer
|
|
* @param {Phaser.Group} group - The Group to check.
|
|
* @param {Phaser.TilemapLayer} tilemapLayer - The layer to check.
|
|
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
|
|
* @param {object} callbackContext - The context in which to run the callbacks.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
*/
|
|
collideGroupVsTilemapLayer: function (group, tilemapLayer, collideCallback, processCallback, callbackContext) {
|
|
|
|
if (group.length === 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
for (var i = 0, len = group.children.length; i < len; i++)
|
|
{
|
|
if (group.children[i].exists)
|
|
{
|
|
this.collideSpriteVsTilemapLayer(group.children[i], tilemapLayer, collideCallback, processCallback, callbackContext);
|
|
}
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* The core separation function to separate two physics bodies.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#separate
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The first Body object to separate.
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The second Body object to separate.
|
|
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this function is set then the sprites will only be collided if it returns true.
|
|
* @param {object} [callbackContext] - The context in which to run the process callback.
|
|
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
|
|
* @return {boolean} Returns true if the bodies collided, otherwise false.
|
|
*/
|
|
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
|
|
|
|
if (!body1.enable || !body2.enable || !this.intersects(body1, body2))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// They overlap. Is there a custom process callback? If it returns true then we can carry on, otherwise we should abort.
|
|
if (processCallback && processCallback.call(callbackContext, body1.sprite, body2.sprite) === false)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
// Do we separate on x or y first?
|
|
|
|
// If we weren't having to carry around so much legacy baggage with us, we could do this properly. But alas ...
|
|
if (this.forceX || Math.abs(this.gravity.y + body1.gravity.y) < Math.abs(this.gravity.x + body1.gravity.x))
|
|
{
|
|
this._result = (this.separateX(body1, body2, overlapOnly) || this.separateY(body1, body2, overlapOnly));
|
|
}
|
|
else
|
|
{
|
|
this._result = (this.separateY(body1, body2, overlapOnly) || this.separateX(body1, body2, overlapOnly));
|
|
}
|
|
|
|
if (overlapOnly)
|
|
{
|
|
// We already know they intersect from the check above, but by this point we know they've now had their overlapX/Y values populated
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return this._result;
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* Check for intersection against two bodies.
|
|
*
|
|
* @method Phaser.Physics.Arcade#intersects
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to check.
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to check.
|
|
* @return {boolean} True if they intersect, otherwise false.
|
|
*/
|
|
intersects: function (body1, body2) {
|
|
|
|
if (body1.right <= body2.position.x)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (body1.bottom <= body2.position.y)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (body1.position.x >= body2.right)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (body1.position.y >= body2.bottom)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
/**
|
|
* The core separation function to separate two physics bodies on the x axis.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#separateX
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate.
|
|
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
|
|
* @return {boolean} Returns true if the bodies were separated, otherwise false.
|
|
*/
|
|
separateX: function (body1, body2, overlapOnly) {
|
|
|
|
// Can't separate two immovable bodies
|
|
if (body1.immovable && body2.immovable)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
this._overlap = 0;
|
|
|
|
// Check if the hulls actually overlap
|
|
if (this.intersects(body1, body2))
|
|
{
|
|
this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS;
|
|
|
|
if (body1.deltaX() === 0 && body2.deltaX() === 0)
|
|
{
|
|
// They overlap but neither of them are moving
|
|
body1.embedded = true;
|
|
body2.embedded = true;
|
|
}
|
|
else if (body1.deltaX() > body2.deltaX())
|
|
{
|
|
// Body1 is moving right and/or Body2 is moving left
|
|
this._overlap = body1.right - body2.x;
|
|
|
|
if ((this._overlap > this._maxOverlap) || body1.checkCollision.right === false || body2.checkCollision.left === false)
|
|
{
|
|
this._overlap = 0;
|
|
}
|
|
else
|
|
{
|
|
body1.touching.none = false;
|
|
body1.touching.right = true;
|
|
body2.touching.none = false;
|
|
body2.touching.left = true;
|
|
}
|
|
}
|
|
else if (body1.deltaX() < body2.deltaX())
|
|
{
|
|
// Body1 is moving left and/or Body2 is moving right
|
|
this._overlap = body1.x - body2.width - body2.x;
|
|
|
|
if ((-this._overlap > this._maxOverlap) || body1.checkCollision.left === false || body2.checkCollision.right === false)
|
|
{
|
|
this._overlap = 0;
|
|
}
|
|
else
|
|
{
|
|
body1.touching.none = false;
|
|
body1.touching.left = true;
|
|
body2.touching.none = false;
|
|
body2.touching.right = true;
|
|
}
|
|
}
|
|
|
|
// Resets the overlapX to zero if there is no overlap, or to the actual pixel value if there is
|
|
body1.overlapX = this._overlap;
|
|
body2.overlapX = this._overlap;
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
|
if (this._overlap !== 0)
|
|
{
|
|
if (overlapOnly || body1.customSeparateX || body2.customSeparateX)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
this._velocity1 = body1.velocity.x;
|
|
this._velocity2 = body2.velocity.x;
|
|
|
|
if (!body1.immovable && !body2.immovable)
|
|
{
|
|
this._overlap *= 0.5;
|
|
|
|
body1.x = body1.x - this._overlap;
|
|
body2.x += this._overlap;
|
|
|
|
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
|
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
|
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
|
this._newVelocity1 -= this._average;
|
|
this._newVelocity2 -= this._average;
|
|
|
|
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
|
|
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
|
|
}
|
|
else if (!body1.immovable)
|
|
{
|
|
body1.x = body1.x - this._overlap;
|
|
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
|
|
}
|
|
else if (!body2.immovable)
|
|
{
|
|
body2.x += this._overlap;
|
|
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
/**
|
|
* The core separation function to separate two physics bodies on the y axis.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#separateY
|
|
* @param {Phaser.Physics.Arcade.Body} body1 - The Body object to separate.
|
|
* @param {Phaser.Physics.Arcade.Body} body2 - The Body object to separate.
|
|
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
|
|
* @return {boolean} Returns true if the bodies were separated, otherwise false.
|
|
*/
|
|
separateY: function (body1, body2, overlapOnly) {
|
|
|
|
// Can't separate two immovable or non-existing bodys
|
|
if (body1.immovable && body2.immovable)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
this._overlap = 0;
|
|
|
|
// Check if the hulls actually overlap
|
|
if (this.intersects(body1, body2))
|
|
{
|
|
this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
|
|
|
|
if (body1.deltaY() === 0 && body2.deltaY() === 0)
|
|
{
|
|
// They overlap but neither of them are moving
|
|
body1.embedded = true;
|
|
body2.embedded = true;
|
|
}
|
|
else if (body1.deltaY() > body2.deltaY())
|
|
{
|
|
// Body1 is moving down and/or Body2 is moving up
|
|
this._overlap = body1.bottom - body2.y;
|
|
|
|
if ((this._overlap > this._maxOverlap) || body1.checkCollision.down === false || body2.checkCollision.up === false)
|
|
{
|
|
this._overlap = 0;
|
|
}
|
|
else
|
|
{
|
|
body1.touching.none = false;
|
|
body1.touching.down = true;
|
|
body2.touching.none = false;
|
|
body2.touching.up = true;
|
|
}
|
|
}
|
|
else if (body1.deltaY() < body2.deltaY())
|
|
{
|
|
// Body1 is moving up and/or Body2 is moving down
|
|
this._overlap = body1.y - body2.bottom;
|
|
|
|
if ((-this._overlap > this._maxOverlap) || body1.checkCollision.up === false || body2.checkCollision.down === false)
|
|
{
|
|
this._overlap = 0;
|
|
}
|
|
else
|
|
{
|
|
body1.touching.none = false;
|
|
body1.touching.up = true;
|
|
body2.touching.none = false;
|
|
body2.touching.down = true;
|
|
}
|
|
}
|
|
|
|
// Resets the overlapY to zero if there is no overlap, or to the actual pixel value if there is
|
|
body1.overlapY = this._overlap;
|
|
body2.overlapY = this._overlap;
|
|
|
|
// Then adjust their positions and velocities accordingly (if there was any overlap)
|
|
if (this._overlap !== 0)
|
|
{
|
|
if (overlapOnly || body1.customSeparateY || body2.customSeparateY)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
this._velocity1 = body1.velocity.y;
|
|
this._velocity2 = body2.velocity.y;
|
|
|
|
if (!body1.immovable && !body2.immovable)
|
|
{
|
|
this._overlap *= 0.5;
|
|
|
|
body1.y = body1.y - this._overlap;
|
|
body2.y += this._overlap;
|
|
|
|
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
|
|
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
|
|
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
|
|
this._newVelocity1 -= this._average;
|
|
this._newVelocity2 -= this._average;
|
|
|
|
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
|
|
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
|
|
}
|
|
else if (!body1.immovable)
|
|
{
|
|
body1.y = body1.y - this._overlap;
|
|
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
|
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
|
if (body2.moves)
|
|
{
|
|
body1.x += body2.x - body2.prev.x;
|
|
}
|
|
}
|
|
else if (!body2.immovable)
|
|
{
|
|
body2.y += this._overlap;
|
|
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
|
|
|
|
// This is special case code that handles things like horizontal moving platforms you can ride
|
|
if (body1.moves)
|
|
{
|
|
body2.x += body1.x - body1.prev.x;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
/**
|
|
* The core separation function to separate a physics body and a tile.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#separateTile
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
* @param {Phaser.Tile} tile - The tile to collide against.
|
|
* @return {boolean} Returns true if the body was separated, otherwise false.
|
|
*/
|
|
separateTile: function (i, body, tile) {
|
|
|
|
// We re-check for collision in case body was separated in a previous step
|
|
if (!body.enable || !tile.intersects(body.position.x, body.position.y, body.right, body.bottom))
|
|
{
|
|
// no collision so bail out (separted in a previous step)
|
|
return false;
|
|
}
|
|
|
|
// They overlap. Any custom callbacks?
|
|
|
|
// A local callback always takes priority over a layer level callback
|
|
if (tile.collisionCallback && !tile.collisionCallback.call(tile.collisionCallbackContext, body.sprite, tile))
|
|
{
|
|
// If it returns true then we can carry on, otherwise we should abort.
|
|
return false;
|
|
}
|
|
else if (tile.layer.callbacks[tile.index] && !tile.layer.callbacks[tile.index].callback.call(tile.layer.callbacks[tile.index].callbackContext, body.sprite, tile))
|
|
{
|
|
// If it returns true then we can carry on, otherwise we should abort.
|
|
return false;
|
|
}
|
|
|
|
// We don't need to go any further if this tile doesn't actually separate
|
|
if (!tile.faceLeft && !tile.faceRight && !tile.faceTop && !tile.faceBottom)
|
|
{
|
|
// This could happen if the tile was meant to be collided with re: a callback, but otherwise isn't needed for separation
|
|
return false;
|
|
}
|
|
|
|
var ox = 0;
|
|
var oy = 0;
|
|
var minX = 0;
|
|
var minY = 1;
|
|
|
|
if (body.deltaAbsX() > body.deltaAbsY())
|
|
{
|
|
// Moving faster horizontally, check X axis first
|
|
minX = -1;
|
|
}
|
|
else if (body.deltaAbsX() < body.deltaAbsY())
|
|
{
|
|
// Moving faster vertically, check Y axis first
|
|
minY = -1;
|
|
}
|
|
|
|
if (body.deltaX() !== 0 && body.deltaY() !== 0 && (tile.faceLeft || tile.faceRight) && (tile.faceTop || tile.faceBottom))
|
|
{
|
|
// We only need do this if both axis have checking faces AND we're moving in both directions
|
|
minX = Math.min(Math.abs(body.position.x - tile.right), Math.abs(body.right - tile.left));
|
|
minY = Math.min(Math.abs(body.position.y - tile.bottom), Math.abs(body.bottom - tile.top));
|
|
}
|
|
|
|
if (minX < minY)
|
|
{
|
|
if (tile.faceLeft || tile.faceRight)
|
|
{
|
|
ox = this.tileCheckX(body, tile);
|
|
|
|
// That's horizontal done, check if we still intersects? If not then we can return now
|
|
if (ox !== 0 && !tile.intersects(body.position.x, body.position.y, body.right, body.bottom))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (tile.faceTop || tile.faceBottom)
|
|
{
|
|
oy = this.tileCheckY(body, tile);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (tile.faceTop || tile.faceBottom)
|
|
{
|
|
oy = this.tileCheckY(body, tile);
|
|
|
|
// That's vertical done, check if we still intersects? If not then we can return now
|
|
if (oy !== 0 && !tile.intersects(body.position.x, body.position.y, body.right, body.bottom))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
|
|
if (tile.faceLeft || tile.faceRight)
|
|
{
|
|
ox = this.tileCheckX(body, tile);
|
|
}
|
|
}
|
|
|
|
return (ox !== 0 || oy !== 0);
|
|
|
|
},
|
|
|
|
/**
|
|
* Check the body against the given tile on the X axis.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#tileCheckX
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
* @param {Phaser.Tile} tile - The tile to check.
|
|
* @return {number} The amount of separation that occured.
|
|
*/
|
|
tileCheckX: function (body, tile) {
|
|
|
|
var ox = 0;
|
|
|
|
if (body.deltaX() < 0 && !body.blocked.left && tile.collideRight && body.checkCollision.left)
|
|
{
|
|
// Body is moving LEFT
|
|
if (tile.faceRight && body.x < tile.right)
|
|
{
|
|
ox = body.x - tile.right;
|
|
|
|
if (ox < -this.TILE_BIAS)
|
|
{
|
|
ox = 0;
|
|
}
|
|
}
|
|
}
|
|
else if (body.deltaX() > 0 && !body.blocked.right && tile.collideLeft && body.checkCollision.right)
|
|
{
|
|
// Body is moving RIGHT
|
|
if (tile.faceLeft && body.right > tile.left)
|
|
{
|
|
ox = body.right - tile.left;
|
|
|
|
if (ox > this.TILE_BIAS)
|
|
{
|
|
ox = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ox !== 0)
|
|
{
|
|
this.processTileSeparationX(body, ox);
|
|
}
|
|
|
|
return ox;
|
|
|
|
},
|
|
|
|
/**
|
|
* Check the body against the given tile on the Y axis.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#tileCheckY
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
* @param {Phaser.Tile} tile - The tile to check.
|
|
* @return {number} The amount of separation that occured.
|
|
*/
|
|
tileCheckY: function (body, tile) {
|
|
|
|
var oy = 0;
|
|
|
|
if (body.deltaY() < 0 && !body.blocked.up && tile.collideDown && body.checkCollision.up)
|
|
{
|
|
// Body is moving UP
|
|
if (tile.faceBottom && body.y < tile.bottom)
|
|
{
|
|
oy = body.y - tile.bottom;
|
|
|
|
if (oy < -this.TILE_BIAS)
|
|
{
|
|
oy = 0;
|
|
}
|
|
}
|
|
}
|
|
else if (body.deltaY() > 0 && !body.blocked.down && tile.collideUp && body.checkCollision.down)
|
|
{
|
|
// Body is moving DOWN
|
|
if (tile.faceTop && body.bottom > tile.top)
|
|
{
|
|
oy = body.bottom - tile.top;
|
|
|
|
if (oy > this.TILE_BIAS)
|
|
{
|
|
oy = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (oy !== 0)
|
|
{
|
|
this.processTileSeparationY(body, oy);
|
|
}
|
|
|
|
return oy;
|
|
|
|
},
|
|
|
|
/**
|
|
* Internal function to process the separation of a physics body from a tile.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#processTileSeparationX
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
* @param {number} x - The x separation amount.
|
|
* @return {boolean} Returns true as a pass-thru to the separateTile method.
|
|
*/
|
|
processTileSeparationX: function (body, x) {
|
|
|
|
if (x < 0)
|
|
{
|
|
body.blocked.left = true;
|
|
}
|
|
else if (x > 0)
|
|
{
|
|
body.blocked.right = true;
|
|
}
|
|
|
|
body.position.x -= x;
|
|
|
|
if (body.bounce.x === 0)
|
|
{
|
|
body.velocity.x = 0;
|
|
}
|
|
else
|
|
{
|
|
body.velocity.x = -body.velocity.x * body.bounce.x;
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* Internal function to process the separation of a physics body from a tile.
|
|
*
|
|
* @private
|
|
* @method Phaser.Physics.Arcade#processTileSeparationY
|
|
* @param {Phaser.Physics.Arcade.Body} body - The Body object to separate.
|
|
* @param {number} y - The y separation amount.
|
|
*/
|
|
processTileSeparationY: function (body, y) {
|
|
|
|
if (y < 0)
|
|
{
|
|
body.blocked.up = true;
|
|
}
|
|
else if (y > 0)
|
|
{
|
|
body.blocked.down = true;
|
|
}
|
|
|
|
body.position.y -= y;
|
|
|
|
if (body.bounce.y === 0)
|
|
{
|
|
body.velocity.y = 0;
|
|
}
|
|
else
|
|
{
|
|
body.velocity.y = -body.velocity.y * body.bounce.y;
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
* Given a Group and a Pointer this will check to see which Group children overlap with the Pointer coordinates.
|
|
* Each child will be sent to the given callback for further processing.
|
|
* Note that the children are not checked for depth order, but simply if they overlap the Pointer or not.
|
|
*
|
|
* @method Phaser.Physics.Arcade#getObjectsUnderPointer
|
|
* @param {Phaser.Pointer} pointer - The Pointer to check.
|
|
* @param {Phaser.Group} group - The Group to check.
|
|
* @param {function} [callback] - A callback function that is called if the object overlaps with the Pointer. The callback will be sent two parameters: the Pointer and the Object that overlapped with it.
|
|
* @param {object} [callbackContext] - The context in which to run the callback.
|
|
* @return {array} An array of the Sprites from the Group that overlapped the Pointer coordinates.
|
|
*/
|
|
getObjectsUnderPointer: function (pointer, group, callback, callbackContext) {
|
|
|
|
if (group.length === 0 || !pointer.exists)
|
|
{
|
|
return;
|
|
}
|
|
|
|
this.quadTree.clear();
|
|
|
|
this.quadTree.reset(this.game.world.bounds.x, this.game.world.bounds.y, this.game.world.bounds.width, this.game.world.bounds.height, this.maxObjects, this.maxLevels);
|
|
|
|
this.quadTree.populate(group);
|
|
|
|
var rect = new Phaser.Rectangle(pointer.x, pointer.y, 1, 1);
|
|
var output = [];
|
|
|
|
this._potentials = this.quadTree.retrieve(rect);
|
|
|
|
for (var i = 0, len = this._potentials.length; i < len; i++)
|
|
{
|
|
if (this._potentials[i].hitTest(pointer.x, pointer.y))
|
|
{
|
|
if (callback)
|
|
{
|
|
callback.call(callbackContext, pointer, this._potentials[i].sprite);
|
|
}
|
|
|
|
output.push(this._potentials[i].sprite);
|
|
}
|
|
}
|
|
|
|
return output;
|
|
|
|
},
|
|
|
|
/**
|
|
* Move the given display object towards the destination object at a steady velocity.
|
|
* If you specify a maxTime then it will adjust the speed (overwriting what you set) so it arrives at the destination in that number of seconds.
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
|
*
|
|
* @method Phaser.Physics.Arcade#moveToObject
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
*/
|
|
moveToObject: function (displayObject, destination, speed, maxTime) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
|
|
|
this._angle = Math.atan2(destination.y - displayObject.y, destination.x - displayObject.x);
|
|
|
|
if (maxTime > 0)
|
|
{
|
|
// We know how many pixels we need to move, but how fast?
|
|
speed = this.distanceBetween(displayObject, destination) / (maxTime / 1000);
|
|
}
|
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Move the given display object towards the pointer at a steady velocity. If no pointer is given it will use Phaser.Input.activePointer.
|
|
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
*
|
|
* @method Phaser.Physics.Arcade#moveToPointer
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
*/
|
|
moveToPointer: function (displayObject, speed, pointer, maxTime) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
pointer = pointer || this.game.input.activePointer;
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
|
|
|
this._angle = this.angleToPointer(displayObject, pointer);
|
|
|
|
if (maxTime > 0)
|
|
{
|
|
// We know how many pixels we need to move, but how fast?
|
|
speed = this.distanceToPointer(displayObject, pointer) / (maxTime / 1000);
|
|
}
|
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Move the given display object towards the x/y coordinates at a steady velocity.
|
|
* If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds.
|
|
* Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
* Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
|
|
*
|
|
* @method Phaser.Physics.Arcade#moveToXY
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {number} x - The x coordinate to move towards.
|
|
* @param {number} y - The y coordinate to move towards.
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec)
|
|
* @param {number} [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
|
|
*/
|
|
moveToXY: function (displayObject, x, y, speed, maxTime) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
if (typeof maxTime === 'undefined') { maxTime = 0; }
|
|
|
|
this._angle = Math.atan2(y - displayObject.y, x - displayObject.x);
|
|
|
|
if (maxTime > 0)
|
|
{
|
|
// We know how many pixels we need to move, but how fast?
|
|
speed = this.distanceToXY(displayObject, x, y) / (maxTime / 1000);
|
|
}
|
|
|
|
displayObject.body.velocity.x = Math.cos(this._angle) * speed;
|
|
displayObject.body.velocity.y = Math.sin(this._angle) * speed;
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Given the angle (in degrees) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
|
* One way to use this is: velocityFromAngle(angle, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
|
*
|
|
* @method Phaser.Physics.Arcade#velocityFromAngle
|
|
* @param {number} angle - The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
|
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
|
*/
|
|
velocityFromAngle: function (angle, speed, point) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
point = point || new Phaser.Point();
|
|
|
|
return point.setTo((Math.cos(this.game.math.degToRad(angle)) * speed), (Math.sin(this.game.math.degToRad(angle)) * speed));
|
|
|
|
},
|
|
|
|
/**
|
|
* Given the rotation (in radians) and speed calculate the velocity and return it as a Point object, or set it to the given point object.
|
|
* One way to use this is: velocityFromRotation(rotation, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
|
|
*
|
|
* @method Phaser.Physics.Arcade#velocityFromRotation
|
|
* @param {number} rotation - The angle in radians.
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated velocity.
|
|
* @return {Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
|
|
*/
|
|
velocityFromRotation: function (rotation, speed, point) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
point = point || new Phaser.Point();
|
|
|
|
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
|
|
|
|
},
|
|
|
|
/**
|
|
* Given the rotation (in radians) and speed calculate the acceleration and return it as a Point object, or set it to the given point object.
|
|
* One way to use this is: accelerationFromRotation(rotation, 200, sprite.acceleration) which will set the values directly to the sprites acceleration and not create a new Point object.
|
|
*
|
|
* @method Phaser.Physics.Arcade#accelerationFromRotation
|
|
* @param {number} rotation - The angle in radians.
|
|
* @param {number} [speed=60] - The speed it will move, in pixels per second sq.
|
|
* @param {Phaser.Point|object} [point] - The Point object in which the x and y properties will be set to the calculated acceleration.
|
|
* @return {Phaser.Point} - A Point where point.x contains the acceleration x value and point.y contains the acceleration y value.
|
|
*/
|
|
accelerationFromRotation: function (rotation, speed, point) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
point = point || new Phaser.Point();
|
|
|
|
return point.setTo((Math.cos(rotation) * speed), (Math.sin(rotation) * speed));
|
|
|
|
},
|
|
|
|
/**
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
*
|
|
* @method Phaser.Physics.Arcade#accelerateToObject
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {any} destination - The display object to move towards. Can be any object but must have visible x/y properties.
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
*/
|
|
accelerateToObject: function (displayObject, destination, speed, xSpeedMax, ySpeedMax) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
|
|
|
this._angle = this.angleBetween(displayObject, destination);
|
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.)
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
*
|
|
* @method Phaser.Physics.Arcade#accelerateToPointer
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {Phaser.Pointer} [pointer] - The pointer to move towards. Defaults to Phaser.Input.activePointer.
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
*/
|
|
accelerateToPointer: function (displayObject, pointer, speed, xSpeedMax, ySpeedMax) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
if (typeof pointer === 'undefined') { pointer = this.game.input.activePointer; }
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
|
|
|
this._angle = this.angleToPointer(displayObject, pointer);
|
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Sets the acceleration.x/y property on the display object so it will move towards the x/y coordinates at the given speed (in pixels per second sq.)
|
|
* You must give a maximum speed value, beyond which the display object won't go any faster.
|
|
* Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course.
|
|
* Note: The display object doesn't stop moving once it reaches the destination coordinates.
|
|
*
|
|
* @method Phaser.Physics.Arcade#accelerateToXY
|
|
* @param {any} displayObject - The display object to move.
|
|
* @param {number} x - The x coordinate to accelerate towards.
|
|
* @param {number} y - The y coordinate to accelerate towards.
|
|
* @param {number} [speed=60] - The speed it will accelerate in pixels per second.
|
|
* @param {number} [xSpeedMax=500] - The maximum x velocity the display object can reach.
|
|
* @param {number} [ySpeedMax=500] - The maximum y velocity the display object can reach.
|
|
* @return {number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
|
|
*/
|
|
accelerateToXY: function (displayObject, x, y, speed, xSpeedMax, ySpeedMax) {
|
|
|
|
if (typeof speed === 'undefined') { speed = 60; }
|
|
if (typeof xSpeedMax === 'undefined') { xSpeedMax = 1000; }
|
|
if (typeof ySpeedMax === 'undefined') { ySpeedMax = 1000; }
|
|
|
|
this._angle = this.angleToXY(displayObject, x, y);
|
|
|
|
displayObject.body.acceleration.setTo(Math.cos(this._angle) * speed, Math.sin(this._angle) * speed);
|
|
displayObject.body.maxVelocity.setTo(xSpeedMax, ySpeedMax);
|
|
|
|
return this._angle;
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the distance between two display objects (like Sprites).
|
|
*
|
|
* @method Phaser.Physics.Arcade#distanceBetween
|
|
* @param {any} source - The Display Object to test from.
|
|
* @param {any} target - The Display Object to test to.
|
|
* @return {number} The distance between the source and target objects.
|
|
*/
|
|
distanceBetween: function (source, target) {
|
|
|
|
this._dx = source.x - target.x;
|
|
this._dy = source.y - target.y;
|
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the distance between a display object (like a Sprite) and the given x/y coordinates.
|
|
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
|
|
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
|
|
*
|
|
* @method Phaser.Physics.Arcade#distanceToXY
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
* @param {number} x - The x coordinate to move towards.
|
|
* @param {number} y - The y coordinate to move towards.
|
|
* @return {number} The distance between the object and the x/y coordinates.
|
|
*/
|
|
distanceToXY: function (displayObject, x, y) {
|
|
|
|
this._dx = displayObject.x - x;
|
|
this._dy = displayObject.y - y;
|
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the distance between a display object (like a Sprite) and a Pointer. If no Pointer is given the Input.activePointer is used.
|
|
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
|
|
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
|
|
*
|
|
* @method Phaser.Physics.Arcade#distanceToPointer
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
|
|
* @return {number} The distance between the object and the Pointer.
|
|
*/
|
|
distanceToPointer: function (displayObject, pointer) {
|
|
|
|
pointer = pointer || this.game.input.activePointer;
|
|
|
|
this._dx = displayObject.x - pointer.x;
|
|
this._dy = displayObject.y - pointer.y;
|
|
|
|
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the angle in radians between two display objects (like Sprites).
|
|
*
|
|
* @method Phaser.Physics.Arcade#angleBetween
|
|
* @param {any} source - The Display Object to test from.
|
|
* @param {any} target - The Display Object to test to.
|
|
* @return {number} The angle in radians between the source and target display objects.
|
|
*/
|
|
angleBetween: function (source, target) {
|
|
|
|
this._dx = target.x - source.x;
|
|
this._dy = target.y - source.y;
|
|
|
|
return Math.atan2(this._dy, this._dx);
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the angle in radians between a display object (like a Sprite) and the given x/y coordinate.
|
|
*
|
|
* @method Phaser.Physics.Arcade#angleToXY
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
* @param {number} x - The x coordinate to get the angle to.
|
|
* @param {number} y - The y coordinate to get the angle to.
|
|
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
|
|
*/
|
|
angleToXY: function (displayObject, x, y) {
|
|
|
|
this._dx = x - displayObject.x;
|
|
this._dy = y - displayObject.y;
|
|
|
|
return Math.atan2(this._dy, this._dx);
|
|
|
|
},
|
|
|
|
/**
|
|
* Find the angle in radians between a display object (like a Sprite) and a Pointer, taking their x/y and center into account.
|
|
*
|
|
* @method Phaser.Physics.Arcade#angleToPointer
|
|
* @param {any} displayObject - The Display Object to test from.
|
|
* @param {Phaser.Pointer} [pointer] - The Phaser.Pointer to test to. If none is given then Input.activePointer is used.
|
|
* @return {number} The angle in radians between displayObject.x/y to Pointer.x/y
|
|
*/
|
|
angleToPointer: function (displayObject, pointer) {
|
|
|
|
pointer = pointer || this.game.input.activePointer;
|
|
|
|
this._dx = pointer.worldX - displayObject.x;
|
|
this._dy = pointer.worldY - displayObject.y;
|
|
|
|
return Math.atan2(this._dy, this._dx);
|
|
|
|
}
|
|
|
|
};
|
|
</pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<div class="clearfix"></div>
|
|
<footer>
|
|
|
|
|
|
<span class="copyright">
|
|
Phaser Copyright © 2012-2014 Photon Storm Ltd.
|
|
</span>
|
|
<br />
|
|
|
|
<span class="jsdoc-message">
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.0-dev</a>
|
|
on Tue Sep 09 2014 15:29:17 GMT+0100 (BST) using the <a href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
|
|
</span>
|
|
</footer>
|
|
</div>
|
|
|
|
|
|
<br clear="both">
|
|
</div>
|
|
|
|
</div>
|
|
<script src="scripts/sunlight.js"></script>
|
|
<script src="scripts/sunlight.javascript.js"></script>
|
|
<script src="scripts/sunlight-plugin.doclinks.js"></script>
|
|
<script src="scripts/sunlight-plugin.linenumbers.js"></script>
|
|
<script src="scripts/sunlight-plugin.menu.js"></script>
|
|
<script src="scripts/jquery.min.js"></script>
|
|
<script src="scripts/jquery.scrollTo.js"></script>
|
|
<script src="scripts/jquery.localScroll.js"></script>
|
|
<script src="scripts/bootstrap-dropdown.js"></script>
|
|
<script src="scripts/toc.js"></script>
|
|
|
|
|
|
<script> Sunlight.highlightAll({lineNumbers:true, showMenu: true, enableDoclinks :true}); </script>
|
|
|
|
<script>
|
|
$( function () {
|
|
$( "#toc" ).toc( {
|
|
anchorName : function(i, heading, prefix) {
|
|
return $(heading).attr("id") || ( prefix + i );
|
|
},
|
|
selectors : "h1,h2,h3,h4",
|
|
showAndHide : false,
|
|
scrollTo : 60
|
|
} );
|
|
$( "#toc>ul" ).addClass( "nav nav-pills nav-stacked" );
|
|
$( "#main span[id^='toc']" ).addClass( "toc-shim" );
|
|
|
|
} );
|
|
</script>
|
|
|
|
|
|
|
|
</body>
|
|
</html>
|