# Simple Liquid Simulation

22 posts

 I’m looking for a simple liquid simulation, does anyone know of a good one that doesn’t take much effort? Visuals or physics or both? Originally posted by UnknownGuardian:Visuals or physics or both? Visuals, I guess. I have a “core” in the center of my player that you can see through, and I want to be able to see liquid splash around inside of it and react to the player moving. Here is a Box2D approach: http://www.emanueleferonato.com/2012/05/16/simulating-mudslime-with-box2d-bitmaps-and-filters/ DO not use circle physics. Use points particles, and the algorithm is as follows: for each i: -add (negative player velocity * multiplier(0.01?)) to i velocity -multiply i velocity by damping factor(0.8?) -for each particle i within a certain radius(20?make sure this is smaller than visual radius): —move both i and j away from each other by dist(i,j) * multiplier(0.05?) -constrain within the circle that is the “core” may require tweaking. If side view, add gravity gently. Originally posted by qwerberberber:DO not use circle physics. Use points particles, and the algorithm is as follows: for each i: -add (negative player velocity * multiplier(0.01?)) to i velocity -multiply i velocity by damping factor(0.8?) -for each particle i within a certain radius(20?make sure this is smaller than visual radius): —move both i and j away from each other by dist(i,j) * multiplier(0.05?) -constrain within the circle that is the “core” may require tweaking. If side view, add gravity gently. Could you elaborate on “Points Particles” and what “j” is? It’s not defined in your pseudocode. Originally posted by RTL_Shadow:Originally posted by qwerberberber:DO not use circle physics. Use points particles, and the algorithm is as follows: for each i: -add (negative player velocity * multiplier(0.01?)) to i velocity -multiply i velocity by damping factor(0.8?) -for each particle i within a certain radius(20?make sure this is smaller than visual radius): —move both i and j away from each other by dist(i,j) * multiplier(0.05?) -constrain within the circle that is the “core” may require tweaking. If side view, add gravity gently. Could you elaborate on “Points Particles” and what “j” is? It’s not defined in your pseudocode. point particles means particles are not separated using circle physics and instead of using circles to represent the particle you only use x ans y. i and j belong to the same set of particles. Fluid dynamics are actually very difficult to simulate. The first challenge is to apply physics to the water particles. Usually circle collisions are used (I think) and you could get away with that, or a simplified version, in a simple top-down game. If the game is side-scrolling, and gravity is applied to them, proper collision is more important. The second issue is displaying the fluid. You could use circles for all particles, but this would create gaps in a blob for example, and things like that don’t occur in nature. Due to surface tension (caused by Hydrogen connections in water), water forms drops and two drops can fuse together easily. One approach I saw once is to blur the image of the particles, and then increase the contrast, so it looks normal again. I simply do NOT understand how to apply “the look”. I’ve looked at dozens of tutorials but they’ve failed to explain the part I really have trouble understanding. step 1: apply blur (got that) step 2: somehow apply this weird threshold thing in this convoluted way that everybody seems to do differently and it makes no sense but makes it look beautiful in the end result. Can someone explain using the threshold and blur together to actually achieve the end result? I can figure out how to do the actual metaball physics. Originally posted by alecz127:step 2: somehow apply this weird threshold thing in this convoluted way that everybody seems to do differently and it makes no sense but makes it look beautiful in the end result. Open photoshop or gimp. Draw a gradient. Apply the “Threshold” filter. Observe results. Think about applying this effect to a blurred circle. Think about what happens when multiple circles in proximity to each other are blurred. http://www.fastswf.com/Nmx6mcc Click on the left side of the window to get the original blurred bitmap, and then the meta-ball effect on the right. I whipped this up quite quick so it looks a bit ugly but the basic principle works. Code: Meta-bitmap blur ```_meta.bitmapData.fillRect(_meta.bitmapData.rect, 0); _meta.bitmapData.threshold(_bitmap.bitmapData, _bitmap.bitmapData.rect, new Point(0, 0), ">", 0x88888822, 0xffffffff); _meta.bitmapData.applyFilter(_meta.bitmapData, _meta.bitmapData.rect, new Point(0, 0), new GlowFilter(0xFFFFFF)); ``` _bitmap is just the original bitmap on the left, which gets gradient filled shapes drawn to its bitmapData. EDIT: Another meta-ball test I found. http://www.fastswf.com/X_xnhtI I have the source if you want it, just ask and ill upload it. @DPbrad I’d love the source if you wouldn’t mind. I think its making more sense. Originally posted by alecz127:@DPbrad I’d love the source if you wouldn’t mind. I think its making more sense. Sure, here you go: http://dpbrad.com/storage/Metaballs.zip That second one looks sexy as heck, especially with the dynamic lighting :) Edit: So here is my current code: ``` var b:LiquidBall; for each ( b in balls ) { //physics if ( b.v.y < 2 ) { b.v.y += gravity; } //collision if ( dist( b, middle ) >= radius - 8 ) { b.v.y = 0; } b.x += b.v.x; b.y += b.v.y; //rendering liquidData.copyPixels( baseBallData, baseBallData.rect, new Point( b.x, b.y ), null, null, true ); } ``` Now, obviously this won’t work, because if a ball goes anywhere besides straight down it will only set y to zero. How do I effectively handle a collision. The container is a circle, and “middle” variable is the center of it. Any idea how to properly handle collisions? I got it, ****: http://www.fastswf.com/ecVXiqU Originally posted by RTL_Shadow:I got it, ****: http://www.fastswf.com/ecVXiqU hells yeah brotha! Way to go! I’m still at square one. x o Then again I had to work a day job all day. Apart from the fact they still bounce at rest, that looks quite good. Wait a minute or two and the particles end up on top of one another. By pushing them around with the mouse you can get them to all collapse into one particle that is no longer affected by gravity with a bit of effort. Methinks there is a bug somewhere, unless you factored in evaporation somehow? Anyway compacting all the droplets into one is way more fun than it has any right to be. Yep, there’s a bug. You can also make them move to the top and stay motionless up there. Looks nice. Yep, there’s a few bugs I need to iron out. Mostly cause I prototyped this for fast-creation, not no bugs. This topic made me wanna try n re-create the effect in spewer, so i made a slime-like liquid simulation here It can get pretty laggy after 500 particles because of all the collisions but i’m working on optimizing it. Click-drag to spawn slime particles.