// A particle moved by Verlet Integration - that means velocity is inferred from it's previous and current position // It requires some kind of Physics system to notify it of the global damping and gravity - hence the registration in construction // Generally particles should be spawned with Physics.addParticle class Particle extends Point{ float px, py, tempX, tempY, ix, iy; Physics p; // Constructor Particle(float x, float y, Physics p){ super(x, y); px = tempX = ix = x; py = tempY = iy = y; this.p = p; } // Movement is executed by this method void verlet(){ tempX = x; tempY = y; x += p.damping * (x - px) + p.gravityX; y += p.damping * (y - py) + p.gravityY; px = tempX; py = tempY; } // Fix the particle to a given point and kill it's velocity void setPosition(float x, float y){ this.x = px = tempX = ix = x; this.y = py = tempY = iy = y; } // Fix the particle to it's initialisation point void pin(){ x = px = tempX = ix; y = py = tempY = iy; } // Get a Line describing the particle's movement Line getLine(){ return new Line(new Point(px, py), new Point(x, y)); } // Get the distance moved in the last frame float speed(){ return sqrt((x-px)*(x-px)+(y-py)*(y-py)); } // Add speed to the particle by displacing it's last location void addVelocity(float x, float y){ px -= x; py -= y; } // Provide a copy of the particle with matching current behaviour and add it to the physics system Particle copy(){ Particle temp = p.addParticle(x, y); temp.px = px; temp.py = py; temp.ix = ix; temp.iy = iy; return temp; } }