# Soft/elastic shapes - primitives

17 replies to this topic

### #1 sump

sump

GMC Member

• New Member
• 1 posts

Posted 07 July 2010 - 12:10 AM

So.. hi everyone
(sorry if this was already discussed, i looked around but couldn't find anything similar)

i've been playing around with primitives and started thinking about making elastic shapes.
the way i went about doing this is by creating a bunch of point objects that react to their nearest neighbors.
the points have a fixed "ideal" distance to be at from each other and if it decreases or grows, force is activated
on the points sending them in the other direction - kind of like a set of points connected by springs.

this is the file with a circle of points:
http://www.mediafire...php?5xzmyw1wlg4

you can move them around, that's pretty much it

the elastic effect is okay i guess, but i was left with two new riddles:
how would i give this shape some volume - when the surface of a soft rubber ball(for example) is pushed on one side, it tends to add to volume on other sides. this would be possible to do with a ball(add tension calculation from the center to each point) but what about weirder shapes?

an other question, is about the actual elasticity - how would one change the shape's elasticity values - something that will help ot go from complete goo(like in the example file) to a tennis ball and then a bowling ball(sorry if my examples suck

anyway this seems like a good discussion to start to me, i'll try to update this post if i'll find any answers - and i'll appreciate any ideas.
(it seemed to me this post belongs in this forum as it is mostly theoretical, if i was wrong, sorry in advance)

Edited by sump, 07 July 2010 - 12:14 AM.

• 0

### #2 ~Dannyboy~

~Dannyboy~

~hoqhuue(|~

• GMC Member
• 2144 posts
• Version:GM8

Posted 07 July 2010 - 02:32 AM

This concept is commonly known as "soft body physics". I found a really great tutorial on the subject once, I can't seem to find it at the moment but I'll link it if I do.

The basic technique is quite simple, it involves 2 types of forces, spring forces and forces due to pressure. There are two of each of these acting on every point, one for each adjacent point.

The spring force between each pair of adjacent points is proportional to the distance between the points, "F = kx". But you'll find that this will cause the polygon to jiggle a lot. To prevent this you need to use a damped spring, "F = kx - dv", where F is the force, k and d are the spring and dampening constants, x is the distance between the point and it's neighbour, and v is the component of the velocity of the point in the direction towards it's neighbour. This force acts in the direction towards the adjacent point.

The force due to pressure is proportional to both the distance between two adjacent points and the pressure in the polygon. The pressure in the polygon is inversely proportional to the total area of the polygon (see below). "P = c/V", where P is the pressure, c is some constant and V is the total area of the polygon (note this is a little weird having the area as the volume because we're using laws which apply to 3D and trying to model them with only 2 dimensions). To get the force due to pressure you simply use, "F = PA", where F is the force due to pressure, P is the pressure and A is half the distance between the point and the adjacent point (as the force really acts on the surface area or the connector between the two points we attribute half the force to each end point). This force acts outwards at a normal to the connector between the two adjacent points.

The area of the polygon can be calculated using this formula:

This is a quick diagram showing a point (green), its two adjacent points (grey), the spring forces (orange) and the force due to pressure (blue).

This combination of spring forces causing the soft body to want to contract and pressure forces causing it to want to expand can create quite believable physics. Of course there are plenty of other forces to consider, gravity, buoyancy, normal and friction forces for collisions, etc. but I won't go into them just now. You will have to 'play' with the constants a lot to get a good effect.

Have fun turning all that into code

EDIT: I dug up an example of this I made some time ago. The vertex count is fairly high so I'm not sure what fps it will run on an older machine, you should be able to get the general idea though. Use the arrow keys to apply a gravity like force to the soft body. link

Edited by ~Dannyboy~, 07 July 2010 - 09:24 AM.

• 1

### #3 KC LC

KC LC

• Retired Staff
• 5309 posts

Posted 18 July 2010 - 05:29 PM

Has anyone else tried to code this? I've had only limited success. I defined a polygon with objects at the vertices, and connected each vertex to its neighbors with springs (obeying Hooke's Law). Then I just sum the forces at each vertex, and integrate to update the x,y positions of each vertex.

That works fairly well for triangular polygons. But for higher order gons, the problem is that sometimes a few vertices can fold in on themselves -- kind of like a Hoberman sphere. Once that happens, they stay that way.

So I added a center point, and connected springs to each vertex from that center. That keeps the gon from collapsing or folding up. But even with lots of vertices, it doesn't look very physical.
• 0

### #4 Pie Person!

Pie Person!

GM 6+ Lover

• GMC Member
• 1973 posts

Posted 18 July 2010 - 07:20 PM

Once a sphere deforms, it's not a sphere anymore, the internal force is not going to be towards the center anymore. Using a "center" for anything but a true sphere is stupid. However, I think there's a good way to solve this. I've thought about it a lot, and I think it's as simple as making every point connected to every other point by a spring. Even if the shape is convex, just try it, I really think it will work perfectly.
• 0

### #5 KC LC

KC LC

• Retired Staff
• 5309 posts

Posted 18 July 2010 - 07:55 PM

Using a "center" for anything but a true sphere is stupid. However, I think there's a good way to solve this. I've thought about it a lot, and I think it's as simple as making every point connected to every other point by a spring.

It didn't strike me as stupid when I tried it. After all, every regular polygon has a center -- just like a sphere. Besides, we can't model a "true sphere" anyway -- only an "N-gon" with large N. So I allowed the (initially) centered point to move around as the shape deformed.

You're right that it doesn't work too well -- but connecting every point to every other point doesn't seem very practical either. And it leads to a monstrous number of springs, as N gets large.

Anyway, I've re-written the code using the normal-force approach that Dannyboy described. Each vertex is connected to its two neighbors by a spring, and at each vertex I calculate a normal vector (based on the local deformation) and add an outward force.

It seems to work alright. It allows the shape to deform and then recover somewhat, without folding inward on itself.
• 0

### #6 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 18 July 2010 - 08:09 PM

Once a sphere deforms, it's not a sphere anymore

Obviously.

Using a "center" for anything but a true sphere is stupid.

Using a small number of linear springs to model a linear elastic continuum is also stupid. But you're wrong, adding a center point isn't stupid, since it's meant to fill the interior. There's nothing special about spheres here. Technically filling in more interior points would make the object behave more like how you would want. I don't know where you got all this sphere nonsense from.

Even if the shape is convex

You must mean concave, because it'll trivially give decent looking results for a convex polygon. It won't give good looking results for concave polygons since the concave portions will act like they're filled with some solid. Intuitively if there's a very small arm sticking out of some body we'd expect for it to be easy to bend. Connecting every one of its vertices to the vertex of every other part of the boundary won't yield this effect.

There are a number of approaches to "solving" this problem (what problem? If you're trying to accurately simulate an elastic body, use the Finite Element method). One is to create a triangle mesh over the interior of the shape using, e.g. a constrained Delaunay Triangulation. Each of the edges in the triangulation can be replaced with a spring.

Another approach is for approximating what is actually an elastic skin with some incompressible medium inside (well, not really incompressible, since it will compress and change its interior pressure to reflect that). Again, the boundary is modeled as a series of springs, but now the volume of the object is computed and a pressure load is applied at each node reflecting the internal volume. This will, of course, tend to make any polygon into a circle, so to approach this, give each boundary node an angle property (each node already has x and y, so give it $\theta$) which represents the change in angle of the two connected edges. Let nodes then impose moments on those edges based on how different that angle is from what the original shape's was. This will give the object shape and volume preserving qualities.

Who would have thought that my expertise in numerical solid mechanics would ever come in handy here?

Edited by Yourself, 18 July 2010 - 08:09 PM.

• 0

### #7 KC LC

KC LC

• Retired Staff
• 5309 posts

Posted 18 July 2010 - 08:37 PM

Let nodes then impose moments on those edges based on how different that angle is from what the original shape's was. This will give the object shape and volume preserving qualities.

But calculating an angle (using inverse trig functions) seems like lots of overhead for real-time in GM. There must be a faster metric that would work (almost) as well.

edit: maybe I could use the built-in "direction_to_point" function for two adjacent nodes, instead of the trig functions. I could compare neighboring facets that way. That's fast.
• 0

### #8 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 18 July 2010 - 10:06 PM

But calculating an angle (using inverse trig functions) seems like lots of overhead for real-time in GM.

Usually you don't actually compute angles, you compute derivatives and assume they're small enough that they're approximately equal to the angle (which is a good assumption if you're modelling something sufficiently stiff in the bending sense). But then I don't imagine that using inverse trig functions are actually that slow in this case. At least not compared with a method where you use a much larger number of springs. But you don't really need to use them, you just need to find some way to map the local node configuration to a force such that the force acts against changes in the configuration.

Here's how it's done in a finite element context, but this is still an approximation (since it assumes small derivatives). There are a huge number of different kinds of beam elements that can be used, but when you start leaving this small displacement assumption behind, you're left with something pretty messy. Masses and linear springs (and rotational springs) are obviously substantially simpler than this, even with inverse trig functions.

Edited by Yourself, 18 July 2010 - 10:06 PM.

• 0

### #9 monocledsardine

monocledsardine

GMC Member

• New Member
• 2345 posts
• Version:GM8

Posted 19 July 2010 - 06:30 AM

Let nodes then impose moments on those edges based on how different that angle is from what the original shape's was. This will give the object shape and volume preserving qualities.

How are nodes supposed to impose movements on edges? I'm unclear on how to handle the resultant node movement when theta is > or < the default.

Edited by monocledsardine, 19 July 2010 - 06:31 AM.

• 0

### #10 Yourself

Yourself

The Ultimate Pronoun

• Retired Staff
• 7341 posts
• Version:Unknown

Posted 19 July 2010 - 06:46 AM

By imposing what amounts to a torque on those edges. So really just a point load on the neighboring points perpendicular to the edge direction.
• 0

### #11 ~Dannyboy~

~Dannyboy~

~hoqhuue(|~

• GMC Member
• 2144 posts
• Version:GM8

Posted 19 July 2010 - 07:24 AM

I like the idea of moments as Yourself suggests. I do wonder how well it will keep it's shape though... Only one way to find out I guess.
• 0

### #12 monocledsardine

monocledsardine

GMC Member

• New Member
• 2345 posts
• Version:GM8

Posted 19 July 2010 - 10:00 PM

By imposing what amounts to a torque on those edges. So really just a point load on the neighboring points perpendicular to the edge direction.

Oh, I see. Thanks, I'll plug this in and see how it works.
• 0

### #13 holomanga

holomanga

GMC Member

• GMC Member
• 146 posts

Posted 23 July 2010 - 07:32 PM

I thought of way to do it. Here it is:

(for a three-point system to simplify stuff)

x1+x2+x3=1

if x1<1/3 then x2=x2+0.01 and x3=x3+0.01

if x2<1/3 then x1=x1+0.01 and x3=x3+0.01

and so on...

this shape would not be elastic, but elastic scripts in your first post may be added to this. Ill try to make a testable demo.

Edited by holomanga, 23 July 2010 - 07:33 PM.

• 0

### #14 TheSnidr

TheSnidr

That guy

• Global Moderators
• 2436 posts
• Version:GM:Studio

Posted 27 July 2010 - 08:18 PM

I've passively been working on such spring physics lately, and I made some demonstrations of how to make balls with different elasticity:

Each ball uses a different system of connecting the springs. The goo ball makes each point connect to its 5 nearest points, the amoeba connects to its 4 nearest points, plus the point at its opposite.
The tennisball has all points connected to all points.

Don't slaughter my limited knowledge of physics! I'm learning to make game physics the hard way: by myself.
I gathered inspiration for the engine here: http://cowboyprogram...5/blob-physics/
I didn't manage to make the blob he made, but it helped me on the way.
• 0

### #15 -=ReNeX=-

-=ReNeX=-

GMC Member

• New Member
• 403 posts

Posted 12 October 2010 - 05:34 PM

And after we manage to compile a general solution for blobs, we can start thinking about mutating blobs (blobs that change the number of vertexes as they are stretched). If it is done correctly, an uncompressible blob with a very low stiffness and no angular stiffness would simulate a fluid. That would be interesting. Also, when two nonrelated vertexes cross each other, the fluid surface was broken and the blob divides. And when two blobs intersect, they mix. Now i will have to test this myself, because i got too interested. Even had an idea about having different colored blobs that mix together changing their colors.
• 0

### #16 ga05as

ga05as

GMC Member

• GMC Member
• 876 posts

Posted 22 April 2012 - 10:00 PM

I thought i would give soft bodies a shot, never tried them before...
This is what i came up with, i am pretty happy with the result

Here is a quick screen record i did so you don't have to download if you don't want to...

My computer is VERY slow with any program, so I imagine it to be smoother on better computers.

Edited by ga05as, 23 April 2012 - 08:00 PM.

• 0

### #17 Futhark

Futhark

• GMC Member
• 678 posts
• Version:GM8.1

Posted 22 April 2012 - 11:01 PM

I thought i would give soft bodies a shot, never tried them before...
This is what i came up with, i am pretty happy with the result

I am stupendously impressed! Sure, it's not super fast but veeery impressive (in my opinion). I enjoyed fiddling around with the softbodies, removing and adding links, making the screen higher and more platforms to fall onto on the way down.

This is one of those "educational" tools that can keep me entertained for hours.

Love it!!!

Edited by Futhark, 22 April 2012 - 11:04 PM.

• 0

### #18 ga05as

ga05as

GMC Member

• GMC Member
• 876 posts

Posted 22 April 2012 - 11:09 PM

Haha! thank-you!

The physics is remarkably simple..
I just made lots of masses and connected them all up with springs,
then i just made any masses repel any other masses that they weren't attached to if they got too close...

And that was the result... It shocked me when i first hit F5.. I don't know how accurate the physics it to real life, but it looks cool! ha
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users