Getting a list of overlapping bodies from an area

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By sxkod

Hi folks

I am trying to find a good way to make explosions. I don’t mean particle system but for eg:
if a grenade explodes, it should find out who are all the nodes / players in a certain area and if they belong to a specific node group - then change their stats.

At present I have some static bodies strewn across the scene and a grenade - an area. On explosion cue, I am scaling the area and then looking for get_overlapping_bodies().

However when I search for the overlapping bodies right after scaling in the same frame, it is not finding the bodies but on second call it finds them. The area does not get repeatedly scaled as I am just setting a scale rather than global_scaling.

Is there a better way than what I am doing? I also thought of raycast but to get an area of influence from the explosion, I will probably have to spin the raycast around and also keep increasing the angle from the ground plane.

Code below:
Thanks all

extends Spatial

var explosion_on=false


func _process(delta):
	if explosion_on==true:
		print($explarea.get_overlapping_bodies())
	explosion_on=false

func _input(event):
	if Input.is_key_pressed(KEY_X):
		var boxsize=4
		$explarea.scale=Vector3(boxsize,boxsize,boxsize)
		print($explarea.get_overlapping_bodies()) ## This does not print the bodies
		explosion_on=true



 

Not to answer my own question but the following helped solve it.

Upon explosion cue [in this case a timer timeout], I set Area.global_scale(Vector3(sz,sz,sz))

Also set exploded=true; Then in the _process, I wait for a few frames and then use Area.get_overlapping_bodies.

For some reason, waiting that few frames gives accurate results where as if I scale the area and immediately ask for overlapping bodies, I wouldn’t get correct list.

sxkod | 2019-03-27 17:56