# move a rotated sprite

## Recommended Posts

I have a sprite that is rotated, how to move it forward?
so if the angle was 0, then it will go 100px to the right
and if 90, 100px downwards

but how to calculate the x and y pixels that the sprite will move

##### Share on other sites

• 1 month later...

You have several ways to go:

• if you use Arcade Physics, you have the velocityFromAngle() helper function ((example here).
• if you remember your Math lessons from school, you can apply sinus and cosinus to your angle and multiply your horizontal and vertical velocity with the results.
• if none of the above and you only move North / East / South or West, you can always use a good ol' switch

##### Share on other sites

```function create() {

this.rot = 0;
this.speedScalar = 1;

this.input.on("pointerdown", (pointer) => {
this.isDown = true;
});

this.input.on("pointerup", (pointer) => {
this.isDown = false;
});

this.input.on("pointermove", (pointer) => {
if (!this.isDown) {
return;
}

this.pointerX = pointer.x;
this.pointerY = pointer.y;

let rot = Math.atan2(
this.obj.y - this.pointerY,
this.obj.x - this.pointerX,
);

this.rot = rot;
this.obj.rotation = rot - Math.PI * 0.5;
});

}

function update() {

if (!this.isDown) {
return;
}

this.obj.x -= Math.cos(this.rot) * this.speedScalar;
this.obj.y -= Math.sin(this.rot) * this.speedScalar;

}```

You can use Math.atan2 to find rotation of the object and you can use Math.sin for y positioning and Math.cos for x positioning. You can test the example above in the Labs

##### Share on other sites

• 1 month later...

thanks, I've solved it

##### Share on other sites

• 3 months later...
On 11/14/2021 at 1:54 PM, talhaozdemir said:

You can use Math.atan2 to find rotation of the object and you can use Math.sin for y positioning and Math.cos for x positioning. You can test the example above in the Labs five nights at freddy's