Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PhysicsDirectBodyState2D: Reported values of get_contact_impulse not as expected #89921

Open
stephanbogner opened this issue Mar 26, 2024 · 3 comments

Comments

@stephanbogner
Copy link

stephanbogner commented Mar 26, 2024

Tested versions

Tested in:

System information

Godot v4.2.1.stable - macOS 14.1.2 - Vulkan (Forward+) - integrated Apple M2 Pro - Apple M2 Pro (10 Threads)

Issue description

Context:

I try to make breakable objects and wanted to use get_contact_impulse for this case do detect "hits".

Problem:

How strong the impulses are seem almost random ... but is hard to test in a reproducible way

My physics lab setup 😂:

  • All boxes are the same (mass, gravity scale, etc.)
  • The top text shows the highest recorded accumulated impulse (sum of all impulses in one _integrate_forces-cycle)
  • The lower text shows the highest recorded speed (length of linear-velocity)
  • Distances setup:
    • The first box has no distance to the ground
    • The second has 1px distance
    • The next three are arbitrary
    • The last two have the same distance to the ground
Screenshot 2024-03-26 at 20 37 37

Outcome:

  • Velocity does not linearly correlate to impulse (but it should ... I think?):
    • Box 1 has no speed but an impulse
    • Box 2 has a impulse/speed ratio of 1.55
    • Box 3 has a impulse/speed ratio of 1.19
    • Box 6 and 7 don't have the same impulse but I guess they should as they have the same fall height and weight (since I sum impulses)
Screenshot 2024-03-26 at 20 38 15

Note

I am not 100% sure if these are bugs or I am misunderstanding the concept of what get_contact_impulse is reporting.

Steps to reproduce

I recommend making a similar physics setup and playing around. Make sure you turn on contact_monitoring and set a value for max_contacts_reported.

I used the following code to track values:

extends RigidBody2D

var highest_speed := 0.0
var highest_impulse := 0.0

func _physics_process(delta):
	var speed = linear_velocity.length()
	if speed > highest_speed:
		highest_speed = speed
		$Velocity.text = str(snapped(highest_speed, 0.1))

func _integrate_forces(state):
	var total_impulse := 0.0
	for i in range(state.get_contact_count()):
		var collided_obj = state.get_contact_collider_object(i)
		var impulse = state.get_contact_impulse(i).length()
		total_impulse += impulse
	
	if total_impulse > highest_impulse:
		highest_impulse = total_impulse
		$Impulse.text = str(snapped(highest_impulse, 0.1))

Minimal reproduction project (MRP)

bug-contact-impulse.zip

@stephanbogner
Copy link
Author

stephanbogner commented Mar 26, 2024

A different setup (bug-contact-impulse.zip):

  • All objects have the exact same wonky shape, weight and mass ...
  • ... but are rotated a bit (between 0° and 5°)...
  • ... which makes the impulses differ widely on impact
Screenshot 2024-03-26 at 21 39 54 Screenshot 2024-03-26 at 21 39 40

@stephanbogner stephanbogner changed the title Reported values of get_contact_impulse not as expected PhysicsDirectBodyState2D: Reported values of get_contact_impulse not as expected Mar 27, 2024
@rburing
Copy link
Member

rburing commented Apr 12, 2024

Thanks for the MRP!

I haven't looked into this myself but others have made PRs for it. Can you test the PR #81654 and comment on whether it fixes the issue?

@stephanbogner
Copy link
Author

stephanbogner commented Apr 21, 2024

@rburing Thanks for the reply.

I built from source (the other approaches mentioned on the page didn't work) and now generally it's much more in line with what I'd expect as an outcome ... but since I don't know enough about actual physics it's hard to say whether it's correct now or not though, but below are the results from my tests.

If you have a more clever idea for how to test properly I'd be happy to make more tests.

Screenshot 2024-04-21 at 22 42 49 Screenshot 2024-04-21 at 22 40 36

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants