examples/game/lib/model/player.js
function(deltaTime){

            var currentTime = new Date().getTime();
            deltaTime = deltaTime || currentTime-this.lastUpdated;

            //Ignore minor updates
            if (deltaTime < 5){
                return this;
            }

            deltaTime /= 1000; //convert to seconds

            //Calculate deltas based on how much time has passed since last update
            var data = this.attributes;

            if (data.isAccelerating){

                var deltaVelocity = this.ACCELERATION * deltaTime;
                var deltaTime1 = 0;

                //Calculate new velocities
                var newVelocityX = data.velocityX + (Math.cos(data.angle) * deltaVelocity);
                var newVelocityY = data.velocityY + (Math.sin(data.angle) * deltaVelocity);

                //If velocities exceed maximums, we have to do some additional logic to calculate new positions
                if (newVelocityX > this.MAX_VELOCITY_X || newVelocityX < -this.MAX_VELOCITY_X){
                    newVelocityX = (newVelocityX > 0) ? this.MAX_VELOCITY_X : -this.MAX_VELOCITY_X;

                    deltaTime1 = getTime(data.velocityX, newVelocityX, this.ACCELERATION);
                    data.posX += getDistance(data.velocityX, newVelocityX, deltaTime1) + (newVelocityX*(deltaTime-deltaTime1));
                }
                else{
                    data.posX += getDistance(data.velocityX, newVelocityX, deltaTime);
                }

                if (newVelocityY > this.MAX_VELOCITY_Y || newVelocityY < -this.MAX_VELOCITY_Y){
                    newVelocityY = (newVelocityY > 0) ? this.MAX_VELOCITY_Y : -this.MAX_VELOCITY_Y;

                    deltaTime1 = getTime(data.velocityY, newVelocityY, this.ACCELERATION);
                    data.posY += getDistance(data.velocityY, newVelocityY, deltaTime1) + (newVelocityY*(deltaTime-deltaTime1));
                }else{
                    data.posY += getDistance(data.velocityY, newVelocityY, deltaTime);
                }

                data.velocityX = newVelocityX;
                data.velocityY = newVelocityY;
            }
            else{
                data.posX +=  data.velocityX * deltaTime;
                data.posY +=  data.velocityY * deltaTime;
            }

            //Check to see if player has exceeded zone boundary, in which case send them to opposite side
            var radius = this.SIZE/2;
            var maxPosX =  Constants.Zone.WIDTH+radius;
            var maxPosY =  Constants.Zone.HEIGHT+radius;

            while (data.posX > maxPosX) data.posX -= maxPosX+radius;
            while (data.posX < -radius) data.posX += maxPosX+radius;

            while (data.posY > maxPosY) data.posY -= maxPosY+radius;
            while (data.posY < -radius) data.posY += maxPosY+radius;

            //Update last updated timestamp
            this.lastUpdated = currentTime;

            return this;
        }
function(deltaTime){

            var currentTime = new Date().getTime();
            deltaTime = deltaTime || currentTime-this.lastUpdated;

            //Ignore minor updates
            if (deltaTime < 5){
                return this;
            }

            deltaTime /= 1000; //convert to seconds

            //Calculate deltas based on how much time has passed since last update
            var data = this.attributes;

            if (data.isAccelerating){

                var deltaVelocity = this.ACCELERATION * deltaTime;
                var deltaTime1 = 0;

                //Calculate new velocities
                var newVelocityX = data.velocityX + (Math.cos(data.angle) * deltaVelocity);
                var newVelocityY = data.velocityY + (Math.sin(data.angle) * deltaVelocity);

                //If velocities exceed maximums, we have to do some additional logic to calculate new positions
                if (newVelocityX > this.MAX_VELOCITY_X || newVelocityX < -this.MAX_VELOCITY_X){
                    newVelocityX = (newVelocityX > 0) ? this.MAX_VELOCITY_X : -this.MAX_VELOCITY_X;

                    deltaTime1 = getTime(data.velocityX, newVelocityX, this.ACCELERATION);
                    data.posX += getDistance(data.velocityX, newVelocityX, deltaTime1) + (newVelocityX*(deltaTime-deltaTime1));
                }
                else{
                    data.posX += getDistance(data.velocityX, newVelocityX, deltaTime);
                }

                if (newVelocityY > this.MAX_VELOCITY_Y || newVelocityY < -this.MAX_VELOCITY_Y){
                    newVelocityY = (newVelocityY > 0) ? this.MAX_VELOCITY_Y : -this.MAX_VELOCITY_Y;

                    deltaTime1 = getTime(data.velocityY, newVelocityY, this.ACCELERATION);
                    data.posY += getDistance(data.velocityY, newVelocityY, deltaTime1) + (newVelocityY*(deltaTime-deltaTime1));
                }else{
                    data.posY += getDistance(data.velocityY, newVelocityY, deltaTime);
                }

                data.velocityX = newVelocityX;
                data.velocityY = newVelocityY;
            }
            else{
                data.posX +=  data.velocityX * deltaTime;
                data.posY +=  data.velocityY * deltaTime;
            }

            //Check to see if player has exceeded zone boundary, in which case send them to opposite side
            var radius = this.SIZE/2;
            var maxPosX =  Constants.Zone.WIDTH+radius;
            var maxPosY =  Constants.Zone.HEIGHT+radius;

            while (data.posX > maxPosX) data.posX -= maxPosX+radius;
            while (data.posX < -radius) data.posX += maxPosX+radius;

            while (data.posY > maxPosY) data.posY -= maxPosY+radius;
            while (data.posY < -radius) data.posY += maxPosY+radius;

            //Update last updated timestamp
            this.lastUpdated = currentTime;

            return this;
        }
function ( deltaTime ) { var currentTime = new Date ( ) . getTime ( ) ; deltaTime = deltaTime || currentTime - this . lastUpdated ; //Ignore minor updates if ( deltaTime < 5 ) { return this ; } deltaTime /= 1000 ; //convert to seconds //Calculate deltas based on how much time has passed since last update var data = this . attributes ; if ( data . isAccelerating ) { var deltaVelocity = this . ACCELERATION * deltaTime ; var deltaTime1 = 0 ; //Calculate new velocities var newVelocityX = data . velocityX + ( Math . cos ( data . angle ) * deltaVelocity ) ; var newVelocityY = data . velocityY + ( Math . sin ( data . angle ) * deltaVelocity ) ; //If velocities exceed maximums, we have to do some additional logic to calculate new positions if ( newVelocityX > this . MAX_VELOCITY_X || newVelocityX < - this . MAX_VELOCITY_X ) { newVelocityX = ( newVelocityX > 0 ) ? this . MAX_VELOCITY_X : - this . MAX_VELOCITY_X ; deltaTime1 = getTime ( data . velocityX , newVelocityX , this . ACCELERATION ) ; data . posX += getDistance ( data . velocityX , newVelocityX , deltaTime1 ) + ( newVelocityX * ( deltaTime - deltaTime1 ) ) ; } else { data . posX += getDistance ( data . velocityX , newVelocityX , deltaTime ) ; } if ( newVelocityY > this . MAX_VELOCITY_Y || newVelocityY < - this . MAX_VELOCITY_Y ) { newVelocityY = ( newVelocityY > 0 ) ? this . MAX_VELOCITY_Y : - this . MAX_VELOCITY_Y ; deltaTime1 = getTime ( data . velocityY , newVelocityY , this . ACCELERATION ) ; data . posY += getDistance ( data . velocityY , newVelocityY , deltaTime1 ) + ( newVelocityY * ( deltaTime - deltaTime1 ) ) ; } else { data . posY += getDistance ( data . velocityY , newVelocityY , deltaTime ) ; } data . velocityX = newVelocityX ; data . velocityY = newVelocityY ; } else { data . posX += data . velocityX * deltaTime ; data . posY += data . velocityY * deltaTime ; } //Check to see if player has exceeded zone boundary, in which case send them to opposite side var radius = this . SIZE / 2 ; var maxPosX = Constants . Zone . WIDTH + radius ; var maxPosY = Constants . Zone . HEIGHT + radius ; while ( data . posX > maxPosX ) data . posX -= maxPosX + radius ; while ( data . posX < - radius ) data . posX += maxPosX + radius ; while ( data . posY > maxPosY ) data . posY -= maxPosY + radius ; while ( data . posY < - radius ) data . posY += maxPosY + radius ; //Update last updated timestamp this . lastUpdated = currentTime ; return this ; }
Updates this player's velocity and position values
@param {number} [deltaTime] Time interval in milliseconds (by default uses the amount of time since function last called)
@returns {*}
Updates this player s velocity and position values