/** * 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 private float pWidth; private float pHeight; private float theta = 0; /** * 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 pWidth, float pHeight) { this.position = position; this.velocity = velocity; this.acceleration = acceleration; this.pWidth = pWidth; this.pHeight = pHeight; } /** * 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; } /** * Sets acceleration to 0. */ public void resetAccel () { this.acceleration.reset(); } /** * Mutator method for setting acceleration: */ public void setAcceleration (Vector2D acceleration) { this.acceleration = acceleration; } /** * Method for rendering the particle: */ public void render () { smooth(); fill(200, 200, 0); stroke(200, 40); ellipse(position.getX(), position.getY(), pWidth, pHeight); } /** * 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, pWidth, pHeight, width, height); position.constrain(pWidth/2.0, width-pWidth/2.0, pHeight/2.0, height-pHeight/2.0); velocity.constrain(-5, 5, -5, 5); } }