Yanifska Posted May 10, 2015 Share Posted May 10, 2015 Hi, I have a revolving planet, object are spawning randomly and need to move with the planet from where they have been spawned.However, I am having a hard time positioning them: they jump out of their expected position at the moment I add them to the revolving planet group .I believe it is because I don't take into account the rotation. So my question is: how can I correct the position of my objects in relation to the rotation of the planet. thank you a lot !Yaniv Link to comment Share on other sites More sharing options...
boolean Posted May 10, 2015 Share Posted May 10, 2015 It's a bit hard to tell without seeing any code, but here's my guess. When you add your satellite objects to the parent planet, their position becomes relative to the parent. So if your planet is in the very center of the screen (eg. x:200,y:200) and your satellite is in the very top left of the screen (eg. x:0,y:0), when you call addChild(satellite) on your parent planet your satellite will jump to the middle of the screen because it's position is now x:0,y:0 relative to its parent (which is in the middle of the screen). The tricky part comes when you want to keep your old position, but still take into account its relative positioning. In this case you originally had the satellite at a world position of x:0 and y:0, so when you child it to the planet their sprite.x will be 0, but their actual sprite.world.x will be 200. Unfortunately I'm not 100% sure how to get the sprite back to its old relative position, but if you know the distance to set the relative positions you can just do that (eg. you know all the satellites should be at -100px from the center of the parent because the planet is 70 pixels large). Link to comment Share on other sites More sharing options...
Yanifska Posted May 10, 2015 Author Share Posted May 10, 2015 hey man,you nailed it and you confirmed my thoughts. For now I correct the position relatively to the offset to the origin, but it is not enough.Because the planet is rotating, I need to take in account the "circular distance" that has been covered. I wonder if there is something automated in Phaser to keep the "screen position" when parenting objects.If not, I will have to check on good old Pythagoras. Anyway thank you for passing by Link to comment Share on other sites More sharing options...
Yanifska Posted May 10, 2015 Author Share Posted May 10, 2015 forget Pythagoras!after spending an hour or two trying to reinvent the wheel I just found the right formula on google so here we go:// when we add the stars to the rotating group they jump out of placethis.items.add(this.star); // reposition the stars relatively to the planetthis.star.x -= this.items.x; this.star.y -= this.items.y; //correct the position of the stars to take into account the rotation of the planetvar correctAngle = this.ground.rotation * -1;var x1 = this.star.x;var y1 = this.star.y;//the formula to calculate rotated X and Yvar x2 = x1*Math.cos(correctAngle) - y1*Math.sin(correctAngle);var y2 = x1*Math.sin(correctAngle) + y1*Math.cos(correctAngle);//bingo!this.star.x = x2;this.star.y = y2; It's late and I didn't test it extensively but I guess it is the way to go.Yaniv Link to comment Share on other sites More sharing options...
boolean Posted May 10, 2015 Share Posted May 10, 2015 Awesome, glad you got it sorted Link to comment Share on other sites More sharing options...
Recommended Posts