How to implement a blast radius in 2D

Godot Version

4.3

Question

I’m very new to Godot and game design in general so please explain your answers like I’m an idiot.

I am trying to have a bomb go off and only impact the first thing it touches in every direction. So if an object is behind a wall it isn’t affected. I would also like to have the wall (TileMapLayer Cells) then update to be damaged which I also have working, but all walls in the Area2D update, even if they are behind other walls.

So far I have used an Area2d and I can detect all the objects in the blast radius and have them run a damage function, but objects behind walls are also ‘hit’ currently. The walls I’m using are TileMapLayer and have their own collision layer set.

How do I get the collision in a given direction stop detecting once it hits an object or a wall?

Any help would be very much appreciated.

This is quite difficult to achieve, because you have to determine when an object is considered to be behind another. The following is my suggestion

i think you have to save all targets inside an array and at the end loop through this array (only necessary for objects that can be damaged). For every object (that could be damaged, for example not necessary for a tilemap) cast a raycast in their direction. When this raycast collides with something else then the object its supposed to → something is in front of it → no damage. Else it will deal damage.

This has the downside of only checking if something is in line of the center of the object its hitting

2 Likes

Thanks, I’ll give this a go and let you know how I get on. I’m already using an array, so I’m half way there already.

It would be great if there was a way to repurpose the pointlight2d and occlusion functionality as that’s exactly the behvaiour I’d like to see except for collision detection.

1 Like

Thanks @herrspaten that worked!

Just in case anyone ends up here looking for a similar solution, here’s the code I used:

	if Input.is_action_just_pressed("alt_shoot"):
		var space_state = get_world_2d().direct_space_state
		for o in object_in_blast_radius:
			if o.has_method("hit_by_blast"):
				var query = PhysicsRayQueryParameters2D.create(global_position, o.global_position, 3, [self])
				var result = space_state.intersect_ray(query)
				if result.collider == o:
					o.hit_by_blast()

I’d still like to see a version of collision detection similar to pointlight2D and occlusion, but I guess I’ll write that into the engine at some point way in the future

1 Like