appsinacup / godot-softbody2d

Godot SoftBody2D - Plugin that adds deformable 2D soft body.
MIT License
257 stars 13 forks source link

Can't apply external force #8

Closed MrJox closed 9 months ago

MrJox commented 9 months ago

Hi, I cannot apply an external force to the softbody. The softbody itself behaves exactly how I want it - it's a soft circular blob.

Now I want to fire at that blob and make it react. How can I achieve it? Functions such as apply_impulse() don't work

Ughuuu commented 9 months ago

Hi. The softbody itself contains a lot of rigidbodies. I could provide a function that applies impulse on all of them, but I'm not sure thats whats expected. You van try getting all rigidbodies children and try some logic. You can also get the center rigidbody and move that.

MrJox commented 9 months ago

As I'm a little newbie in this I would love to see an example or two

Ughuuu commented 9 months ago

Ill try to add one when i have some time. What is ur use case though? What are you expecting to happen in your game? How do you want to move the softbody?

MrJox commented 9 months ago

I want to shoot at the softbody blob, make it have a visual impact in place of collision, as well as change the blob's position in the direction of the shooting

Ughuuu commented 9 months ago

Can you provide also an image or something of what you want? Or if you find in an example somewhere or if you can replicate with a rigidbody.

MrJox commented 9 months ago

image

So roughly, here's the idea:

  1. Imagine a balloon, filled with water. It stays in a resting state
  2. An external force is applied to the left of balloon with the force direction Vector2(+1, 0). Example: balloon was kicked or shoot with a rubber bullet.
  3. Balloon is moved to the right, as an impact of a force applied to it's left corner
  4. Balloon returns to a resting state
Ughuuu commented 9 months ago

Interesting. And is this not what happens when you hit the softbody right now? That would be what I would expect to happen without needing to apply any force to it.

MrJox commented 9 months ago

I fire bullets at it and push myself against the softbody, however it has no effect. The physics work only with gravity. Maybe I'm missing something in my setup though.

The softbody is setup this way: Node2D (root node) -- SoftBody2D (texture is provided, bones are present)

If I simply drop the body from air against a surface, it works exactly as it should. But the problem is I don't understand how to apply external forces to it, like an impulse to the side

Ughuuu commented 9 months ago

Can you provide an example project or a video?

MrJox commented 9 months ago

What exactly would you like to see? I cant share the project as its NDA

Ughuuu commented 9 months ago

A test project where you hit a softbody and it doesnt move. In my tests the softbody moves if it gets hit by another body, even if gravity is 0.

MrJox commented 9 months ago

Where can I see you project? It may help me understand what I'm missing

MrJox commented 9 months ago

Hi, I was able to apply external force by calling apply_central_force() on a specific bone RigidBody2D.

Is there a way to figure out which bone was closest, when external object collided with the soft body?

Ughuuu commented 9 months ago

Not unless you get all rigidbodies and check which one is closest.

Ughuuu commented 9 months ago

I'm adding now some new samples for your use case though.

MrJox commented 9 months ago

Thank you very much!

MrJox commented 9 months ago

Meanwhile I did a few more tests myself - I created a softbody (its just a solid circle texture) with the following parameters: Vertex interval - 173 Radius - 10 Total Mass - 10 Shape Type - CIrcle Joint type - pin No physics material override Texture size is 512x512

image

Scene hierarchy is the following: CharacterBody2D (Root) -- SoftBody2D -- RigidBody2D (for bullet collision test) ---- CollisionShape2D -- StaticBody2D (for ground collision test) ---- CollisionShape2D

CharacterBody2D node transform is scaled down to 0.1 on both axes.

The idea was to test how the softbody reacts to collision against an external force - a bullet that hits the softbody from horizontal direction. I set bullet mass to a ridiculous number (1000000 kg). The softbody mass is 10 kg. The bullet speed is 2000 * delta in X direction.

Test result: Bullet reaches the softbody, bullet gets stuck against the softbody and doesn't affect it

Ughuuu commented 9 months ago

Can you as well send a video of this? From screenshot seems like the softbody isnt constructed but its a voronoi diagram instead.

Also just to reduce of what could go wrong, try for now to not scale softbody.

Ughuuu commented 9 months ago

Also if u can get on project discord, itll be easier to check whats wrong there.

MrJox commented 9 months ago

Scale indeed was one of the reasons it didnt work very well. I think node scale forfeits bone radius, which results in wrong behaviour when external rigid body collides into bones

Ughuuu commented 9 months ago

Added an example with apply_force in tutorial.gd file.