How to make SOFTBODIES (Polygons)

I have recently created easy-to-use softbodies (blob-like shapes)!
The code has customizable parameters and can easily be changed as you like!
Here’s a step-by-step guide on how to make a working softbody!

Objects:

Center

The center will serve as the center of the blob where the shape is built around.

Point

(Variables: vx, vy, Index, oldX, oldY)

The points are what is holding the shape together and what the shape is formed by, this is what most of the math will be acting upon.

BlobFiller

(Shape painter)

The shape painter will fill in and outline (optional) your blob for a nice looking aesthetic.

Scene variables:

(The variables with descriptions may be customized as you like.)

Index
Angle
Points - The amount of points in the polygon.
Radius - The width of a line drawn from the side of the shape to the center.

Events:


Just a clarification for the last event, the number can be changed to make the blob seem more like a liquid or a rubber or jelly-like substance.

I’ll do you a solid and give you the entire thing, but don’t forget to remove everything that’s in the event when you first create it.

const Radius = runtimeScene.getVariables().get("Radius").getAsNumber();
const points = runtimeScene.getObjects("Point");
const center = runtimeScene.getObjects("Center")[0];
if (!center || points.length !== points.length) return;

const targetArea = 3.14159265359 * Radius ** 2;

let area = 0;
for (let i = 0; i < points.length; i++) {
    const A = points[i];
    const B = points[(i + 1) % points.length];
    area += A.getX() * B.getY() - B.getX() * A.getY();
}
const currentArea = Math.abs(area / 2);

const strengthMultiplier = 10;
// Change strengthMultiplier if you feel as if the blob is growing too much or too little on impact with an object.
const areaDiffFactor = ((targetArea - currentArea) / targetArea) * strengthMultiplier;

const cx = center.getX();
const cy = center.getY();

for (const point of points) {
    const dx = point.getX() - cx;
    const dy = point.getY() - cy;

    const length = Math.sqrt(dx * dx + dy * dy);
    if (length === 0) continue;

    const unitX = dx / length;
    const unitY = dy / length;

    const moveX = unitX * areaDiffFactor;
    const moveY = unitY * areaDiffFactor;

    point.setX(point.getX() + moveX);
    point.setY(point.getY() + moveY);
}

Now for the finishing touch.


All done!
Don’t forget to tell me if anything isn’t working, or if you have any feedback or suggestions, feel free to let me know!
Edit: I forgot to mention that if you don’t want the blob to follow your cursor you can add physics to the point/center objects to add gravity and collisions.

1 Like

Hi ResinTheFuriousMage, I tried this. It was a bit stressful typing the Change position of Point stuff. But anyway, I’m not sure of the results. I put the shape painter in the scene, gave it a fill color and unchecked the relative to scene setting. I made the scene variables Points 12 and Radius 8. Nothing happened. Then I changed the Radius to 2 and I got some jagged colors of the shape painter outline color. After that I changed the Radius back to 8 and I still got the outline colour bits so the no result from before was weird.

I removed the hide settings for Center and Point.

What did you mean by this? Sorry if I should get it. I don’t think I made any mistakes, so is this part very significant? I added all the javascript stuff.

Update
I was missing some brackets and now have circles instead of jagged triangles.
The tricky part to type is one of the Change position of Point actions. I was missing some brackets in both the x and y. I notice though that they are inconsistent between x and y.

Here’s the x and y that I think are now the same as the screenshot.

((Center.X() + cos(Index * Pi()/(Points/2)) * Radius) - Point.X()) * 0.15

((Center.Y() + sin(Index * (Pi()/(Points/2))) * Radius) - Point.Y()) * 0.15

So is everything working as intended?
The first few events are the most important, I’d recommend only using the shape painter after finishing everything with the physics.
And if you’re still confused about what I said.

I was talking about the first line of code in the JavaScript event when you first made the event, but it wouldn’t have been very significant if you forgot, it just added a blue colored background.
The math is very precise, so yes, one mess up would cause the whole thing to be incorrect.

1 Like

You’re really amazing to work all this out and in such a short amount of time. I still can’t get it right though.

I don’t think so? It’s not like your build from your other post. I found one more missing set of brackets and it made a small difference. I get the same small circles following the mouse. As soon as I stop moving the mouse the screen goes to a green color.

Which physics? Should some of the objects have the physics behavior?


BlobPreviewTest

Could you show me your variables?
It would be much easier to have your point and center objects be 8x8, or 4x4 to make it better to see what’s going on.
Make sure you turn on “Clear the rendered image between each frame” in the shape painter.
I believe you’re somehow filling the entire screen with the shape painter.
Edit: After re-watching the clip, it seems the point disappeared.
And no, none of the objects need the physics behavior, when I say “physics,” I’m referring to the math that makes the blob move.

1 Like

Done. When I had the rendered image checked I didn’t see anything which is why I had changed it.

It doesn’t show in the gif, but now when I stop the cursor there’s a brief crescent moon shape with the shape painter outline color and the fill color.

BlobPreviewTest2

Could you make your points and the center visible?
Also I’d recommend making the radius bigger for now, try 50 for a test.
You should see all of your points in a circle around the center, or, depending on the amount of points you have, it would be better to say “equally spaced around the center”.

1 Like

Sorry about that, I forgot to change it back.

Originally when I was missing brackets, making the radius smaller helped there actually be something on the screen. Now that I’ve made it really big I get a beautiful ball! This gif is with Center and Points visible but they’re not actually showing. What kind of objects are needed for it to ‘bounce’ off? I tried putting a static physics object there but nothing changed.
Update, I forgot to give the physics behaviour to Point. Woohoo, now the fun begins.

BlobPreviewTest3

1 Like

I hope you have fun!
Making this took me a couple of days, and it was a really fun experience!
Make sure to send me the game in DMs when you’re done! :wink:
A quick little tip, I’d recommend making the outline smaller if the blob is going to be smaller, try setting a scale, like for every 10 pixels wide the radius is, is 1 pixel for the outline.

1 Like

I’ll try your suggestions, thank you. I ended up giving both Point and Center the physics behavior. This is the result with no adjustments of any kind.

Amazing work, well done to you.

BlobPreviewTest4

1 Like

Last playtime for now. I substituted a circle for the line for the drawing action. I put the Center object in the scene instead of creating it, and stuck some eyes on it. The ‘wrinkles’ are because of low quality gif saving to get it small enough to post here.

BlobPreviewTest5

2 Likes