We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm trying to implement a simple follow behaviour where each object in the ArrayList follows the next object in the ArrayList. At the moment it seems like all the objects are following each other though? I'm assuming it's a problem with the loop but can't figure it out..
ArrayList<Mover> movers;
void setup() {
size(640, 360);
movers = new ArrayList<Mover>();
for (int i = 0; i < 4; i++) {
movers.add(new Mover());
}
}
void draw() {
background(255);
for (int i = 0; i < movers.size(); i++) {
Mover m = movers.get(i);
float alpha = i*85;
m.follow(movers);
m.update();
m.display(alpha);
m.boundary();
}
}
class Mover {
PVector location;
PVector velocity;
PVector acceleration;
float mass;
float radius;
float maxspeed;
float maxforce;
float damping;
Mover() {
location = new PVector(random(width), random(height));
velocity = new PVector();
acceleration = new PVector();
radius = 20;
maxspeed = 2;
maxforce = 0.3;
damping = 0.98;
}
void follow(ArrayList<Mover> movers) {
for (int i = 0; i < movers.size()-1; i++) {
Mover other = movers.get(i+1);
PVector neighbour = PVector.sub(other.location, location);
neighbour.setMag(maxspeed);
PVector steer = PVector.sub(neighbour, velocity);
steer.limit(maxforce);
applyForce(steer);
}
}
void applyForce(PVector force) {
force.copy();
acceleration.add(force);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
velocity.mult(damping);
location.add(velocity);
acceleration.mult(0);
}
void display(float alpha) {
fill(alpha);
stroke(0);
strokeWeight(1);
ellipse(location.x, location.y, radius, radius);
}
void boundary() {
if (location.x < 0) {
location.x = width;
}
if (location.x > width) {
location.x = 0;
}
if (location.y < 0) {
location.y = height;
}
if (location.y > height) {
location.y = 0;
}
}
}
Answers
Add some debug statements. Don't just try and guess what it's doing, get it to tell you.
Add some comments whilst you're there.
your method
follow
in the class is wrongI would pass each ball an id named
ID
.instead of having a
for
loop you just want that each ball follows the other ball with the IDid-1
.That means that ball 0 doesn't have a ball to follow, so 0 should be treated special. He is the leader and needs an own movement pattern.
I just wrote it here, looks nice. Tell me if you want to see it
http://Bl.ocks.org/GoSubRoutine/fa085945d45152786698f44a9523ccac
If I could see it that would be great. I'm just starting out so i'm not sure how I would go about passing each ball an ID that could be used in the function.
There is a tutorial about classes called objects, see constructor there in the tutorial
https://www.processing.org/tutorials/objects/