10 years ago by Sledge
What I am trying to implement is a camera that follows the player with some lag. My method is to compute the distance vector comparing the player position and the screen position, then I move the screen along that line a step size every frame.
However, when I run this code, the screen flies off to the upper left indefinitely. Can anyone see where I may have made a mistake?
Below is the code from the update method in my MyGame entity in main.js.
However, when I run this code, the screen flies off to the upper left indefinitely. Can anyone see where I may have made a mistake?
Below is the code from the update method in my MyGame entity in main.js.
update: function() { // screen tracks the player var player = this.getEntitiesByType( EntityPlayer )[0]; if( player ) { var speed = 5; var deltat = 1.0; var vx = (player.pos.x - this.screen.x); var vy = (player.pos.y - this.screen.y); var mag = Math.sqrt(Math.pow(vx,2) + Math.pow(vy,2)); var ux = vx/(mag); var uy = vy/(mag); this.screen.x = this.screen.x + ux*deltat*speed - ig.system.width/2; this.screen.y = this.screen.y + uy*deltat*speed - ig.system.height/2; } // Update all entities and backgroundMaps if( !this.showStats ) { this.parent(); } else { if( ig.input.state( 'contine' )){ this.showStats = false; this.levelExit.nextLevel(); this.parent(); } } },