Getting the collision point when Area2D and a TileMapLayer collide or "Am I misusing Area2D here?"

Godot Version

4.4.1-stable

Question

I often found myself in the pickle that I’m creating a node with a detection area


and then when I need to move it and bounce it around I have to jump through hoops only to realize in the end that it won’t work because the TileMapLayer doesn’t give the coordinates of the Tile at the collision.

func on_body_entered_wall_detection(body: Node2D):
	if body is TileMapLayer:
		var space_state = get_world_2d().direct_space_state
		var query = PhysicsRayQueryParameters2D.create(
			global_position,
			(body.global_position - global_position),
			$WallDetection.collision_mask
		)
		var intersect_result = space_state.intersect_ray(query)
		var collision_normal = intersect_result.normal as Vector2
		direction = direction.bounce(collision_normal)

Even when other Area2Ds collide, I’m unable to make them bounce. Is it easier using a PhysicsBody like RigidBody2D? Do I have to keep something in mind?

The current game is a top down game that doesn’t really use any physics (apart from the player that’s sliding along walls or so on).

Thanks a ton for your thoughts!

I think it’s better to use a RigidBody2D in this case. Setting it to bounce is even explained in the settings (set a physics material and see the bounce property documentation). It worked for me setting it’s linear_velocity (instead of updating it’s position in the _physics_process():

linear_velocity = MOVE_SPEED * direction

If there are other thoughts about it I still very much care to read them :slight_smile: .