How to turn on/of physics globally in a scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By brylie

I have a scene with several bowling balls that fall when the game starts. I would like to add a button that enables physics when clicked, so that the balls do not fall until the button is pressed. How can I turn physics on and off from GDScript?

:bust_in_silhouette: Reply From: Diet Estus

You can turn off physics for a node using set_physics_process(false). This means _physics_process() will not be called for this node.

For example, in your Ball node, you can have:

func _ready():
    set_physics_process(false)

You can then set up your button to call set_physics_process(true) for the Ball node.


Alternatively, you could give the Ball a custom Boolean like physics_on that is initially false.

Then you can have:

func _physics_process():
    if physics_on:
        # falling code goes here

You can set up your button such that it changes physics_on to true.

thanks ! , it works !

ruruarchy | 2020-06-16 03:52

:bust_in_silhouette: Reply From: Lorèloi

If the balls you use are RigidBodies, you can use set_use_custom_integrator(). All rigid bodies have this method to turn on, or turn off, the integration of forces (like gravity) performed by Godot.

When you set set_use_custom_integrator(false), Godot performs integrations of forces to compute the new speed and position of the body (This is the default behavior). When you set set_use_custom_integrator(true), Godot doesn’t integrate forces itself. That is, it ignores every forces including gravity. But you can still use the method func _integrate_forces( body_state ) to define any physic calculations yourself.

So in your case, I guess you could program your button so every bowling balls of your scene toogle their custom integrator to true or false. I suppose it won’t represent much code if your button sends signals and if your bowling balls have the same script.

You may also want to immobilize your bodies once the custom integrator is used :

func _integrate_forces( body_state ):
    if is_using_custom_integrator() :
        body_state.set_linear_velocity( Vector2(0,0) )
        body_state.set_angular_velocity( 0 )

Hi,

ı have the same issue but couldn’t do it. Can you be more solid on explaining mouse click! In the code, no mouse is represented

Okan Ozdemir | 2019-06-04 08:34

:bust_in_silhouette: Reply From: PontiffPomelo

I don’t know a better way to do this scene-specifically, but globally you can use

Physics2DServer.set_active(false)

and

PhysicsServer.set_active(false)

This will disable ALL physics, including all Area/Body checks as well.

This is AMAZING. I disabled this, and my game’s FPS jumped way up!

eod | 2021-04-25 03:43