a simple flock configuration and a few problems
in
Programming Questions
•
11 months ago
Hey guys!
I 'm totally new to the entire processing concept so sorry any kind of help here is much appreciated!
I have a simple code here with an ArrayList of 5 circles that sould (normally) attract to each other and come together.
Although there are some problems popping up. Here is the entire configuration:
//main-----------------------------------------------------------------------------------------------------------------------------
capsule[] agents = new capsule[5];
void setup() {
size(300,300);
smooth();
for (int i = 0; i < agents.length; i++) { //create capsule objects
agents[i] = new capsule();
}
}
void draw() {
background(200);
for (int i = 0; i < agents.length; i++) {
agents[i].update(); //Call functions on Capsule object
agents[i].applyForce();
agents[i].seek();
agents[i].arrive();
agents[i].checkEdges();
agents[i].display();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
//class---------------------------------------------------------------------------------------------------------------------------
class capsule {
PVector location;
PVector velocity;
PVector acceleration;
float maxforce;
float maxspeed;
capsule(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(0,0);
location = new PVector(random(1,0),random(0,0));
maxspeed = 4;
maxforce = 0.1;
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
location.add(velocity);
acceleration.mult(0);
}
void applyForce(PVector force) { //Newton’s second law; we could divide by mass if we wanted.
acceleration.add(force);
}
void seek(PVector target) { //seek steering force algorithm
PVector desired = PVector.sub(target,location);
desired.normalize();
desired.mult(maxspeed);
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce);
applyForce(steer);
}
void arrive(PVector target) {
PVector desired = PVector.sub(target,location);
float d = desired.mag(); //The distance is the magnitude of the vector pointing from location to target.
desired.normalize();
if (d < 100) { //If we are closer than 100 pixels...
float m = map(d,0,100,0,maxspeed); //...set the magnitude according to how close we are
desired.mult(m);
} else {
desired.mult(maxspeed); //Otherwise, proceed at maximum speed.
}
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce);
applyForce(steer);
}
void display() {
stroke(0);
fill(255);
ellipse(location.x,location.y,20,20);
pushMatrix();
translate(location.x,location.y);
popMatrix();
}
void checkEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------
the problem is that:
the constructor stage_01_vectors_array_steering.capsule() is undefined...
any sollutions?
:)
1