/** * Flade - Flash Dynamics Engine * Release 0.4 alpha * ParticleSystem class * Copyright 2004, 2005 Alec Cove * * This file is part of Flade. The Flash Dynamics Engine. * * Flade is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Flade is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Flade; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * Flash is a registered trademark of Macromedia */ ParticleSystem = function() { this.particles = new Array(); this.surfaces = new Array(); this.constraints = new Array(); this.wheels = new Array(); // default values this.gravity = new Vector(0,1); this.coeffRest = 1 + 0.5; this.coeffFric = 0.01; // surface friction this.coeffDamp = 0.99; // global damping } ParticleSystem.prototype.addParticle = function(px, py) { var p = new Particle(px, py); this.particles.push(p); return p; } ParticleSystem.prototype.addWheel = function(px, py, r) { var w = new Wheel(px, py, r); this.wheels.push(w); return w; } ParticleSystem.prototype.addSurface = function(s) { this.surfaces.push(s); } ParticleSystem.prototype.addConstraint = function(p1, p2) { var c = new Constraint(p1, p2); this.constraints.push(c); return c; } ParticleSystem.prototype.addAngularConstraint = function(p1, p2, p3) { var ac = new AngularConstraint(p1, p2, p3); this.constraints.push(ac); return ac; } ParticleSystem.prototype.setKfr = function(kfr) { this.coeffRest = 1 + kfr; } ParticleSystem.prototype.setFriction = function(f) { this.coeffFric = f; } ParticleSystem.prototype.setDamping = function(d) { this.coeffDamp = d; } ParticleSystem.prototype.setGravity = function(gx, gy) { this.gravity.x = gx; this.gravity.y = gy; } ParticleSystem.prototype.addRectangle = function(center, rWidth, rHeight) { return new Rectangle(this, center, rWidth, rHeight); } ParticleSystem.prototype.verlet = function() { for (var i = 0; i < this.particles.length; i++) { this.particles[i].verlet(this); } for (var k = 0; k < this.wheels.length; k++) { this.wheels[k].verlet(this); } } ParticleSystem.prototype.satisfyConstraints = function() { for (var n = 0; n < this.constraints.length; n++) { this.constraints[n].resolve(); } } ParticleSystem.prototype.checkCollisions = function() { for (var j = 0; j < this.surfaces.length; j++) { var s = this.surfaces[j]; for (var i = 0; i < this.particles.length; i++) { var p = this.particles[i]; p.checkCollision(s, this); } for (var k = 0; k < this.wheels.length; k++) { var w = this.wheels[k]; w.checkCollision(s, this); } } } ParticleSystem.prototype.paintSurfaces = function() { for (var j = 0; j < psystem.surfaces.length; j++) { this.surfaces[j].paint(); } } ParticleSystem.prototype.paintParticles = function() { for (var j = 0; j < psystem.particles.length; j++) { this.particles[j].paint(); } } ParticleSystem.prototype.paintConstraints = function() { for (var j = 0; j < psystem.constraints.length; j++) { this.constraints[j].paint(); } } ParticleSystem.prototype.paintWheels = function() { for (var j = 0; j < psystem.wheels.length; j++) { this.wheels[j].paint(); } } ParticleSystem.prototype.timeStep = function() { this.verlet(); this.satisfyConstraints(); this.checkCollisions(); }