# Rotating around a point

## Recommended Posts

I need my space ship to follow the rotation of the planet it lands on. See example below:

My current approach is as follows:

`ship.position.x = planet.x;ship.position.y = planet.y;ship.pivot.set(planet.width);ship.rotation = planet.rotation;`

However, this results in the ship getting an incorrect position and rotation. See this (quite laggy) example.

Ideally, I would like to be able to write:

`ship.pivot.set(planet.position); // rotate around this global positionship.rotation = planet.rotation;`

Any ideas?

##### Share on other sites

Just a thought: When your ship lands and the planet is already at lets say 45° and you just copy that then the ship will be set at 45° too.

It would make more sense if you increment the ships rotation by the same amount that you increment the planets rotation with.

##### Share on other sites

I think the easiest way to do this would be to put the planet and the ship in a container (DIsplayObjectContainer) and then rotate the container.

##### Share on other sites

• 2 weeks later...

I think the easiest way to do this would be to put the planet and the ship in a container (DIsplayObjectContainer) and then rotate the container.

I tried it, but couldn't get it working. Luckily, it turned out to be pretty easy to handle the rotation "manually". Here's the gist of it:

`    var planet = shipEntity.misc.landedOnPlanet;    var rot = planet.position.rotation - shipEntity.misc.startRotation;    shipEntity.position.rotation = rot - Math.PI / 2;    shipEntity.position.x = planet.position.x - Math.cos(rot) * planet.sprite.radius * 1.2;    shipEntity.position.y = planet.position.y - Math.sin(rot) * planet.sprite.radius * 1.2;`

where

`shipEntity.misc.startRotation = entity.position.rotation - Math.atan2(planet.position.y - shipEntity.position.y, planet.position.x - shipEntity.position.x);`

Here's another laggy video: http://recordit.co/I8QzIw

## 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.