I mean how am I supposed to alter the constructor of the superclass in order to
match it with the subclass' one(?) or vice verca...
Here is the entire class although I don't think it will make it much easier :(
class Capsule { //the basic class that defines the chararacteristics of the agents
PVector location;
PVector velocity;
PVector acceleration;
float r; //the radius of the circles-spheres/ agents
float wandertheta;
float maxforce; // Maximum steering force
float maxspeed; // Maximum speed
color fillColor; //differentiation of agent types are based on complementary coloring
int name; // gives an ID - number to each different agent
Capsule(float x, float y, color c, int nam) { //the class constructor
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
location = new PVector(x,y);
r = 6;
wandertheta = 0;
maxspeed = 0.5;
maxforce = 0.05;
fillColor = c;
name = nam;
}
void Default() {
boundaries();
update();
display();
wander();
}
void update() { // Method to update location
velocity.add(acceleration); // Update velocity
velocity.limit(maxspeed); // Limit speed
location.add(velocity); // Reset accelertion to 0 each cycle
acceleration.mult(0);
}
void wander() { // Method that makes the agent float into 2D space
float wanderR = 25; // Radius for our "wander circle"
float wanderD = 30; // Distance for our "wander circle"
float change = 0.5;
wandertheta += random(-change,change); // Randomly change wander theta
// calculating the new location to steer towards on the wander circle
PVector circleloc = velocity.get(); // Start with velocity
circleloc.normalize(); // Normalize to get heading
circleloc.mult(wanderD); // Multiply by distance
circleloc.add(location); // Make it relative to boid's location
float h = velocity.heading2D(); // We need to know the heading to offset wandertheta
PVector circleOffSet = new PVector(wanderR*cos(wandertheta+h),wanderR*sin(wandertheta+h));
PVector target = PVector.add(circleloc,circleOffSet);
seek(target);
}
void applyForce(PVector force) { // Newton's second law; We could add mass here if we want A = F / M
acceleration.add(force);
}
// A method that calculates and applies a steering force towards a target
// STEER = DESIRED MINUS VELOCITY
void seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
desired.normalize(); // Normalize desired and scale to maximum speed
desired.mult(maxspeed);
PVector steer = PVector.sub(desired,velocity); // Steering = Desired minus Velocity
steer.limit(maxforce); // Limit to maximum steering force
applyForce(steer);
}
// Method that lowers the speed of the agent by the time he reaches the target - see: "buffer zone"
void arrive(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
float d = desired.mag();
desired.normalize(); // Normalize desired and scale with arbitrary damping within 100 pixels
if (d < 6) {
float m = map(d,0,15,0,maxspeed);
desired.mult(m);
} else {
desired.mult(maxspeed);
}
PVector steer = PVector.sub(desired,velocity); // Steering = Desired minus Velocity
steer.limit(5*maxforce); // Limit to maximum steering force
applyForce(steer);
}
void display() { // Draws an ellipse that represents an agent-capsule
fill(fillColor);
stroke(0);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, r, r);
popMatrix();
}
//Method that checkes the maximum floating range of the agents
void boundaries() {
PVector desired = null;
if (location.x < d) {
desired = new PVector(maxspeed, velocity.y);
}
else if (location.x > width -d) {
desired = new PVector(-maxspeed, velocity.y);
}
if (location.y < d) {
desired = new PVector(velocity.x, maxspeed);
}
else if (location.y > height-d) {
desired = new PVector(velocity.x, -maxspeed);
}
if (desired != null) {
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired, velocity);
steer.limit(2*maxforce);
applyForce(steer);
}
}
// Method that separates the agents from one-another -- see: "buffer zone"
void separate (ArrayList<Capsule> agent) {
float desiredseparation = r*4; //4*r = separation ONLY for wandering
PVector sum = new PVector();
int count = 0;
// For every agent in the system, check if it's too close
for (Capsule v : agent) {
float d = PVector.dist(location, v.location);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(location, v.location);
diff.normalize();
diff.div(d); // Weight by distance
sum.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
sum.div(count);
sum.normalize(); // Our desired vector is the average scaled to maximum speed
sum.mult(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
// Method that separates the agents from one-another -- see: "buffer zone"
void separateSpecial (ArrayList<Capsule> agent) {
float desiredseparation = r; //
PVector sum = new PVector();
int count = 0;
// For every agent in the system, check if it's too close
for (Capsule v : agent) {
float d = PVector.dist(location, v.location);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(location, v.location);
diff.normalize();
diff.div(d); // Weight by distance
sum.add(diff);
count++; // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
sum.div(count);
sum.normalize(); // Our desired vector is the average scaled to maximum speed
sum.mult(maxspeed);
// Implement Reynolds: Steering = Desired - Velocity
PVector steer = PVector.sub(sum, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
// Alignment
// For every nearby agent in the system, calculate the average velocity
PVector align (ArrayList<Capsule> agent) {
float neighbordist = 50;
PVector sum = new PVector(0,0);
int count = 0;
for (Capsule v : agent) {
float d = PVector.dist(location,v.location);
if ((d > 0) && (d < neighbordist)) {
sum.add(v.velocity);
count++;
}
}
if (count > 0) {
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
return steer;
} else {
return new PVector(0,0);
}
}
}
class MW1 extends Capsule {
MW1() {
super();
}
void display() {
fill(255,0,0);
stroke(0);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, r, r);
popMatrix();
}
}
class FW1 extends Capsule {
FW1() {
super();
}
void display() {
fill(0,255,0);
stroke(0);
pushMatrix();
translate(location.x, location.y);
ellipse(0, 0, r, r);
popMatrix();
}
}