rigidbody –  get constant collision impulse values for colliding inflexible our bodies in Godot?

rigidbody – get constant collision impulse values for colliding inflexible our bodies in Godot?

[ad_1]

You could possibly make every object compute its personal impulse utilizing linear momentum change. This answer makes use of Alerts and tracks state variables manually. It really works for linear momentum solely, however you may prolong it to incorporate angular momentum too.

Add a script to a inflexible physique. A variable will retailer the item’s linear velocity earlier than updates at every physics body:

extends RigidBody3D

var prev_velocity: Vector3

func _physics_process(_delta: float) -> void:
    prev_velocity = linear_velocity

To configure the inflexible physique to emit alerts when colliding with one other physique, from the Inspector, set Contact Monitor to True and Max Contacts Reported to a worth bigger than 0 (I set it to 4 simply in case). Relying on the item form, you might need to allow Steady CD too.

Now, join the body_entered Sign of the inflexible physique to its personal Script in a brand new Receiver Methodology. This technique will probably be emitted routinely after the physics engine has resolved all collisions. Subsequently, the inflexible physique will determine what impulse was utilized to show its earlier velocity into the present one:

func _on_body_entered(physique: Node) -> void:
    var impulse = mass * (linear_velocity - last_velocity)
    print(title + " collide with " + physique.title + " power " + str(impulse.size()))

The above means: physics engine utilized a power of impulse magnitude for physics course of’ delta seconds to the item because of resolving all collision pairs, inflicting its linear velocity to modify from prev_velocity (earlier body) to linear_velocity (present body).

The above snippet works when the item collides each with static our bodies and different inflexible our bodies. RB pairs will compute impulses that can clarify their last velocities.

If “unusual numbers” begin showing, this is because of the truth that I did not take angular momentum under consideration, so complete momentum continues to be there within the Physics Server however wasn’t computed in my customized script.

[ad_2]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply