blitz-foundation / monkey2

zlib License
3 stars 0 forks source link

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

Open Pharmhaus-2 opened 5 years ago

Pharmhaus-2 commented 5 years ago

Original Author: DruggedBunny

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.)