Remove node temporarily from physic world - set_physics_process

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

Hi all,

I want to temporarily remove a node from the physic world (but to be re-introduced later on). Image a big slider, with cubes ( the node ) rolling over it.

I want, upon clicking / tapping a node, to have the node removed from the physical world calculation, go up 10 unit in y axis, until mouse / finger is released, then the node should be back into the physics world.

I would have assumed that set_physics_process( false / true) should do the job but it is not working for me. Here is a sample code snippet:

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	set_physics_process( true )
	set_process_input( true )

then my pickup logic (called from _input_event() )

func _on_pickup_cube( curObj ):
        #curObj is the rigid object
	var parentName = get_parent().name
	print(">>>>>Object picked up:" + parentName + " curObj: " + str(curObj) )
	curObj.get_node("Mesh-cube").set_surface_material(0, null)
	var position = Vector3(0,0,0)
	var impulse = Vector3(0,20,0)

	#curObj.set_process( false ) 
	curObj.set_physics_process( false )
	#curObj.apply_impulse(position, impulse)	#ok
	#curObj.set_mode(  MODE_STATIC ) 
	#print( "after set custom integrator ")

To summarize, the following calls are not crashing but have no effect when turned on (node keeps on rolling)

  • set_process()
  • set_physics_process()
  • set_use_custom_integrator()

where as the below take effect as expected (although not my need):

  • apply_impulse()
  • set_mode()
  • set_surface_material()

Checked this and this but didn’t work for me…
Any idea / tips are welcome :slight_smile:

:bust_in_silhouette: Reply From: kidscancode

set_physics_process(false) doesn’t remove the object from the physics engine, it disables the _physics_process() callback on the object, so any code you have in that function would not execute.

If you want the body to stop moving/responding to gravity, you can set it to “static” mode:

body.mode = RigidBody2D.MODE_STATIC

If you want it to stop colliding with things, you can change its collision layer or disable its collision shape with (for example):

$CollisionShape2D.disabled = true

Any idea how I could do this on a KinematicBody2D?

Modley | 2023-01-05 19:36

The same way - disable the collision shape or change the collision mask.

kidscancode | 2023-01-05 19:48

I meant how to stop the KinematicBody from reacting to physics

Modley | 2023-01-06 09:52