Skip to content

particlesystem

particlesystem

A particle system: an emitter that spawns short-lived particles, moves them, fades them between a start and end size and color, and draws them in one batch. Configure it with the setters, call update each frame and draw to show it. With no texture the particles are colored squares; with a texture they are textured quads.

ParticleSystem

type ParticleSystem = ref object

An emitter that spawns short-lived particles and animates them. Without

a texture the particles are colored squares; with one they are textured quads.

Fields

  • texture Texture

newParticleSystem

proc newParticleSystem(texture: Texture = nil; maxParticles = 2000): ParticleSystem

A particle system, optionally drawing a texture per particle. Configure it

with the setters, update it every frame and draw it to show it.

Parameters

  • texture (Texture)
  • maxParticles (auto)

Returns

ParticleSystem

setPosition

proc setPosition(ps: ParticleSystem; x, y: float)

Move the emitter; new particles spawn here.

Parameters

  • ps (ParticleSystem)
  • x (float)
  • y (float)

setEmissionRate

proc setEmissionRate(ps: ParticleSystem; rate: float)

How many particles spawn per second.

Parameters

  • ps (ParticleSystem)
  • rate (float)

setParticleLifetime

proc setParticleLifetime(ps: ParticleSystem; min, max: float)

How long each particle lives, in seconds, picked from this range.

Parameters

  • ps (ParticleSystem)
  • min (float)
  • max (float)

setSpeed

proc setSpeed(ps: ParticleSystem; min, max: float)

The initial speed of each particle, picked from this range.

Parameters

  • ps (ParticleSystem)
  • min (float)
  • max (float)

setDirection

proc setDirection(ps: ParticleSystem; radians: float)

The direction particles fly out in.

Parameters

  • ps (ParticleSystem)
  • radians (float)

setSpread

proc setSpread(ps: ParticleSystem; radians: float)

The angle of the cone around the direction that particles scatter into.

Parameters

  • ps (ParticleSystem)
  • radians (float)

setLinearAcceleration

proc setLinearAcceleration(ps: ParticleSystem; ax, ay: float)

A constant acceleration on every particle, like gravity or wind.

Parameters

  • ps (ParticleSystem)
  • ax (float)
  • ay (float)

setSizes

proc setSizes(ps: ParticleSystem; startSize, endSize: float)

Each particle's size fades from start to end over its life.

Parameters

  • ps (ParticleSystem)
  • startSize (float)
  • endSize (float)

setColors

proc setColors(ps: ParticleSystem; startColor, endColor: Color)

Each particle's color fades from start to end over its life. Fading the

end alpha to 0 makes particles dissolve.

Parameters

  • ps (ParticleSystem)
  • startColor (Color)
  • endColor (Color)

setSpin

proc setSpin(ps: ParticleSystem; min, max: float)

How fast each particle rotates, in radians per second, from this range.

Parameters

  • ps (ParticleSystem)
  • min (float)
  • max (float)

start

proc start(ps: ParticleSystem)

Resume emitting after stop.

Parameters

  • ps (ParticleSystem)

stop

proc stop(ps: ParticleSystem)

Stop emitting. Particles already alive keep moving until they expire.

Parameters

  • ps (ParticleSystem)

isActive

proc isActive(ps: ParticleSystem): bool

Whether the emitter is emitting.

Parameters

  • ps (ParticleSystem)

Returns

bool

count

proc count(ps: ParticleSystem): int

How many particles are alive right now.

Parameters

  • ps (ParticleSystem)

Returns

int

emit

proc emit(ps: ParticleSystem; count: int)

Spawn count particles right now, regardless of the emission rate.

Parameters

  • ps (ParticleSystem)
  • count (int)

update

proc update(ps: ParticleSystem; dt: float)

Advance the system by dt seconds: spawn, move, age and retire particles.

Call every frame from the update callback.

Parameters

  • ps (ParticleSystem)
  • dt (float)

draw

proc draw(ps: ParticleSystem; nim2d: Nim2d; x = 0.0; y = 0.0)

Draw every live particle in one batch, optionally offset by (x, y).

Parameters

  • ps (ParticleSystem)
  • nim2d (Nim2d)
  • x (auto)
  • y (auto)
Generated with mkdocstrings-nim