blitz-research / monkey2

zlib License
133 stars 43 forks source link

Physics request: allow pausing [update: with solution] #426

Open DruggedBunny opened 5 years ago

DruggedBunny commented 5 years ago

I'm trying to find a way to pause the physics simulation, but it looks like at present I'm going to have to iterate through all bodies and set their velocities/torques manually (and store these so I can un-pause!).

While digging around, I found this solution by another project using Bullet.

Little bit later: this seems to work with minor hacking to world.monkey2 as below:

    Property TimeScale:Float ()
        Return timescale
        Setter (new_timescale:Float)
            timescale = new_timescale
    End
    ' (Private)
    Field timescale:Float = 1.0

    ...

    Method Update( elapsed:Float )

        resetCollisions()

        _btworld.stepSimulation( elapsed,_scene.MaxSubSteps,timescale * 1.0/_scene.UpdateRate )

... then in my game I just call:

        If Keyboard.KeyHit (Key.P)
            Game.GameScene.World.TimeScale = Not Game.GameScene.World.TimeScale
        Endif

... and it pauses/unpauses nicely. Can even do super slowmo and that seems to work well too!

        If Keyboard.KeyHit (Key.Minus)
            Game.GameScene.World.TimeScale = Game.GameScene.World.TimeScale * 0.5
        Endif

(Actually, looks like I need to store timescale before pausing, as Not timescale always restores to 1.0 regardless of reduced scale before Not timescale.)

blitz-research commented 5 years ago

I've tweaked UpdateRate so setting it to 0 can be used to pause updates, instead of just causing a divide by 0! eg:

If Keyboard.KeyHit(Key.Space) _scene.UpdateRate=_scene.UpdateRate ? 0 Else 60

I do like the TimeScale idea but will need to give it a bit more testing before adding it.

On Thu, Sep 20, 2018 at 1:38 AM DruggedBunny notifications@github.com wrote:

I'm trying to find a way to pause the physics simulation, but it looks like at present I'm going to have to iterate through all bodies and set their velocities/torques manually (and store these so I can un-pause!).

While digging around, I found this solution https://github.com/armory3d/armory/issues/755#issuecomment-403943912 by another project using Bullet.

Little bit later: this seems to work with minor hacking to world.monkey2 as below:

Property TimeScale:Float () Return timescale Setter (new_timescale:Float) timescale = new_timescale End ' (Private) Field timescale:Float = 1.0

...

Method Update( elapsed:Float )

  resetCollisions()

  _btworld.stepSimulation( elapsed,_scene.MaxSubSteps,timescale * 1.0/_scene.UpdateRate )

... then in my game I just call:

  If Keyboard.KeyHit (Key.P)
      Game.GameScene.World.TimeScale = Not Game.GameScene.World.TimeScale
  Endif

... and it pauses/unpauses nicely. Can even do super slowmo and that seems to work well too!

  If Keyboard.KeyHit (Key.Minus)
      Game.GameScene.World.TimeScale = Game.GameScene.World.TimeScale * 0.5
  Endif

(Actually, looks like I need to store timescale before pausing, as Not timescale always restores to 1.0 regardless of reduced scale before Not timescale.)

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/blitz-research/monkey2/issues/426, or mute the thread https://github.com/notifications/unsubscribe-auth/ADU3Qp2H-iq7nQEr4VCOyOIjtiuGdxFNks5ucki5gaJpZM4WwL_c .

DruggedBunny commented 5 years ago

OK, cool... I have been using the timescale thing to pause (though now sidetracked into writing an audio mixer thing so I can pause/resume all channels in the correct state), and to slow the scene down on dying, and it does seem fine -- my explosions seem to work exactly the same on hitting terrain, for example, just in cool slo-mo. I noticed anything above 1.0 is capped at 1.0, so seems to accept 0.0 - 1.0 only, but it's definitely a nice effect to have.