# How to make sprite move in the direction it's facing?

## Recommended Posts

I'm making a game with Asteroid like controls, but I'm in doubt about how to move the sprite in the direction it is facing.
What's the easiest way to do this?

##### Share on other sites

Sorry for the short answer but have a look at using vectors and vector-based maths rather than cartesians points to move your sprites around, it greatly simplifies this type of movement. Many physics libraries can help you out.

##### Share on other sites

Cool, thanks. I will. Pixi doesn't have anything like that?

##### Share on other sites

Nope, pixi just draws stuff really really fast! Actually it does interactive stuff too but thats by-and-by.

I can vouch for P2 and also matterJS, both allow any rendering front-end to be bolted on (not all physics libraries do, some like to be tied to the rendering, which, in my opinion is plain wrong, do one thing well), in my experience P2 was easier to integrate, its a very powerful package. Both libraries have a pixi example in their source repos.

You should be able to find examples of bolting either physics lib and pixi together, or if you know phaser then phaser packages P2 if you dont mind using slightly older versions.

If you want to roll your own though, a simplified physics library just to help you move stuff around, its not too difficult (if you know your maths a bit) and a good exercise. The physics only gets hard when you want a fairly realistic simulation, stuff like collisions and everything can get hairy, just moving around isn't too difficult.

##### Share on other sites

You don't need a physics library for that. Simple trigonometry is enough.

``````function move(object, distance) {
object.x = object.x + distance * Math.cos(object.rotation);
object.y = object.y + distance * Math.sin(object.rotation);
}
``````

`https://jsfiddle.net/11av8s0m/`

##### Share on other sites

Better use Ticker and mutiply that thing on delta time.