/** Class: Particle by: Frederic Green created: 11/1/2006 A rudimentary class for representing a particle with its own position, velocity, and acceleration. Accessor and mutator methods are included for these three quantities. The "move()" method governs the behavior of the particle. The "render()" method governs its appearance. */ public class Particle { private Vector2D position, velocity, acceleration; private float size; //size of particle; in this case, the diameter of a circle /** Default constructor (probably don't want to use this): */ public Particle () { position = null; velocity = null; acceleration = null; size = 0; } /** Parameterized constructor expects values for position, velocity, acceleration, and particle size (in that order). */ public Particle (Vector2D position, Vector2D velocity, Vector2D acceleration, float size) { this.position = position; this.velocity = velocity; this.acceleration = acceleration; this.size = size; } /** Accessor method for getting position: */ public Vector2D getPosition () { return position; } /** Accessor method for getting x-component of position: */ public float getX() { return this.position.getX(); } /** Accessor method for getting y-component of position: */ public float getY() { return this.position.getY(); } /** Accessor method for getting velocity: */ public Vector2D getVelocity () { return velocity; } /** Accessor method for getting acceleration: */ public Vector2D getAcceleration() { return acceleration; } /** Reset velocity to 0: */ void resetVelocity () { velocity.reset(); } /** Mutator method for setting x-component of position: */ public void setX (float x) { this.position.setX(x); } /** Mutator method for setting y-component of position: */ public void setY (float y) { this.position.setY(y); } /** Mutator method for setting velocity: */ public void setVelocity (Vector2D velocity) { this.velocity = velocity; } /** Mutator method for setting acceleration: */ public void setAcceleration (Vector2D acceleration) { this.acceleration = acceleration; } /** Method for rendering the particle: */ public void render () { smooth(); fill(255); noStroke(); ellipse(position.getX(), position.getY(), size, size); } /** Method for moving the particle (assuming it's moving by itself): */ public void move() { position = position.plus(velocity); velocity = velocity.plus(acceleration); velocity.detectCollision(position, size, size, width, height); position.constrain(size/2.0, width-size/2.0, size/2.0, height-size/2.0); velocity.constrain(-5, 5, -5, 5); } }