The type name says it all, I'm trying to move the object forward depending on the angle it is at.
Here is my code:
xView = this.x-this.width/2; yView = this.y-this.height/2; playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, xView, yView, this.width, this.height); playerCtx.save(); playerCtx.clearRect(0, 0, game.width, game.height); playerCtx.translate(xView, yView); playerCtx.rotate(angle *Math.PI/180); playerCtx.drawImage(imgSprite, 0, 0, this.width, this.height, -xView, -yView, this.width, this.height); playerCtx.restore(); } if(Game.controls.left) { angle-=1; if(Game.controls.up){ this.x += this.speed * Math.cos(angle * Math.PI / 180); this.y -= this.speed * Math.sin(angle * Math.PI / 180); }
The object does not move according to var angle .
EDIT
I could not understand why my code did not work, so I used a sprite sheet containing 36 different angles. This works, but the rotation is too fast. If someone could tell me why the above does not work properly, or how I would like to make the following function slower:
if(Game.controls.right) { currentFrame++; angle+=10; }
By slower, I mean when the left key is held down, angle+10; currentFrame++; angle+10; currentFrame++; rises to speed, and adding more frames may take too long.
EDIT
Added Jfiddle for my original question, the angle variable moves with rotation, for example, if the object is facing right, the angle will be 90, but the object still does not look like moving it to the right, although the camera does.
javascript html5-canvas rotation canvas 2d
user2582299
source share