Jump to content

sprite rotation. Get updated position?


Recommended Posts


I have been generating planet systems. All planets are set to pivot around the same point. There is no physics involved, just static display.

    this.planetSprite = game.add.sprite(x, y, skin);
    this.planetSprite.pivot.x = orbite/size;
    this.planetSprite.rotation = getRandomInt(0,360);

The rotation and display is working fine but the coordinates are not updated after rotation. Is there a specific property to call to get new coordinates or should I manually calculate it?


Link to comment
Share on other sites

The sprite's position never actually changes, which is why you don't get a change in the coordinates.  If you want the apparent position of the planet use something like this:

apparent_x = x + Math.cos(rotation)*orbit_radius*scale;

apparent_y = y + Math.sin(rotation)*orbit_radius*scale;

EDIT: Needing to multiply by scale depends on whether you want scale to affect the orbit radius or not.  That's probably obvious, I just wanted to clarify.

Link to comment
Share on other sites

Thanks for the answer :)

Also, when I inspect the sprite properties, there is the previousPosition one which seems to contain the correct coordinates but appears to be updated asynchroniously or something... not sure.

well to be accurate, I made this function in the end :

function GetRotated(cx, cy, x, y, angle) {
    var radians = (Math.PI/180) * angle;
    var translatedX = (x - cx) * Math.cos(radians) - (y - cy) * Math.sin(radians) + cx;
    var translatedY = (x - cx) * Math.sin(radians) - (y - cy) * Math.cos(radians) + cy;
    return {x: parseInt(translatedX), y: parseInt(translatedY)};


Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...