How to move “obstacle” objects. Corona SDK Physics Box2D. Physics Update?

I’m trying to understand the proper way to move “obstacle” objects in corona/box2d. I want to know how to write my code so that the movement is deterministic and in-step with the physics engine. Is this possible?

I’m familiar with Unity which has a physics update (FixedUpdate). Where you can add a bit of code to execute on every physics step. This keeps the physics deterministic, frame and system time independent.

For example, in my game I want to do things like: Make a platform that moves back and forth, make an object that scales up and down in size. I would still like the physics to work properly and allow the ball in my game to bounce off of these for example. Depending on the case, I imagine that I should use either kinematic or dynamic bodies.

I’ve searched around and the way to periodically manipulate these objects is to use:

  • timer.performWithDelay. But this has an issue mentioned in the docs “Timers run on system time. If the app is suspended, running timers will not be automatically paused”
  • Listening to an enterFrame event. But this has a similar issue, if the frame rate drops then the object won’t be moved enough. We can use delta time to deal with this, but this has determinism issues (moving an object too much on a lag spike).
  • transition.to. I’m guessing this is implemented with timer.performWithDelay and has the same issues.

I don’t think that any of these will behave deterministic. Any suggestions? Thanks for the help.

For timers and transitions you would have to manage pausing the timers when your app suspends. But more importantly you don’t want to try and move things outside of Box2D if you can help it. Therefore an enterFrame listener may fit your needs the best. However the frames are not guaranteed to fire exactly every frame. You can to compensate using delta time.

https://coronalabs.com/blog/2013/06/18/guest-tutorial-delta-time-in-corona/

The tutorial just moves the object, but you can modify it to use physics forces adjusted for the time of the frame.

Rob

For timers and transitions you would have to manage pausing the timers when your app suspends. But more importantly you don’t want to try and move things outside of Box2D if you can help it. Therefore an enterFrame listener may fit your needs the best. However the frames are not guaranteed to fire exactly every frame. You can to compensate using delta time.

https://coronalabs.com/blog/2013/06/18/guest-tutorial-delta-time-in-corona/

The tutorial just moves the object, but you can modify it to use physics forces adjusted for the time of the frame.

Rob