Jump to content

sprite rotation. Get updated position?


jilonor
 Share

Recommended Posts

Hi, 

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.scale.setTo(size);
    this.planetSprite.anchor.setTo(0.5);
    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?

thanks

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

 Share

  • Recently Browsing   0 members

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