2D Collision detection before instanciation

Godot Version

v4.3stable

Question

I searched on the web and did not find anything that work. I try to instanciate a rigidbody representing a meteorite. I would like to be able to detect if another meteorite is already there before creating a new one. I found a tuto on raycasting but I would want to intersect with a circle. I found that the docs are quite unclear and using AI is so misleading because of the version change. Thanks :slight_smile:

The easiest way is to use a “Shapecast2D” instead of a raycast. Or if all the meteorites are the same size you can just put two raycast on the edges of the meteorite

I finally find something that work :

func is_space_free_in_circle(position: Vector2,radius: float):
	var space_state = get_world_2d().get_direct_space_state()
	var query = PhysicsShapeQueryParameters2D.new()
	var circle_shape = CircleShape2D.new()
	circle_shape.radius = radius
	query.shape = circle_shape
	query.transform = Transform2D(0,position)
	query.collide_with_areas = false
	query.collide_with_bodies = true
	var result = space_state.intersect_shape(query)
	return result.size() == 0
1 Like