# Rotate Sprite to face a specific coordinate

## Recommended Posts

I am trying to move my Arcade Sprite to a specific point (let's say it's (50|15) and the sprite is currently at (300|500)). Therefore I am using the moveTo() function.

The movement of the object works but the direction it is facing is completely wrong. So how can I let my sprite rotate to this point? (with a short transition would be very nice)

##### Share on other sites

There are Phaser.Math methods to compute the angle between two points or two coordinate pairs. Then you just tween sprite.body.angle.

##### Share on other sites

Thanks for your reply. Unfortunately I can't exactly find the methods you are mentioning in the docs. https://photonstorm.github.io/phaser3-docs/Phaser.Math.html

Can you maybe give me an example?

Pave

##### Share on other sites

Hi @Pave, I'm new into Phaser but maybe you can check this example: https://github.com/jackyrusly/jrgame

There's a "direction" parameter sent, is that what you are looking for?

Best

##### Share on other sites

Thanks @Damian Pastorini unfortunately this is just a sprite with animations, supporting only the four directions: UP, LEFT, DOWN and RIGHT.

In want to be able to rotate my sprite exactly to a point, so I need to use all 360 degree and not only the four directions.

##### Share on other sites

@samme thanks for the correct link. Now I found the Math function BetweenPoints().

Unfortunately I can't get it to work.

Here is part of my code:

``````class Enemy extends Phaser.Physics.Arcade.Sprite {

constructor (scene, x,y)
{
super(scene, x,y);
}

preUpdate (time, delta)
{
this.rotateFunc();
}

rotateFunc(){
let currentPoint = new Phaser.Geom.Point(this.x,this.y+this.displayHeight/2);
let pointToMoveTo = new Phaser.Geom.Point(Player.players.getChildren()[0].x, Player.players.getChildren()[0].y);
this.rotation = Phaser.Math.Angle.BetweenPoints(currentPoint, pointToMoveTo);
}
}
``````

In this example I want to rotate the enemy sprite to face the player sprite. For the error see the attached video.

##### Share on other sites

Is it that the enemy direction looks wrong?

For that you need a texture where the ship is facing rightwards, because that's the direction of rotation=0.

##### Share on other sites

You were correct! Thanks @samme

I could solve the problem by subtracting π/2 from the rotation.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.