Hello all. So far all the gracious answers on this forum have kept me from posting, but this one is stumping me too hard.
I have a class “Root”, which is an ellipse that gets drawn following a random and varying direction.
I’ve been trying to integrate collision detection by filling the ellipses previously drawn positions into an ArrayList and then, within the class itself, checking the current location.x and location.y position of the ellipse against the previous location.x and location.y positions stored in the ArrayList (see “PVector storeDrawn()” and “void collsionDetect()” ).
At the moment there are two major problems:
-
The for loop is checking the current position against the position from the frame / iteration immediately before hand - so the if statement of “dist < 40?” is always going to be true.
-
The ellipse also crosses over previously drawn positions without the if statement triggering (picture attached). So even though the for array is collecting the previous positions, and is having the current position checked against them, it’s still not detecting a collision between the two - and I dont know why.
I suspect I’m going about the ArrayList / for loop combination incorrectly. But can’t figure this one out any further without help!
Root[] root = new Root[1];
void setup() {
size (800, 800, P2D);
for (int i = 0; i < root.length; i++) {
root[i] = new Root(0, 200);
}
}
void draw() {
for (int i = 0; i < root.length; i++) {
root[i].update();
root[i].display();
root[i].noEdges();
root[i].collisionDetect();
}
}
class Root {
ArrayList<PVector> drawn = new ArrayList<PVector>();
PVector bottomTarget;
PVector location;
PVector velocity;
PVector acceleration;
PVector direction;
float t;
float radius = 5;
float tx;
float ty;
int topSpeed = 1;
int counter = 0;
Boolean collided = false;
Root(float dirX, float dirY) {
location = new PVector(width / 2, 0);
velocity = new PVector(0, 0);
direction = new PVector(dirX, dirY);
}
void update () {
PVector bottomTarget = new PVector(width / 2, height);
PVector acceleration = PVector.sub(bottomTarget, location);
PVector randTurn = new PVector(noise(tx), noise(ty));
acceleration.add(direction);
acceleration.setMag(1);
acceleration.normalize();
acceleration.mult(1);
velocity.add(acceleration);
velocity.limit(topSpeed);
location.add(randTurn);
location.add(velocity);
tx += 0.02;
ty += 0.04;
}
void display() {
fill(0);
ellipse(location.x, location.y, radius, radius);
storeDrawn();
}
// store the current xy pos as a pvector and move onto next cell in arraylist
PVector storeDrawn() {
PVector v = new PVector(0, 0);
drawn.add( new PVector(location.x, location.y));
for (int i = 0; i < drawn.size(); i++) {
v = drawn.get(i);
}
return v;
}
// check if current xy pos will collide with any previous xy pos
void collisionDetect() {
if ( millis() / 1000 > 5) {
for (int i = 0; i < drawn.size(); i++) {
if (dist(location.x, location.y, drawn.get(i).x, drawn.get(i).y) < 40) {
direction.add(20, 0);
alterPath();
collided = true;
}
}
}
}
// random path variation
void alterPath() {
velocity.add( -1, 0);
}
// teleport to other edge on canvas edge collision
void noEdges() {
if (location.x > width) {
location.x = 0;
} else if (location.x < 0) {
location.x = width;
//radius += 5;
}
if (location.y > height) {
location.y = 0;
} else if (location.y < 0) {
location.y = height;
}
}
void growInsidePot() {
if ((location.x > width) || (location.x < 0)) {
velocity.x = velocity.x * -1;
//println("in function x:", velocity.x);
}
if ((location.y > height) || (location.y < 0)) {
velocity.y = velocity.y * -1;
//println("in function x:", velocity.y);
}
}
}