From c6cc2c9d7122bc40cdc3fbe8c244568d760b8984 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 12 Feb 2014 05:34:31 +0000 Subject: [PATCH] Updated p2.js to latest build. Checked tests. Added Debug draw (needs rotation support). --- build/p2.js | 18 +++++++- examples/wip/p26.js | 1 + examples/wip/p27.js | 86 +++++++++++++++++++++++++++++++++++++++ src/gameobjects/Sprite.js | 9 +++- src/physics/Body.js | 29 +++++++++++-- src/utils/Debug.js | 40 ++++++++++++++++++ 6 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 examples/wip/p27.js diff --git a/build/p2.js b/build/p2.js index 6abd4da4..e558d094 100644 --- a/build/p2.js +++ b/build/p2.js @@ -6740,7 +6740,7 @@ Body.prototype.updateAABB = function() { angle = shapeAngles[i] + this.angle; // Get shape world offset - vec2.rotate(offset,shapeOffsets[i],angle); + vec2.rotate(offset,shapeOffsets[i],this.angle); vec2.add(offset,offset,this.position); // Get shape AABB @@ -9101,6 +9101,18 @@ function World(options){ bodyB : null, }; + /** + * Fired after the Broadphase has collected collision pairs in the world. + * Inside the event handler, you can modify the pairs array as you like, to + * prevent collisions between objects that you don't want. + * @event postBroadphase + * @param {Array} pairs An array of collision pairs. If this array is [body1,body2,body3,body4], then the body pairs 1,2 and 3,4 would advance to narrowphase. + */ + this.postBroadphaseEvent = { + type:"postBroadphase", + pairs:null, + }; + /** * Enable / disable automatic body sleeping * @property allowSleep @@ -9295,6 +9307,10 @@ World.prototype.internalStep = function(dt){ // Broadphase var result = broadphase.getCollisionPairs(this); + // postBroadphase event + this.postBroadphaseEvent.pairs = result; + this.emit(this.postBroadphaseEvent); + // Narrowphase np.reset(this); for(var i=0, Nresults=result.length; i!==Nresults; i+=2){ diff --git a/examples/wip/p26.js b/examples/wip/p26.js index 2682f70a..1105cffc 100644 --- a/examples/wip/p26.js +++ b/examples/wip/p26.js @@ -26,6 +26,7 @@ function create() { // Works // box.body.setPolygon({}, -100, 100, -100, 0, 100, 0, 100, 100, 50, 50); + // Works box.body.setPolygon({}, -1, 1, -1, 0, 1, 0, 1, 1, 0.5, 0.5); diff --git a/examples/wip/p27.js b/examples/wip/p27.js new file mode 100644 index 00000000..c354f6ed --- /dev/null +++ b/examples/wip/p27.js @@ -0,0 +1,86 @@ + +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 bmd; +var move = false; +var start = 0; +var end = 0; + +function create() { + + bmd = game.add.bitmapData(800, 600); + game.add.image(0, 0, bmd); + + box = game.add.sprite(300, 300, 'box'); + // box.anchor.set(0.5); + + box.physicsEnabled = true; + + // box.body.rotateLeft(10); + + // 95x95 + // box.body.setRectangle(64, 64); + + // box.body.setRectangle(64, 64, 95/2,95/2); + + // Works + // box.body.setPolygon({}, [ [-1, 1], [-1, 0], [1, 0], [1, 1], [0.5, 0.5] ]); + + // Works + // box.body.setPolygon({}, [-1, 1], [-1, 0], [1, 0], [1, 1], [0.5, 0.5]); + + // Works + // box.body.setPolygon({}, -100, 100, -100, 0, 100, 0, 100, 100, 50, 50); + + // Works + // box.body.setPolygon({}, -1, 1, -1, 0, 1, 0, 1, 1, 0.5, 0.5); + + box.body.setZeroDamping(); + + game.input.onDown.addOnce(startTiming, this); + +} + +function startTiming() { + + start = game.time.now; + end = start + 1000; + move = true; + +} + +function update() { + + if (move) + { + box.body.moveLeft(100); + + // if (game.time.now >= end) + // { + // move = false; + // var duration = game.time.now - start; + // console.log('Test over. Distance: ', box.x, 'duration', duration); + // } + } + else + { + box.body.setZeroVelocity(); + } + +} + +function render() { + + game.debug.renderShape(box.body, 0); + + // game.debug.renderText('x: ' + box.body.velocity.x, 32, 32); + // game.debug.renderText('y: ' + box.body.velocity.y, 32, 64); + +} diff --git a/src/gameobjects/Sprite.js b/src/gameobjects/Sprite.js index 66628bd2..0231da20 100644 --- a/src/gameobjects/Sprite.js +++ b/src/gameobjects/Sprite.js @@ -826,8 +826,12 @@ 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. +* By default Sprites won't add themselves to the physics world. By setting physicsEnabled to true a Rectangle physics body is +* attached to this Sprite matching its placement and dimensions, and will then start to process physics world updates. +* You can access all physics related properties via Sprite.body. +* +* Important: Enabling a Sprite for physics will automatically set `Sprite.anchor` to 0.5 s0 the physics body is centered on the Sprite. +* If you need a different result then adjust or re-create the Body shape offsets manually, and/or reset the anchor after enabling physics. * * @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. @@ -847,6 +851,7 @@ Object.defineProperty(Phaser.Sprite.prototype, "physicsEnabled", { if (this.body === null) { this.body = new Phaser.Physics.Body(this); + this.anchor.set(0.5); } } else diff --git a/src/physics/Body.js b/src/physics/Body.js index 64191d82..25d8d4be 100644 --- a/src/physics/Body.js +++ b/src/physics/Body.js @@ -36,7 +36,7 @@ Phaser.Physics.Body = function (sprite) { * @property {p2.Body} data - The p2 Body data. * @protected */ - this.data = new p2.Body({ position:[this.px2p(sprite.x), this.px2p(sprite.y)], mass: 1 }); + this.data = new p2.Body({ position:[this.px2p(sprite.position.x), this.px2p(sprite.position.y)], mass: 1 }); /** * @property {Phaser.PointProxy} 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. @@ -425,6 +425,27 @@ Phaser.Physics.Body.prototype = { if (typeof offsetY === 'undefined') { offsetY = 0; } if (typeof rotation === 'undefined') { rotation = 0; } + // var px = 0; + // var py = 0; + + // if (sprite.anchor.x !== 0) + // { + // px = (sprite.width / 2) + (-sprite.width * sprite.anchor.x); + // } + // else + // { + // px = sprite.width / 2; + // } + + // if (sprite.anchor.y !== 0) + // { + // py = (sprite.height / 2) + (-sprite.height * sprite.anchor.y); + // } + // else + // { + // py = sprite.height / 2; + // } + this.clearShapes(); this.data.addShape(new p2.Rectangle(this.px2p(width), this.px2p(height)), [this.px2p(offsetX), this.px2p(offsetY)], rotation); @@ -433,7 +454,7 @@ Phaser.Physics.Body.prototype = { /** * 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 + * 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.Body#setPolygon * @param {object} options - An object containing the build options: @@ -483,8 +504,8 @@ Phaser.Physics.Body.prototype = { // Now process them into p2 values for (var p = 0; p < path.length; p++) { - // path[p][0] = this.px2p(path[p][0]); - // path[p][1] = this.px2p(path[p][1]); + path[p][0] = this.px2p(path[p][0]); + path[p][1] = this.px2p(path[p][1]); } // console.log('points'); diff --git a/src/utils/Debug.js b/src/utils/Debug.js index 3da168af..c5327f9a 100644 --- a/src/utils/Debug.js +++ b/src/utils/Debug.js @@ -893,6 +893,46 @@ Phaser.Utils.Debug.prototype = { }, + /** + * @method Phaser.Utils.Debug#renderVertices + * @param {array} vertices + * @param {string} [color='rgb(255,255,255)'] - The color the polygon is stroked in. + */ + renderShape: function (body, id, color, context) { + + if (this.context === null && context === null) + { + return; + } + + color = color || 'rgb(255,255,255)'; + + this.start(0, 0, color); + + var x = body.sprite.x; + var y = body.sprite.y; + + var points = body.data.shapes[id].vertices; + + var ox = x + this.game.math.p2px(body.data.shapeOffsets[id][0]); + var oy = y + this.game.math.p2px(body.data.shapeOffsets[id][1]); + + this.context.beginPath(); + this.context.moveTo(ox + this.game.math.p2px(points[0][0]), oy + this.game.math.p2px(points[0][1])); + + for (var i = 1; i < points.length; i++) + { + this.context.lineTo(ox + this.game.math.p2px(points[i][0]), oy + this.game.math.p2px(points[i][1])); + } + + this.context.closePath(); + this.context.strokeStyle = color; + this.context.stroke(); + + this.stop(); + + }, + /** * @method Phaser.Utils.Debug#renderPolygon * @param {array} polygon