UNPKG

fieldkit

Version:

Basic building blocks for computational design projects. Written in CoffeeScript for browser and server environments.

193 lines (140 loc) 4.45 kB
// Generated by CoffeeScript 1.6.3 (function() { var Particle, Particle2, Particle3, State, Vec2, Vec3, vector, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; vector = require('../math/vector'); Vec2 = vector.Vec2; Vec3 = vector.Vec3; 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 */ Particle = (function() { Particle.prototype.id = 0; Particle.prototype.state = State.ALIVE; Particle.prototype.age = 0; Particle.prototype.lifetime = -1; Particle.prototype.position = null; Particle.prototype.drag = 0.03; Particle.prototype.prev = null; Particle.prototype.force = null; Particle.prototype.velocity = null; function Particle(id) { this.id = id; this.size = 1; this.isLocked = false; } Particle.prototype.clearVelocity = function() { return this.prev.set(this.position); }; Particle.prototype.scaleVelocity = function(amount) { return this.prev.lerp(this.position, 1.0 - amount); }; Particle.prototype.setPosition = function(v) { this.position.set(v); return this.prev.set(v); }; Particle.prototype.lock = function() { return this.state = State.LOCKED; }; Particle.prototype.unlock = function() { return this.state = State.ALIVE; }; Particle.prototype.die = function() { return this.state = State.DEAD; }; Particle.prototype.idle = function() { return this.state = State.IDLE; }; Particle.prototype.toString = function() { return "Particle(" + this.position + ")"; }; return Particle; })(); /* 3D VerLer Particle */ Particle3 = (function(_super) { var tmp; __extends(Particle3, _super); tmp = new Vec3(); function Particle3(id) { this.id = id; this.position = new Vec3(); this.prev = new Vec3(); this.force = new Vec3(); this.velocity = new Vec3(); } Particle3.prototype.update = function() { this.age++; if (this.state > State.ALIVE) { return; } if (this.lifetime > 0 && this.age === this.lifetime) { this.state = State.DEAD; } tmp.set(this.position); this.position.x += (this.position.x - this.prev.x) + this.force.x; this.position.y += (this.position.y - this.prev.y) + this.force.y; this.position.z += (this.position.z - this.prev.z) + this.force.z; this.prev.set(tmp); this.prev.lerp(this.position, this.drag); return this.force.zero(); }; Particle3.prototype.setPosition3 = function(x, y, z) { this.position.set3(x, y, z); return this.prev.set3(x, y, z); }; return Particle3; })(Particle); /* 2D VerLer Particle */ Particle2 = (function(_super) { var tmp; __extends(Particle2, _super); tmp = new Vec2(); function Particle2(id) { this.id = id; this.position = new Vec2(); this.prev = new Vec2(); this.force = new Vec2(); this.velocity = new Vec2(); } Particle2.prototype.update = function() { this.age++; if (this.state > State.ALIVE) { return; } if (this.lifetime > 0 && this.age === this.lifetime) { this.state = State.DEAD; } tmp.set(this.position); this.position.x += (this.position.x - this.prev.x) + this.force.x; this.position.y += (this.position.y - this.prev.y) + this.force.y; this.prev.set(tmp); this.prev.lerp(this.position, this.drag); return this.force.zero(); }; Particle2.prototype.setPosition2 = function(x, y) { this.position.set2(x, y); return this.prev.set2(x, y); }; return Particle2; })(Particle); module.exports = { Particle: Particle, Particle2: Particle2, Particle3: Particle3, State: State }; }).call(this);