Skip to content

Commit 283e38a

Browse files
committed
Time to optimise the rebound a little.
1 parent be6489f commit 283e38a

2 files changed

Lines changed: 69 additions & 65 deletions

File tree

examples/wip/blocked.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function create() {
3232
// gravity into floor jiggle
3333
function test8() {
3434

35-
game.physics.gravity.y = 250;
35+
// game.physics.gravity.y = -250;
3636

3737
sprite = game.add.sprite(400, 100, 'ball');
3838
sprite.body.collideWorldBounds = true;
@@ -50,8 +50,8 @@ function test8() {
5050

5151
function launch8() {
5252

53-
// sprite.body.velocity.y = 200;
54-
// sprite2.body.velocity.y = 200;
53+
sprite.body.velocity.y = -200;
54+
sprite2.body.velocity.y = -200;
5555

5656
}
5757

src/physics/arcade/Body.js

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,6 @@ Phaser.Physics.Arcade.Body = function (sprite) {
109109
*/
110110
this.center = new Phaser.Point(this.x + this.halfWidth, this.y + this.halfHeight);
111111

112-
/**
113-
* @property {number} _sx - Internal cache var.
114-
* @private
115-
*/
116-
this._sx = sprite.scale.x;
117-
118-
/**
119-
* @property {number} _sy - Internal cache var.
120-
* @private
121-
*/
122-
this._sy = sprite.scale.y;
123-
124112
/**
125113
* @property {Phaser.Point} motionVelocity - The data from the updateMotion function.
126114
*/
@@ -209,22 +197,19 @@ Phaser.Physics.Arcade.Body = function (sprite) {
209197
* For example allowCollision.up = false means it won't collide when the collision happened while moving up.
210198
* @property {object} allowCollision - An object containing allowed collision.
211199
*/
212-
// This would be faster as an array
213200
this.allowCollision = { none: false, any: true, up: true, down: true, left: true, right: true };
214201

215202
/**
216203
* This object is populated with boolean values when the Body collides with another.
217204
* touching.up = true means the collision happened to the top of this Body for example.
218205
* @property {object} touching - An object containing touching results.
219206
*/
220-
// This would be faster as an array
221207
this.touching = { none: true, up: false, down: false, left: false, right: false };
222208

223209
/**
224210
* This object is populated with previous touching values from the bodies previous collision.
225211
* @property {object} wasTouching - An object containing previous touching results.
226212
*/
227-
// This would be faster as an array
228213
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
229214

230215
/**
@@ -292,17 +277,9 @@ Phaser.Physics.Arcade.Body = function (sprite) {
292277
this.overlapY = 0;
293278

294279
/**
295-
* @property {boolean} canSleep - A Body that canSleep will have its velocity set to zero if it falls below sleepThreshold for longer than sleepDuration.
280+
* @property {number} friction - The amount of friction this body experiences during motion.
296281
* @default
297282
*/
298-
this.sleeping = false;
299-
this.canSleep = false;
300-
this.sleepMin = new Phaser.Point(-20, -20);
301-
this.sleepMax = new Phaser.Point(20, 20);
302-
this.sleepDuration = 2000; // ms
303-
this._sleepTimer = 0; // ms
304-
this._drag = 0;
305-
this._debug = 0;
306283
this.friction = 0;
307284

308285
/**
@@ -317,8 +294,37 @@ Phaser.Physics.Arcade.Body = function (sprite) {
317294
*/
318295
this.collideWorldBounds = false;
319296

297+
/**
298+
* This object is populated with boolean values when the Body collides with the World bounds or a Tile.
299+
* For example if blocked.up is true then the Body cannot move up.
300+
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any.
301+
*/
320302
this.blocked = { up: false, down: false, left: false, right: false };
321303

304+
/**
305+
* @property {number} _dx - Internal cache var.
306+
* @private
307+
*/
308+
this._dx = 0;
309+
310+
/**
311+
* @property {number} _dy - Internal cache var.
312+
* @private
313+
*/
314+
this._dy = 0;
315+
316+
/**
317+
* @property {number} _sx - Internal cache var.
318+
* @private
319+
*/
320+
this._sx = sprite.scale.x;
321+
322+
/**
323+
* @property {number} _sy - Internal cache var.
324+
* @private
325+
*/
326+
this._sy = sprite.scale.y;
327+
322328
};
323329

324330
Phaser.Physics.Arcade.Body.prototype = {
@@ -374,11 +380,11 @@ Phaser.Physics.Arcade.Body.prototype = {
374380
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
375381
this.preRotation = this.sprite.angle;
376382

377-
if (this.canSleep && this.sleeping && (this.velocity.equals(this.prevVelocity) === false || this.acceleration.isZero() === false))
378-
{
379-
this.sleeping = false;
380-
this._sleepTimer = 0;
381-
}
383+
// if (this.canSleep && this.sleeping && (this.velocity.equals(this.prevVelocity) === false || this.acceleration.isZero() === false))
384+
// {
385+
// this.sleeping = false;
386+
// this._sleepTimer = 0;
387+
// }
382388

383389
this.x = this.preX;
384390
this.y = this.preY;
@@ -513,29 +519,44 @@ Phaser.Physics.Arcade.Body.prototype = {
513519
{
514520
// Separate
515521
this.y += this.overlapY;
516-
this.preY = this.y; // because we don't want any delta from a separation
517522

518-
// Reflect?
519-
if (this.velocity.y < -this.minBounceVelocity && this.bounce.y !== 0)
523+
// console.log(this._debug, 'blocked up', this.overlapY, 'v', this.velocity.y, 'min', this.minBounceVelocity, 'mv', this.motionVelocity.y, 'd', this.deltaY(), 'newy', this.y);
524+
525+
// Reflect? It's the velocity AFTER the bounce we need to test! And only if there is a bounce value
526+
if (this.bounce.y !== 0 && this.velocity.y != 0)
520527
{
521-
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
522-
this.velocity.y += this.motionVelocity.y;
523528
this.velocity.y *= -this.bounce.y;
529+
530+
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
531+
532+
if (this._dy > this.minBounceVelocity)
533+
{
534+
this.y += this._dy;
535+
this.velocity.y += this.motionVelocity.y;
536+
// console.log(this._debug, 'rb', this._dy, 'delta', this.deltaY(), 'newy', this.y);
537+
}
538+
else
539+
{
540+
// Kill it dead :)
541+
this.preY = this.y; // because we don't want any delta from a separation
542+
this.velocity.y = 0;
543+
this.motionVelocity.y = 0;
544+
// console.log(this._debug, 'void1', this.velocity.y, 'delta', this.deltaY());
545+
}
524546
}
525547
else
526548
{
549+
// Kill it dead :)
550+
this.preY = this.y; // because we don't want any delta from a separation
527551
this.velocity.y = 0;
528552
this.motionVelocity.y = 0;
553+
// console.log(this._debug, 'void2', this.velocity.y, 'delta', this.deltaY());
529554
}
530-
531-
console.log(this._debug, 'blocked up', this.overlapY, 'v', this.velocity.y, 'min', this.minBounceVelocity, 'mv', this.motionVelocity.y, 'd', this.deltaY());
532555
}
533556
else if (this.blocked.down)
534557
{
535558
// Separate
536559
this.y -= this.overlapY;
537-
// this.preY = this.y; // because we don't want any delta from a separation
538-
// this.sprite.y -= this.deltaY();
539560

540561
// console.log(this._debug, 'blocked down', this.overlapY, 'v', this.velocity.y, 'min', this.minBounceVelocity, 'mv', this.motionVelocity.y, 'd', this.deltaY(), 'newy', this.y);
541562

@@ -544,19 +565,13 @@ Phaser.Physics.Arcade.Body.prototype = {
544565
{
545566
this.velocity.y *= -this.bounce.y;
546567

547-
var oy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
568+
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
548569

549-
if (oy < -this.minBounceVelocity)
570+
if (this._dy < -this.minBounceVelocity)
550571
{
551-
552-
553-
// if (this.velocity.y < -this.minBounceVelocity)
554-
// {
555-
// var oy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
556-
this.y += oy;
572+
this.y += this._dy;
557573
this.velocity.y += this.motionVelocity.y;
558-
// console.log(this._debug, 'rb', this.velocity.y, 'delta', this.deltaY(), 'newy', this.y);
559-
// console.log(this._debug, 'rb', oy, 'delta', this.deltaY(), 'newy', this.y);
574+
// console.log(this._debug, 'rb', this._dy, 'delta', this.deltaY(), 'newy', this.y);
560575
}
561576
else
562577
{
@@ -654,39 +669,28 @@ Phaser.Physics.Arcade.Body.prototype = {
654669
*/
655670
checkWorldBounds: function () {
656671

657-
if (this.x <= this.game.world.bounds.x)
672+
if (this.x < this.game.world.bounds.x)
658673
{
659674
this.overlapX = this.game.world.bounds.x - this.x;
660675
this.blocked.left = true;
661-
// this.x = this.game.world.bounds.x;
662-
// this.preX = this.x;
663-
// this.velocity.x *= -this.bounce.x;
664676
}
665-
else if (this.right >= this.game.world.bounds.right)
677+
else if (this.right > this.game.world.bounds.right)
666678
{
667679
this.overlapX = this.right - this.game.world.bounds.right;
668680
this.blocked.right = true;
669-
// this.x = this.game.world.bounds.right - this.width;
670-
// this.preX = this.x;
671-
// this.velocity.x *= -this.bounce.x;
672681
}
673682

674683
if (this.y < this.game.world.bounds.y)
675684
{
676685
this.overlapY = this.game.world.bounds.y - this.y;
677686
this.blocked.up = true;
678-
// this.y = this.game.world.bounds.y;
679-
// this.preY = this.y;
680-
// this.velocity.y *= -this.bounce.y;
687+
// console.log(this._debug, 'cw', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
681688
}
682689
else if (this.bottom > this.game.world.bounds.bottom)
683690
{
684691
this.overlapY = this.bottom - this.game.world.bounds.bottom;
685692
this.blocked.down = true;
686693
// console.log(this._debug, 'cw', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
687-
// this.y = this.game.world.bounds.bottom - this.height;
688-
// this.preY = this.y;
689-
// this.velocity.y *= -this.bounce.y;
690694
}
691695

692696
},

0 commit comments

Comments
 (0)