Integrating p2.js.

This commit is contained in:
photonstorm 2014-02-10 16:01:30 +00:00
parent 6f835d7696
commit 47e23096bd
12 changed files with 11071 additions and 18 deletions

View file

@ -48,11 +48,16 @@
<script src="$path/src/pixi/core/Circle.js"></script> <script src="$path/src/pixi/core/Circle.js"></script>
<script src="$path/src/pixi/utils/Detector.js"></script> <script src="$path/src/pixi/utils/Detector.js"></script>
<script src="$path/src/physics/arcade/SAT.js"></script>
<script src="$path/src/physics/arcade/ArcadePhysics.js"></script>
<script src="$path/src/physics/arcade/Body.js"></script>
*/ */
echo <<<EOL echo <<<EOL
<script src="$path/build/p2.js"></script>
<script src="$path/src/Intro.js"></script> <script src="$path/src/Intro.js"></script>
<script src="$path/src/pixi/Pixi.js"></script> <script src="$path/src/pixi/Pixi.js"></script>
<script src="$path/src/Phaser.js"></script> <script src="$path/src/Phaser.js"></script>
@ -173,9 +178,8 @@
<script src="$path/src/utils/Debug.js"></script> <script src="$path/src/utils/Debug.js"></script>
<script src="$path/src/utils/Color.js"></script> <script src="$path/src/utils/Color.js"></script>
<script src="$path/src/physics/arcade/SAT.js"></script> <script src="$path/src/physics/World.js"></script>
<script src="$path/src/physics/arcade/ArcadePhysics.js"></script> <script src="$path/src/physics/Body.js"></script>
<script src="$path/src/physics/arcade/Body.js"></script>
<script src="$path/src/particles/Particles.js"></script> <script src="$path/src/particles/Particles.js"></script>
<script src="$path/src/particles/arcade/ArcadeParticles.js"></script> <script src="$path/src/particles/arcade/ArcadeParticles.js"></script>

10013
build/p2.js Normal file

File diff suppressed because it is too large Load diff

View file

@ -75,6 +75,7 @@
<html> <html>
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<meta name="viewport" content="initial-scale=1 maximum-scale=1 user-scalable=0 minimal-ui" />
<title>phaser</title> <title>phaser</title>
<base href="../"></base> <base href="../"></base>
<script src="_site/js/jquery-2.0.3.min.js" type="text/javascript"></script> <script src="_site/js/jquery-2.0.3.min.js" type="text/javascript"></script>

130
examples/wip/p21.js Normal file
View file

@ -0,0 +1,130 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }, false);
function preload() {
game.load.image('box', 'assets/sprites/block.png');
}
var world;
var box;
var box2;
var boxShape;
var boxBody;
var boxShape2;
var boxBody2;
var planeShape;
var planeBody;
function p2px(v) {
return v *= -20;
}
function px2p(v) {
return v * -0.05;
}
function create() {
game.renderer.roundPixels = true;
box = game.add.image(300, 200, 'box');
box.anchor.set(0.5);
box2 = game.add.image(300, 10, 'box');
box2.anchor.set(0.5);
world = new p2.World();
boxShape = new p2.Rectangle(px2p(box.width), px2p(box.height));
boxBody = new p2.Body({ mass: 1, position:[px2p(box.x), px2p(box.y)], angularVelocity: 1 });
boxBody.addShape(boxShape);
boxShape2 = new p2.Rectangle(px2p(box2.width), px2p(box2.height));
boxBody2 = new p2.Body({ mass: 1, position:[px2p(box2.x), px2p(box2.y)], angularVelocity: 1 });
boxBody2.addShape(boxShape2);
world.addBody(boxBody);
world.addBody(boxBody2);
// Add a plane
planeShape = new p2.Plane();
planeBody = new p2.Body({ mass: 0, position:[0, px2p(550)] });
// planeBody = new p2.Body();
planeBody.addShape(planeShape);
world.addBody(planeBody);
}
function update() {
// Move physics bodies forward in time
world.step(1/60);
box.x = p2px(boxBody.position[0]);
box.y = p2px(boxBody.position[1]);
box.rotation = boxBody.angle; // Rotate to the box body frame
box2.x = p2px(boxBody2.position[0]);
box2.y = p2px(boxBody2.position[1]);
box2.rotation = boxBody2.angle; // Rotate to the box body frame
}
function render() {
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);
// drawbox();
}
function drawbox() {
var ctx = game.context;
ctx.save();
ctx.translate(game.width/2, game.height/2); // Translate to the center
ctx.scale(50, -50); // Zoom in and flip y axis
ctx.lineWidth = 0.05;
ctx.strokeStyle = 'rgb(255,255,255)';
ctx.beginPath();
var x = boxBody.position[0],
y = boxBody.position[1];
ctx.save();
ctx.translate(x, y); // Translate to the center of the box
ctx.rotate(boxBody.angle); // Rotate to the box body frame
ctx.rect(-boxShape.width/2, -boxShape.height/2, boxShape.width, boxShape.height);
ctx.stroke();
ctx.closePath();
// ctx.restore();
// ctx.save();
// ctx.translate(game.width/2, game.height/2); // Translate to the center
// ctx.scale(50, -50); // Zoom in and flip y axis
// ctx.lineWidth = 0.05;
// ctx.strokeStyle = 'rgb(255,255,255)';
// ctx.beginPath();
// var y = planeBody.position[1];
// ctx.rotate(0); // Rotate to the box body frame
// ctx.moveTo(-game.width, y);
// ctx.lineTo( game.width, y);
// ctx.stroke();
// ctx.closePath();
ctx.restore();
}

94
examples/wip/p22.js Normal file
View file

@ -0,0 +1,94 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('box', 'assets/sprites/block.png');
}
var box;
var box2;
function p2px(v) {
return v *= -20;
}
function px2p(v) {
return v * -0.05;
}
function create() {
box = game.add.sprite(200, 0, 'box');
box2 = game.add.sprite(400, 0, 'box');
box2.physicsEnabled = true;
box2.body.mass = 2;
/*
// Add a plane
planeShape = new p2.Plane();
planeBody = new p2.Body({ mass: 0, position:[0, px2p(550)] });
// planeBody = new p2.Body();
planeBody.addShape(planeShape);
world.addBody(planeBody);
*/
}
function update() {
}
function render() {
// game.debug.renderText('x: ' + p2px(boxBody.position[0]), 32, 32);
// game.debug.renderText('y: ' + p2px(boxBody.position[1]), 32, 64);
// game.debug.renderText('r: ' + boxBody.angle, 32, 96);
// drawbox();
}
function drawbox() {
var ctx = game.context;
ctx.save();
ctx.translate(game.width/2, game.height/2); // Translate to the center
ctx.scale(50, -50); // Zoom in and flip y axis
ctx.lineWidth = 0.05;
ctx.strokeStyle = 'rgb(255,255,255)';
ctx.beginPath();
var x = boxBody.position[0],
y = boxBody.position[1];
ctx.save();
ctx.translate(x, y); // Translate to the center of the box
ctx.rotate(boxBody.angle); // Rotate to the box body frame
ctx.rect(-boxShape.width/2, -boxShape.height/2, boxShape.width, boxShape.height);
ctx.stroke();
ctx.closePath();
// ctx.restore();
// ctx.save();
// ctx.translate(game.width/2, game.height/2); // Translate to the center
// ctx.scale(50, -50); // Zoom in and flip y axis
// ctx.lineWidth = 0.05;
// ctx.strokeStyle = 'rgb(255,255,255)';
// ctx.beginPath();
// var y = planeBody.position[1];
// ctx.rotate(0); // Rotate to the box body frame
// ctx.moveTo(-game.width, y);
// ctx.lineTo( game.width, y);
// ctx.stroke();
// ctx.closePath();
ctx.restore();
}

View file

@ -16,9 +16,7 @@ function create() {
game.stage.backgroundColor = '#ff5500'; game.stage.backgroundColor = '#ff5500';
game.renderer.useFillRect = false; sprite = game.add.sprite(0, -200, 'pic');
sprite = game.add.sprite(0.5, 0, 'pic');
// g = game.add.group(null, 'billy'); // g = game.add.group(null, 'billy');

View file

@ -42,6 +42,10 @@ var Phaser = Phaser || {
UP: 3, UP: 3,
DOWN: 4, DOWN: 4,
DYNAMIC: 1,
STATIC: 2,
KINEMATIC: 4,
CANVAS_PX_ROUND: false, CANVAS_PX_ROUND: false,
CANVAS_CLEAR_RECT: true CANVAS_CLEAR_RECT: true

View file

@ -179,7 +179,8 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
this.world = null; this.world = null;
/** /**
* @property {Phaser.Physics.PhysicsManager} physics - Reference to the physics manager. * // {Phaser.Physics.Arcade.ArcadePhysics} physics - Reference to the physics manager.
* @property {Phaser.Physics.World} physics - Reference to the physics world.
* @default * @default
*/ */
this.physics = null; this.physics = null;
@ -446,7 +447,8 @@ Phaser.Game.prototype = {
this.tweens = new Phaser.TweenManager(this); this.tweens = new Phaser.TweenManager(this);
this.input = new Phaser.Input(this); this.input = new Phaser.Input(this);
this.sound = new Phaser.SoundManager(this); this.sound = new Phaser.SoundManager(this);
this.physics = new Phaser.Physics.Arcade(this); // this.physics = new Phaser.Physics.Arcade(this);
this.physics = new Phaser.Physics.World(this);
this.particles = new Phaser.Particles(this); this.particles = new Phaser.Particles(this);
this.plugins = new Phaser.PluginManager(this, this); this.plugins = new Phaser.PluginManager(this, this);
this.net = new Phaser.Net(this); this.net = new Phaser.Net(this);
@ -614,6 +616,7 @@ Phaser.Game.prototype = {
this.tweens.update(); this.tweens.update();
this.sound.update(); this.sound.update();
this.input.update(); this.input.update();
this.physics.update();
this.state.update(); this.state.update();
this.world.update(); this.world.update();
this.particles.update(); this.particles.update();

View file

@ -112,9 +112,9 @@ Phaser.Sprite = function (game, x, y, key, frame) {
this.input = null; this.input = null;
/** /**
* @property {Phaser.Physics.Arcade.Body} body - By default Sprites have a Phaser.Physics Body attached to them. You can operate physics actions via this property, or null it to skip all physics updates. * @property {Phaser.Physics.Body|null} body - The Sprites physics Body. Will be null unless physics has been enabled via `Sprite.physicsEnabled = true`.
*/ */
this.body = new Phaser.Physics.Arcade.Body(this); this.body = null;
/** /**
* @property {number} health - Health value. Used in combination with damage() to allow for quick killing of Sprites. * @property {number} health - Health value. Used in combination with damage() to allow for quick killing of Sprites.
@ -193,13 +193,13 @@ Phaser.Sprite.prototype.preUpdate = function() {
this._cache[2] = this.rotation; this._cache[2] = this.rotation;
this._cache[4] = 0; this._cache[4] = 0;
if (this.body) // if (this.body)
{ // {
this.body.x = (this.world.x - (this.anchor.x * this.width)) + this.body.offset.x; // this.body.x = (this.world.x - (this.anchor.x * this.width)) + this.body.offset.x;
this.body.y = (this.world.y - (this.anchor.y * this.height)) + this.body.offset.y; // this.body.y = (this.world.y - (this.anchor.y * this.height)) + this.body.offset.y;
this.body.preX = this.body.x; // this.body.preX = this.body.x;
this.body.preY = this.body.y; // this.body.preY = this.body.y;
} // }
return false; return false;
} }
@ -633,7 +633,7 @@ Phaser.Sprite.prototype.play = function (name, frameRate, loop, killOnComplete)
* If you wish to work in radians instead of degrees use the property Sprite.rotation instead. Working in radians is also a little faster as it doesn't have to convert the angle. * If you wish to work in radians instead of degrees use the property Sprite.rotation instead. Working in radians is also a little faster as it doesn't have to convert the angle.
* *
* @name Phaser.Sprite#angle * @name Phaser.Sprite#angle
* @property {number} angle - The angle of this Image in degrees. * @property {number} angle - The angle of this Sprite in degrees.
*/ */
Object.defineProperty(Phaser.Sprite.prototype, "angle", { Object.defineProperty(Phaser.Sprite.prototype, "angle", {
@ -818,3 +818,38 @@ Object.defineProperty(Phaser.Sprite.prototype, "inputEnabled", {
} }
}); });
/**
* By default Sprites won't add themselves to the physics world. By setting physicsEnabled to true a Physics Body is
* attached to this Sprite and it will then start to process physics world updates. Access all of its properties via Sprite.body.
*
* @name Phaser.Sprite#physicsEnabled
* @property {boolean} physicsEnabled - Set to true to add this Sprite to the physics world. Set to false to destroy the body and remove it from the physics world.
*/
Object.defineProperty(Phaser.Sprite.prototype, "physicsEnabled", {
get: function () {
return (this.body !== null);
},
set: function (value) {
if (value)
{
if (this.body === null)
{
this.body = new Phaser.Physics.Body(this);
}
}
else
{
if (this.body)
{
this.body.destroy();
}
}
}
});

View file

@ -1280,6 +1280,28 @@ Phaser.Math = {
}, },
/**
* Convert p2 physics value to pixel scale.
*
* @method Phaser.Math#p2px
* @param {number} v - The value to convert.
* @return {number} The scaled value.
*/
p2px: function (v) {
return v *= -20;
},
/**
* Convert pixel value to p2 physics scale.
*
* @method Phaser.Math#px2p
* @param {number} v - The value to convert.
* @return {number} The scaled value.
*/
px2p: function (v) {
return v * -0.05;
},
/** /**
* Convert degrees to radians. * Convert degrees to radians.
* *

710
src/physics/Body.js Normal file
View file

@ -0,0 +1,710 @@
/**
* @author Richard Davey <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 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.
*
* @class Phaser.Physics.Body
* @classdesc Physics Body Constructor
* @constructor
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
*/
Phaser.Physics.Body = function (sprite) {
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
*/
this.sprite = sprite;
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = sprite.game;
/**
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
*/
this.offset = new Phaser.Point();
this.shape = null;
// this.data = new p2.Body({ mass: 0, position:[this.px2p(sprite.x), this.px2p(sprite.y)], angularVelocity: 1 });
this.data = new p2.Body({ position:[this.px2p(sprite.x), this.px2p(sprite.y)] });
/**
* @property {number} _sx - Internal cache var.
* @private
*/
this._sx = sprite.scale.x;
/**
* @property {number} _sy - Internal cache var.
* @private
*/
this._sy = sprite.scale.y;
// Set-up the default shape
this.setRectangle(sprite.width, sprite.height, 0, 0);
this.game.physics.addBody(this.data);
// Set-up contact events
// this.sprite.events.onBeginContact = new Phaser.Signal();
// this.sprite.events.onEndContact = new Phaser.Signal();
};
Phaser.Physics.Body.prototype = {
/*
* Add a shape to the body. You can pass a local transform when adding a shape,
* so that the shape gets an offset and angle relative to the body center of mass.
* Will automatically update the mass properties and bounding radius.
*
* @method Phaser.Physics.Body#addShape
*/
addShape: function (shape, offsetX, offsetY, angle) {
if (typeof offsetX === 'undefined') { offsetX = 0; }
if (typeof offsetY === 'undefined') { offsetY = 0; }
if (typeof angle === 'undefined') { angle = 0; }
return this.data.addShape(shape, [ this.px2p(offsetX), this.px2p(offsetY) ], angle);
},
/**
* Moves the shape offsets so their center of mass becomes the body center of mass.
*
* @method Phaser.Physics.Body#adjustCenterOfMass
*/
adjustCenterOfMass: function () {
this.data.adjustCenterOfMass();
},
// applyDamping
// applyForce
// fromPolygon
/**
* Remove a shape from the Body.
*
* @method Phaser.Physics.Body#removeShape
*/
removeShape: function (shape) {
return this.data.removeShape(shape);
},
/**
* Sets the force on the body to zero.
*
* @method Phaser.Physics.Body#setZeroForce
*/
setZeroForce: function () {
this.data.setZeroForce();
},
// toLocalFrame
// toWorldFrame
/**
* Internal method that updates the Body scale in relation to the parent Sprite.
*
* @method Phaser.Physics.Body#updateScale
* @protected
*/
updateScale: function () {
// if (this.polygon)
// {
// this.polygon.scale(this.sprite.scale.x / this._sx, this.sprite.scale.y / this._sy);
// }
// else
// {
// this.shape.r *= Math.max(this.sprite.scale.x, this.sprite.scale.y);
// }
this._sx = this.sprite.scale.x;
this._sy = this.sprite.scale.y;
},
/**
* Internal method that updates the Body position in relation to the parent Sprite.
*
* @method Phaser.Physics.Body#preUpdate
* @protected
*/
preUpdate: function () {
// this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x;
// this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y;
// This covers any motion that happens during this frame, not since the last frame
// this.preX = this.x;
// this.preY = this.y;
// this.preRotation = this.sprite.angle;
// this.rotation = this.preRotation;
if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy)
{
this.updateScale();
}
},
/**
* Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
*
* @method Phaser.Physics.Body#postUpdate
* @protected
*/
postUpdate: function () {
/*
if (this.moves)
{
this.game.physics.checkBounds(this);
this.reboundCheck(true, true, true);
this._dx = this.deltaX();
this._dy = this.deltaY();
if (this._dx < 0)
{
this.facing = Phaser.LEFT;
}
else if (this._dx > 0)
{
this.facing = Phaser.RIGHT;
}
if (this._dy < 0)
{
this.facing = Phaser.UP;
}
else if (this._dy > 0)
{
this.facing = Phaser.DOWN;
}
if (this._dx !== 0 || this._dy !== 0)
{
this.sprite.x += this._dx;
this.sprite.y += this._dy;
}
if (this.allowRotation && this.deltaZ() !== 0)
{
this.sprite.angle += this.deltaZ();
}
if (this.sprite.scale.x !== this._sx || this.sprite.scale.y !== this._sy)
{
this.updateScale();
}
}
*/
this.sprite.x = this.p2px(this.data.position[0]);
this.sprite.y = this.p2px(this.data.position[1]);
this.sprite.rotation = this.data.angle;
},
/**
* Resets the Body motion values: velocity, acceleration, angularVelocity and angularAcceleration.
* Also resets the forces to defaults: gravity, bounce, minVelocity,maxVelocity, angularDrag, maxAngular, mass, friction and checkCollision if 'full' specified.
*
* @method Phaser.Physics.Body#reset
* @param {boolean} [full=false] - A full reset clears down settings you may have set, such as gravity, bounce and drag. A non-full reset just clears motion values.
*/
reset: function (full) {
/*
if (typeof full === 'undefined') { full = false; }
if (full)
{
this.gravity.setTo(0, 0);
this.bounce.setTo(0, 0);
this.minVelocity.setTo(5, 5);
this.maxVelocity.setTo(1000, 1000);
this.angularDrag = 0;
this.maxAngular = 1000;
this.mass = 1;
this.friction = 0.0;
this.checkCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
}
this.velocity.setTo(0, 0);
this.acceleration.setTo(0, 0);
this.angularVelocity = 0;
this.angularAcceleration = 0;
this.blocked = { x: 0, y: 0, up: false, down: false, left: false, right: false };
this.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.offset.x;
this.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.offset.y;
this.preX = this.x;
this.preY = this.y;
this.updateBounds();
this.contacts.length = 0;
*/
},
/**
* Destroys this Body and all references it holds to other objects.
*
* @method Phaser.Physics.Body#destroy
*/
destroy: function () {
this.sprite = null;
/*
this.collideCallback = null;
this.collideCallbackContext = null;
this.customSeparateCallback = null;
this.customSeparateContext = null;
this.contacts.length = 0;
*/
},
/**
* Sets this Body to use a circle of the given radius for all collision.
* The Circle will be centered on the center of the Sprite by default, but can be adjusted via the Body.offset property and the setCircle x/y parameters.
*
* @method Phaser.Physics.Body#setCircle
* @param {number} radius - The radius of this circle (in pixels)
* @param {number} [offsetX=0] - The x amount the circle will be offset from the Sprites center.
* @param {number} [offsetY=0] - The y amount the circle will be offset from the Sprites center.
*/
setCircle: function (radius, offsetX, offsetY) {
// if (typeof offsetX === 'undefined') { offsetX = this.sprite._cache.halfWidth; }
// if (typeof offsetY === 'undefined') { offsetY = this.sprite._cache.halfHeight; }
// this.type = Phaser.Physics.Arcade.CIRCLE;
// this.shape = new SAT.Circle(new SAT.Vector(this.sprite.x, this.sprite.y), radius);
// this.polygon = null;
// this.offset.setTo(offsetX, offsetY);
},
/**
* Sets this Body to use a rectangle for all collision.
* If you don't specify any parameters it will be sized to match the parent Sprites current width and height (including scale factor) and centered on the sprite.
*
* @method Phaser.Physics.Body#setRectangle
* @param {number} [width] - The width of the rectangle. If not specified it will default to the width of the parent Sprite.
* @param {number} [height] - The height of the rectangle. If not specified it will default to the height of the parent Sprite.
* @param {number} [translateX] - The x amount the rectangle will be translated from the Sprites center.
* @param {number} [translateY] - The y amount the rectangle will be translated from the Sprites center.
*/
setRectangle: function (width, height, offsetX, offsetY) {
if (typeof width === 'undefined') { width = this.sprite.width; }
if (typeof height === 'undefined') { height = this.sprite.height; }
// if (typeof translateX === 'undefined') { translateX = this.sprite.width / 2; }
// if (typeof translateY === 'undefined') { translateY = this.sprite.height / 2; }
// This means 1 shape per body, need to move this to an array or similar
this.shape = new p2.Rectangle(this.px2p(width), this.px2p(height));
this.data.addShape(this.shape);
this.offset.setTo(0, 0);
},
/**
* Sets this Body to use a convex polygon for collision.
* The points are specified in a counter-clockwise direction and must create a convex polygon.
* Use Body.translate and/or Body.offset to re-position the polygon from the Sprite origin.
*
* @method Phaser.Physics.Body#setPolygon
* @param {(SAT.Vector[]|number[]|...SAT.Vector|...number)} points - This can be an array of Vectors that form the polygon,
* a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be
* all the points of the polygon e.g. `setPolygon(new SAT.Vector(), new SAT.Vector(), ...)`, or the
* arguments passed can be flat x,y values e.g. `setPolygon(x,y, x,y, x,y, ...)` where `x` and `y` are Numbers.
*/
setPolygon: function (points) {
/*
this.type = Phaser.Physics.Arcade.POLYGON;
this.shape = null;
if (!Array.isArray(points))
{
points = Array.prototype.slice.call(arguments);
}
if (typeof points[0] === 'number')
{
var p = [];
for (var i = 0, len = points.length; i < len; i += 2)
{
p.push(new SAT.Vector(points[i], points[i + 1]));
}
points = p;
}
this.polygon = new SAT.Polygon(new SAT.Vector(this.sprite.center.x, this.sprite.center.y), points);
this.offset.setTo(0, 0);
*/
},
/**
* Convert p2 physics value to pixel scale.
*
* @method Phaser.Math#p2px
* @param {number} v - The value to convert.
* @return {number} The scaled value.
*/
p2px: function (v) {
return v *= -20;
},
/**
* Convert pixel value to p2 physics scale.
*
* @method Phaser.Math#px2p
* @param {number} v - The value to convert.
* @return {number} The scaled value.
*/
px2p: function (v) {
return v * -0.05;
}
};
Phaser.Physics.Body.prototype.constructor = Phaser.Physics.Body;
/**
* @name Phaser.Physics.Body#allowSleep
* @property {boolean} allowSleep -
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "allowSleep", {
get: function () {
return this.data.allowSleep;
},
set: function (value) {
if (value !== this.data.allowSleep)
{
this.data.allowSleep = value;
}
}
});
/**
* The angle of the Body in degrees from its original orientation. Values from 0 to 180 represent clockwise rotation; values from 0 to -180 represent counterclockwise rotation.
* Values outside this range are added to or subtracted from 360 to obtain a value within the range. For example, the statement Body.angle = 450 is the same as Body.angle = 90.
* If you wish to work in radians instead of degrees use the property Body.rotation instead. Working in radians is faster as it doesn't have to convert values.
*
* @name Phaser.Physics.Body#angle
* @property {number} angle - The angle of this Body in degrees.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "angle", {
get: function() {
return Phaser.Math.wrapAngle(Phaser.Math.radToDeg(this.data.angle));
},
set: function(value) {
this.data.angle = Phaser.Math.degToRad(Phaser.Math.wrapAngle(value));
}
});
/**
* Damping is specified as a value between 0 and 1, which is the proportion of velocity lost per second.
* @name Phaser.Physics.Body#angularDamping
* @property {number} angularDamping - The angular damping acting acting on the body.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "angularDamping", {
get: function () {
this.data.angularDamping;
},
set: function (value) {
this.data.angularDamping = value;
}
});
/**
* @name Phaser.Physics.Body#angularForce
* @property {number} angularForce - The angular force acting on the body.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "angularForce", {
get: function () {
this.data.angularForce;
},
set: function (value) {
this.data.angularForce = value;
}
});
/**
* @name Phaser.Physics.Body#angularVelocity
* @property {number} angularVelocity - The angular velocity of the body.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "angularVelocity", {
get: function () {
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.Body#damping
* @property {number} damping - The linear damping acting on the body in the velocity direction.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "damping", {
get: function () {
this.data.damping;
},
set: function (value) {
this.data.damping = value;
}
});
/**
* @name Phaser.Physics.Body#fixedRotation
* @property {boolean} fixedRotation -
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "fixedRotation", {
get: function () {
return this.data.fixedRotation;
},
set: function (value) {
if (value !== this.data.fixedRotation)
{
this.data.fixedRotation = value;
// update anything?
}
}
});
// force
/**
* @name Phaser.Physics.Body#inertia
* @property {number} inertia - The inertia of the body around the Z axis..
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "inertia", {
get: function () {
this.data.inertia;
},
set: function (value) {
this.data.inertia = value;
}
});
/**
* @name Phaser.Physics.Body#mass
* @property {number} mass -
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "mass", {
get: function () {
return this.data.mass;
},
set: function (value) {
if (value !== this.data.mass)
{
this.data.mass = value;
this.data.updateMassProperties();
}
}
});
/**
* @name Phaser.Physics.Body#motionState
* @property {number} motionState - The type of motion this body has. Should be one of: Body.STATIC (the body does not move), Body.DYNAMIC (body can move and respond to collisions) and Body.KINEMATIC (only moves according to its .velocity).
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "motionState", {
get: function () {
return this.data.motionState;
},
set: function (value) {
if (value !== this.data.motionState)
{
this.data.motionState = value;
// update?
}
}
});
/**
* The angle of the Body in radians.
* If you wish to work in degrees instead of radians use the Body.angle property instead. Working in radians is faster as it doesn't have to convert values.
*
* @name Phaser.Physics.Body#rotation
* @property {number} rotation - The angle of this Body in radians.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "rotation", {
get: function() {
return this.data.angle;
},
set: function(value) {
this.data.angle = value;
}
});
/**
* @name Phaser.Physics.Body#sleepSpeedLimit
* @property {number} sleepSpeedLimit - .
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "sleepSpeedLimit", {
get: function () {
this.data.sleepSpeedLimit;
},
set: function (value) {
this.data.sleepSpeedLimit = value;
}
});
// velocity
/**
* @name Phaser.Physics.Body#x
* @property {number} x - The x coordinate of this Body.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "x", {
get: function () {
this.p2px(this.data.position[0]);
},
set: function (value) {
this.data.position[0] = this.px2p(value);
}
});
/**
* @name Phaser.Physics.Body#y
* @property {number} y - The y coordinate of this Body.
*/
Object.defineProperty(Phaser.Physics.Body.prototype, "y", {
get: function () {
this.p2px(this.data.position[1]);
},
set: function (value) {
this.data.position[1] = this.px2p(value);
}
});

39
src/physics/World.js Normal file
View file

@ -0,0 +1,39 @@
/**
* @author Richard Davey <rich@photonstorm.com>
* @copyright 2014 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* @class Phaser.Physics
*/
Phaser.Physics = {};
/**
* @class Phaser.Physics.World
* @classdesc Physics World Constructor
* @constructor
* @param {Phaser.Game}
*/
Phaser.Physics.World = function (game) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
p2.World.call(this);
};
Phaser.Physics.World.prototype = Object.create(p2.World.prototype);
Phaser.Physics.World.prototype.constructor = Phaser.Physics.World;
/**
* @method Phaser.Physics.World.prototype.update
*/
Phaser.Physics.World.prototype.update = function () {
this.step(1 / 60);
};