reconfiguring the objects array example
in
Programming Questions
•
10 months ago
Hey there!
I 'm trying to reconfigure the example of Array Objects found here:
with another class and even though it is compiling, it doesn't preview anything. Probably a silly mistake from a noob... This is the code btw:
int unit = 40;
int count;
Capsules[] agents;
float d;
void setup() {
size(640, 360);
noStroke();
int wideCount = width / unit;
int highCount = height / unit;
count = wideCount * highCount;
agents = new Capsules[count];
d=10;
int index = 0;
for (int i = 0; i < 20; i++) {
agents[index++] = new Capsules(random(width),random(height),color(255,0,0),i);
}
}
void draw() {
background(0);
for (int i = 0; i < count; i++) {
agents[i].Default();
}
}
//-----------------------class----------------------------------
class Capsules { //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
Capsules(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 = 200; // Distance for our "wander circle"
float change = 0.05;
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 < 200) {
float m = map(d,0,800,0,4*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<Capsules> agent) {
float desiredseparation = 4*r; //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 (Capsules 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<Capsules> agent) {
float desiredseparation = r; // r = separation ONLY for clustering
PVector sum = new PVector();
int count = 0;
// For every agent in the system, check if it's too close
for (Capsules 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<Capsules> agent) {
float neighbordist = 50;
PVector sum = new PVector(0,0);
int count = 0;
for (Capsules 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);
}
}
}
1