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

Fix push bodies #470

Merged
merged 1 commit into from
Jul 1, 2023
Merged

Conversation

BastiaanOlij
Copy link
Member

Re-implement being able to push bodies around with the player body

Fixes #467

@BastiaanOlij BastiaanOlij added the bug Something isn't working label Jun 7, 2023
@BastiaanOlij BastiaanOlij added this to the 4.2.0 milestone Jun 7, 2023
@BastiaanOlij BastiaanOlij self-assigned this Jun 7, 2023
@BastiaanOlij BastiaanOlij force-pushed the fix_push_bodies branch 4 times, most recently from caf6f10 to 8193e7b Compare June 7, 2023 11:48
@Malcolmnixon
Copy link
Collaborator

There are a few problems with the impulse application logic.

Impulse Strength

Currently the impulse strength doesn't vary based on whether the player is walking into the object, or sliding almost perpendicular to the object. This one's a fairly simple fix - just change the strength calculation to be based on the dot-product between the player velocity and the surface normal as follows:

var strength = p_velocity.dot(-with.get_normal(0)) * push_strength_factor

This feels more natural when walking into objects in glancing blows vs straight on.

Low Mass Bullets

Another problem which is more challenging is that if you try pushing a RigidBody3D with a low mass (0.0001) then it will fire off faster than a bullet, as the "impulse" results in a massive velocity. You want heavier objects to be more resistant to moving, but you don't want lighter objects to be affected so much that they move faster than the player. I'm not sure how to fix that problem.

Relative-Velocity Impacts

A third problem is with reference-frames. If the player is in a train moving at 10m/s and just brushes up against an object, the p_velocity of the player will be around 10m/s, even though the velocity "at the point of contact" is tiny. The solution is to change from using the players absolute velocity, to instead use the relative velocity between the player and the RigidBody3D contact point. The problem is it seems to be difficult to ask Godot what the contact point velocity is. There's PhysicsDirectBodyState.get_contact_collider_velocity_at_position which looks roughly correct. It's just odd that KinematicCollision3D doesn't expose it.

@BastiaanOlij
Copy link
Member Author

@Malcolmnixon sorry for the late response.

Indeed tricky issues, I've adjusted the logic somewhat.

Impulse Strength

The impulse strength change you suggested makes sense so I applied that as you suggested.

Low mass bullets

The issue with low mass bullets is much more tricky. We're talking about transfer of energy as we're hitting an object. So our mass (which doesn't exist in CharacterBody3D) times the velocity of impact determines how much force we're impacting the object with. The lighter the object, the more the resulting velocity is of this energy being applied to the object.

However more is going on than that simplistic highschool view of physics as generally bumping into small objects in your living room, doesn't sent them flying through the room like crazy. I'm not to read up on this so I'm also not sure what the missing ingredient is and how to properly calculate this transfer of energy.

I might see if I can fine some time looking into Godots physics engine because this is no different from what the internal code should be doing when two rigidbodies collide.

Relative-Velocity Impacts

The relative velocity thing is an issue, get_contact_collider_velocity_at_position seems to be the correct solution but if it's not exposed we'll need to fix that upstream first so we can't rely on it yet. For now I have added that we subtract the velocity of the object we're hitting so we are working with the relative velocity between the two objects. Won't solve the issue for when we're on a moving platform I wager.

Copy link
Collaborator

@Malcolmnixon Malcolmnixon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works very nicely now.

@BastiaanOlij BastiaanOlij merged commit f09354a into GodotVR:master Jul 1, 2023
@BastiaanOlij BastiaanOlij deleted the fix_push_bodies branch July 1, 2023 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lost push rigid body feature
2 participants