[TechDemo] liqöd - Water Simulation

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

[TechDemo] liqöd - Water Simulation

Post by Ranguna259 »

Hello everyone :D

For the past month I've been working on water simulation and I've finally decided to post the code here.

The algorithm uses SPH to simulate water like physics and it's a basic copy from this code here with help from this post. It simulates water in a zero gravity enviroment (because I haven't implemented particle-to-rigid body collision yet) and it lookes really incredible.
Image

This demo also works on Android (and possibly iOS) using the corresponding ports (Android, iOS) but it's slow and that's why I need help threading things up. It supports multitouch too.

This is not the final code and I'm hoping you guys can help me out with this, namely performance wise (threading and such).
The main algorithm is located in liquid.applyLiquidConstraint function and this is not in any way binded with the LÖVE's physics module.

You can find liqöd's git repo here.

Keys:
Windows:
Left click - Spawn particles
Escape - Close demo
Mobile:
Touch press - Spawn particles
Attachments
liquid.love
liqöd updated
(4.96 KiB) Downloaded 314 times
Last edited by Ranguna259 on Wed Jan 21, 2015 9:10 pm, edited 1 time in total.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
User avatar
jack0088
Prole
Posts: 33
Joined: Fri Mar 22, 2013 4:59 am

Re: [TechDemo] liqöd - Water Simulation

Post by jack0088 »

super cool! makes me want to code cool things again. 8B
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [TechDemo] liqöd - Water Simulation

Post by Ranguna259 »

Thanks :3
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
rexjericho
Prole
Posts: 44
Joined: Sat Dec 15, 2012 7:55 am

Re: [TechDemo] liqöd - Water Simulation

Post by rexjericho »

Hey, this is really cool. I've been looking into SPH fluid simulation over the past few months.

I looked at your code, and modified a few lines to add gravity to the simulation to see how it would look. Press "g" to toggle gravity.

One way that could help with speeding up your program is to reduce the number of tables that are created each time step. Reusing tables will also ease the load on the garbage collector. Especially reuse of tables in loops within loops.

Example:

Instead of something like this:

Code: Select all

for i = 1,liquid._vars.numActiveParticles do
    local particle = liquid._vars.liquid[index]

    for i,v in ipairs(particle.neighbors) do
      local relativePosition = {liquid._vars.scaledPositions[v][1] - liquid._vars.scaledPositions[index][1],
                                              liquid._vars.scaledPositions[v][2] - liquid._vars.scaledPositions[index][2]}
      -- rest of loop
    end
end
Try this:

Code: Select all

local relativePosition = {}
for i = 1,liquid._vars.numActiveParticles do
    local particle = liquid._vars.liquid[index]

    for i,v in ipairs(particle.neighbors) do
      relativePosition[1] = liquid._vars.scaledPositions[v][1] - liquid._vars.scaledPositions[index][1]
      relativePosition[2] = liquid._vars.scaledPositions[v][2] - liquid._vars.scaledPositions[index][2]
      -- rest of loop
    end
end
Attachments
liqod-gravity.love
Press "g" to toggle gravity.
(4.43 KiB) Downloaded 257 times
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [TechDemo] liqöd - Water Simulation

Post by Ranguna259 »

Awesome job implementing bounderies :D
BTW, my code already has gravity but it was set to 0, you can find it on line 278 in my code and 286 in yours, at the end of the line change .0 to 9.8/300.
rexjericho wrote:One way that could help with speeding up your program is to reduce the number of tables that are created each time step. Reusing tables will also ease the load on the garbage collector. Especially reuse of tables in loops within loops.
I tested that and it got a little slower, I removed the local call from relativePosition, relativeVelocity and the d variables and created them right at the beginning of the applyLiquidConstraint function and I got these results:
Before changing:
120-160 FPS with 400 particles
liquid bfr.love
Before changing local calls
(4.18 KiB) Downloaded 140 times
After changing:
80-115 FPS with 400 particles
liquid aft.love
After changing local calls
(4.21 KiB) Downloaded 154 times
I'm looking into threading the code but I'm having difficulties because the current API thread doesn't support tables withing tables.

What's min_x and min_y for in your code ?
I'm going to merge your code with mine and upload it to github if you don't mind.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
rexjericho
Prole
Posts: 44
Joined: Sat Dec 15, 2012 7:55 am

Re: [TechDemo] liqöd - Water Simulation

Post by rexjericho »

Ranguna259 wrote:Awesome job implementing bounderies :D
What's min_x and min_y for in your code ?
I'm going to merge your code with mine and upload it to github if you don't mind.
I don't mind at all.

(x_min, x_max) and (y_min, y_max) were for figuring out the bounds for particle.position. Then I found that I could multiply the position by liquid._vars.scale to get the relative position on the screen. x_min/y_min is always 0, and x_max/y_max aren't actually used since I figured out that scale thing.

I tried your above modifications and I also noticed a slowdown in the liquid_aft version. I had about 64fps before and 55fps after when simulating 400 particles.

I tried modifying liquid_bfr myself to make some optimizations, but they did not have an effect on framerate when simulating 400 particles. The modifications did have a slight impact when simulating a larger number of particles. I had 16 fps before vs 24 fps in the after version with 1700 particles. I wasn't able to measure a difference in FPS until simulating over 1500 particles.

It will be cool to see this fluid interact with Box2D rigid bodies when you get that working.

I implemented an alternate method of fluid-object interaction when writing my own SPH fluid simulation that seemed to work well. The objects were treated as a collection of fluid particles packed together into a shape. The object particles interact with regular fluid particles as if they were a fluid, except that their motion is controlled by an external force instead of by the force of the fluid. Here's a video showing what I mean:
https://www.youtube.com/watch?v=LIKSFU_4YTo
Attachments
liquid after.love
(4.25 KiB) Downloaded 142 times
liquid before.love
(4.12 KiB) Downloaded 111 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: [TechDemo] liqöd - Water Simulation

Post by micha »

This thing is impressive!!
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [TechDemo] liqöd - Water Simulation

Post by Ranguna259 »

rexjericho wrote:I implemented an alternate method of fluid-object interaction when writing my own SPH fluid simulation that seemed to work well.
Thank you
I love your blog and your game looks awesome too :D
rexjericho wrote:I don't mind at all.
Merged and updated.
I added the boundary as a post update function and I loved the way the fluid moved when the gravity was applied to the delta table of each neighbor so I added that too as a new type of gravity/water (press "w" to change between gravity types).
liquid.love
(4.96 KiB) Downloaded 161 times
rexjericho wrote:I tried modifying liquid_bfr myself to make some optimizations, but they did not have an effect on framerate when simulating 400 particles. The modifications did have a slight impact when simulating a larger number of particles. I had 16 fps before vs 24 fps in the after version with 1700 particles. I wasn't able to measure a difference in FPS until simulating over 1500 particles.
A little FPS improvement is better than no improvement at all, I'll look into your liquid_after to see what you did and I'll try to implement it in the current code.

rexjericho wrote:It will be cool to see this fluid interact with Box2D rigid bodies when you get that working.

I implemented an alternate method of fluid-object interaction when writing my own SPH fluid simulation that seemed to work well. The objects were treated as a collection of fluid particles packed together into a shape. The object particles interact with regular fluid particles as if they were a fluid, except that their motion is controlled by an external force instead of by the force of the fluid. Here's a video showing what I mean:
https://www.youtube.com/watch?v=LIKSFU_4YTo
I also thought about that but I never got into coding it because that'd probably have a major impact on performance due to simulating a huge quantities of particles so I decided to go with another approach, I'll call a queryBoundingBox to the objects on screen with a function that sorts the particles that are inside AABBs by using the grid, then I simply check if they are inside object by testing a point, if they are I'll run a raycast from the current point to the last point, see where it intersect with the object, move the particle to the intersection point and change velocities with the normal of the the object side that the particle intersected with, based my idea on this post.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [TechDemo] liqöd - Water Simulation

Post by davisdude »

Very cool library!
One minor note: you press "g" to change the gravity, not "w".
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: [TechDemo] liqöd - Water Simulation

Post by Ranguna259 »

davisdude wrote:Very cool library!
One minor note: you press "g" to change the gravity, not "w".
Thank you :)
I said that the "w" key was to change the gravity "type" no the actual gravity :P
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest