Grenade Launcher Explosion Physics

Godot 4

I want to make some realistic grenade blowing up physics.
My problem is that I want to make the impulse on surrounding rigid bodies relative to the distance from the the explosion. However when I use the distance_to() function it makes it so that the further the object is the more of an impulse it gets. Can someone help me in inverting this? Here is the code:

extends Node3D

var impact_force = 0

func _ready():
	$AnimationPlayer.play("Init")
	
func _on_animation_player_animation_finished(Init):
	queue_free()

func _on_damage_zone_body_entered(body):
	if body.is_in_group("RigidBody"):
		impact_force = 20 - self.global_position.distance_to(body.global_position)
		body.apply_central_impulse((self.global_position - body.global_position) * -impact_force + Vector3(0, 1, 0))

An inversly proportional relationship can be achieved by dividing

1 / distance_to

can you show what could be coded to achieve this

func _on_damage_zone_body_entered(body):
	if body.is_in_group("RigidBody"):
		impact_force = 20 - self.global_position.distance_to(body.global_position)

		var inverse_force: float = 1.0 / impact_force

		body.apply_central_impulse(inverse_force + Vector3(0, 1, 0))

still isn’t working but if you could apply it to this.
The 20 - … was stupid maths I tried to use
And the force_mult is just a multiplier to make the impulse stronger

func _on_damage_zone_body_entered(body):
	if body.is_in_group("RigidBody"):
		impact_force = -self.global_position.distance_to(body.global_position) * force_mult
		body.apply_central_impulse((self.global_position - body.global_position) * impact_force + Vector3(0, 1, 0))

divide by distance to have less force if there is more distance.

Think if they were 1 unit away then force_mult / 1.0 would give the full force_mult value. If the object was 2 units away then force_mult / 2.0 would be half as much force

so impact force divided by distance

I did It!

func _on_damage_zone_body_entered(body):
	var distance = self.global_position.distance_to(body.global_position)
	if body.is_in_group("RigidBody"):
		impact_force = (9 - distance) * force_mult
		body.apply_central_impulse((self.global_position - body.global_position) * (impact_force / distance) + Vector3(0, 1, 0))
1 Like