Get collision impact information

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Abgenullt
:warning: Old Version Published before Godot 3 was released.

Hello,

I have a rigid body with the body_enter signal connected.

How can I get information about the impact of the collision inside of the signal method?

I want to react to it in some way.

Thank you!

By impact, do you mean the relative velocity of the collision?

Bojidar Marinov | 2016-03-10 09:52

I mean the “force” or “strength” of the impact / collision. For example, I would apply more damage, if the objects colliding with more power. It’s hard for me to describe :slight_smile:

Abgenullt | 2016-03-10 10:00

That’s exactly what I said :smiley: I might answer a bit later though, since I’m a bit busy ATM.

Bojidar Marinov | 2016-03-10 11:05

I would like to know the answer to this as well. I tried making damage proportional to the difference in velocity between two objects, but this doesn’t take rotation into account, and is therefore useless for larger objects. I would manually take rotation into account, if only I could get the x,y of the collision…

keke | 2017-03-15 01:14

:bust_in_silhouette: Reply From: carlos21

It should be get_collider_velocity() from the body parameter.

Example:

func on_body_enter(body):
    body.get_collider_velocity() <-- this returns a vector2 velocity

Thank you,

but this only works on KinematicBody2D objects.
I have a RigidBody2D object because I need the original physics behavior.

Abgenullt | 2016-03-11 07:00

You should still be able to get the velocity from a rigidbody2d with the function get_Linear_velocity().

func on_body_enter(body):
    body.get_linear_velocity()

carlos21 | 2016-03-11 20:51

This doesn’t provide me the strength of a hit.

Is there a method to get the relative velocity for a collision, for any type of bodies.
This could be the first step.

For example Unity provides a Collision.impulse method.
Does Godot have something like this?

Abgenullt | 2016-03-14 09:31

:bust_in_silhouette: Reply From: waniwira

You can calculate impact from RigidBody.linear_velocity, but beware that signal BodyEntered happen after contact/collision, so linear_velocity will drop based on how it collide. to trick this, make one buffed value for linear_velocity time after time using func _integrate_forces (state)

func _integrate_forces(state):
   buf_v = linear_velocity
   buf2_v = buf_v
   print(buf2_v.length())

Hope this help