moving objects while under influence of gravity and bouncing against other objects and walls

Hi -
Scenario:
I am developing a game similar to the game ‘float’ where objects are spawned at the top and fall to the ground due to gravity. The game objective it to touch the objects to keep them from falling down. In the process they bounce against other spawned objects. If they touch the ground the objects are removed.

Problem:
My code reliably handles multiple objects hitting the ground and they get removed correctly (without crashing the physics engine). But when I touch an object and try to apply applyLinearImpulse to the object, it crashes the physics engine (all physics bodies vanish from the screen).
This happens pretty randomly. I have tried all the suggestions that I found on these forums.
including suggestions from
http://jonbeebe.tumblr.com/post/2515495262/corona-collision-no-no

The basic concept is to minimize collision events which I have accomplished using collision filters. So I have eliminated most collision events between the objects. The only collision event that is triggered is, when the objects touch the ground.

In the actual touch event on the object (each object has its own collision event handler), I store the movement to be applied to the object and set a one time timer (after 1 ms). In that timer event handler, I pause the physics engine, make my changes to the object (applyLinearImpulse) and then restart the physics engine. (I tried setting object.is BodyActive = false instead of pausing physics and still got the same random crashes)
I have added lineardamping and angular damping to all objects to slow them down a bit.
But is seems like whatever I do, under some conditions (the objects are bouncing against each other and some are getting removed while hitting the ground), my move timer fires while physics engine is still calculating and this causes a crash. If I reduce the objects to about 5, that reduces the probability of crashes but they do happen randomly.
Is there a definitive way in corona to move objects while objects are controlled by physics? [import]uid: 21208 topic_id: 17482 reply_id: 317482[/import]

I haven’t experienced this myself but out of interest, are you using event.phase == “ended” or are you trying to make changes before collision has been resolved?

Peach :slight_smile: [import]uid: 52491 topic_id: 17482 reply_id: 66390[/import]

I am using event.phase == “ended” and within that I am setting a timer. The timer listener does the actual move. So theoretically, the code shouldn’t conflict with the physics calculation. But since objects are constantly bouncing against each other or hitting the ground, maybe the timer fires at the wrong time.
Is there a way the physics engine can notify corona that it is done with all its calculations/moves and allow some corona code to execute before continuing with physics? That is the only way I can think of avoiding this problem.

I have spent over 2 days making numerous changes but the random crashes persist. Any help is greatly appreciated.
[import]uid: 21208 topic_id: 17482 reply_id: 66520[/import]

Possibly using a pre collision event to cancel the timer so it doesn’t go off during collision might help your situation? Beyond that, is it crashing on “ended” even when you don’t use a timer? [import]uid: 52491 topic_id: 17482 reply_id: 66606[/import]

When the collision event fires, it usually does so multiple times, rather than just the once. (as an example, try; print(“something”) in the collison event, it’ll usually print a number of times for each collision)

You could try setting a switch in each objects collision handler so that the timer only fires once per collision.

For example, try setting “canTimerFire = true” for each object then set “canTimerFire = false” in the collision (event.phase == “began”)

When (event.phase == “ended”) occurs, set “canTimerFire = true” again.

If you have your timer within function set up with an enterFrame listener which only triggers when “canTimerFire = true” then this would eliminate any unwanted timers going off.

Does that make sense? [import]uid: 67933 topic_id: 17482 reply_id: 66811[/import]

It looks like removing objects inside collision handler is not a problem. (Seems like display.remove(object) waits for physics engine to complete)
But when I try to move the object, it crashes the game at random times. I do physics.pause() before moving the object and physics.start() at the end. Even then, the move object conflicts with the physics engine. No amount of flags seem to work. I tried setting a flag (donotmove) in precollision and reset it in postcollision. So theoretically, the move shouldn’t happen during collision, but in reality some conflicts happen that crashes the physics engine. I have put debug print statements everywhere but it looks like all remove_object and move_object functions complete correctly and then all physics bodies disappear at some random time. (No error/warning message and the actual display objects vanish)

The only solution that has worked is to reduce # of objects and slow them down, so the move doesn’t conflict with the collision. But that results in very poor game experience.

Has anybody had similar problems? [import]uid: 21208 topic_id: 17482 reply_id: 66839[/import]