Simple 2D Collision

Godot Version 4.3

Hi, is it possible to check 2D collisions in a call(able?) function? The inbuilt function can’t, I think.

context,
I did a custom move player script ('cuz fun), it uses a while-loop to shift in pixels the player until a collision occurs. Since the collision check occurs after the while loop is finished, the player frequently gets stuck in collision and unholy things happen :confused:

(yes, I took it from Celeste, no, looking at it likely won’t solve this problem unless you’re gonna tell me to rewrite it using the inbuilt physics)
Code:

func _moveX(direction: float):
	remainder_distX += direction
	var dist = _round(remainder_distX)

	if dist != 0:
		remainder_distX -= dist
		var sign = sign(dist)

		while dist != 0:
			position.x += sign
			if is_colliding: # modified by default collide/exit functions
				position.x -= sign
				break
			dist -= sign

physics properties like “is_colliding” are only updated in the physics-frame which is the _physics_process-method. So your approach will not work unless you use raycasts and force them to update

aw, that’s a shame, maybe I’ll make my own collision detection or go back to a normal approach hahaha
thanks for the reply!

Could also try either moving this to/calling it during the physics_process function or look into the physics engine itself and run whatever code checks for collisions manually.

1 Like