Storyboard and Physics implementation question

Hi, 

Probably a basic Newbie question and apologies if its been answered elsewhere, i’ve looked but can’t seem to find a definitive answer…

But basically what is the correct way to setup and use physics when using the storyboard API?

In a storyboard scene i’ve declared the ‘local physics = require(physics)’ at the top of my code and outside any storyboard function making it local to that storyboard scene but where should I call ‘Physics.Start()’ and setup my physic bodies? and what is the correct way to pause, remove any physics bodies when leaving and returning to scenes?

One bit of documentation mentions I should put ‘physics.start()’ within the ‘scene:enterScene’ function but if i create my physic bodies from within the ‘scene:createScene’ they fail to run and I get a ‘Corona Simulator ERROR: physics.start() has not been called.’ message in the terminal output. Am i right in thinking i have to start and then pause the physics API at the start of the ‘scene:createScene’ function and then restart the Physics once the ‘scene:enterScene’ function is called?

Also when creating Physic bodies, do they have to be assigned to the storyBoard ScreenGroup? I’m guessing they do and If so what happens on purging, removing and re-entering Scenes and the Physics API, where should I be pausing or stopping the physics? Will bodies be deleted from the physics API once a scene is left and if so do i need to recreate them. I’m guessing I should approach them in the same way as when i’m working with display objects and groups from within the storyBoard API?

Any help or pointers would be greatly appreciated!!

Physics isn’t my strong suit, but I would guess you probably should initially require physics in main.lua, call .start() then .pause() to get the system initialized.  Then in your storyboard scenes, you could do physics.start() in enterScene to start things moving, and then physics.pause() in exitScene.

Let me see if Brent has any thoughts on this…

Hi @mrphil,

I would say, there’s not really any “rule” on this… just make sure the physics engine begins running from the start, so any change you make to its configuration (like setting gravity) works as expected. Where you create bodies is ultimately your decision, but I think “createScene” is logical.

If you’re getting that error message that physics hasn’t been started, then you need to just start up the engine and make sure the Lua reference to the physics handle is properly scoped. Typically, I just start the physics engine immediately after requiring the physics library. I don’t pause it unless I really need to pause it, like during a pause menu overlay or something like that.

As for managing physics bodies between scenes, think of them very much like other display objects… just that they have physics applied to them too. So, if you add them to the scene’s “self” group, they will be removed when you clear the scene, just like other display objects. If you need to keep these objects around, then it’s your responsibility to keep them there by not adding them to the self group or not fully clearing the scene… then just pause the physics engine and hide them (visually) from the user.

Brent

Physics isn’t my strong suit, but I would guess you probably should initially require physics in main.lua, call .start() then .pause() to get the system initialized.  Then in your storyboard scenes, you could do physics.start() in enterScene to start things moving, and then physics.pause() in exitScene.

Let me see if Brent has any thoughts on this…

Hi @mrphil,

I would say, there’s not really any “rule” on this… just make sure the physics engine begins running from the start, so any change you make to its configuration (like setting gravity) works as expected. Where you create bodies is ultimately your decision, but I think “createScene” is logical.

If you’re getting that error message that physics hasn’t been started, then you need to just start up the engine and make sure the Lua reference to the physics handle is properly scoped. Typically, I just start the physics engine immediately after requiring the physics library. I don’t pause it unless I really need to pause it, like during a pause menu overlay or something like that.

As for managing physics bodies between scenes, think of them very much like other display objects… just that they have physics applied to them too. So, if you add them to the scene’s “self” group, they will be removed when you clear the scene, just like other display objects. If you need to keep these objects around, then it’s your responsibility to keep them there by not adding them to the self group or not fully clearing the scene… then just pause the physics engine and hide them (visually) from the user.

Brent