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

Add SoftBody3D::apply_impulse() and SoftBody3D::apply_force() #100463

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PiCode9560
Copy link

@PiCode9560 PiCode9560 commented Dec 16, 2024

Closes godotengine/godot-proposals#1013

The goal of this PR is to add apply_impulse and apply_force to the Softbody node

Adding them to the GodotPhysics and JoltPhysics.

Added functions

Softbody3D:
void apply_central_force(force: Vector3)
void apply_force(force: Vector3, point_index: int)
void apply_central_impulse(impulse: Vector3)
void apply_impulse(impulse: Vector3, point_index: int)

PhysicsServer3D:
void soft_body_apply_central_force(body: RID, force: Vector3)
void soft_body_apply_central_impulse(body: RID, impulse: Vector3)
void soft_body_apply_point_force(body: RID, force: Vector3, point_index: int)
void soft_body_apply_point_impulse(body: RID, impulse: Vector3, point_index: int)

Built-in documentation

Show documentation

PhysicsServer3D

● void soft_body_apply_central_force(body: RID, force: Vector3)

Distributes and applies an force to all vertices. A force is time dependent and meant to be applied every physics update.

● void soft_body_apply_central_impulse(body: RID, impulse: Vector3)

Distributes and applies an impulse to all vertices.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

● void soft_body_apply_point_force(body: RID, force: Vector3, point_index: int)

Applies a force to a vertex. A force is time dependent and meant to be applied every physics update.

● void soft_body_apply_point_impulse(body: RID, impulse: Vector3, point_index: int)

Applies an impulse to a vertex.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

SoftBody3D

● void apply_central_force(force: Vector3)

Distributes and applies an force to all vertices. A force is time dependent and meant to be applied every physics update.

● void apply_central_impulse(impulse: Vector3)

Distributes and applies an impulse to all vertices.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

● void apply_force(force: Vector3, point_index: int)

Applies a force to a vertex. A force is time dependent and meant to be applied every physics update.

● void apply_impulse(impulse: Vector3, point_index: int)

Applies an impulse to a vertex.

An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).

Demo

These are how the functions compared to rigidbody.

The impulses have a strength of 0.2.
The forces have a strength of 15.

Jolt physics:

Jolt.SoftBody.mp4

Godot physics:

Godot.SoftBody.mp4

Implementation Details

Godot apply_impulse

GodotSoftbody3D already have apply_node_impulse method which apply impulse to a vertex. So I just use that for the softbody.apply_impulse.

Godot apply_force

GodotSoftbody3D have vertex.f which stores the applied forces, and it will handle it somewhere. Therefore, I only need to add a method that add the forces to it.

Jolt apply_impulse

JoltSoftbody3D didn't have a method to apply impulse. So I added JoltSoftBody3D::apply_node_impulse. It works like the GodotSoftBody3D::apply_node_impulse which just add impulse * vertex.inverse_mass to the vertex velocity.

Jolt apply_force

[Outdated]
I notice that the core JoltPhysics didn't have a way to apply force to softbody vertex. Hence, I have to add that functionality into the core of the JoltPhysics engine.

I added vertex.mForce, which store the forces that are going to be applied. Then applies it in the SoftBodyMotionProperties::IntegratePositions and reset the force on SoftBodyMotionProperties::UpdateSoftBodyState.

apply_central_impulse/force

For SoftBody.apply_central_impulse and SoftBody.apply_central_force I added apply_central_impulse and apply_central_force methods to jolt and godot. It works like the apply_node_impulse/force but the impulse/force is divided by the vertex count and applies it to all vertices.

@PiCode9560
Copy link
Author

PiCode9560 commented Dec 16, 2024

It's fine, right? If I add it to jolt and godotphysics in the same PR.

Edit: I'm just going to do it, I think it's fine.

@PiCode9560
Copy link
Author

apply_force added.

The Softbody class didn't have apply_force method, so I have to make it myself. I don't really know how force works. So I just tried stealing it from the GodotBody3D. Also, assuming that the GodotSoftBody3D's node.f handle the forces, this is how the force is done: node.f += p_force;
After testing it, the force looks kind of correct. So... good enough for now!

@adamscott adamscott changed the title Adding apply_impulse and apply_force to softbody Adding SoftBody3D::apply_impulse() and SoftBody3D::apply_force() Dec 16, 2024
@PiCode9560
Copy link
Author

apply_impulse added to jolt.

Most of the code from the apply_node_impulse is just copied from other methods, So I'm not 100% sure if all the error checking is necessary.

@PiCode9560
Copy link
Author

What I made with it so far:

softbody.controller.mp4

@PiCode9560
Copy link
Author

PiCode9560 commented Dec 17, 2024

Next, I'm working on adding apply_force() to jolt, but I can't figure out how to do that. If there's smart people out there want to give suggestions, you are welcome to.

Edit: Or just, @mihe HELP!

@AThousandShips AThousandShips changed the title Adding SoftBody3D::apply_impulse() and SoftBody3D::apply_force() Add SoftBody3D::apply_impulse() and SoftBody3D::apply_force() Dec 17, 2024
@PiCode9560
Copy link
Author

I think I figure it out, from comparing jolt with godot physics' GodotSoftBody3D and GodotBody3D.

What I understand is, Jolt softbody don't have the functionality to apply force. So I think I have to add the functionality to the SoftBodyMotionProperties class.

@PiCode9560
Copy link
Author

Added apply_force to jolt, but not sure if it behaves properly.

Also, I just found out that sometimes when running the scene, the softbody just disappear. IDK how that happened.

@PiCode9560
Copy link
Author

Added apply_central_force and apply_central_impulse to jolt and godot.

The way it works is just applying the force/impulse to all vertices, but the force/impulse is divided by the vertices count.

@PiCode9560
Copy link
Author

These are how the functions compared to rigidbody.

The impulses have a strength of 0.2.
The forces have a strength of 15.

Jolt physics:

softbody.jolt.mp4

Godot physics:

softbody.godot.mp4

@PiCode9560 PiCode9560 marked this pull request as ready for review December 18, 2024 13:41
@PiCode9560 PiCode9560 requested review from a team as code owners December 18, 2024 13:41
@PiCode9560
Copy link
Author

It's ready to review!

@PiCode9560 PiCode9560 force-pushed the softbody-apply-force-and-impulse branch 2 times, most recently from 5962e89 to beb2413 Compare December 18, 2024 18:17
@PiCode9560 PiCode9560 requested a review from a team as a code owner December 18, 2024 18:17
@PiCode9560 PiCode9560 requested a review from jrouwe December 27, 2024 18:02
@jrouwe
Copy link
Contributor

jrouwe commented Dec 27, 2024

Looks good to me!

One nitpick would be to update all of Jolt to 248253f88a5945c2032ffb397101afb9f88aeca5 or higher. I didn't initially suggest this because I knew there was another PR in progress that was touching the same files. But a full integrate can happen another time too.

@PiCode9560
Copy link
Author

I've never done full integration before, because of that, I don't know how to do it. So let's just do full integration another time. :D

@PiCode9560
Copy link
Author

Now, can someone else review it, merge it, or whatever to do next.

@PiCode9560 PiCode9560 force-pushed the softbody-apply-force-and-impulse branch from 08bf583 to ba422d7 Compare December 31, 2024 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add functions to apply forces and impulses on SoftBody nodes
3 participants