2D soft bodies: Gelly and moldable?
Asked Answered
I

1

18

I am using Matter.js physics in an attempt to create soft bodies. I was able to create a body like this:

matter.js soft body

However I am not sure if this is the "soft body" I want. It is true that this body is not entirely rigid and has that bouncy feel when it collides and gets dragged. I was looking for a body that shares similarities with a gelly. This image might visually help explaining the concept:

wanted-gelly-body

I was wondering how these type of bodies can be made. Is it the same as the as matter.js soft body but with a very specific type of properties? I can only get the body to be kind of rigid-squared and not as moldable and circular as I would like it to be.

I am also interesting in manipulating the physics body with in-game interactions which would increase or decrease the physics body size which leads me once more to the conclusion that the type of body that I want must be quite moldable.

Can matter.js handle this or do I have to change the physics engine? Any solutions to approach this?

NOTE: I am using Phaser.js for some in-game components but matter.js physics for physics manipulation because I believe Phaser integrated Physics can't simulate this type of complex body.

Thanks

EDIT: It is very similar to this Box2d :roll soft body ball. I just need to do that with a js engine I guess. Is there any?

Indehiscent answered 16/10, 2016 at 19:25 Comment(7)
You should add in the matter.js code that you used to attempt this, so that if someone does know how to tweak it to get it to behave the way that you want, they will be able to provide that info. Currently the question is pretty broad and likely to get down votes for not providing code. It may be that someone that knows will look at your code and what you are trying to do and confirm that matter.js won't work but without putting it up, people are less likely to even try to help.Nunatak
@Nunatak I agree with you for real. It is just that doing what I did is bassically just creating a new type of body already defined by matter.js. I didn't think it was too relevantIndehiscent
Once my friend made a gelly like in the image, he created a circular structure with the nodes and inside he filled with separated objects and kept a empty space to the objects move freely. The ring that contained the objects had some elasticity so its form would change easily. I don't know about Matter.js, but I guess you can make something similar.Goodness
@PhasedEvolution I hear ya. It actually might not be. But there are a number of folks that look at a question, don't see any code, vote it down and move on. Guidelines say to include code and many people figure that if they have limited time to help folks, they will goto people that followed the instructions. Just a suggestion to help stack the deck in your favor in terms of getting the eyes looking at the problem that need to look at it. :-) Not sure if softwarerecs.stackexchange.com would be right for finding a js engine. Take a look at the how to post there and what they help with.Nunatak
@Nunatak I will see what I can do there. It is also nice to receive good advices from more experienced users. Thank you :)Indehiscent
I'm not familiar with the phaser framework, but there's nothing in principle wrong with your model (e.g. it should be able to produce a result similar to what you want). However, you seem to be looking for plasticity which means that deformation of the body is retained.Menopause
@PhasedEvolution: perhaps close to what you want is a math structure called metaballs. Search "javascript metaballs" for various different takes on it. I find this one particularly insightful.Jillian
M
8

As I mentioned in the comments, I am not familiar with phaser or how you would actually implement this within a Javascript framework. My goal here is maybe to give you some ideas on different ways to proceed, so hopefully you'll find this answer useful.


I will try to answer this:

I was wondering how these type of bodies can be made. ... I can only get the body to be kind of rigid-squared and not as moldable and circular as I would like it to be.

It is not necessarily clear what you want given this sentence. As I noted in comments, what I think you are looking for is plasticity, and I will describe a way which you could possible "cheat" that look with somewhat simple tools.

At the moment you describe the motion of your body by "It is true that this body is not entirely rigid and has that bouncy feel when it collides and gets dragged.". Currently your model works as such:

  1. A point is connected to all other points as given in your mesh.
  2. Every time step, a force is calculated between each pair. The total force on a joint (or point) is the sum of all these pair wise forces.
  3. Each joint is associated with a part of the body (i.e. it has some mass m) and you calculate its acceleration with acceleration = force/m. From there on we calculate velocity and finally position.

The most interesting part of the steps above is nr 2, as that will greatly influence the motion of the whole body. A very common way to implement it is as an elastic potential that for a certain distance between two points gives some force. Like so:

function elasticPotential(p1, p2) {
    // Given two positions p1 and p2 we calculate a force between them
    distance = sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2) + pow(p1.z - p2.z, 2));
    force = force_given_distance(distance); // A popular choice here is for example a spring force
    return force;
}

Now, you already have the function described above built in in your framework, so you don't have to implement it. The reason I'm describing this is because it is essential to understanding how we can create plasticity. The problem with the above is that nothing will retain deformation---the nature of the elastic potential is that it has some rest configuration (most likely your first configuration) and it will always try to get back to that shape. We want the shape to remember how it was mis-shaped. This is what plasticity is.

Simple plasticity

Note first here that the problem of plasticity is a big research topic and in many cases far from trivial. My idea is as follows: if the distance between two connected points are larger than some threshold, remesh the points in the current configuration. That is,

for each pair(p1, p2):
    if distance(p1, p2) > threshold:
        recalculate_connection(p1, p2)

As you can see this is a very simple model for plasticity, and most likely not physically correct. However, it should be possible to get interesting behaviours my playing with the remeshing together with what elastic potential you choose.

If you provide me with more details I might be able to discuss the problem further, but right now I feel this answer is already longer than it should be.


TL;DR: Create a "moldable" shape by remeshing your body during deformation. It might be tricky to get an exact desired physical behaviour, but it should be possible to create something that looks "gelly-like".

Menopause answered 24/10, 2016 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.