fieldkit
Version:
Basic building blocks for computational design projects. Written in CoffeeScript for browser and server environments.
132 lines (94 loc) • 2.44 kB
text/coffeescript
vector = require '../math/vector'
Vec2 = vector.Vec2
Vec3 = vector.Vec3
# States of the particle
State =
ALIVE: 0
LOCKED: 1
IDLE: 2
DEAD: 3
###
VerLer Particle Baseclass
FIELD flavoured particle integrator.
Supports Verlet-style integration for 'strict' relationships e.g. Springs + Constraints
and also Euler-style continous force integration for smooth/ flowing behaviour e.g. Flocking
###
class Particle
id: 0
state: State.ALIVE
age: 0
lifetime: -1
position: null
drag: 0.03
# Verlet style previous position
prev: null
# Euler force
force: null
velocity: null
constructor: () ->
= 1
= false
clearVelocity: -> .set
scaleVelocity: (amount) -> .lerp , 1.0 - amount
setPosition: (v) ->
.set v
.set v
lock: -> = State.LOCKED
unlock: -> = State.ALIVE
die: -> = State.DEAD
idle: -> = State.IDLE
toString: -> "Particle(#{@position})"
###
3D VerLer Particle
###
class Particle3 extends Particle
tmp = new Vec3()
constructor: () ->
= new Vec3()
= new Vec3()
= new Vec3()
= new Vec3()
update: ->
++
return if > State.ALIVE
= State.DEAD if > 0 and ==
# integrate velocity
tmp.set
.x += ((.x - .x) + .x)
.y += ((.y - .y) + .y)
.z += ((.z - .z) + .z)
.set tmp
.lerp ,
.zero()
setPosition3: (x, y, z) ->
.set3 x, y, z
.set3 x, y, z
###
2D VerLer Particle
###
class Particle2 extends Particle
tmp = new Vec2()
constructor: () ->
= new Vec2()
= new Vec2()
= new Vec2()
= new Vec2()
update: ->
++
return if > State.ALIVE
= State.DEAD if > 0 and ==
# integrate velocity
tmp.set
.x += ((.x - .x) + .x)
.y += ((.y - .y) + .y)
.set tmp
.lerp ,
.zero()
setPosition2: (x, y) ->
.set2 x, y
.set2 x, y
module.exports =
Particle: Particle
Particle2: Particle2
Particle3: Particle3
State: State