No collision detection with custom_integrator

Godot Version

v4.4.1

Question

How do I detect collision, when using custom_integrator?

Problem

Inside my func _integrate_forces I move the rigid body with state.linear_velocity = velocity with velocity being: velocity = Vector3(2, 0, 0).
Once it hits a collision shape it stops, while I expect it to change direction whenever it detects a collision. It appears that the rigidBody somehow detects it can not move forward, but doesnt really detect a “colission”, since I’m not getting any integers from state.get_contact_count(). Below is my code:

extends RigidBody3D

@export var velocity: Vector3 = Vector3(2, 0, 0)

func _ready() -> void:
	#Schaltet die eingebaute Physik aus und gibtuns volle Kontrolle
	#Node muss initialisiert sein, deswegen in _ready()
	#Es gibt eine physics und idle processing. Idle läuft jeden Frame, Physics läuft 60 iterations/minute
	custom_integrator = true
	

func _integrate_forces(state: PhysicsDirectBodyState3D) -> void:
	state.linear_velocity = velocity
	if state.linear_velocity != Vector3.ZERO:
		print("I am still going")
	#counts the amount of object hit in one frame
	for i in range(state.get_contact_count()):
		#counts what object it hit and changes behavior based on that
		print("Detected collision, " + str(i))
		var id = state.get_contact_collider_id(i)
		print("This is the id: " + str(id))
		var collider = state.get_contact_collider_object(i)
		if collider:
			print("touched")
			velocity = -velocity
			break

func _physics_process(delta: float) -> void:
	pass

To detect collisions you need to enable RigidBody3D.contact_monitor and set the RigidBody3D.max_contacts_reprorted property to > 0. This is explained in the documentation of the function PhysicsDirectBodyState3D.get_contact_count()

1 Like