Playing around with fluid simulation

Update :slight_smile:

Thanks a lot!

I managed to create a function to change color with keys, using pixelflow.

I’ll paste it for future reference:

// FLUID SIMULATION EXAMPLE
import com.thomasdiewald.pixelflow.java.DwPixelFlow;
import com.thomasdiewald.pixelflow.java.fluid.DwFluid2D;

// fluid simulation

DwFluid2D fluid2;

// render target
PGraphics2D pg_fluid;

float bom;
float den;

public void setup() {
  size(800, 800, P2D);

  // library context
  DwPixelFlow context = new DwPixelFlow(this);

  // fluid simulation
  fluid2 = new DwFluid2D(context, width, height, 1);

  // some fluid parameters
  fluid2.param.dissipation_velocity = 0.2f;
  fluid2.param.dissipation_density  = 1f;
  
  pg_fluid = (PGraphics2D) createGraphics(width, height, P2D);
}


public void draw() {    
  
  addColor();
  
  // update simulation

  fluid2.update();

  // clear render target
  pg_fluid.beginDraw();
  pg_fluid.background(0);
  pg_fluid.endDraw();

  // render fluid stuff
 
  fluid2.renderFluidTextures(pg_fluid, 0);

  // display
  image(pg_fluid, 0, 0);
}

void addColor() {
      fluid2.addCallback_FluiData(new  DwFluid2D.FluidData() {
    public void update(DwFluid2D fluid2) {
      
        float px     = mouseX;
        float py     = height-mouseY;
        float vx     = (mouseX - pmouseX) * +15;
        float vy     = (mouseY - pmouseY) * -15;
        
        fluid2.addVelocity(px, py, 14, vx, vy);
       
        if (mousePressed) {
        fluid2.addVelocity(px, py, 14, vx, vy);
        }

        if (keyPressed) {
         if (key == '1'){
        float bom  = 0.1f;
            fluid2.addDensity (px, py, 20, bom, 0.4f,0.1, 1.0f);
         }
         if (key == '2'){
         float bom = 0.7f;
             fluid2.addDensity (px, py, 20, bom, 0.4f,0.1, 1.0f);
         }               
        } 
  
       
        
    
       }
      });
}
1 Like