Best way to use physics in asynchronous groups?

I’m creating a spaceship game where the asteroids are all in a display group (foreGroup) and the spaceship is in a different group. As the player moves around, the asteroid group moves around on the screen relative to the ship. 

TLDR: When the asteroids group moves, the ship remains in the center. The issue is that the physics bodies don’t work anymore. What’s the best way to fix this? My current train of thought is to move the ship with the asteroids, but I’m having issues figuring out how. 

I can’t think of any useful code for figuring this out, but if you want it I can provide it.

This is a really common problem and you need to read the physics forum and guides to understand what’s going on.

However, in short, all display objects work from 0,0 relative to their parent display group. All physics objects work from 0,0 relative to the screen.

This means that if you move one group separate from another group, the display objects appear to move on the screen but the bodies will not, which means you will not get the collisions you’re expecting. Don’t do it.

When you want parallax, etc, in physics worlds you should be putting all display groups which contain display objects which have physics bodies into the same overall parent group. Move that group and you’ll be fine.

If you want to, for example, keep one character in the centre of the screen while you’re moving it within it’s parent group, you should move it, then use the :localToContent() function to work out it’s location relative to the screen’s 0,0. Then you can work out where to position it’s overall parent group.

I’ve made some changes based on your input, but I still can’t figure out :localToContent despite the documentation. The spaceship and the asteroids are in the same layer now, and I’ve been trying the following command to position the spaceship:

ship.x, ship.y = ship:localToContent(ship.x, ship.y)

Am I completely lost here? My thinking was to reposition the ship to the stage coordinates, but this isn’t working. Where am I going wrong? 

Looks like you’re trying to make a basic tracking camera system, where the player is at the center of the world (or offset if you prefer) and the world moves around the player as the player moves to keep the player in the ‘same’ position relative to the screen.

camera.gif

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/camera/

Is that a fair assessment of what you want to do?

If the answer is, “YES”:

  1. Download current version of SSK2 validation app: https://github.com/roaminggamer/SSK2/raw/master/validation.zip

  2. Run test #8 and confirm this is basically what you want. (Click and drag on scene to move player with virtual joystick).

  3. Open the sub-folder: tests/camera/ and see the file 001_tracking.lua for how it was made.

  4. Optionally get the latest version of SSK2 here: https://github.com/roaminggamer/SSK2

validationsampler.jpg

Yes, that looks like that will do. 

Good deal.  Good luck on your project.  I think once you get the hang of it, using my layering and camera system will make your life much easier.

Thanks. Before I jump in, where can I find the legal licensing information? I rooted around on the website for a bit, but was unable to find anything. 

While I very much appreciate you wanting to comply with licensing rules (I really do).  

I have never bothered to place any of the recognized licenses on this collection of code.

Essentially, as long as you don’t package up SSK2 itself and try to make money off it as a standalone product I don’t care what you do with it.

Use it freely to make games and apps to your hearts content.

If you’re still nervous about licensing, then I suggest examining the code and borrowing the bits that are useful to you by writing your own modules based on mine.    

Thanks again for asking.  

Note: Ask anyone here by the way.  I’ve been around for years and SSK has been around for years, always free (except for that one time where I experimented with monetizing it… total failure).   So free it is.

This is one of the old product pages where I tried monetizing and gave up: https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2

One more note.  If this is a issue because you need to incorporate this into a professional product you are making for a third party, let me know and we’ll figure out a way to make your client happy.

This is a really common problem and you need to read the physics forum and guides to understand what’s going on.

However, in short, all display objects work from 0,0 relative to their parent display group. All physics objects work from 0,0 relative to the screen.

This means that if you move one group separate from another group, the display objects appear to move on the screen but the bodies will not, which means you will not get the collisions you’re expecting. Don’t do it.

When you want parallax, etc, in physics worlds you should be putting all display groups which contain display objects which have physics bodies into the same overall parent group. Move that group and you’ll be fine.

If you want to, for example, keep one character in the centre of the screen while you’re moving it within it’s parent group, you should move it, then use the :localToContent() function to work out it’s location relative to the screen’s 0,0. Then you can work out where to position it’s overall parent group.

I’ve made some changes based on your input, but I still can’t figure out :localToContent despite the documentation. The spaceship and the asteroids are in the same layer now, and I’ve been trying the following command to position the spaceship:

ship.x, ship.y = ship:localToContent(ship.x, ship.y)

Am I completely lost here? My thinking was to reposition the ship to the stage coordinates, but this isn’t working. Where am I going wrong? 

Looks like you’re trying to make a basic tracking camera system, where the player is at the center of the world (or offset if you prefer) and the world moves around the player as the player moves to keep the player in the ‘same’ position relative to the screen.

camera.gif

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/camera/

Is that a fair assessment of what you want to do?

If the answer is, “YES”:

  1. Download current version of SSK2 validation app: https://github.com/roaminggamer/SSK2/raw/master/validation.zip

  2. Run test #8 and confirm this is basically what you want. (Click and drag on scene to move player with virtual joystick).

  3. Open the sub-folder: tests/camera/ and see the file 001_tracking.lua for how it was made.

  4. Optionally get the latest version of SSK2 here: https://github.com/roaminggamer/SSK2

validationsampler.jpg

Yes, that looks like that will do. 

Good deal.  Good luck on your project.  I think once you get the hang of it, using my layering and camera system will make your life much easier.

Thanks. Before I jump in, where can I find the legal licensing information? I rooted around on the website for a bit, but was unable to find anything. 

While I very much appreciate you wanting to comply with licensing rules (I really do).  

I have never bothered to place any of the recognized licenses on this collection of code.

Essentially, as long as you don’t package up SSK2 itself and try to make money off it as a standalone product I don’t care what you do with it.

Use it freely to make games and apps to your hearts content.

If you’re still nervous about licensing, then I suggest examining the code and borrowing the bits that are useful to you by writing your own modules based on mine.    

Thanks again for asking.  

Note: Ask anyone here by the way.  I’ve been around for years and SSK has been around for years, always free (except for that one time where I experimented with monetizing it… total failure).   So free it is.

This is one of the old product pages where I tried monetizing and gave up: https://marketplace.coronalabs.com/asset/super-starter-kit-2-ssk2