Comparing intersection points of array object list
in
Core Library Questions
•
1 month ago
Greetings! I had created an array of a class object in my code. I am having a bit of trouble comparing if the objects intersect each other and wondering if I can get a push in the right direction;
main loop (important elements):
- cellOne[] mover = new cellOne[30];
- void setup()
- {
- size(displayWidth, displayHeight, P3D);
- for (int i = 0; i < mover.length; i ++ ) { // Initialize each Car using a for loop.
- mover[i] = new cellOne();
- }
- }
- void draw(){
- for (int i = 0; i < mover.length; i++) {
- mover[i].update();
- mover[i].checkEdges();
- mover[i].display();
- }
- }
and now my class:
- class cellOne{
- color c = color(100,50,77);
- float x,y;
- PVector location;
- PVector velocity;
- PVector acceleration;
- float topspeed;
- float r = 48;
- cellOne(){
- location = new PVector(random(displayWidth), random(displayHeight));
- velocity = new PVector(0, 0);
- topspeed = .8;
- x = random(2);
- y = random(2);
- }
- void update() {
- acceleration = PVector.random2D();
- acceleration.mult(random(2));
- velocity.add(acceleration);
- velocity.limit(topspeed);
- location.add(velocity);
- }
- void display() {
- stroke(0);
- strokeWeight(2);
- fill(127);
- ellipse(location.x, location.y, r,r);
- }
- void filler() {
- stroke(0);
- strokeWeight(2);
- fill(2);
- ellipse(location.x, location.y, r,r);
- }
- void checkEdges() {
- if (location.x > displayWidth) {
- location.x = 0;
- }
- else if (location.x < 0) {
- location.x = displayWidth;
- }
- if (location.y > displayHeight) {
- location.y = 0;
- }
- else if (location.y < 0) {
- location.y = displayHeight;
- }
- }
- boolean intersect() {
- // Objects can be passed into functions as arguments too!
- float distance = dist(x,y,location.x,location.y); // Calculate distance
- //location.add(r);
- // Compare distance to sum of radii
- if (distance <r) {
- return true;
- } else {
- return false;
- }
- }
- }
1