stop physics

If you add this to the eggs sample included it crashes the simulator if you press the stop physics button

local function stopThePhysics ( event ) physics.stop() end local stopButton = ui.newButton{ default = "buttonRed.png", over = "buttonRedOver.png", x = 160, y = 350, onPress = stopThePhysics, text = "Stop Physics", emboss = true } [import]uid: 8561 topic_id: 2084 reply_id: 302084[/import]

I think the only place you can safely call the physics.stop() operation in in onSystem->applicationExit (which may be a redundant call anyway).

Use physics.pause() instead & all should be fine. This is probably the behaviour you want anyway. [import]uid: 3953 topic_id: 2084 reply_id: 6263[/import]

Hey thanks!
I’ll give that a try.
[import]uid: 8561 topic_id: 2084 reply_id: 6267[/import]

physics.stop() tears down the Box2D world. I think it’ll currently work fine if you remove all the physics bodies first (although it shouldn’t crash in any case!)

Presumably, the expected physics.stop() behavior with physics objects onscreen is that all the objects get turned back into non-physical display objects. As MarkHenryC says, this is probably less useful in general than calling physics.pause(). [import]uid: 3007 topic_id: 2084 reply_id: 6415[/import]

Ok…great…actually in my obj c game I was destroying the world after each level and creating a new world for each level. So I thought I would use the same model.
[import]uid: 8561 topic_id: 2084 reply_id: 6418[/import]

I just spent 3 days trying to find the cause of a crash bug in my app., and it turns out to be caused by calling physics.stop().

Basically I set up my physics world for each level, and tear it down whenever I reset or start a new level. I call physics.stop() when cleaning up the old level. If I don’t then there are some residual physical effects on the next level loaded.
The crash is intermittent, happening any time between 2 and 20 times after I reset the old level, which made it very difficult to track down (“bus error” console messages aren’t very useful, either).

So there is something flaky going on with physics.stop(). It’s quite possible I haven’t removed every single physics object before I call it, but as evank said 9 months ago, it still shouldn’t crash in either case!
[import]uid: 9422 topic_id: 2084 reply_id: 41201[/import]

Trying to figure out a workaround that let me still use physics.stop() and the only one that seems to work is to be super-anal about removing EVERY SINGLE object used in the simulation before calling physics.stop().

So if all your physics objects are in a layer group called, say “layer_physical”, then something like this before calling physics.stop() should prevent the crash:

for i = layer_physical.numChildren, 1,-1 do
layer_physical[i]:removeSelf()
end

Notice the for loop is moving backwards through the list of objects since if you go forward from 1 to numChildren to delete them it will actually skip every other object due to the way indexing works on a shrinking list of objects.

I still think there should be a better warning from Corona than a bus error crash in the event that there are some physics objects left in the simulation after physics.stop() is called. Seems like a pretty common and easy mistake to make (especially for programming newbs like me!) [import]uid: 9422 topic_id: 2084 reply_id: 41406[/import]