gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
write code for a fluid simulation is p5, make it trigger on click
- User
write code for a fluid simulation is p5, make it trigger on click
- Assistant
Sure! Here's an example of a 2D fluid simulation in p5.js that triggers on click:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/addons/p5.sound.min.js"></script> <title>Fluid Simulation</title> </head> <body> <script> let canvasSize = 400; // Size of the canvas let gridSize = 40; // Number of cells in the grid let grid; let cellSize; let fluidDensity = 0.1; // Density of the fluid let fluidViscosity = 0.01; // Viscosity of the fluid function setup() { createCanvas(canvasSize, canvasSize); cellSize = canvasSize / gridSize; grid = create2DArray(gridSize); } function draw() { background(255); updateFluid(); drawFluid(); } function updateFluid() { for (let i = 0; i < gridSize; i++) { for (let j = 0; j < gridSize; j++) { let velocity = createVector(0, 0); // Velocity vector let pressure = 0; // Pressure scalar // Calculate the velocity and pressure based on the fluid properties for (let x = -1; x <= 1; x++) { for (let y = -1; y <= 1; y++) { let neighborI = i + x; let neighborJ = j + y; // Check if the neighbor is within the boundaries of the grid if (neighborI >= 0 && neighborI < gridSize && neighborJ >= 0 && neighborJ < gridSize) { let neighbor = grid[neighborI][neighborJ]; // Add the velocity and pressure contribution from the neighbor velocity.add(neighbor.velocity); pressure += neighbor.pressure; } } } // Average the velocity and pressure contributions velocity.div(9); pressure /= 9; // Update the velocity and pressure for the current cell let cell = grid[i][j]; cell.velocity = velocity; cell.pressure = pressure; } } } function drawFluid() { for (let i = 0; i < gridSize; i++) { for (let j = 0; j < gridSize; j++) { let x = i * cellSize; let y = j * cellSize; let cell = grid[i][j]; // Draw the fluid density using grayscale colors noStroke(); fill(cell.density * 255); rect(x, y, cellSize, cellSize); } } } function mousePressed() { // Add a density value at the mouse position to trigger the fluid simulation let i = floor(mouseX / cellSize); let j = floor(mouseY / cellSize); // Check if the mouse position is within the boundaries of the grid if (i >= 0 && i < gridSize && j >= 0 && j < gridSize) { let cell = grid[i][j]; cell.density += 1; } } // Create a 2D array of cells function create2DArray(size) { let arr = []; for (let i = 0; i < size; i++) { arr.push([]); for (let j = 0; j < size; j++) { arr[i].push({ density: 0, velocity: createVector(0, 0), pressure: 0 }); } } return arr; } </script> </body> </html>When you click on the canvas, it will add a density value at the mouse position, causing the fluid simulation to be triggered. The density value is represented by grayscale colors in the visualization.