UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

91 lines (90 loc) 2.71 kB
import { vec2 } from "gl-matrix"; import Circle from "../../shapes/circle"; import Texture from "../../texture/texture"; import Color from "../../utils/color"; import Physics from "../physics"; import kdTree from "./kdTree"; import Particle from "./particle"; export declare const MAX_FLUID_PARTICLES = 1000; export interface FluidOptions { restDensity: number; smoothingRadius: number; stiffness: number; stiffnessNear: number; particleRadius: number; maxParticles: number; collisionGroup: number; color?: Color; zIndex?: number; renderThreshold?: number; debug?: boolean; debugTex?: Texture; } /** * Simulates a fluid using SPH (Smooth Particle Hydrodynamics). * * @see [SPH Fluid Sim in Processing](https://hmbd.wordpress.com/2019/06/10/sph-fluid-simulation-in-processing/) * @see [Processing Code](https://github.com/DanielsSim/hmbd/blob/master/Processing/SPH/Fluid.pde) * @see [Particle-based Viscoelastic Fluid Sim](http://www.ligum.umontreal.ca/Clavet-2005-PVFS/pvfs.pdf) */ export default class Fluid { particles: Particle[]; restDensity: number; smoothingRadius: number; smoothingRadiusSqr: number; stiffness: number; stiffnessNear: number; particleRadius: number; maxParticles: number; collisionGroup: number; kdTree: kdTree; color: Color; renderThreshold: number; /** * The z layer of the fluid. */ zIndex: number; debug: boolean; debugTex: Texture; debugShapes: Map<Particle, Circle>; /** * The physics world the fluid sim was added to. */ physics: Physics; /** * Creates an {@link Fluid} with the given options. * * @param opts The options to setup the fluid with */ constructor(opts: FluidOptions); /** * Adds a particle to the fluid at the given position. * * @param pos The position to create the particle at in world space. * @returns Wether or not the particle was added */ addParticle(pos: vec2): boolean; /** * Steps the fluid simulation forward in time. * * @param delta The time since the last update */ update(delta: number): void; private translate; /** * Integrate any forces on the particle, including gravity, and integrate velocity. * * Also resets the particle's density from the last update. * * @param delta The time since the last update */ integrate(delta: number, gravity: vec2): void; /** * Queues the fluid to be renderer using the fluid renderer. */ render(): void; /** * Renders the particles of the fluid as individual circles. */ debugRender(): void; }