Hi!
I would like to share with you DEFLUID - my asset to create 2D fluid simulation based on metaballs:
(Logo made with DesignEvo)
It contains a premade game object that serves as an emitter of metaballs - game objects with a sprite and a special material that uses a custom fragment program to simulate viscosity of the fluid.
Set the Defluid script properties as you wish or even copy the Defluid game object and create your own metaballs - for example with different collision objects, etc.
Defluid emitter properties:
Num of metaballs - number of created at once game objects
Scale of metaballs - uniform scale of a metaball game object
Emitter width - width of the box in which the metaballs are created
Emitter height - height of the box in which the metaballs are created
Dry Each After - will “dry” - animate each object’s scale down and at the end delete it after each amount of time (in seconds, set to 0 to turn drying off)
Create at init - create num_of_metaballs objects at the defluid object creation
Debug physics - turn on Defold built-in physics debug visualisation
Messages:
You can then send messages to this game objects:
activate – create metaballs based on object’s properties
change – change object’s properties: num_of_metaballs, emitter_width, emitter_height, dry_each_after
force – apply one force vector to each metaball
dry – “dry” fluid - animate each metaball’s scale down to zero and at the end delete that object
remove – delete all metaballs instantly
Additionaly you can set up the property of defluid_plane:
Fluid Color - allows to change the color of metaballs
and send message to defluid_plane to change the color of metaballs:
change_color – message.color [vmath.vector4()] - change tint of rendered metaballs
The best option to get familiar with this is to check out my example
-- DEFLUID example
-- This example shows how can you modify defluid game object by sending messages to its script.
function init(self)
msg.post("/defluid", "activate") -- activate creation of metaballs (fluid)
msg.post("/defluid", "change", { dry_each_after = 1 }) -- change param of defluid
timer.delay(3.5, false, function() -- after 3.5 s push up all metaballs
msg.post("/defluid", "force", { force = vmath.vector3(0,10000000,0) })
end)
msg.post("/defluid", "dry") -- activate drying (dry a metaball every second)
end
function final(self)
msg.post("/defluid", "remove") -- if needed you can destroy metaballs anytime
end
To make it work:
- use included render with a custom render script (modify Render to
/defluid/defluid.render
in your game.project) - add Rendercam library dependency in your game.project (I used https://github.com/rgrams/rendercam/archive/v1.0.1.zip)
- add Rendercam camera object to your collection
- add defluid_plane object (which has a render target plane) to your collection
- adjust your game.project to your needs - modify gravity, max collisions and max contacts (in example I used respectively -1000, 4096, 4096)
- add defluid object(s) to your collection and adjust its properties
Note: Defluid metaballs have its default collision objects with:
group: defluid
masks: defluid
, ground
, mob
Enjoy it and create awesome games with Defluid!
May Deforce be with you!
Paweł