import processing.core.*; public class Fish { PApplet p; PVector position; PVector velocity; float pondWidth; float pondHeight; float pondX; float pondY; boolean alive; float size; //float angle; Fish (PApplet p, PVector position, float size) { pondWidth = 580; pondHeight = 380; pondX = 10; pondY = 10; this.p = p; this.position = position; this.size = size; velocity = new PVector(p.random(-1, 3), p.random(-1, 3)); alive = true; } void update() { position.add(velocity); } void detectBound() { if (position.x + size * 5 > pondWidth && alive) { velocity.x = - velocity.x; } if (position.x < pondX + size * 6 && alive) { velocity.x = - velocity.x; } if (position.y > pondHeight - size * 2.5 && alive) { velocity.y = - velocity.y; } if (position.y < pondY + size * 2.5 && alive) { velocity.y = - velocity.y; } } boolean detectCollision(Fish otherFish) { if (p.abs(position.x - otherFish.position.x) < (size * 5 + otherFish.size * 6)&& p.abs(position.y - otherFish.position.y) < (size * 2 + otherFish.size *2.5)) { return true; } else { return false; } } void kill() { velocity.x = 0; velocity.y = 0; alive = false; } boolean detectOffScreen() { if (position.x - size * 6 == pondX || position.x + size * 5 == pondWidth || position.y - size * 2.5 == pondY || position.y + size * 2.5 == pondHeight) { return true; } return false; } void bounce(Fish otherFish) { float angle = p.atan2(position.y - otherFish.position.y, position.x - otherFish.position.x); velocity.x = 1 * p.cos(angle); velocity.y = 1 * p.sin(angle); otherFish.velocity.x = 1 * p.cos(angle - p.PI); otherFish.velocity.y = 1 * p.sin(angle - p.PI); } }