Skip to content

Commit 10d105d

Browse files
committed
Acceleration tested and working fine. Proper accurate friction added and working really nicely, so much better than 'drag' used to. Considering removing drag, although will break the API history.
1 parent 754219a commit 10d105d

4 files changed

Lines changed: 40 additions & 103 deletions

File tree

examples/wip/acceleration.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function create() {
2929
car.body.collideWorldBounds = true;
3030
car.body.bounce.setTo(0.8, 0.8);
3131
car.body.allowRotation = true;
32+
car.body.friction = 10;
3233

3334
// car.body.drag.x = 10;
3435
// car.body.drag.y = 10;
@@ -50,36 +51,36 @@ function start() {
5051

5152
function update() {
5253

53-
if (car.x >= 400 && car.body.velocity.x > 0)
54-
{
55-
car.body.velocity.x = 0;
56-
car.body.acceleration.x = 0;
57-
var total = game.time.now - s;
58-
console.log(game.time.physicsElapsed);
59-
console.log('total ms', total, 'px/sec', car.x/(total/1000));
60-
}
54+
// if (car.x >= 400 && car.body.velocity.x > 0)
55+
// {
56+
// car.body.velocity.x = 0;
57+
// car.body.acceleration.x = 0;
58+
// var total = game.time.now - s;
59+
// console.log(game.time.physicsElapsed);
60+
// console.log('total ms', total, 'px/sec', car.x/(total/1000));
61+
// }
6162

6263
// car.body.velocity.x = 0;
6364
// car.body.velocity.y = 0;
64-
// car.body.angularVelocity = 0;
65+
car.body.angularVelocity = 0;
6566

66-
// car.body.acceleration.x = 0;
67-
// car.body.acceleration.y = 0;
67+
car.body.acceleration.x = 0;
68+
car.body.acceleration.y = 0;
6869

6970
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
7071
{
7172
car.body.angularVelocity = -200;
72-
// car.body.acceleration.x = -10;
73+
// car.body.acceleration.x = -100;
7374
}
7475
else if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
7576
{
7677
car.body.angularVelocity = 200;
77-
// car.body.acceleration.x = 10;
78+
// car.body.acceleration.x = 100;
7879
}
7980

8081
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
8182
{
82-
// game.physics.accelerationFromRotation(car.rotation, 10, car.body.acceleration);
83+
game.physics.accelerationFromRotation(car.rotation, 400, car.body.acceleration);
8384

8485
// car.body.acceleration.x = 10;
8586
// car.body.acceleration.copyFrom(game.physics.velocityFromAngle(car.angle, 30));
@@ -96,6 +97,7 @@ function update() {
9697

9798
function render() {
9899

99-
game.debug.renderSpriteInfo(car, 32, 32);
100+
// game.debug.renderSpriteInfo(car, 32, 32);
101+
game.debug.renderBodyInfo(car, 16, 24);
100102

101103
}

examples/wip/bunny.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
this.bunny.body.velocity.x = -500;
4141
}
4242

43-
// var melon = this.melonGroup.getFirstExists(true);
44-
// melon.x = this.bunny.x;
45-
// melon.y = this.bunny.y - 40;
43+
var melon = this.melonGroup.getFirstExists(true);
44+
melon.x = this.bunny.x;
45+
melon.y = this.bunny.y - 40;
4646

4747
this.carrot.x = this.bunny.x;
4848
this.carrot.y = this.bunny.y - 20;

src/physics/arcade/Body.js

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ Phaser.Physics.Arcade.Body = function (sprite) {
315315
this._sleepTimer = 0; // ms
316316
this._drag = 0;
317317
this._debug = 0;
318-
// this.friction = 0.99;
318+
this.friction = 0.9;
319319
// this._debug = 0;
320320

321321
/**
@@ -387,7 +387,7 @@ Phaser.Physics.Arcade.Body.prototype = {
387387
this.preY = (this.sprite.world.y - (this.sprite.anchor.y * this.height)) + this.offset.y;
388388
this.preRotation = this.sprite.angle;
389389

390-
if (this.canSleep && this.sleeping && this.velocity.equals(this.prevVelocity) === false)
390+
if (this.canSleep && this.sleeping && (this.velocity.equals(this.prevVelocity) === false || this.acceleration.isZero() === false))
391391
{
392392
this.sleeping = false;
393393
this._sleepTimer = 0;
@@ -450,33 +450,19 @@ Phaser.Physics.Arcade.Body.prototype = {
450450

451451
applyMotion: function () {
452452

453-
// Apply drag - this should be proportionally applied, not linearly like below
454-
if (this.drag.x !== 0 && this.acceleration.x === 0)
453+
if (this.friction > 0 && this.acceleration.isZero())
455454
{
456-
this._drag = this.drag.x * this.game.time.physicsElapsed;
457-
458-
if (this.velocity.x > 0)
455+
if (this.speed > this.friction)
459456
{
460-
this.velocity.x -= this._drag;
457+
this.speed -= this.friction;
461458
}
462-
else if (this.velocity.x < 0)
459+
else
463460
{
464-
this.velocity.x += this._drag;
461+
this.speed = 0;
465462
}
466-
}
467-
468-
if (this.drag.y !== 0 && this.acceleration.y === 0)
469-
{
470-
this._drag = this.drag.y * this.game.time.physicsElapsed;
471463

472-
if (this.velocity.y > 0)
473-
{
474-
this.velocity.y -= this._drag;
475-
}
476-
else if (this.velocity.y < 0)
477-
{
478-
this.velocity.y += this._drag;
479-
}
464+
this.velocity.x = Math.cos(this.angle) * this.speed;
465+
this.velocity.y = Math.sin(this.angle) * this.speed;
480466
}
481467

482468
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
@@ -485,74 +471,22 @@ Phaser.Physics.Arcade.Body.prototype = {
485471
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
486472
this.velocity.y += this.motionVelocity.y;
487473

488-
489-
/*
490-
if (this.drag.x !== 0 && this.acceleration.x === 0)
491-
{
492-
this._drag = this.drag.x * this.game.time.physicsElapsed;
493-
494-
if (this.velocity.x - this._drag > 0)
495-
{
496-
this.velocity.x -= this._drag;
497-
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
498-
this.velocity.x += this.motionVelocity.x;
499-
}
500-
else if (this.velocity.x + this.drag.x < 0)
501-
{
502-
this.velocity.x += this._drag;
503-
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
504-
this.velocity.x += this.motionVelocity.x;
505-
}
506-
else
507-
{
508-
this.velocity.x = 0;
509-
// this.preX = this.x;
510-
// this.motionVelocity.x = 0;
511-
}
512-
}
513-
else
514-
{
515-
this.x += this.game.time.physicsElapsed * (this.velocity.x + this.motionVelocity.x / 2);
516-
this.velocity.x += this.motionVelocity.x;
517-
}
518-
519-
if (this.drag.y !== 0 && this.acceleration.y === 0)
474+
if (this.velocity.x > this.maxVelocity.x)
520475
{
521-
this._drag = this.drag.y * this.game.time.physicsElapsed;
522-
523-
if (this.velocity.y - this._drag > 0)
524-
{
525-
this.velocity.y -= this._drag;
526-
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
527-
this.velocity.y += this.motionVelocity.y;
528-
}
529-
else if (this.velocity.y + this.drag.y < 0)
530-
{
531-
this.velocity.y += this._drag;
532-
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
533-
this.velocity.y += this.motionVelocity.y;
534-
}
535-
else
536-
{
537-
this.velocity.y = 0;
538-
// this.preY = this.y;
539-
// this.motionVelocity.y = 0;
540-
}
476+
this.velocity.x = this.maxVelocity.x;
541477
}
542-
else
478+
else if (this.velocity.x < -this.maxVelocity.x)
543479
{
544-
this.y += this.game.time.physicsElapsed * (this.velocity.y + this.motionVelocity.y / 2);
545-
this.velocity.y += this.motionVelocity.y;
480+
this.velocity.x = -this.maxVelocity.x;
546481
}
547-
*/
548482

549-
if (this.velocity.x > this.maxVelocity.x)
483+
if (this.velocity.y > this.maxVelocity.y)
550484
{
551-
// this.velocity.x = this.maxVelocity.x;
485+
this.velocity.y = this.maxVelocity.y;
552486
}
553-
else if (this.velocity.x < -this.maxVelocity.x)
487+
else if (this.velocity.y < -this.maxVelocity.y)
554488
{
555-
// this.velocity.x = -this.maxVelocity.x;
489+
this.velocity.y = -this.maxVelocity.y;
556490
}
557491

558492
if (this.collideWorldBounds)
@@ -606,7 +540,7 @@ Phaser.Physics.Arcade.Body.prototype = {
606540

607541
if (this.collideWorldBounds)
608542
{
609-
// this.checkWorldBounds();
543+
this.checkWorldBounds();
610544
}
611545

612546
this.center.setTo(this.x + this.halfWidth, this.y + this.halfHeight);

src/system/StageScaleMode.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ Phaser.StageScaleMode.prototype = {
260260
this._width = this.width;
261261
this._height = this.height;
262262

263-
// console.log('startFullScreen', this._width, this._height);
263+
// This needs updating to match the final spec:
264+
// http://generatedcontent.org/post/70347573294/is-your-fullscreen-api-code-up-to-date-find-out-how-to
264265

265266
if (element['requestFullScreen'])
266267
{

0 commit comments

Comments
 (0)