physics.pause() in Game template

I opened a new game template and I noticed in the level1.lua script this code:

– We need physics started to add bodies, but we don’t want the simulaton

– running until the scene is on the screen.

physics.start()

physics.pause()

I’m not sure why it’s pausing physics straight after starting it? In other game tutorials and templates this only seemed to be used to pause the physics engine when pausing the game. Also the physics as far as I can see doesn’t seem to be paused as objects still go up and fall back down again.

This is pretty simple actually.

It takes time to create a scene (milliseconds, but still time). If you’re using a scene manager like composer you may be transitioning the scene on screen (fade from black, crossFade with the previous scene, slide on to the screen, etc.) These take times again, usually in milliseconds but time that is visible and after the first frame update after you created the scene.

Take the “HelloPhysics” sample (doesn’t use a scene manager) for example.  If you are doing this using Composer, you would likely have a menu scene with a play button and then fade the game scene in over say 500ms (half a second).

If the physics simulation is actually running while scene:create() is finishing or any time before scene:show() provide’s a “did” phase (meaning the scene is fully on screen and ready to interact with), that crate will fall to the ground before anyone see’s the game scene. Scenes can also be preloaded which calls scene:create(). This means you can’t really trust that the user is ready to interact with the scene while scene:create() runs and completes.  The only true signal that the scene is ready for interaction to wait on scene:show()'s “did” phase.

You wouldn’t want to frustrate your users by having your aliens blast them before you see them or your zombies eating them before they ever see the scene.

However,  you have to call physics.start() before you can add physics bodies.  So pausing physics will stop the simulation and still let you add your physics bodies.  When you’re ready for the simulation to start for real (i.e. scene:show()'s “did” phase, then you call physics.start() again to unpause physics and let the world take off for you.

Rob

Ok. And so why do other templates and tutorials not use this then?

They may not be composer or scene manager based and expect the objects to render and be interactive immediately. Perhaps they transition fast enough there isn’t enough time elapsing. Perhaps they didn’t think about it.

Feel free to not pause physics. To me, it’s a safety net.

Rob

@hasen6,  BTW - Which template(s) are you referring to.  Can you provide links or the name(s) of the templates?

I know in some of my templates pause physics during create and restart it once the scene has transitioned in.

They do use it in other templates but only like in pause game or something. But the new game template has pause.physics running straight after starting it. That’s why I was confused about it.

EDIT: Actually I only saw physics.pause in a tutorial template. Other templates didn’t seem to use it.

Also, the physics engine has to be started before you can add any physics bodies to objects*.

Try the following code in a blank project…

local physics=require("physics") local foo = display.newRect(100,100,100,100) physics.addBody(foo, "dynamic") physics.start()

…and then compare it to this…

local physics=require("physics") local foo = display.newRect(100,100,60,60) physics.start() physics.addBody(foo, "dynamic")

So the physics engine has to be started in order to be able to add the physics properties to the display objects.  As has already been mentioned you may not want things moving about and interacting until the scene is fully on screen so the engine is paused until the right time at which point the engine is started again and things can get going.

I hope this makes sense (it does in my head!).

[edit]

*which I’ve just noticed Rob mentioned in his post that I obviously didn’t read fully.  Sorry Rob.

Ok I see. Not sure why templates don’t use it. Maybe my search is not working but I couldn’t find physics.pause() used in Ponywolf’s templates like the Sticker Knight Platformer and the Endless Skateboarder. They don’t seem to be used at all, never mind right after starting physics.

Endless skateboarder doesn’t have moving objects other than the player which will stand still until you interact with the player. Sticker Knight limits where enemies move and it may not be a necessary action. Enemies don’t attack you right away. Though I would argue that sticker Knight could benefit from it. Every developer can do what they want. Things like pausing physics is what I consider a best practice. developer X might have a different opinion. Welcome to the world of programming.

EDIT: Trying to reply from my phone was an editing disaster. Sorry for the bad grammar…

Rob

Ok I see. Thanks. I just figured physics.pause was to stop the physics engine from working…like well…like pausing a video. And with the tutorial I watched using physics.pause when the game was paused and stated it ‘paused all physics in the game’ just went further to cement that belief.

The primary purpose of physics.pause() is to let you pause your game.  But this is exactly why we are doing it immediately after calling physics.start() but before any bodies are added. The simulation should be in a paused state until you’re actually ready for it to run, then you unpause it by calling physics.start() a second time.

Rob

OK I see. But I noticed that even without calling physics.start() a second time, the characters can jump endlessly and are continuously affected by physics. That’s also what confused me. Like for example with the new game template there is a crate that falls down at the beginning. But if I add a button to the crate so I touch it to make it jump, it can jump over and over and still falls back down again.

Maybe there’s something I’m not understanding quite right about how this works. It was my thinking that if physics was paused then jumping after this point should not be affected by gravity?

That can only be true if start() was called again somewhere.

This is pretty simple actually.

It takes time to create a scene (milliseconds, but still time). If you’re using a scene manager like composer you may be transitioning the scene on screen (fade from black, crossFade with the previous scene, slide on to the screen, etc.) These take times again, usually in milliseconds but time that is visible and after the first frame update after you created the scene.

Take the “HelloPhysics” sample (doesn’t use a scene manager) for example.  If you are doing this using Composer, you would likely have a menu scene with a play button and then fade the game scene in over say 500ms (half a second).

If the physics simulation is actually running while scene:create() is finishing or any time before scene:show() provide’s a “did” phase (meaning the scene is fully on screen and ready to interact with), that crate will fall to the ground before anyone see’s the game scene. Scenes can also be preloaded which calls scene:create(). This means you can’t really trust that the user is ready to interact with the scene while scene:create() runs and completes.  The only true signal that the scene is ready for interaction to wait on scene:show()'s “did” phase.

You wouldn’t want to frustrate your users by having your aliens blast them before you see them or your zombies eating them before they ever see the scene.

However,  you have to call physics.start() before you can add physics bodies.  So pausing physics will stop the simulation and still let you add your physics bodies.  When you’re ready for the simulation to start for real (i.e. scene:show()'s “did” phase, then you call physics.start() again to unpause physics and let the world take off for you.

Rob

Ok. And so why do other templates and tutorials not use this then?

They may not be composer or scene manager based and expect the objects to render and be interactive immediately. Perhaps they transition fast enough there isn’t enough time elapsing. Perhaps they didn’t think about it.

Feel free to not pause physics. To me, it’s a safety net.

Rob

@hasen6,  BTW - Which template(s) are you referring to.  Can you provide links or the name(s) of the templates?

I know in some of my templates pause physics during create and restart it once the scene has transitioned in.

They do use it in other templates but only like in pause game or something. But the new game template has pause.physics running straight after starting it. That’s why I was confused about it.

EDIT: Actually I only saw physics.pause in a tutorial template. Other templates didn’t seem to use it.

Also, the physics engine has to be started before you can add any physics bodies to objects*.

Try the following code in a blank project…

local physics=require("physics") local foo = display.newRect(100,100,100,100) physics.addBody(foo, "dynamic") physics.start()

…and then compare it to this…

local physics=require("physics") local foo = display.newRect(100,100,60,60) physics.start() physics.addBody(foo, "dynamic")

So the physics engine has to be started in order to be able to add the physics properties to the display objects.  As has already been mentioned you may not want things moving about and interacting until the scene is fully on screen so the engine is paused until the right time at which point the engine is started again and things can get going.

I hope this makes sense (it does in my head!).

[edit]

*which I’ve just noticed Rob mentioned in his post that I obviously didn’t read fully.  Sorry Rob.

Ok I see. Not sure why templates don’t use it. Maybe my search is not working but I couldn’t find physics.pause() used in Ponywolf’s templates like the Sticker Knight Platformer and the Endless Skateboarder. They don’t seem to be used at all, never mind right after starting physics.