phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.94k stars 7.08k forks source link

velocity never reaches 0 if linearDamping is used. #471

Closed ghost closed 10 years ago

ghost commented 10 years ago

Hi,

I discovered a small but significant problem within the applyDamping function where the velocity would never be set to zero because of an if clause. Version is 1.1.5.

That is the actual version:

/**
    * Internal method that checks the acceleration and applies damping if not set.
    *
    * @method Phaser.Physics.Arcade.Body#applyDamping
    * @protected
    */
    applyDamping: function () {

        if (this.linearDamping > 0 && this.acceleration.isZero())
        {
            if (this.speed > this.linearDamping)
            {
                this.speed -= this.linearDamping;
            }
            else
            {
                this.speed = 0;
            }

            //  Don't bother if speed 0
            if (this.speed > 0)
            {
                this.velocity.x = Math.cos(this.angle) * this.speed;
                this.velocity.y = Math.sin(this.angle) * this.speed;

                this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
                this.angle = Math.atan2(this.velocity.y, this.velocity.x);
            }
        }

I've deleted the if clause so now linearDamping is applyed correctly. I don't know how to make merge/pull requests or something so please feel free to change your code.

/**
    * Internal method that checks the acceleration and applies damping if not set.
    *
    * @method Phaser.Physics.Arcade.Body#applyDamping
    * @protected
    */
    applyDamping: function () {

        if (this.linearDamping > 0 && this.acceleration.isZero())
        {
            if (this.speed > this.linearDamping)
            {
                this.speed -= this.linearDamping;
            }
            else
            {
                this.speed = 0;
            }

            // actually we need to bother if speed = 0 so velocity can become 0
            this.velocity.x = Math.cos(this.angle) * this.speed;
            this.velocity.y = Math.sin(this.angle) * this.speed;

            this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
            this.angle = Math.atan2(this.velocity.y, this.velocity.x);

        }

    },
ghost commented 10 years ago

Added version...

photonstorm commented 10 years ago

Thanks, I've added this into the dev version.