Collision Detection help
in
Programming Questions
•
1 year ago
Hello again! Hopefully this will be the last time I'll need help with my project. I am having trouble with my collision detection. I spawned 6 enemy circles (forgive the sloppiness, I didn't want to deal with arrays) and they all have the same code for collision detection. Unfortunately only 1 or 2 of them is working and I can't find out why it won't detect. So any help would be awesome. Thanks!
- float x;
- float y;
- float myX= 50;
- float myY=35;
- float easing =.1;
- float r=random(100);
- float speed = 15;
- float rX;
- float rY;
- float r2X;
- float r2Y;
- float r3X;
- float r3Y;
- float r4X;
- float r4Y;
- float r5X;
- float r5Y;
- float r6X;
- float r6Y;
- void setup () {
- frameRate(60);
- noCursor();
- size (600, 600);
- smooth();
- ellipseMode(CENTER);
- }
- void draw() {
- rX +=random(-25, 25);
- rY += random(-25, 25);
- rX= constrain(rX, 0, width);
- rY = constrain(rY, 0, height);
- background(51);
- myX=mouseX;
- float dx=myX -x;
- if (abs(dx) > 1) {
- x+=dx * easing;
- }
- myY=mouseY;
- float dy=myY -y;
- if (abs(dy) > 1) {
- y += dy * easing;
- }
- fill(65, 255, 41);
- ellipse(x, y, 33, 33);
- float badThingX= rX;
- float badThingY= rY;
- fill(255, 0, 0);
- ellipse(rX, rY, 33, 33);
- ellipse(r2X, r2Y, 33, 33);
- ellipse(r3X, r3Y, 33, 33);
- ellipse(r4X, r4Y, 33, 33);
- ellipse(r5X, r5Y, 33, 33);
- ellipse (r6X, r6Y, 33, 33);
- float distanceToEachOther= sqrt(sq(myX-rX)+sq(myY-rY));
- if (distanceToEachOther <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- float distanceToEachOther2= sqrt(sq(myX-r2X)+sq(myY-r2Y));
- if (distanceToEachOther2 <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- float distanceToEachOther3= sqrt(sq(myX-r3X)+sq(myY-r3Y));
- if (distanceToEachOther3 <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- float distanceToEachOther4= sqrt(sq(myX-r4X)+sq(myY-r4Y));
- if (distanceToEachOther4 <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- float distanceToEachOther5= sqrt(sq(myX-r5X)+sq(myY-r5Y));
- if (distanceToEachOther5 <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- float distanceToEachOther6= sqrt(sq(myX-r6X)+sq(myY-r6Y));
- if (distanceToEachOther6 <=33) {
- println("FAIL");
- }
- else {
- println("WIN");
- }
- r2X +=random(-25, 25);
- r2Y += random(-25, 25);
- r2X= constrain(r2X, 0, width);
- r2Y = constrain(r2Y, 0, height);
- r3X +=random(-25, 25);
- r3Y += random(-25, 25);
- r3X= constrain(r3X, 0, width);
- r3Y = constrain(r3Y, 0, height);
- r4X +=random(-25, 25);
- r4Y += random(-25, 25);
- r4X= constrain(r4X, 0, width);
- r4Y = constrain(r4Y, 0, height);
- r5X +=random(-25, 25);
- r5Y += random(-25, 25);
- r5X= constrain(r5X, 0, width);
- r5Y = constrain(r5Y, 0, height);
- r6X +=random(-25, 25);
- r6Y += random(-25, 25);
- r6X= constrain(r6X, 0, width);
- r6Y = constrain(r6Y, 0, height);
- }
1