phaser/docs/Body.js__.html

2366 lines
69 KiB
HTML
Raw Normal View History

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Phaser Source: physics/p2/Body.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 ">
2014-04-10 22:13:43 +00:00
<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 ">
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Animation.html">Animation</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.AnimationManager.html">AnimationManager</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.AnimationParser.html">AnimationParser</a>
</li>
2014-04-29 14:39:02 +00:00
<li class="class-depth-1">
<a href="Phaser.ArrayList.html">ArrayList</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.BitmapData.html">BitmapData</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.BitmapText.html">BitmapText</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Button.html">Button</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Cache.html">Cache</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Camera.html">Camera</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Canvas.html">Canvas</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Circle.html">Circle</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Color.html">Color</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Device.html">Device</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Easing.html">Easing</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Back.html">Back</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Bounce.html">Bounce</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Circular.html">Circular</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Cubic.html">Cubic</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Elastic.html">Elastic</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Exponential.html">Exponential</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Linear.html">Linear</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Quadratic.html">Quadratic</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Quartic.html">Quartic</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Quintic.html">Quintic</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Easing.Sinusoidal.html">Sinusoidal</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Ellipse.html">Ellipse</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Events.html">Events</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Filter.html">Filter</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Frame.html">Frame</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.FrameData.html">FrameData</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Game.html">Game</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.GameObjectCreator.html">GameObjectCreator</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.GameObjectFactory.html">GameObjectFactory</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Gamepad.html">Gamepad</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.GamepadButton.html">GamepadButton</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Graphics.html">Graphics</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Group.html">Group</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Image.html">Image</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Input.html">Input</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.InputHandler.html">InputHandler</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Key.html">Key</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Keyboard.html">Keyboard</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Line.html">Line</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.LinkedList.html">LinkedList</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Loader.html">Loader</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.LoaderParser.html">LoaderParser</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Math.html">Math</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Mouse.html">Mouse</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.MSPointer.html">MSPointer</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Net.html">Net</a>
</li>
2014-04-10 22:13:43 +00:00
<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>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Particles.Arcade.Emitter.html">Emitter</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Physics.html">Physics</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Physics.Arcade.html">Arcade</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.Arcade.Body.html">Body</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Physics.Ninja.html">Ninja</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.Ninja.AABB.html">AABB</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.Ninja.Body.html">Body</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.Ninja.Circle.html">Circle</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.Ninja.Tile.html">Tile</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Physics.P2.html">P2</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.Body.html">Body</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.BodyDebug.html">BodyDebug</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.CollisionGroup.html">CollisionGroup</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.ContactMaterial.html">ContactMaterial</a>
</li>
2014-04-10 22:13:43 +00:00
<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>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.GearConstraint.html">GearConstraint</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.InversePointProxy.html">InversePointProxy</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.LockConstraint.html">LockConstraint</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.Material.html">Material</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.PointProxy.html">PointProxy</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.PrismaticConstraint.html">PrismaticConstraint</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.RevoluteConstraint.html">RevoluteConstraint</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-3">
<a href="Phaser.Physics.P2.Spring.html">Spring</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Plugin.html">Plugin</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.PluginManager.html">PluginManager</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Point.html">Point</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Pointer.html">Pointer</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Polygon.html">Polygon</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.QuadTree.html">QuadTree</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.RandomDataGenerator.html">RandomDataGenerator</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Rectangle.html">Rectangle</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.RenderTexture.html">RenderTexture</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.RequestAnimationFrame.html">RequestAnimationFrame</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.RetroFont.html">RetroFont</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.ScaleManager.html">ScaleManager</a>
</li>
2014-04-10 22:13:43 +00:00
<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>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.SinglePad.html">SinglePad</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Sound.html">Sound</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.SoundManager.html">SoundManager</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Sprite.html">Sprite</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.SpriteBatch.html">SpriteBatch</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Stage.html">Stage</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.State.html">State</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.StateManager.html">StateManager</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Text.html">Text</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Tile.html">Tile</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Tilemap.html">Tilemap</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.TilemapLayer.html">TilemapLayer</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.TilemapParser.html">TilemapParser</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Tileset.html">Tileset</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.TileSprite.html">TileSprite</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Time.html">Time</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Timer.html">Timer</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.TimerEvent.html">TimerEvent</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Touch.html">Touch</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Tween.html">Tween</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.TweenManager.html">TweenManager</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.Utils.html">Utils</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-2">
<a href="Phaser.Utils.Debug.html">Debug</a>
</li>
2014-04-10 22:13:43 +00:00
<li class="class-depth-1">
<a href="Phaser.World.html">World</a>
</li>
</ul>
</li>
2014-05-20 09:12:48 +00:00
</ul>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<div id="main">
<h1 class="page-title">Source: physics/p2/Body.js</h1>
<section>
<article>
<pre class="sunlight-highlight-javascript linenums">/**
* @author Richard Davey &lt;rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Physics Body is typically linked to a single Sprite and defines properties that determine how the physics body is simulated.
* These properties affect how the body reacts to forces, what forces it generates on itself (to simulate friction), and how it reacts to collisions in the scene.
* In most cases, the properties are used to simulate physical effects. Each body also has its own property values that determine exactly how it reacts to forces and collisions in the scene.
* By default a single Rectangle shape is added to the Body that matches the dimensions of the parent Sprite. See addShape, removeShape, clearShapes to add extra shapes around the Body.
* Note: When bound to a Sprite to avoid single-pixel jitters on mobile devices we strongly recommend using Sprite sizes that are even on both axis, i.e. 128x128 not 127x127.
*
* @class Phaser.Physics.P2.Body
* @classdesc Physics Body Constructor
* @constructor
* @param {Phaser.Game} game - Game reference to the currently running game.
* @param {Phaser.Sprite} [sprite] - The Sprite object this physics body belongs to.
* @param {number} [x=0] - The x coordinate of this Body.
* @param {number} [y=0] - The y coordinate of this Body.
* @param {number} [mass=1] - The default mass of this Body (0 = static).
*/
Phaser.Physics.P2.Body = function (game, sprite, x, y, mass) {
sprite = sprite || null;
x = x || 0;
y = y || 0;
if (typeof mass === 'undefined') { mass = 1; }
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
/**
* @property {Phaser.Physics.P2} world - Local reference to the P2 World.
*/
this.world = game.physics.p2;
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
*/
this.sprite = sprite;
/**
* @property {number} type - The type of physics system this body belongs to.
*/
this.type = Phaser.Physics.P2JS;
/**
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
*/
this.offset = new Phaser.Point();
/**
* @property {p2.Body} data - The p2 Body data.
* @protected
*/
this.data = new p2.Body({ position: [ this.world.pxmi(x), this.world.pxmi(y) ], mass: mass });
this.data.parent = this;
/**
* @property {Phaser.InversePointProxy} velocity - The velocity of the body. Set velocity.x to a negative value to move to the left, position to the right. velocity.y negative values move up, positive move down.
*/
this.velocity = new Phaser.Physics.P2.InversePointProxy(this.world, this.data.velocity);
/**
* @property {Phaser.InversePointProxy} force - The force applied to the body.
*/
this.force = new Phaser.Physics.P2.InversePointProxy(this.world, this.data.force);
/**
* @property {Phaser.Point} gravity - A locally applied gravity force to the Body. Applied directly before the world step. NOTE: Not currently implemented.
*/
this.gravity = new Phaser.Point();
/**
* Dispatched when a first contact is created between shapes in two bodies. This event is fired during the step, so collision has already taken place.
* The event will be sent 4 parameters: The body it is in contact with, the shape from this body that caused the contact, the shape from the contact body and the contact equation data array.
* @property {Phaser.Signal} onBeginContact
*/
this.onBeginContact = new Phaser.Signal();
/**
* Dispatched when contact ends between shapes in two bodies. This event is fired during the step, so collision has already taken place.
* The event will be sent 3 parameters: The body it is in contact with, the shape from this body that caused the contact and the shape from the contact body.
* @property {Phaser.Signal} onEndContact
*/
this.onEndContact = new Phaser.Signal();
/**
* @property {array} collidesWith - Array of CollisionGroups that this Bodies shapes collide with.
*/
this.collidesWith = [];
/**
* @property {boolean} removeNextStep - To avoid deleting this body during a physics step, and causing all kinds of problems, set removeNextStep to true to have it removed in the next preUpdate.
*/
this.removeNextStep = false;
/**
* @property {Phaser.Physics.P2.BodyDebug} debugBody - Reference to the debug body.
*/
this.debugBody = null;
/**
* @property {boolean} _collideWorldBounds - Internal var that determines if this Body collides with the world bounds or not.
* @private
*/
this._collideWorldBounds = true;
/**
* @property {object} _bodyCallbacks - Array of Body callbacks.
* @private
*/
this._bodyCallbacks = {};
/**
* @property {object} _bodyCallbackContext - Array of Body callback contexts.
* @private
*/
this._bodyCallbackContext = {};
/**
* @property {object} _groupCallbacks - Array of Group callbacks.
* @private
*/
this._groupCallbacks = {};
/**
* @property {object} _bodyCallbackContext - Array of Grouo callback contexts.
* @private
*/
this._groupCallbackContext = {};
// Set-up the default shape
if (sprite)
{
this.setRectangleFromSprite(sprite);
2014-03-24 12:41:45 +00:00
if (sprite.exists)
{
this.game.physics.p2.addBody(this);
}
}
};
Phaser.Physics.P2.Body.prototype = {
/**
* Sets a callback to be fired any time a shape in this Body impacts with a shape in the given Body. The impact test is performed against body.id values.
* The callback will be sent 4 parameters: This body, the body that impacted, the Shape in this body and the shape in the impacting body.
* Note that the impact event happens after collision resolution, so it cannot be used to prevent a collision from happening.
* It also happens mid-step. So do not destroy a Body during this callback, instead set safeDestroy to true so it will be killed on the next preUpdate.
*
* @method Phaser.Physics.P2.Body#createBodyCallback
* @param {Phaser.Sprite|Phaser.TileSprite|Phaser.Physics.P2.Body|p2.Body} object - The object to send impact events for.
* @param {function} callback - The callback to fire on impact. Set to null to clear a previously set callback.
* @param {object} callbackContext - The context under which the callback will fire.
*/
createBodyCallback: function (object, callback, callbackContext) {
var id = -1;
if (object['id'])
{
id = object.id;
}
else if (object['body'])
{
id = object.body.id;
}
if (id > -1)
{
if (callback === null)
{
delete (this._bodyCallbacks[id]);
delete (this._bodyCallbackContext[id]);
}
else
{
this._bodyCallbacks[id] = callback;
this._bodyCallbackContext[id] = callbackContext;
}
}
},
/**
* Sets a callback to be fired any time this Body impacts with the given Group. The impact test is performed against shape.collisionGroup values.
* The callback will be sent 4 parameters: This body, the body that impacted, the Shape in this body and the shape in the impacting body.
* This callback will only fire if this Body has been assigned a collision group.
* Note that the impact event happens after collision resolution, so it cannot be used to prevent a collision from happening.
* It also happens mid-step. So do not destroy a Body during this callback, instead set safeDestroy to true so it will be killed on the next preUpdate.
*
* @method Phaser.Physics.P2.Body#createGroupCallback
* @param {Phaser.Physics.CollisionGroup} group - The Group to send impact events for.
* @param {function} callback - The callback to fire on impact. Set to null to clear a previously set callback.
* @param {object} callbackContext - The context under which the callback will fire.
*/
createGroupCallback: function (group, callback, callbackContext) {
if (callback === null)
{
delete (this._groupCallbacks[group.mask]);
delete (this._groupCallbacksContext[group.mask]);
}
else
{
this._groupCallbacks[group.mask] = callback;
this._groupCallbackContext[group.mask] = callbackContext;
}
},
/**
* Gets the collision bitmask from the groups this body collides with.
*
* @method Phaser.Physics.P2.Body#getCollisionMask
* @return {number} The bitmask.
*/
getCollisionMask: function () {
var mask = 0;
if (this._collideWorldBounds)
{
mask = this.game.physics.p2.boundsCollisionGroup.mask;
}
for (var i = 0; i &lt; this.collidesWith.length; i++)
{
mask = mask | this.collidesWith[i].mask;
}
return mask;
},
/**
* Updates the collisionMask.
*
* @method Phaser.Physics.P2.Body#updateCollisionMask
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision group will be added to all Shapes in this Body.
*/
updateCollisionMask: function (shape) {
var mask = this.getCollisionMask();
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionMask = mask;
}
}
else
{
shape.collisionMask = mask;
}
},
/**
* Sets the given CollisionGroup to be the collision group for all shapes in this Body, unless a shape is specified.
* This also resets the collisionMask.
*
* @method Phaser.Physics.P2.Body#setCollisionGroup
* @param {Phaser.Physics.CollisionGroup} group - The Collision Group that this Bodies shapes will use.
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision group will be added to all Shapes in this Body.
*/
setCollisionGroup: function (group, shape) {
var mask = this.getCollisionMask();
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionGroup = group.mask;
this.data.shapes[i].collisionMask = mask;
}
}
else
{
shape.collisionGroup = group.mask;
shape.collisionMask = mask;
}
},
/**
* Clears the collision data from the shapes in this Body. Optionally clears Group and/or Mask.
*
* @method Phaser.Physics.P2.Body#clearCollision
* @param {boolean} [clearGroup=true] - Clear the collisionGroup value from the shape/s?
* @param {boolean} [clearMask=true] - Clear the collisionMask value from the shape/s?
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision data will be cleared from all Shapes in this Body.
*/
clearCollision: function (clearGroup, clearMask, shape) {
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
if (clearGroup)
{
this.data.shapes[i].collisionGroup = null;
}
if (clearMask)
{
this.data.shapes[i].collisionMask = null;
}
}
}
else
{
if (clearGroup)
{
shape.collisionGroup = null;
}
if (clearMask)
{
shape.collisionMask = null;
}
}
if (clearGroup)
{
this.collidesWith.length = 0;
}
},
/**
* Adds the given CollisionGroup, or array of CollisionGroups, to the list of groups that this body will collide with and updates the collision masks.
*
* @method Phaser.Physics.P2.Body#collides
* @param {Phaser.Physics.CollisionGroup|array} group - The Collision Group or Array of Collision Groups that this Bodies shapes will collide with.
* @param {function} [callback] - Optional callback that will be triggered when this Body impacts with the given Group.
* @param {object} [callbackContext] - The context under which the callback will be called.
* @param {p2.Shape} [shape] - An optional Shape. If not provided the collision mask will be added to all Shapes in this Body.
*/
collides: function (group, callback, callbackContext, shape) {
if (Array.isArray(group))
{
for (var i = 0; i &lt; group.length; i++)
{
if (this.collidesWith.indexOf(group[i]) === -1)
{
this.collidesWith.push(group[i]);
if (callback)
{
this.createGroupCallback(group[i], callback, callbackContext);
}
}
}
}
else
{
if (this.collidesWith.indexOf(group) === -1)
{
this.collidesWith.push(group);
if (callback)
{
this.createGroupCallback(group, callback, callbackContext);
}
}
}
var mask = this.getCollisionMask();
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].collisionMask = mask;
}
}
else
{
shape.collisionMask = mask;
}
},
/**
* Moves the shape offsets so their center of mass becomes the body center of mass.
*
* @method Phaser.Physics.P2.Body#adjustCenterOfMass
*/
adjustCenterOfMass: function () {
this.data.adjustCenterOfMass();
},
/**
* Apply damping, see http://code.google.com/p/bullet/issues/detail?id=74 for details.
*
* @method Phaser.Physics.P2.Body#applyDamping
* @param {number} dt - Current time step.
*/
applyDamping: function (dt) {
this.data.applyDamping(dt);
},
/**
* Apply force to a world point. This could for example be a point on the RigidBody surface. Applying force this way will add to Body.force and Body.angularForce.
*
* @method Phaser.Physics.P2.Body#applyForce
* @param {number} force - The force to add.
* @param {number} worldX - The world x point to apply the force on.
* @param {number} worldY - The world y point to apply the force on.
*/
applyForce: function (force, worldX, worldY) {
2014-05-20 09:12:48 +00:00
this.data.applyForce(force, [this.world.pxmi(worldX), this.world.pxmi(worldY)]);
},
/**
* Sets the force on the body to zero.
*
* @method Phaser.Physics.P2.Body#setZeroForce
*/
setZeroForce: function () {
this.data.setZeroForce();
},
/**
* If this Body is dynamic then this will zero its angular velocity.
*
* @method Phaser.Physics.P2.Body#setZeroRotation
*/
setZeroRotation: function () {
this.data.angularVelocity = 0;
},
/**
* If this Body is dynamic then this will zero its velocity on both axis.
*
* @method Phaser.Physics.P2.Body#setZeroVelocity
*/
setZeroVelocity: function () {
this.data.velocity[0] = 0;
this.data.velocity[1] = 0;
},
/**
* Sets the Body damping and angularDamping to zero.
*
* @method Phaser.Physics.P2.Body#setZeroDamping
*/
setZeroDamping: function () {
this.data.damping = 0;
this.data.angularDamping = 0;
},
/**
* Transform a world point to local body frame.
*
* @method Phaser.Physics.P2.Body#toLocalFrame
* @param {Float32Array|Array} out - The vector to store the result in.
* @param {Float32Array|Array} worldPoint - The input world vector.
*/
toLocalFrame: function (out, worldPoint) {
return this.data.toLocalFrame(out, worldPoint);
},
/**
* Transform a local point to world frame.
*
* @method Phaser.Physics.P2.Body#toWorldFrame
* @param {Array} out - The vector to store the result in.
* @param {Array} localPoint - The input local vector.
*/
toWorldFrame: function (out, localPoint) {
return this.data.toWorldFrame(out, localPoint);
},
/**
* This will rotate the Body by the given speed to the left (counter-clockwise).
*
* @method Phaser.Physics.P2.Body#rotateLeft
* @param {number} speed - The speed at which it should rotate.
*/
rotateLeft: function (speed) {
this.data.angularVelocity = this.world.pxm(-speed);
},
/**
* This will rotate the Body by the given speed to the left (clockwise).
*
* @method Phaser.Physics.P2.Body#rotateRight
* @param {number} speed - The speed at which it should rotate.
*/
rotateRight: function (speed) {
this.data.angularVelocity = this.world.pxm(speed);
},
/**
* Moves the Body forwards based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveForward
* @param {number} speed - The speed at which it should move forwards.
*/
moveForward: function (speed) {
var magnitude = this.world.pxmi(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.velocity[0] = magnitude * Math.cos(angle);
this.data.velocity[1] = magnitude * Math.sin(angle);
},
/**
* Moves the Body backwards based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveBackward
* @param {number} speed - The speed at which it should move backwards.
*/
moveBackward: function (speed) {
var magnitude = this.world.pxmi(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.velocity[0] = -(magnitude * Math.cos(angle));
this.data.velocity[1] = -(magnitude * Math.sin(angle));
},
/**
* Applies a force to the Body that causes it to 'thrust' forwards, based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#thrust
* @param {number} speed - The speed at which it should thrust.
*/
thrust: function (speed) {
var magnitude = this.world.pxmi(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.force[0] += magnitude * Math.cos(angle);
this.data.force[1] += magnitude * Math.sin(angle);
},
/**
* Applies a force to the Body that causes it to 'thrust' backwards (in reverse), based on its current angle and the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#rever
* @param {number} speed - The speed at which it should reverse.
*/
reverse: function (speed) {
var magnitude = this.world.pxmi(-speed);
var angle = this.data.angle + Math.PI / 2;
this.data.force[0] -= magnitude * Math.cos(angle);
this.data.force[1] -= magnitude * Math.sin(angle);
},
/**
* If this Body is dynamic then this will move it to the left by setting its x velocity to the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveLeft
* @param {number} speed - The speed at which it should move to the left, in pixels per second.
*/
moveLeft: function (speed) {
this.data.velocity[0] = this.world.pxmi(-speed);
},
/**
* If this Body is dynamic then this will move it to the right by setting its x velocity to the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveRight
* @param {number} speed - The speed at which it should move to the right, in pixels per second.
*/
moveRight: function (speed) {
this.data.velocity[0] = this.world.pxmi(speed);
},
/**
* If this Body is dynamic then this will move it up by setting its y velocity to the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveUp
* @param {number} speed - The speed at which it should move up, in pixels per second.
*/
moveUp: function (speed) {
this.data.velocity[1] = this.world.pxmi(-speed);
},
/**
* If this Body is dynamic then this will move it down by setting its y velocity to the given speed.
* The speed is represented in pixels per second. So a value of 100 would move 100 pixels in 1 second (1000ms).
*
* @method Phaser.Physics.P2.Body#moveDown
* @param {number} speed - The speed at which it should move down, in pixels per second.
*/
moveDown: function (speed) {
this.data.velocity[1] = this.world.pxmi(speed);
},
/**
* Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
*
* @method Phaser.Physics.P2.Body#preUpdate
* @protected
*/
preUpdate: function () {
if (this.removeNextStep)
{
this.removeFromWorld();
this.removeNextStep = false;
}
},
/**
* Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
*
* @method Phaser.Physics.P2.Body#postUpdate
* @protected
*/
postUpdate: function () {
this.sprite.x = this.world.mpxi(this.data.position[0]);
this.sprite.y = this.world.mpxi(this.data.position[1]);
if (!this.fixedRotation)
{
this.sprite.rotation = this.data.angle;
}
},
/**
* Resets the Body force, velocity (linear and angular) and rotation. Optionally resets damping and mass.
*
* @method Phaser.Physics.P2.Body#reset
* @param {number} x - The new x position of the Body.
* @param {number} y - The new x position of the Body.
* @param {boolean} [resetDamping=false] - Resets the linear and angular damping.
* @param {boolean} [resetMass=false] - Sets the Body mass back to 1.
*/
reset: function (x, y, resetDamping, resetMass) {
if (typeof resetDamping === 'undefined') { resetDamping = false; }
if (typeof resetMass === 'undefined') { resetMass = false; }
this.setZeroForce();
this.setZeroVelocity();
this.setZeroRotation();
if (resetDamping)
{
this.setZeroDamping();
}
if (resetMass)
{
this.mass = 1;
}
this.x = x;
this.y = y;
},
/**
* Adds this physics body to the world.
*
* @method Phaser.Physics.P2.Body#addToWorld
*/
addToWorld: function () {
if (this.data.world !== this.game.physics.p2.world)
{
this.game.physics.p2.addBody(this);
}
},
/**
* Removes this physics body from the world.
*
* @method Phaser.Physics.P2.Body#removeFromWorld
*/
removeFromWorld: function () {
if (this.data.world === this.game.physics.p2.world)
{
this.game.physics.p2.removeBodyNextStep(this);
}
},
/**
* Destroys this Body and all references it holds to other objects.
*
* @method Phaser.Physics.P2.Body#destroy
*/
destroy: function () {
this.removeFromWorld();
this.clearShapes();
this._bodyCallbacks = {};
this._bodyCallbackContext = {};
this._groupCallbacks = {};
this._groupCallbackContext = {};
if (this.debugBody)
{
this.debugBody.destroy();
}
this.debugBody = null;
this.sprite = null;
},
/**
* Removes all Shapes from this Body.
*
* @method Phaser.Physics.P2.Body#clearShapes
*/
clearShapes: function () {
var i = this.data.shapes.length;
while (i--)
{
this.data.removeShape(this.data.shapes[i]);
}
this.shapeChanged();
},
/**
* Add a shape to the body. You can pass a local transform when adding a shape, so that the shape gets an offset and an angle relative to the body center of mass.
* Will automatically update the mass properties and bounding radius.
*
* @method Phaser.Physics.P2.Body#addShape
* @param {p2.Shape} shape - The shape to add to the body.
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Shape} The shape that was added to the body.
*/
addShape: function (shape, offsetX, offsetY, rotation) {
if (typeof offsetX === 'undefined') { offsetX = 0; }
if (typeof offsetY === 'undefined') { offsetY = 0; }
if (typeof rotation === 'undefined') { rotation = 0; }
this.data.addShape(shape, [this.world.pxmi(offsetX), this.world.pxmi(offsetY)], rotation);
this.shapeChanged();
return shape;
},
/**
* Adds a Circle shape to this Body. You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addCircle
* @param {number} radius - The radius of this circle (in pixels)
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Circle} The Circle shape that was added to the Body.
*/
addCircle: function (radius, offsetX, offsetY, rotation) {
var shape = new p2.Circle(this.world.pxm(radius));
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Adds a Rectangle shape to this Body. You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addRectangle
* @param {number} width - The width of the rectangle in pixels.
* @param {number} height - The height of the rectangle in pixels.
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
*/
addRectangle: function (width, height, offsetX, offsetY, rotation) {
var shape = new p2.Rectangle(this.world.pxm(width), this.world.pxm(height));
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Adds a Plane shape to this Body. The plane is facing in the Y direction. You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addPlane
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Plane} The Plane shape that was added to the Body.
*/
addPlane: function (offsetX, offsetY, rotation) {
var shape = new p2.Plane();
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Adds a Particle shape to this Body. You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addParticle
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Particle} The Particle shape that was added to the Body.
*/
addParticle: function (offsetX, offsetY, rotation) {
var shape = new p2.Particle();
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Adds a Line shape to this Body.
* The line shape is along the x direction, and stretches from [-length/2, 0] to [length/2,0].
* You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addLine
* @param {number} length - The length of this line (in pixels)
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Line} The Line shape that was added to the Body.
*/
addLine: function (length, offsetX, offsetY, rotation) {
var shape = new p2.Line(this.world.pxm(length));
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Adds a Capsule shape to this Body.
* You can control the offset from the center of the body and the rotation.
*
* @method Phaser.Physics.P2.Body#addCapsule
* @param {number} length - The distance between the end points in pixels.
* @param {number} radius - Radius of the capsule in radians.
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Capsule} The Capsule shape that was added to the Body.
*/
addCapsule: function (length, radius, offsetX, offsetY, rotation) {
var shape = new p2.Capsule(this.world.pxm(length), radius);
return this.addShape(shape, offsetX, offsetY, rotation);
},
/**
* Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points. The shape must be simple and without holes.
* This function expects the x.y values to be given in pixels. If you want to provide them at p2 world scales then call Body.data.fromPolygon directly.
*
* @method Phaser.Physics.P2.Body#addPolygon
* @param {object} options - An object containing the build options:
* @param {boolean} [options.optimalDecomp=false] - Set to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
* @param {boolean} [options.skipSimpleCheck=false] - Set to true if you already know that the path is not intersecting itself.
* @param {boolean|number} [options.removeCollinearPoints=false] - Set to a number (angle threshold value) to remove collinear points, or false to keep all points.
* @param {(number[]|...number)} points - An array of 2d vectors that form the convex or concave polygon.
* Either [[0,0], [0,1],...] or a flat array of numbers that will be interpreted as [x,y, x,y, ...],
* or the arguments passed can be flat x,y values e.g. `setPolygon(options, x,y, x,y, x,y, ...)` where `x` and `y` are numbers.
* @return {boolean} True on success, else false.
*/
addPolygon: function (options, points) {
options = options || {};
points = Array.prototype.slice.call(arguments, 1);
var path = [];
// Did they pass in a single array of points?
if (points.length === 1 && Array.isArray(points[0]))
{
path = points[0].slice(0);
}
else if (Array.isArray(points[0]))
{
path = points[0].slice(0);
}
else if (typeof points[0] === 'number')
{
// We've a list of numbers
for (var i = 0, len = points.length; i &lt; len; i += 2)
{
path.push([points[i], points[i + 1]]);
}
}
// top and tail
var idx = path.length - 1;
if (path[idx][0] === path[0][0] && path[idx][1] === path[0][1])
{
path.pop();
}
// Now process them into p2 values
for (var p = 0; p &lt; path.length; p++)
{
path[p][0] = this.world.pxmi(path[p][0]);
path[p][1] = this.world.pxmi(path[p][1]);
}
var result = this.data.fromPolygon(path, options);
this.shapeChanged();
return result;
},
/**
* Remove a shape from the body. Will automatically update the mass properties and bounding radius.
*
* @method Phaser.Physics.P2.Body#removeShape
* @param {p2.Circle|p2.Rectangle|p2.Plane|p2.Line|p2.Particle} shape - The shape to remove from the body.
* @return {boolean} True if the shape was found and removed, else false.
*/
removeShape: function (shape) {
return this.data.removeShape(shape);
},
/**
* Clears any previously set shapes. Then creates a new Circle shape and adds it to this Body.
*
* @method Phaser.Physics.P2.Body#setCircle
* @param {number} radius - The radius of this circle (in pixels)
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
*/
setCircle: function (radius, offsetX, offsetY, rotation) {
this.clearShapes();
2014-04-10 22:13:43 +00:00
return this.addCircle(radius, offsetX, offsetY, rotation);
},
/**
* Clears any previously set shapes. The creates a new Rectangle shape at the given size and offset, and adds it to this Body.
* If you wish to create a Rectangle to match the size of a Sprite or Image see Body.setRectangleFromSprite.
*
* @method Phaser.Physics.P2.Body#setRectangle
* @param {number} [width=16] - The width of the rectangle in pixels.
* @param {number} [height=16] - The height of the rectangle in pixels.
* @param {number} [offsetX=0] - Local horizontal offset of the shape relative to the body center of mass.
* @param {number} [offsetY=0] - Local vertical offset of the shape relative to the body center of mass.
* @param {number} [rotation=0] - Local rotation of the shape relative to the body center of mass, specified in radians.
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
*/
setRectangle: function (width, height, offsetX, offsetY, rotation) {
if (typeof width === 'undefined') { width = 16; }
if (typeof height === 'undefined') { height = 16; }
this.clearShapes();
return this.addRectangle(width, height, offsetX, offsetY, rotation);
},
/**
* Clears any previously set shapes.
* Then creates a Rectangle shape sized to match the dimensions and orientation of the Sprite given.
* If no Sprite is given it defaults to using the parent of this Body.
*
* @method Phaser.Physics.P2.Body#setRectangleFromSprite
* @param {Phaser.Sprite|Phaser.Image} [sprite] - The Sprite on which the Rectangle will get its dimensions.
* @return {p2.Rectangle} The Rectangle shape that was added to the Body.
*/
setRectangleFromSprite: function (sprite) {
if (typeof sprite === 'undefined') { sprite = this.sprite; }
this.clearShapes();
return this.addRectangle(sprite.width, sprite.height, 0, 0, sprite.rotation);
},
/**
* Adds the given Material to all Shapes that belong to this Body.
* If you only wish to apply it to a specific Shape in this Body then provide that as the 2nd parameter.
*
* @method Phaser.Physics.P2.Body#setMaterial
* @param {Phaser.Physics.P2.Material} material - The Material that will be applied.
* @param {p2.Shape} [shape] - An optional Shape. If not provided the Material will be added to all Shapes in this Body.
*/
setMaterial: function (material, shape) {
if (typeof shape === 'undefined')
{
for (var i = this.data.shapes.length - 1; i >= 0; i--)
{
this.data.shapes[i].material = material;
}
}
else
{
shape.material = material;
}
},
/**
* Updates the debug draw if any body shapes change.
*
* @method Phaser.Physics.P2.Body#shapeChanged
*/
shapeChanged: function() {
if (this.debugBody)
{
this.debugBody.draw();
}
},
2014-03-24 12:41:45 +00:00
/**
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
* The shape data format is based on the custom phaser export in.
*
* @method Phaser.Physics.P2.Body#addPhaserPolygon
2014-03-24 12:41:45 +00:00
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
*/
addPhaserPolygon: function (key, object) {
var data = this.game.cache.getPhysicsData(key, object);
var createdFixtures = [];
// Cycle through the fixtures
for (var i = 0; i &lt; data.length; i++)
{
var fixtureData = data[i];
var shapesOfFixture = this.addFixture(fixtureData);
// Always add to a group
2014-03-24 12:41:45 +00:00
createdFixtures[fixtureData.filter.group] = createdFixtures[fixtureData.filter.group] || [];
createdFixtures[fixtureData.filter.group] = createdFixtures[fixtureData.filter.group].concat(shapesOfFixture);
// if (unique) fixture key is provided
if (fixtureData.fixtureKey)
{
createdFixtures[fixtureData.fixtureKey] = shapesOfFixture;
}
2014-03-24 12:41:45 +00:00
}
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
return createdFixtures;
},
2014-03-24 12:41:45 +00:00
/**
2014-04-29 14:39:02 +00:00
* Add a polygon fixture. This is used during #loadPolygon.
2014-03-24 12:41:45 +00:00
*
* @method Phaser.Physics.P2.Body#addFixture
2014-03-24 12:41:45 +00:00
* @param {string} fixtureData - The data for the fixture. It contains: isSensor, filter (collision) and the actual polygon shapes.
* @return {array} An array containing the generated shapes for the given polygon.
2014-03-24 12:41:45 +00:00
*/
addFixture: function (fixtureData) {
var generatedShapes = [];
if (fixtureData.circle)
{
var shape = new p2.Circle(this.world.pxm(fixtureData.circle.radius));
shape.collisionGroup = fixtureData.filter.categoryBits;
shape.collisionMask = fixtureData.filter.maskBits;
shape.sensor = fixtureData.isSensor;
2014-03-24 12:41:45 +00:00
var offset = p2.vec2.create();
offset[0] = this.world.pxmi(fixtureData.circle.position[0] - this.sprite.width/2);
offset[1] = this.world.pxmi(fixtureData.circle.position[1] - this.sprite.height/2);
2014-03-24 12:41:45 +00:00
this.data.addShape(shape, offset);
generatedShapes.push(shape);
2014-03-24 12:41:45 +00:00
}
else
{
var polygons = fixtureData.polygons;
2014-03-24 12:41:45 +00:00
var cm = p2.vec2.create();
for (var i = 0; i &lt; polygons.length; i++)
{
var shapes = polygons[i];
2014-03-24 12:41:45 +00:00
var vertices = [];
for (var s = 0; s &lt; shapes.length; s += 2)
{
vertices.push([ this.world.pxmi(shapes[s]), this.world.pxmi(shapes[s + 1]) ]);
}
var shape = new p2.Convex(vertices);
// Move all vertices so its center of mass is in the local center of the convex
for (var j = 0; j !== shape.vertices.length; j++)
{
var v = shape.vertices[j];
p2.vec2.sub(v, v, shape.centerOfMass);
}
p2.vec2.scale(cm, shape.centerOfMass, 1);
cm[0] -= this.world.pxmi(this.sprite.width / 2);
cm[1] -= this.world.pxmi(this.sprite.height / 2);
shape.updateTriangles();
shape.updateCenterOfMass();
shape.updateBoundingRadius();
shape.collisionGroup = fixtureData.filter.categoryBits;
shape.collisionMask = fixtureData.filter.maskBits;
shape.sensor = fixtureData.isSensor;
this.data.addShape(shape, cm);
generatedShapes.push(shape);
}
}
return generatedShapes;
},
/**
* Reads the shape data from a physics data file stored in the Game.Cache and adds it as a polygon to this Body.
*
* @method Phaser.Physics.P2.Body#loadPolygon
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
* @return {boolean} True on success, else false.
*/
2014-04-29 14:39:02 +00:00
loadPolygon: function (key, object) {
var data = this.game.cache.getPhysicsData(key, object);
2014-04-29 14:39:02 +00:00
// We've multiple Convex shapes, they should be CCW automatically
var cm = p2.vec2.create();
for (var i = 0; i &lt; data.length; i++)
{
2014-04-29 14:39:02 +00:00
var vertices = [];
2014-04-29 14:39:02 +00:00
for (var s = 0; s &lt; data[i].shape.length; s += 2)
{
2014-04-29 14:39:02 +00:00
vertices.push([ this.world.pxmi(data[i].shape[s]), this.world.pxmi(data[i].shape[s + 1]) ]);
}
2014-04-29 14:39:02 +00:00
var c = new p2.Convex(vertices);
2014-04-29 14:39:02 +00:00
// Move all vertices so its center of mass is in the local center of the convex
for (var j = 0; j !== c.vertices.length; j++)
{
2014-04-29 14:39:02 +00:00
var v = c.vertices[j];
p2.vec2.sub(v, v, c.centerOfMass);
}
2014-04-29 14:39:02 +00:00
p2.vec2.scale(cm, c.centerOfMass, 1);
2014-04-29 14:39:02 +00:00
cm[0] -= this.world.pxmi(this.sprite.width / 2);
cm[1] -= this.world.pxmi(this.sprite.height / 2);
2014-04-29 14:39:02 +00:00
c.updateTriangles();
c.updateCenterOfMass();
c.updateBoundingRadius();
this.data.addShape(c, cm);
}
2014-04-29 14:39:02 +00:00
this.data.aabbNeedsUpdate = true;
this.shapeChanged();
return true;
},
/**
2014-04-29 14:39:02 +00:00
* DEPRECATED: This method will soon be removed from the API. Please avoid using.
* Reads the physics data from a physics data file stored in the Game.Cache.
2014-04-29 14:39:02 +00:00
* It will add the shape data to this Body, as well as set the density (mass).
*
2014-04-29 14:39:02 +00:00
* @method Phaser.Physics.P2.Body#loadData
* @param {string} key - The key of the Physics Data file as stored in Game.Cache.
* @param {string} object - The key of the object within the Physics data file that you wish to load the shape data from.
* @return {boolean} True on success, else false.
*/
2014-04-29 14:39:02 +00:00
loadData: function (key, object) {
var data = this.game.cache.getPhysicsData(key, object);
if (data && data.shape)
{
this.mass = data.density;
2014-04-29 14:39:02 +00:00
return this.loadPolygon(key, object);
}
}
};
Phaser.Physics.P2.Body.prototype.constructor = Phaser.Physics.P2.Body;
2014-03-24 12:41:45 +00:00
/**
* Dynamic body.
* @property DYNAMIC
* @type {Number}
* @static
*/
Phaser.Physics.P2.Body.DYNAMIC = 1;
/**
* Static body.
* @property STATIC
* @type {Number}
* @static
*/
Phaser.Physics.P2.Body.STATIC = 2;
/**
* Kinematic body.
* @property KINEMATIC
* @type {Number}
* @static
*/
Phaser.Physics.P2.Body.KINEMATIC = 4;
/**
* @name Phaser.Physics.P2.Body#static
* @property {boolean} static - Returns true if the Body is static. Setting Body.static to 'false' will make it dynamic.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "static", {
get: function () {
2014-03-24 12:41:45 +00:00
return (this.data.motionState === Phaser.Physics.P2.Body.STATIC);
},
set: function (value) {
if (value && this.data.motionState !== Phaser.Physics.P2.Body.STATIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.STATIC;
this.mass = 0;
}
else if (!value && this.data.motionState === Phaser.Physics.P2.Body.STATIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.DYNAMIC;
if (this.mass === 0)
{
this.mass = 1;
}
}
}
});
/**
* @name Phaser.Physics.P2.Body#dynamic
* @property {boolean} dynamic - Returns true if the Body is dynamic. Setting Body.dynamic to 'false' will make it static.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "dynamic", {
get: function () {
2014-03-24 12:41:45 +00:00
return (this.data.motionState === Phaser.Physics.P2.Body.DYNAMIC);
},
set: function (value) {
if (value && this.data.motionState !== Phaser.Physics.P2.Body.DYNAMIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.DYNAMIC;
if (this.mass === 0)
{
this.mass = 1;
}
}
else if (!value && this.data.motionState === Phaser.Physics.P2.Body.DYNAMIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.STATIC;
this.mass = 0;
}
}
});
/**
* @name Phaser.Physics.P2.Body#kinematic
* @property {boolean} kinematic - Returns true if the Body is kinematic. Setting Body.kinematic to 'false' will make it static.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "kinematic", {
get: function () {
2014-03-24 12:41:45 +00:00
return (this.data.motionState === Phaser.Physics.P2.Body.KINEMATIC);
},
set: function (value) {
if (value && this.data.motionState !== Phaser.Physics.P2.Body.KINEMATIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.KINEMATIC;
this.mass = 4;
}
else if (!value && this.data.motionState === Phaser.Physics.P2.Body.KINEMATIC)
{
2014-03-24 12:41:45 +00:00
this.data.motionState = Phaser.Physics.P2.Body.STATIC;
this.mass = 0;
}
}
});
/**
* @name Phaser.Physics.P2.Body#allowSleep
* @property {boolean} allowSleep -
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "allowSleep", {
get: function () {
return this.data.allowSleep;
},
set: function (value) {
if (value !== this.data.allowSleep)
{
this.data.allowSleep = value;
}
}
});
/**
* The angle of the Body in degrees from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement Body.angle = 450 is the same as Body.angle = 90.
* If you wish to work in radians instead of degrees use the property Body.rotation instead. Working in radians is faster as it doesn't have to convert values.
*
* @name Phaser.Physics.P2.Body#angle
* @property {number} angle - The angle of this Body in degrees.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "angle", {
get: function() {
return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.data.angle));
},
set: function(value) {
this.data.angle = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value));
}
});
/**
* Damping is specified as a value between 0 and 1, which is the proportion of velocity lost per second.
* @name Phaser.Physics.P2.Body#angularDamping
* @property {number} angularDamping - The angular damping acting acting on the body.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "angularDamping", {
get: function () {
return this.data.angularDamping;
},
set: function (value) {
this.data.angularDamping = value;
}
});
/**
* @name Phaser.Physics.P2.Body#angularForce
* @property {number} angularForce - The angular force acting on the body.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "angularForce", {
get: function () {
return this.data.angularForce;
},
set: function (value) {
this.data.angularForce = value;
}
});
/**
* @name Phaser.Physics.P2.Body#angularVelocity
* @property {number} angularVelocity - The angular velocity of the body.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "angularVelocity", {
get: function () {
return this.data.angularVelocity;
},
set: function (value) {
this.data.angularVelocity = value;
}
});
/**
* Damping is specified as a value between 0 and 1, which is the proportion of velocity lost per second.
* @name Phaser.Physics.P2.Body#damping
* @property {number} damping - The linear damping acting on the body in the velocity direction.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "damping", {
get: function () {
return this.data.damping;
},
set: function (value) {
this.data.damping = value;
}
});
/**
* @name Phaser.Physics.P2.Body#fixedRotation
* @property {boolean} fixedRotation -
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "fixedRotation", {
get: function () {
return this.data.fixedRotation;
},
set: function (value) {
if (value !== this.data.fixedRotation)
{
this.data.fixedRotation = value;
}
}
});
/**
* @name Phaser.Physics.P2.Body#inertia
* @property {number} inertia - The inertia of the body around the Z axis..
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "inertia", {
get: function () {
return this.data.inertia;
},
set: function (value) {
this.data.inertia = value;
}
});
/**
* @name Phaser.Physics.P2.Body#mass
* @property {number} mass -
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "mass", {
get: function () {
return this.data.mass;
},
set: function (value) {
if (value !== this.data.mass)
{
this.data.mass = value;
this.data.updateMassProperties();
}
}
});
/**
* @name Phaser.Physics.P2.Body#motionState
* @property {number} motionState - The type of motion this body has. Should be one of: Body.STATIC (the body does not move), Body.DYNAMIC (body can move and respond to collisions) and Body.KINEMATIC (only moves according to its .velocity).
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "motionState", {
get: function () {
return this.data.motionState;
},
set: function (value) {
if (value !== this.data.motionState)
{
this.data.motionState = value;
}
}
});
/**
* The angle of the Body in radians.
* If you wish to work in degrees instead of radians use the Body.angle property instead. Working in radians is faster as it doesn't have to convert values.
*
* @name Phaser.Physics.P2.Body#rotation
* @property {number} rotation - The angle of this Body in radians.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "rotation", {
get: function() {
return this.data.angle;
},
set: function(value) {
this.data.angle = value;
}
});
/**
* @name Phaser.Physics.P2.Body#sleepSpeedLimit
* @property {number} sleepSpeedLimit - .
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "sleepSpeedLimit", {
get: function () {
return this.data.sleepSpeedLimit;
},
set: function (value) {
this.data.sleepSpeedLimit = value;
}
});
/**
* @name Phaser.Physics.P2.Body#x
* @property {number} x - The x coordinate of this Body.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "x", {
get: function () {
return this.world.mpxi(this.data.position[0]);
},
set: function (value) {
this.data.position[0] = this.world.pxmi(value);
}
});
/**
* @name Phaser.Physics.P2.Body#y
* @property {number} y - The y coordinate of this Body.
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "y", {
get: function () {
return this.world.mpxi(this.data.position[1]);
},
set: function (value) {
this.data.position[1] = this.world.pxmi(value);
}
});
/**
* @name Phaser.Physics.P2.Body#id
* @property {number} id - The Body ID. Each Body that has been added to the World has a unique ID.
* @readonly
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "id", {
get: function () {
return this.data.id;
}
});
/**
* @name Phaser.Physics.P2.Body#debug
* @property {boolean} debug - Enable or disable debug drawing of this body
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "debug", {
get: function () {
return (!this.debugBody);
},
set: function (value) {
if (value && !this.debugBody)
{
// This will be added to the global space
this.debugBody = new Phaser.Physics.P2.BodyDebug(this.game, this.data);
}
else if (!value && this.debugBody)
{
this.debugBody.destroy();
this.debugBody = null;
}
}
});
/**
* A Body can be set to collide against the World bounds automatically if this is set to true. Otherwise it will leave the World.
* Note that this only applies if your World has bounds! The response to the collision should be managed via CollisionMaterials.
2014-05-20 09:12:48 +00:00
*
* @name Phaser.Physics.P2.Body#collideWorldBounds
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
*/
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "collideWorldBounds", {
get: function () {
return this._collideWorldBounds;
},
set: function (value) {
if (value && !this._collideWorldBounds)
{
this._collideWorldBounds = true;
this.updateCollisionMask();
}
else if (!value && this._collideWorldBounds)
{
this._collideWorldBounds = false;
this.updateCollisionMask();
}
}
});
</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>
2014-05-20 09:12:48 +00:00
on Tue May 20 2014 10:05:49 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( {
2014-04-10 22:13:43 +00:00
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>