Physics debugging

I have a problem with physics that I haven’t been able to solve. I’d post code, but I wouldn’t know where to start, as it’s spread across multiple modules.

A little background. In my game, which is under development, you flick ice cubes into glasses. Each level provides tougher challenges, but basically there are glasses placed in various positions and you have a certain number of cubes and have to get one in each glass without knocking any over.

At level init time, my level loader draws the new rocks (static physics objects) with the glasses (dynamic physics objects) on top of some. The glasses are initialized a few pixels above the rocks they are to rest on, and they settle into place nicely. So for the most part, it works.

Except when it doesn’t. In the current level config, every time the fifth level starts, the glass doesn’t sit down right and tumbles over, breaking on the ground below.

I’m not doing anything different based on which level is being played, and in fact, if I start on level five, I can’t recreate the problem. If I make that level the first and the fifth, it works fine on level 1 but fails on 5. I’m guessing there is some kind of cumulative effect building up that rears its ugly head by the time I get to the fifth level.

Anyone have an idea where I can start debugging? Or maybe a suggestion on how to better initialize my levels?

Thanks for reading this. Oh, and here is a screenshot to give an idea of what the game looks like.

http://mikesellsoftware.com/OnTheRocks.png [import]uid: 58455 topic_id: 15367 reply_id: 315367[/import]

Update: If I hardcode a physics.pause() for that level, I can see the glass resting just a few pixels above the surface of the rock, as I expected.

Head scratcher. [import]uid: 58455 topic_id: 15367 reply_id: 56761[/import]

Update #2. I set gravity to 0, 1.0 and then on a timer, reset to normal. On this level, the glass comes down at a 45 degree angle.

Still scratching head… [import]uid: 58455 topic_id: 15367 reply_id: 56781[/import]

I’ve had similar problems where objects in a newly created level have some “ghost” physics forces from a previous level affecting them. I thought I was rebuilding the levels from scratch each time, but as you’ve found out the physics simulation in Corona follows its own rules.

The only thing that works is to make sure you remove EVERY object in the physics world completely after a level. Then call physics.stop(). Then for the next level completely rebuild all the objects before calling physics.start().

Oh, and if you don’t delete and nil out EVERY SINGLE physics object in the old level you will get a very difficult to track down intermittent crash bug. Fair warning :slight_smile: [import]uid: 9422 topic_id: 15367 reply_id: 56795[/import]

Thanks for the tip. The crash bug isn’t so intermittent, though. It happens every time on launch. Probably has to do with the fact that event listeners holding onto references just got the rug pulled out from under them.

If I do this

physics.stop()  
resetLevel() -- cleanup all physics objects  
physics.start()  

the crash becomes intermittent, and the original problem is changed to collisions sometimes not being recognized.

Anyone from Ansca have any tips on level/physics cleanup? It’s times like this I start to consider C++/OpenGL again :-/ [import]uid: 58455 topic_id: 15367 reply_id: 56868[/import]

Fixed, kind of. It tried pausing and stopping physics, but the problem persisted. I tried stopping and starting fixes, and that introduced a bus error.

The workaround is to set glass.isFixedRotation=true on creation, and false on postCollision with a boulder. Certainly not a root cause fix, but at least I’m past it. [import]uid: 58455 topic_id: 15367 reply_id: 56933[/import]

Sorry, I wasn’t clear in my earlier post. The intermittent crash bug/bus error is what I was trying to describe if you do what you show in post #4 but somehow fail to completely remove all physics objects in your resetLevel() routine.

And it’s actually quite easy to miss a few physics objects during cleanup. I propose to anyone at Ansca who may be listening to add a new physics.clean() or some such command to completely clear out they physics simulation in one fell swoop.

In the mean time, here’s how I clear out my physics objects:

First, make sure you insert all the physics objects into the same display group as you create them. That way you can iterate through the group later when you clean up and not miss anything.

Then, on clean up, iterate BACKWARDS through the group to delete them all. If you iterate forwards it WON’T delete every other object in the group and you’ll either get the bus error crash or the ghost physics effects on the next level.

local groupPhysical --the group where you insert all physics objects  
  
-- create and insert physics objects  
-- call physics start  
-- play level  
-- call physics stop  
-- call cleanup function  
  
local function cleanupPhysicsObjects()  
 for i = groupPhysical.numChildren,1,-1 do --reverse order so won't skip items  
 groupPhysical[i]:removeSelf()  
end  
  
--now you can safely build a new level  

I had exactly your problem, and this is what solved it. No more bus error crash, no ghost physics, no need to hack in a fix that doesn’t address the root cause (which I, too, tried at first.)
[import]uid: 9422 topic_id: 15367 reply_id: 56961[/import]

Thanks, that worked for me with one small change. I call physics.stop(), cleanup physics objects, call physics.start(), THEN create and add physics objects for the new level.

When I tried physics.start() after adding objects, the simulator crashed. In fact, I thought it was documented that it was supposed to be called before any other physics functions (e.g. addBody). [import]uid: 58455 topic_id: 15367 reply_id: 56978[/import]