FERRETGame engine over Babylon.js
Ferret

@ferret/physics-media

water, mud, wind and air as media with drag, buoyancy and flow; the standard atmosphere.

19 exports · source in packages/physics-media/src

Classes

GasVolume class

class GasVolume

A bounded amount of gas obeying the ideal gas law p·V = n·R·T: an envelope, an air pocket in a ballast tank, a diving bell. Not a gas flow field: the whole volume has one pressure and one temperature.

moles: number
temperature: number
volume: number
gamma: numberratio of specific heats: 1.4 for a diatomic gas such as air
molarMass: numberkg/mol
constructor(o: { moles: number; temperature: number; volume: number; gamma?: number; molarMass?: number })
static ofAir(atm: Atmosphere, altitude: number, volume: number): GasVolumeA volume holding the air of the atmosphere at an altitude (an open envelope at the start).
get pressure()
get density()
get mass()
get cv()Heat capacity at constant volume, J/K.
setVolume(v: number, mode: 'isothermal' | 'adiabatic' = 'isothermal')Change the volume; isothermal keeps T (heat flows out), adiabatic keeps the heat in (T changes with V^(1−γ)).
heat(joules: number, at: 'volume' | 'pressure' = 'volume')Add heat at constant volume (the pressure rises) or at constant pressure (the volume grows). Returns the new volume.
addMoles(dn: number)Let moles out (a leak) or in.

packages/physics-media/src/atmosphere.ts:66

Functions

exponentialAtmosphere function

function exponentialAtmosphere(seaLevelDensity = 1.225, scaleHeight = 8500, temperature = 288.15): Atmosphere

Density falls as exp(−h/H) with the scale height H: the simple model for a game world.

packages/physics-media/src/atmosphere.ts:33

gasMassFlow function

function gasMassFlow(p0: number, T0: number, p1: number, area: number, o: { gamma?: number; R?: number; Cd?: number } = {}): number

Mass flow, kg/s, of a perfect gas from a space at p0, T0 through an opening of effective area area (m2) into a space at p1: subsonic from the isentropic nozzle law, choked when p1/p0 is below the critical ratio (2/(γ+1))^(γ/(γ−1)). R is the specific gas constant J/(kg·K). Zero when p1 >= p0. The one implementation behind combustion chambers, steam ports and the pneumatic network.

packages/physics-media/src/atmosphere.ts:55

gustAt function

function gustAt(t: number, g: GustDef): number

A deterministic gust, −1..1 in time: two sines with phases from the seed.

packages/physics-media/src/index.ts:74

media function

function media(): MediaCapability

packages/physics-media/src/index.ts:89

sphereCd function

function sphereCd(re: number): number

Drag coefficient of a sphere against the Reynolds number: Stokes' 24/Re at the bottom, a plateau of about 0.44 at the top.

packages/physics-media/src/index.ts:66

submergedFraction function

function submergedFraction(y: number, r: number, surface: number): number

The fraction of a sphere of radius r with its centre at y that lies below a horizontal plane at surface (a spherical cap).

packages/physics-media/src/index.ts:58

uniformAtmosphere function

function uniformAtmosphere(density = 1.2, temperature = 288.15): Atmosphere

The same air everywhere (a closed hall, a test).

packages/physics-media/src/atmosphere.ts:43

Interfaces

AirState interface

interface AirState

The air above the ground: density, pressure and temperature by altitude. A game gives its own for another planet or a game world.

density: number
pressure: number
temperature: number

packages/physics-media/src/atmosphere.ts:2

Atmosphere interface

interface Atmosphere
at(altitude: number): AirState

packages/physics-media/src/atmosphere.ts:3

GustDef interface

interface GustDef

media. Water, mud, wind, air: a medium has a density and a viscosity (and a flow). A body in it feels

  - drag, Stokes at low Reynolds numbers and quadratic at high ones (Schiller–Naumann for a sphere), against the flow;
  - buoyancy, ρ_medium · V_submerged · g, from the exact submerged part of a sphere (a spherical cap against the surface);
  - rotational drag.

Air is the default medium, with density and temperature from the atmosphere (the International Standard Atmosphere unless the game gives its own) and a wind that may gust. It can be left off. A body half in water and half in air feels the volume-weighted blend.

values (each at world / level / zone / entity scope):

  air          true: the air is a medium (false = vacuum)                                (true)
  atmosphere   an Atmosphere                                                             (the standard atmosphere)
  seaLevel     the y of altitude zero                                                    (0)
  wind         [x, y, z] m/s at the reference height; windExponent / windRefHeight give a power-law profile (0 = uniform)
  gust         { amplitude, period, seed? }: a deterministic gust along the wind
  media        MediumDef[]: { name, density, viscosity, flow?, surface?, zone? } — `surface` is the y of the top of the water,
               `zone` limits the medium to a zone of
amplitude: number
period: number
seed?: number

packages/physics-media/src/index.ts:26

MediaCapability interface

interface MediaCapability extends Capability
media: Map<string, MediumDef>
addMedium(m: MediumDef): void
removeMedium(name: string): void
at(host: PhysicsHost, p: Vec3, ctx?: ScopeContext): MediumSampleThe medium at a point.
sample(host: PhysicsHost, p: Vec3, r: number, ctx?: ScopeContext): { density: number; viscosity: number; flow: Vec3; fraction: number; medium: string }The blend a sphere of radius r at p sits in.

packages/physics-media/src/index.ts:79

MediumDef interface

interface MediumDef
name: string
density: number
viscosity: number
flow?: Vec3
surface?: number | (() => number)the y of the top of the medium; a function when it moves (a reservoir that fills and drains)
zone?: string
bounds?: { min: Vec3; max: Vec3 }the box the medium fills (a tank): outside it the medium is not there
gust?: GustDef

packages/physics-media/src/index.ts:27

Constants

EXAMPLE_MEDIA const

const EXAMPLE_MEDIA: { readonly water: { readonly name: "water"; readonly density: 998; readonly viscosity: 0.001; }; readonly seawater: { readonly name: "seawater"; readonly density: 1025; readonly viscosity: 0.0011; }; readonly mud: { readonly name: "mud"; readonly density: 1800; readonly viscosity: 0.5; }; readonly honey: { readonly name: "honey"; readonly density: 1420; readonly viscosity: 10; }; }

Water, mud and honey, as examples.

packages/physics-media/src/index.ts:50

MEDIA_DEFAULTS const

const MEDIA_DEFAULTS: { air: boolean; atmosphere: Atmosphere; seaLevel: number; wind: Vec3; windExponent: number; windRefHeight: number; gust: GustDef | undefined; media: MediumDef[]; cd: number | undefined; angularDrag: boolean; samples: number; }

packages/physics-media/src/index.ts:44

R_AIR const

const R_AIR: 287.052874

packages/physics-media/src/atmosphere.ts:5

R_GAS const

const R_GAS: 8.314462618

packages/physics-media/src/atmosphere.ts:6

standardAtmosphere const

const standardAtmosphere: Atmosphere

The International Standard Atmosphere up to 47 km (troposphere, tropopause, stratosphere layers), clamped below sea level to sea-level values.

packages/physics-media/src/atmosphere.ts:10

vacuum const

const vacuum: Atmosphere

No air: a vacuum (space, a vacuum chamber).

packages/physics-media/src/atmosphere.ts:48