Skip to content

Commit 58fb248

Browse files
committed
Optimised Body loop, removed sleep properties, implemented minBounceVelocity. Working as expected at last :)
1 parent 283e38a commit 58fb248

2 files changed

Lines changed: 46 additions & 64 deletions

File tree

examples/wip/blocked.js

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

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

3737
sprite = game.add.sprite(400, 100, 'ball');
3838
sprite.body.collideWorldBounds = true;
3939
sprite.body.bounce.setTo(0.8, 0.8);
4040
sprite.body.minBounceVelocity = 0.8;
41+
sprite.body.velocity.x = -400;
4142

4243
sprite2 = game.add.sprite(500, 100, 'ball');
4344
sprite2.body.collideWorldBounds = true;
4445
sprite2.body.bounce.setTo(0.5, 0.5);
45-
sprite2.body.minBounceVelocity = 0.5;
46+
sprite2.body.minBounceVelocity = 0.8;
4647

4748
game.input.onDown.add(launch8, this);
4849

4950
}
5051

5152
function launch8() {
5253

53-
sprite.body.velocity.y = -200;
54-
sprite2.body.velocity.y = -200;
54+
// sprite.body.velocity.x = -200;
55+
sprite2.body.velocity.x = 200;
5556

5657
}
5758

src/physics/arcade/Body.js

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -472,40 +472,50 @@ Phaser.Physics.Arcade.Body.prototype = {
472472
{
473473
// Separate
474474
this.x += this.overlapX;
475-
this.preX = this.x; // because we don't want any delta from a separation
476475

477-
// Reflect?
478-
if (this.velocity.x > this.minBounceVelocity && this.bounce.x !== 0)
476+
// console.log(this._debug, 'blocked left', this.x, this.overlapX);
477+
478+
this.velocity.x *= -this.bounce.x;
479+
480+
this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
481+
482+
if (this._dx > this.minBounceVelocity)
479483
{
480-
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
484+
this.x += this._dx;
481485
this.velocity.x += this.motionVelocity.x;
482-
this.velocity.x *= -this.bounce.x;
483-
// console.log(this._debug, 'blocked down + r', oy, 'v', this.velocity.y, 'mv', this.motionVelocity.y, 'd', this.deltaY());
486+
// console.log(this._debug, 'blocked left', this._dx, 'overlap', this.overlapX, 'delta', this.deltaX(), 'newy', this.x);
484487
}
485488
else
486489
{
490+
// Kill it dead :)
491+
this.preX = this.x; // because we don't want any delta from a separation
487492
this.velocity.x = 0;
488493
this.motionVelocity.x = 0;
494+
// console.log(this._debug, 'blocked left KILL', this._dx, 'overlap', this.overlapX, 'delta', this.deltaX(), 'newy', this.x);
489495
}
490496
}
491497
else if (this.blocked.right)
492498
{
493499
// Separate
494500
this.x -= this.overlapX;
495-
this.preX = this.x; // because we don't want any delta from a separation
496501

497-
// Reflect?
498-
if (this.velocity.y < -this.minBounceVelocity && this.bounce.y !== 0)
502+
this.velocity.x *= -this.bounce.x;
503+
504+
this._dx = this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
505+
506+
if (this._dx < -this.minBounceVelocity)
499507
{
500-
this.x -= this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
508+
this.x += this._dx;
501509
this.velocity.x += this.motionVelocity.x;
502-
this.velocity.x *= -this.bounce.x;
503-
// console.log(this._debug, 'blocked up + r', oy, 'v', this.velocity.y, 'mv', this.motionVelocity.y, 'd', this.deltaY());
510+
// console.log(this._debug, 'blocked right', this._dx, 'overlap', this.overlapX, 'delta', this.deltaX(), 'newy', this.x);
504511
}
505512
else
506513
{
514+
// Kill it dead :)
515+
this.preX = this.x; // because we don't want any delta from a separation
507516
this.velocity.x = 0;
508517
this.motionVelocity.x = 0;
518+
// console.log(this._debug, 'blocked right KILL', this._dx, 'overlap', this.overlapX, 'delta', this.deltaX(), 'newy', this.x);
509519
}
510520
}
511521
else
@@ -520,75 +530,46 @@ Phaser.Physics.Arcade.Body.prototype = {
520530
// Separate
521531
this.y += this.overlapY;
522532

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);
533+
this.velocity.y *= -this.bounce.y;
524534

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)
535+
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
536+
537+
if (this._dy > this.minBounceVelocity)
527538
{
528-
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-
}
539+
this.y += this._dy;
540+
this.velocity.y += this.motionVelocity.y;
546541
}
547542
else
548543
{
549544
// Kill it dead :)
550545
this.preY = this.y; // because we don't want any delta from a separation
551546
this.velocity.y = 0;
552547
this.motionVelocity.y = 0;
553-
// console.log(this._debug, 'void2', this.velocity.y, 'delta', this.deltaY());
548+
// console.log(this._debug, 'void1', this.velocity.y, 'delta', this.deltaY());
554549
}
555550
}
556551
else if (this.blocked.down)
557552
{
558553
// Separate
559554
this.y -= this.overlapY;
560555

561-
// 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);
556+
this.velocity.y *= -this.bounce.y;
557+
558+
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
562559

563-
// Reflect? It's the velocity AFTER the bounce we need to test! And only if there is a bounce value
564-
if (this.bounce.y !== 0 && this.velocity.y != 0)
560+
if (this._dy < -this.minBounceVelocity)
565561
{
566-
this.velocity.y *= -this.bounce.y;
567-
568-
this._dy = this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
569-
570-
if (this._dy < -this.minBounceVelocity)
571-
{
572-
this.y += this._dy;
573-
this.velocity.y += this.motionVelocity.y;
574-
// console.log(this._debug, 'rb', this._dy, 'delta', this.deltaY(), 'newy', this.y);
575-
}
576-
else
577-
{
578-
// Kill it dead :)
579-
this.preY = this.y; // because we don't want any delta from a separation
580-
this.velocity.y = 0;
581-
this.motionVelocity.y = 0;
582-
// console.log(this._debug, 'void1', this.velocity.y, 'delta', this.deltaY());
583-
}
562+
this.y += this._dy;
563+
this.velocity.y += this.motionVelocity.y;
564+
// console.log(this._debug, 'rb', this._dy, 'delta', this.deltaY(), 'newy', this.y);
584565
}
585566
else
586567
{
587568
// Kill it dead :)
588569
this.preY = this.y; // because we don't want any delta from a separation
589570
this.velocity.y = 0;
590571
this.motionVelocity.y = 0;
591-
// console.log(this._debug, 'void2', this.velocity.y, 'delta', this.deltaY());
572+
// console.log(this._debug, 'void1', this.velocity.y, 'delta', this.deltaY());
592573
}
593574
}
594575
else
@@ -642,13 +623,11 @@ Phaser.Physics.Arcade.Body.prototype = {
642623
{
643624
this.facing = Phaser.UP;
644625
this.sprite.y += this.deltaY();
645-
// console.log(this._debug, 'post up', this.deltaY(), this.sprite.y);
646626
}
647627
else if (this.deltaY() > 0)
648628
{
649629
this.facing = Phaser.DOWN;
650630
this.sprite.y += this.deltaY();
651-
// console.log(this._debug, 'post down', this.deltaY(), this.sprite.y);
652631
}
653632

654633
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);
@@ -673,24 +652,26 @@ Phaser.Physics.Arcade.Body.prototype = {
673652
{
674653
this.overlapX = this.game.world.bounds.x - this.x;
675654
this.blocked.left = true;
655+
// console.log(this._debug, 'cwl', this.overlapX, this.x, this.game.world.bounds.x);
676656
}
677657
else if (this.right > this.game.world.bounds.right)
678658
{
679659
this.overlapX = this.right - this.game.world.bounds.right;
680660
this.blocked.right = true;
661+
// console.log(this._debug, 'cwr', this.overlapX, this.x, this.game.world.bounds.x);
681662
}
682663

683664
if (this.y < this.game.world.bounds.y)
684665
{
685666
this.overlapY = this.game.world.bounds.y - this.y;
686667
this.blocked.up = true;
687-
// console.log(this._debug, 'cw', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
668+
// console.log(this._debug, 'cwu', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
688669
}
689670
else if (this.bottom > this.game.world.bounds.bottom)
690671
{
691672
this.overlapY = this.bottom - this.game.world.bounds.bottom;
692673
this.blocked.down = true;
693-
// console.log(this._debug, 'cw', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
674+
// console.log(this._debug, 'cwd', this.overlapY, this.y, this.height, this.bottom, this.game.world.bounds.bottom);
694675
}
695676

696677
},

0 commit comments

Comments
 (0)