Fluid simulation 2D - metaballs

Ok, so I was encouraged by @pkeod to create a fluid simulation using metaballs. I read a lot about it and except mathematical shaders there are solutions where I need to:

  1. Create a ‘particle’ go with a ‘falloff’ sprite, so that it’s alpha channel represents density falling off from the center.

  2. Draw a lot of instances of above to a render target with additive blending.

  3. Create a shader that will draw fluid if the alpha of each pixel is above a threshold value. It will create an effect of viscosity of those metaballs.

I’m creating game objects with the sprite and dynamic collision object for this. I created a material for those called metaball. I’m trying to create a working render script now. I think I created kind of the effect I wanted, because it’s behaving like metaballs, as you can see here:

metaballs metaballs2

BUT it’s my first time creating such render pipeline, understanding render targets etc, so the whole screen isn’t looking like I want it :frowning: I was basing on @ross.grams’ rendercam lighting render script, because there were two targets and at the end they were merged on the third one - a plane. I understand thoroughly the pipeline and read a lot about it on our pages and about OpenGL and metaballs, though still can’t figure out what am I doing wrong :frowning: I’m stuck at this point:

Physics debug allows me to see what’s going on really, but the render is far away from that :confounded: A first I don’t understand why the render is like a negative - only on the background sprite.

Here’s my project, maybe someone could help me, please?

Water2D.zip (410.0 KB)

You forgot to add a rendercam camera to your collection?

It does look like the debug physics is not drawing in the correct way. I think it does work with vanilla rendercam so you might want to compare the render scripts to figure out what’s wrong.

If I disable the render target and draw the particles with the debug then the position is correct. So most likely easiest solution to make this work would be to look at current vanilla rendercam and transplant the render target stuff over to it.

For your physics, I would lower the scale of the physics in game.project, then play with increasing the mass of the particles and increasing the gravity of the world.

It looks like you have most of the pieces figured out you just need to put them together piece by piece so each works and then the whole works.

2 Likes

What is vanilla rendercam?

I mostly repaired the render target material, where I was multiplaying layers instead of adding them. I added an object that I want to apply buoyancy, but without it, the object floats even fine :smiley: Still, the physics debug draw is not matching the render :confused:

metaballs3

3 Likes

I mean this https://github.com/rgrams/rendercam

https://raw.githubusercontent.com/rgrams/rendercam/master/rendercam/rendercam.render_script

You can see your render script (made based off an old example maybe?) is much different than the version currently live. You should probably use the current live up to date version as a bases and merge the render target stuff into it no matter what.

@britzl didn’t you make something to make using render targets easier?

2 Likes

Ok, I get it, vanilla :smiley:

Maybe this?

I’m just looking into this. Btw, @britzl’s examples should be more exposed on Defold’s main page imo :wink: Maybe you should update examples page a bit?

1 Like

I haven’t made a really official announcement of this yet. I’m not sure it’s useful enough yet or user friendly enough. I think it has potential, but I don’t want to announce anything until I know for sure. I need to spend some time coding an actual game using it. Need to game jam!

3 Likes

The idea looks great! :wink: Also the usage is very simplified, I’d rather start with examples like this, instead of diving into render pipeline and learning everything from scratch. When I was thinking about post-process effects I would imagine something like this - applying a commonly used post-process without really diving into how it is made.
For me, still this is the difficult topic, though I was learning it a lot. I managed to understand thoroughly shaders, but drawing in Defold is still complex at the start :confused:
It was what I was lacking in Defold from the begining - ready lights components, easy normal maps for sprites, fluid simulations, post-process examples like Unity or Godot have. Of course, I’m not taking into consideration 3D features, because I was looking for a 2D engine. Naturally, those brainless modules do not force you to learn and I think I must be grateful for that in Defold, though still, for the beginners it’s something that allows to judge the engine, unfortunately.

Time for another Defold game jam, boss!

7 Likes

Hmm, I don’t think I’ve ever seen a Boss Jam?!
Maybe we should to that? Straight to the point :slight_smile:

7 Likes

Good idea!

2 Likes

DEFOLD GAME JAM :astonished: !! Awesome!! I’m in.
(Man we hadn’t had one for so looooong)

1 Like

Your render script is based on an older version of Rendercam, and Defold changed the draw debug function last month. If you copy the debug rendering chunk from the current Rendercam render script and paste it in your render script in the “base render” chunk, after drawing particles, and remove the last two lines of the update function, then it will work correctly.

5 Likes

Thanks, I’m preparing it now to put on the asset page, for anyone who want to create some sequel of Where is my water? or Happy Glass :smiley:

6 Likes
DEBUG:SCRIPT: url: [main:/instance298#collsionobject]	vmath.vector3(19.734327316284, -71.744812011719, 0)
ERROR:GAMEOBJECT: Component '/instance298#collsionobject' could not be found when dispatching message 'apply_force' sent from main:/emitter#script

I’m saving the id and url of the created game objects to the table and then I’m using the same url to print it and to send data:

print(ball.url, go.get_world_position(ball.id))
msg.post(ball.url, "apply_force", {force = vmath.vector3(1,0,0), position = go.get_world_position(ball.id)})

Anyone know why?

Should this not be collisionobject?

3 Likes

Thanks! Indeed :smiley: I can always rely on you guys if it comes to typos :smiley:

2 Likes