Skip to content

Commit a0d5754

Browse files
committed
P2 updates finished.
1 parent 956fa49 commit a0d5754

3 files changed

Lines changed: 83 additions & 14 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,18 @@ Version 2.4 - "Katar" - in dev
245245
* Phaser.BitmapText no longer extends PIXI.BitmapText but replaces it entirely.
246246
* Phaser.Text no longer extends PIXI.Text but replaces it entirely. Phaser.Text now natively extends a Phaser Sprite, meaning it can be enabled for physics, damaged, etc.
247247
* Mouse.button and MSPointer.button have been removed. They never supported complex button events (such as holding down 2 buttons and releasing just one) or any buttons other than left and right. They have been replaced with the far more robust and accurate Pointer button properties such as `Pointer.leftButton`, `Pointer.rightButton` and so on.
248+
249+
### p2.js Upgraded to version 0.7.0
250+
251+
Phaser has been upgraded internally to use the new release of p2 physics. All Phaser based API call signatures remain unchanged unless listed below.
252+
253+
For the full list of p2 additions please read [their change log](https://github.com/schteppe/p2.js/releases/tag/v0.7.0).
254+
248255
* The P2.Body.onBeginContact arguments have changed. It now sends 5 arguments: The Phaser.P2.Body, the p2.Body, the p2 Shape from Body A, the p2 Shape from Body B and the contact equations array. Note that the Phaser.P2.Body may be null if you collide with a 'native' p2 body (such as the world bounds). However the p2.Body argument will always be populated.
249256
* The P2.Body.onEndContact arguments have changed. It now sends 4 arguments: The Phaser.P2.Body, the p2.Body, the p2 Shape from Body A and the p2 Shape from Body B. Note that the Phaser.P2.Body may be null if this is the end of a contact with a 'native' p2 body (such as the world bounds). However the p2.Body argument will always be populated.
250-
251-
### p2.js Upgraded to version 0.7
257+
* P2.Body.applyImpulse allows you to apply an impulse to a Body. An impulse is a force added to a body during a short period of time.
258+
* P2.Body.applyImpulseLocal allows you to apply an impulse to a point local to the Body. An impulse is a force added to a body during a short period of time.
259+
* P2.Body.getVelocityAtPoint gets the velocity of a point in the body.
252260

253261
### New Features
254262

src/physics/p2/Body.js

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,20 @@ Phaser.Physics.P2.Body.prototype = {
435435

436436
},
437437

438+
/**
439+
* Gets the velocity of a point in the body.
440+
*
441+
* @method Phaser.Physics.P2.Body#getVelocityAtPoint
442+
* @param {Array} result - A vector to store the result in.
443+
* @param {Array} relativePoint - A world oriented vector, indicating the position of the point to get the velocity from.
444+
* @return {Array} The result vector.
445+
*/
446+
getVelocityAtPoint: function (result, relativePoint) {
447+
448+
return this.data.getVelocityAtPoint(result, relativePoint);
449+
450+
},
451+
438452
/**
439453
* Apply damping, see http://code.google.com/p/bullet/issues/detail?id=74 for details.
440454
*
@@ -447,6 +461,38 @@ Phaser.Physics.P2.Body.prototype = {
447461

448462
},
449463

464+
/**
465+
* Apply impulse to a point relative to the body.
466+
* This could for example be a point on the Body surface. An impulse is a force added to a body during a short
467+
* period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
468+
*
469+
* @method Phaser.Physics.P2.Body#applyImpulse
470+
* @param {Float32Array|Array} impulse - The impulse vector to add, oriented in world space.
471+
* @param {number} worldX - A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be exerted on the center of mass.
472+
* @param {number} worldY - A point relative to the body in world space. If not given, it is set to zero and all of the impulse will be exerted on the center of mass.
473+
*/
474+
applyImpulse: function (impulse, worldX, worldY) {
475+
476+
this.data.applyImpulse(impulse, [this.world.pxmi(worldX), this.world.pxmi(worldY)]);
477+
478+
},
479+
480+
/**
481+
* Apply impulse to a point local to the body.
482+
* This could for example be a point on the Body surface. An impulse is a force added to a body during a short
483+
* period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
484+
*
485+
* @method Phaser.Physics.P2.Body#applyImpulseLocal
486+
* @param {Float32Array|Array} impulse - The impulse vector to add, oriented in world space.
487+
* @param {number} localX - A local point on the body.
488+
* @param {number} localY - A local point on the body.
489+
*/
490+
applyImpulseLocal: function (force, localX, localY) {
491+
492+
this.data.applyImpulseLocal(force, [this.world.pxmi(localX), this.world.pxmi(localY)]);
493+
494+
},
495+
450496
/**
451497
* 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.
452498
*
@@ -1631,7 +1677,7 @@ Object.defineProperty(Phaser.Physics.P2.Body.prototype, "inertia", {
16311677

16321678
/**
16331679
* @name Phaser.Physics.P2.Body#mass
1634-
* @property {number} mass -
1680+
* @property {number} mass - The mass of the body.
16351681
*/
16361682
Object.defineProperty(Phaser.Physics.P2.Body.prototype, "mass", {
16371683

src/physics/p2/BodyDebug.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
9090
draw: function() {
9191

9292
var angle, child, color, i, j, lineColor, lw, obj, offset, sprite, v, verts, vrot, _j, _ref1;
93+
9394
obj = this.body;
9495
sprite = this.canvas;
9596
sprite.clear();
@@ -106,10 +107,8 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
106107
while (i !== l)
107108
{
108109
child = obj.shapes[i];
109-
offset = obj.shapeOffsets[i];
110-
angle = obj.shapeAngles[i];
111-
offset = offset || 0;
112-
angle = angle || 0;
110+
offset = child.position || 0;
111+
angle = child.angle || 0;
113112

114113
if (child instanceof p2.Circle)
115114
{
@@ -127,7 +126,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
127126
{
128127
this.drawLine(sprite, child.length * this.ppu, lineColor, lw);
129128
}
130-
else if (child instanceof p2.Rectangle)
129+
else if (child instanceof p2.Box)
131130
{
132131
this.drawRectangle(sprite, offset[0] * this.ppu, offset[1] * this.ppu, angle, child.width * this.ppu, child.height * this.ppu, lineColor, color, lw);
133132
}
@@ -153,9 +152,10 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
153152
},
154153

155154
/**
156-
* Draws the P2 shapes to the Graphics object.
155+
* Draws a p2.Box to the Graphics object.
157156
*
158157
* @method Phaser.Physics.P2.BodyDebug#drawRectangle
158+
* @private
159159
*/
160160
drawRectangle: function(g, x, y, angle, w, h, color, fillColor, lineWidth) {
161161

@@ -165,12 +165,14 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
165165
g.lineStyle(lineWidth, color, 1);
166166
g.beginFill(fillColor);
167167
g.drawRect(x - w / 2, y - h / 2, w, h);
168+
168169
},
169170

170171
/**
171-
* Draws a P2 Circle shape.
172+
* Draws a p2.Circle to the Graphics object.
172173
*
173174
* @method Phaser.Physics.P2.BodyDebug#drawCircle
175+
* @private
174176
*/
175177
drawCircle: function(g, x, y, angle, radius, color, lineWidth) {
176178

@@ -186,9 +188,10 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
186188
},
187189

188190
/**
189-
* Draws a P2 Line shape.
191+
* Draws a p2.Line to the Graphics object.
190192
*
191193
* @method Phaser.Physics.P2.BodyDebug#drawLine
194+
* @private
192195
*/
193196
drawLine: function(g, len, color, lineWidth) {
194197

@@ -202,9 +205,10 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
202205
},
203206

204207
/**
205-
* Draws a P2 Convex shape.
208+
* Draws a p2.Convex to the Graphics object.
206209
*
207210
* @method Phaser.Physics.P2.BodyDebug#drawConvex
211+
* @private
208212
*/
209213
drawConvex: function(g, verts, triangles, color, fillColor, lineWidth, debug, offset) {
210214

@@ -272,9 +276,10 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
272276
},
273277

274278
/**
275-
* Draws a P2 Path.
279+
* Draws a p2.Path to the Graphics object.
276280
*
277281
* @method Phaser.Physics.P2.BodyDebug#drawPath
282+
* @private
278283
*/
279284
drawPath: function(g, path, color, fillColor, lineWidth) {
280285

@@ -342,9 +347,10 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
342347
},
343348

344349
/**
345-
* Draws a P2 Plane shape.
350+
* Draws a p2.Plane to the Graphics object.
346351
*
347352
* @method Phaser.Physics.P2.BodyDebug#drawPlane
353+
* @private
348354
*/
349355
drawPlane: function(g, x0, x1, color, lineColor, lineWidth, diagMargin, diagSize, maxLength, angle) {
350356

@@ -368,6 +374,12 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
368374

369375
},
370376

377+
/**
378+
* Draws a p2.Capsule to the Graphics object.
379+
*
380+
* @method Phaser.Physics.P2.BodyDebug#drawCapsule
381+
* @private
382+
*/
371383
drawCapsule: function(g, x, y, angle, len, radius, color, fillColor, lineWidth) {
372384

373385
if (typeof lineWidth === 'undefined') { lineWidth = 1; }
@@ -406,6 +418,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
406418
* Picks a random pastel color.
407419
*
408420
* @method Phaser.Physics.P2.BodyDebug#randomPastelHex
421+
* @private
409422
*/
410423
randomPastelHex: function() {
411424

@@ -428,6 +441,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
428441
* Converts from RGB to Hex.
429442
*
430443
* @method Phaser.Physics.P2.BodyDebug#rgbToHex
444+
* @private
431445
*/
432446
rgbToHex: function(r, g, b) {
433447
return this.componentToHex(r) + this.componentToHex(g) + this.componentToHex(b);
@@ -437,6 +451,7 @@ Phaser.Utils.extend(Phaser.Physics.P2.BodyDebug.prototype, {
437451
* Component to hex conversion.
438452
*
439453
* @method Phaser.Physics.P2.BodyDebug#componentToHex
454+
* @private
440455
*/
441456
componentToHex: function(c) {
442457

0 commit comments

Comments
 (0)