How to use Composer and Physics together?

If I have a start menu that has physics on it and hit the play button to go to another scene with physics in it the physics body’s seem to stay. I was just wondering is there a way to just turn off all physics body’s in sceneGroup some how. I currently handle my levels this is with:

composer.recycleOnSceneChange = true

which takes along time to load.

Thanks,

Scott 

You’re not inserting your objects into the scene group.

If you don’t manage the objects with scene’s sceneGroup, it can’t clean up.

We talked pretty extensively about the ‘Composer’ scene manager on the Corona Geek hangout:

http://coronalabs.com/blog/coronageek/composer-library-series-hangout-highlights/

, and I wrote up a number of samples for the hangout here:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

Also, if you Google ‘corona sdk composer blog’ you’ll find plenty of articles on this.  Read them and you’ll see what I mean by sceneGroup and managing objects.

Unless I missed the point of your question… Are the objects managed, but you simple want to remove their bodies while the scene is ‘hidden’?

If so, there is no easy solution.  You’d need to remove the bodies yourself while retaining the objects:

https://docs.coronalabs.com/daily/api/library/physics/removeBody.html

Calling physics.stop() seems to remove physics bodies.  With composer, if you’re not transitioning the objects off screen (i.e. fade, crossFade), the physics bodies will stay on screen and be active even if the scene has been hidden. If you transitioned them off screen, they would be out of the way for any new bodies you created.

An easy way to solve this is simply call physics.stop() in the menu scene’s scene:hide() function. Then if you remove the scene when you come back to menu.lua, scene:create() will fire and add the physics bodies back when it re-creates the scene. If you don’t want to remove the scene, you should probably consider adding the physics bodes in the scene:show()'s will phase.

Rob

thank you,

scott

Hi Rob

I have also been encountering the same challenge with persisting physics bodies even when their display objects have been removed. Changing the transition effect of composer.gotoScene() from “fade” to other types eg “slideDown” and using physics.stop() only makes the physics bodies invisible even in the physics.setDrawMode(“hybrid”).

Is there any other way how physics bodies of previous scenes can be eliminated?

thanks
Sedrack

Hi Sedrack,

So if I understand, you want to completely destroy/eliminate the physics bodies when you leave the scene, and you won’t need them ever again? For example, if the player returns to that scene, you’ll re-create them and you won’t need any memory of the ones that existed before? If so, you should consider destroying the scene entirely, not just “recycling” it. That should remove the physics bodies entirely… if it doesn’t, then maybe you didn’t insert them into the scene’s view group.

Best regards,

Brent

Hi;

Brent,I think I did not have all my objects placed in one scene group using self.view because even after I set recycleOnSceneChange in composer.removeScene() to false, the bodies still persisted.

However, I found another way to eliminate them. Since the scenes change after a collision,using timer.performWithDelay(), I queued all the physics bodies to be removed 10milliseconds after the collision which transitions scenes. Hope this doesn’t affect performance.

I also run into this source code( https://www.dropbox.com/s/vznlv750x5fpl1z/Flappy%20Bat%20-%20Composer.zip ) which I think my be helpful to anyone facing challenges with implementing physics while using composer thanks to Mark Falkland

Thanks
Sedrack

You’re not inserting your objects into the scene group.

If you don’t manage the objects with scene’s sceneGroup, it can’t clean up.

We talked pretty extensively about the ‘Composer’ scene manager on the Corona Geek hangout:

http://coronalabs.com/blog/coronageek/composer-library-series-hangout-highlights/

, and I wrote up a number of samples for the hangout here:

http://github.com/roaminggamer/CoronaGeek/raw/master/Hangouts/composer_scene_manager.zip

Also, if you Google ‘corona sdk composer blog’ you’ll find plenty of articles on this.  Read them and you’ll see what I mean by sceneGroup and managing objects.

Unless I missed the point of your question… Are the objects managed, but you simple want to remove their bodies while the scene is ‘hidden’?

If so, there is no easy solution.  You’d need to remove the bodies yourself while retaining the objects:

https://docs.coronalabs.com/daily/api/library/physics/removeBody.html

Calling physics.stop() seems to remove physics bodies.  With composer, if you’re not transitioning the objects off screen (i.e. fade, crossFade), the physics bodies will stay on screen and be active even if the scene has been hidden. If you transitioned them off screen, they would be out of the way for any new bodies you created.

An easy way to solve this is simply call physics.stop() in the menu scene’s scene:hide() function. Then if you remove the scene when you come back to menu.lua, scene:create() will fire and add the physics bodies back when it re-creates the scene. If you don’t want to remove the scene, you should probably consider adding the physics bodes in the scene:show()'s will phase.

Rob

thank you,

scott

Hi Rob

I have also been encountering the same challenge with persisting physics bodies even when their display objects have been removed. Changing the transition effect of composer.gotoScene() from “fade” to other types eg “slideDown” and using physics.stop() only makes the physics bodies invisible even in the physics.setDrawMode(“hybrid”).

Is there any other way how physics bodies of previous scenes can be eliminated?

thanks
Sedrack

Hi Sedrack,

So if I understand, you want to completely destroy/eliminate the physics bodies when you leave the scene, and you won’t need them ever again? For example, if the player returns to that scene, you’ll re-create them and you won’t need any memory of the ones that existed before? If so, you should consider destroying the scene entirely, not just “recycling” it. That should remove the physics bodies entirely… if it doesn’t, then maybe you didn’t insert them into the scene’s view group.

Best regards,

Brent

Hi;

Brent,I think I did not have all my objects placed in one scene group using self.view because even after I set recycleOnSceneChange in composer.removeScene() to false, the bodies still persisted.

However, I found another way to eliminate them. Since the scenes change after a collision,using timer.performWithDelay(), I queued all the physics bodies to be removed 10milliseconds after the collision which transitions scenes. Hope this doesn’t affect performance.

I also run into this source code( https://www.dropbox.com/s/vznlv750x5fpl1z/Flappy%20Bat%20-%20Composer.zip ) which I think my be helpful to anyone facing challenges with implementing physics while using composer thanks to Mark Falkland

Thanks
Sedrack