Fixing the rotation of a background group so that everything rotates around the display center.

Hi peeps,

Just need a quick hand with my rotation.

My game is set in space. My ship is fixed into position in the center of the screen, and as you look at it it does not move from this position.

All the background elements are placed into groups and then these are placed into one, larger than screen size group.

When you move the ship, the background group moves instead, to give the illusion that the ship is moving. The background group can rotate fine and move up,down, left & right at the moment.

The problem is the type of rotation. If you move the ship, to say the left of eg. planet earth, when you rotate the ship, the earth and the background group rotates but in a fixed position relative to the ship. ie. if you move to the left of planet earth, the planet rotates but rotates next to you on the right.

What I actually want is the background group to rotate around the display center so therefore giving the illusion of rotating around the ship. ie. if you move to the left of planet earth and then rotate, planet earth rotates in a wide circle around the ship (ie. display center).

I hope I have explained this properly. If you need clarification please ask.

I have included this crude diagram to help put my idea across. Hope it helps…

Looks like you’re going to have to calculate the circumference of a circle and identify the x/y based on some input from the player. 

Here’s some code from the code base:

http://developer.coronalabs.com/code/ball-light-sphere

http://developer.coronalabs.com/code/simple-particle-gravity-path-plotting

Thank you for your reply Panc, you have come to the rescue once again,

please be patient with me :slight_smile:

Am I wrong in thinking that it should be much simpler than this? As I am wanting the background to rotate around the screen’s ie. display center coordinates? ie. as you said I need to identify the x/y based on some input from the player. This is the center of my ship.

I have had a look at the examples and while the rotation looks right it doesn’t tell me if you can move the center point as you would a ship in space and the objects would rotate as described on my posts above.

My colleage has just giving me the idea that a physics joint connected from my ships center point to the coordinate of the background immediately below my ships center point would allow me to rotate the background how I want. Any thought’s?

If you’re using physics in your game, then yep, that might be the best way. I find that, in my apps, I try to avoid physics interactions as much as possible since they seem to be more trouble than they are worth. But I’d suggest you try it out and see if it jibes with what you’re looking for. 

As far as how difficult the examples are, they deal with calculating the circumference of a circle, plotting it and constantly updating the location of an object based on those two functions. There’s a ton of math that’s involved so it can get hairy. I’ve never attempted what you’re describing, and to be honest, you’re a braver person than I. Thinking about it’s implementation gives me a headache :wink:

Thanks again Panc for your reply :).

Since this is a space game I think physics is the way to go.

Its just the fact that I have a ship which stays fixed in the center of the screen which complicates things. And I’m not going to change this. I have considered taking the easy way out and rotating the ship on a fixed x,y background but this would be not as good as having everything rotate around you.

I’m trying an adapted version of this code: http://developer.coronalabs.com/code/orbital-rotation at the moment.

physics.addBody( myShip ) ; myShip.bodyType = “static”
physics.addBody( myBackgroundGroup ) ; myBackgroundGroup.bodyType = “dynamic”
local myJoint
local function makeJoint(event)
myJoint = physics.newJoint(“pivot”, myBackgroundGroup, myShip, myShip.x, myShip.y)
end
Runtime:addEventListener(“enterFrame”, makeJoint)

But its not working at all. The physics.newJoint expects both of the objects to be physics bodies. I have set the background group to dynamic, but because I need the ship to stay in the same place all the time it is problematic for this code. If you set it to dynamic it actually moves from its center position, which is not what I want, or if you set it to static the background will not move, but it will rotate, so I can’t check if its working or not. And with the rotation you get with the ship being dynamic, the background still rotates around the the same point which is the corner of the earth so the joint seems ineffective. I have tried changing the reference points on my background and its children with worse results.

I’m sure there is a way to do this whether its with joints or not, I am just not seeing it at the moment.

I am adamant that I will figure this out :slight_smile: Any other way to do this game would be a bit pants. Does anyone have any ideas?

I’ll try and simplify this for others who are reading and who might be able to help.

You have the display’s center point, and you have the background group, which I need to connect to each other in some way to enable the following: I need to be able to scroll around the background group like any other game map, and I need to be able to rotate this background group around the screens center point which is where the ship is set.

(Please note that I already have the controls in place to move and rotate the ship/background, its just rotating the background group around the screen center while being able to scroll around the background group which is the problem).

The solution to the problem should be quite simple considering that I only have two things to connect which is the background group and the ship.

But the simple solution is evading me at the moment… so if anyone has any idea’s, please share :slight_smile:

I didn’t realize you wanted to rotate everything, and not just the Earth object. Have you taken a look at thisjoystick code? I think it might be what you are driving at. Try the “advanced” and “2 stick” examples. 

Thanks again for your reply Panc,

Since I am a beginner to Corona and although I know where the code is which I need to use, I’ve had a hard time deciphering exactly how I can take it and adapt it to my own code… Is there a simpler solution? This question is open to anyone who has the answer…

Thanks for your help in advance :slight_smile:

I’ve seen this same exact question several times in the past week. The solution/option that other ppl most often suggest is using weld joints on your physics objects. I’d check that out if I were you.

PS: if this is a space game then physics would be the wrong way to go, because outer space doesn’t have any friction :wink:

Looks like you’re going to have to calculate the circumference of a circle and identify the x/y based on some input from the player. 

Here’s some code from the code base:

http://developer.coronalabs.com/code/ball-light-sphere

http://developer.coronalabs.com/code/simple-particle-gravity-path-plotting

Thank you for your reply Panc, you have come to the rescue once again,

please be patient with me :slight_smile:

Am I wrong in thinking that it should be much simpler than this? As I am wanting the background to rotate around the screen’s ie. display center coordinates? ie. as you said I need to identify the x/y based on some input from the player. This is the center of my ship.

I have had a look at the examples and while the rotation looks right it doesn’t tell me if you can move the center point as you would a ship in space and the objects would rotate as described on my posts above.

My colleage has just giving me the idea that a physics joint connected from my ships center point to the coordinate of the background immediately below my ships center point would allow me to rotate the background how I want. Any thought’s?

If you’re using physics in your game, then yep, that might be the best way. I find that, in my apps, I try to avoid physics interactions as much as possible since they seem to be more trouble than they are worth. But I’d suggest you try it out and see if it jibes with what you’re looking for. 

As far as how difficult the examples are, they deal with calculating the circumference of a circle, plotting it and constantly updating the location of an object based on those two functions. There’s a ton of math that’s involved so it can get hairy. I’ve never attempted what you’re describing, and to be honest, you’re a braver person than I. Thinking about it’s implementation gives me a headache :wink:

Thanks again Panc for your reply :).

Since this is a space game I think physics is the way to go.

Its just the fact that I have a ship which stays fixed in the center of the screen which complicates things. And I’m not going to change this. I have considered taking the easy way out and rotating the ship on a fixed x,y background but this would be not as good as having everything rotate around you.

I’m trying an adapted version of this code: http://developer.coronalabs.com/code/orbital-rotation at the moment.

physics.addBody( myShip ) ; myShip.bodyType = “static”
physics.addBody( myBackgroundGroup ) ; myBackgroundGroup.bodyType = “dynamic”
local myJoint
local function makeJoint(event)
myJoint = physics.newJoint(“pivot”, myBackgroundGroup, myShip, myShip.x, myShip.y)
end
Runtime:addEventListener(“enterFrame”, makeJoint)

But its not working at all. The physics.newJoint expects both of the objects to be physics bodies. I have set the background group to dynamic, but because I need the ship to stay in the same place all the time it is problematic for this code. If you set it to dynamic it actually moves from its center position, which is not what I want, or if you set it to static the background will not move, but it will rotate, so I can’t check if its working or not. And with the rotation you get with the ship being dynamic, the background still rotates around the the same point which is the corner of the earth so the joint seems ineffective. I have tried changing the reference points on my background and its children with worse results.

I’m sure there is a way to do this whether its with joints or not, I am just not seeing it at the moment.

I am adamant that I will figure this out :slight_smile: Any other way to do this game would be a bit pants. Does anyone have any ideas?

I’ll try and simplify this for others who are reading and who might be able to help.

You have the display’s center point, and you have the background group, which I need to connect to each other in some way to enable the following: I need to be able to scroll around the background group like any other game map, and I need to be able to rotate this background group around the screens center point which is where the ship is set.

(Please note that I already have the controls in place to move and rotate the ship/background, its just rotating the background group around the screen center while being able to scroll around the background group which is the problem).

The solution to the problem should be quite simple considering that I only have two things to connect which is the background group and the ship.

But the simple solution is evading me at the moment… so if anyone has any idea’s, please share :slight_smile:

I didn’t realize you wanted to rotate everything, and not just the Earth object. Have you taken a look at thisjoystick code? I think it might be what you are driving at. Try the “advanced” and “2 stick” examples. 

Thanks again for your reply Panc,

Since I am a beginner to Corona and although I know where the code is which I need to use, I’ve had a hard time deciphering exactly how I can take it and adapt it to my own code… Is there a simpler solution? This question is open to anyone who has the answer…

Thanks for your help in advance :slight_smile:

I’ve seen this same exact question several times in the past week. The solution/option that other ppl most often suggest is using weld joints on your physics objects. I’d check that out if I were you.

PS: if this is a space game then physics would be the wrong way to go, because outer space doesn’t have any friction :wink: