Need help on circle collision! Thanks

edited May 2017 in Questions about Code

Hello, I really need help on this project I have to finish within 5 hours. I have random circles coming down the screen, with a red circle in the middle. My goal is to make it so that if the Red circle comes in contact with any grey circle, they both disappear. Thank you so much for the help!

Tagged:

Answers

  • int xship=250; 
    int yship=250; 
    float x[]; 
    float y[];
    
    void setup() { 
      size(500, 500); 
      x = new float [100]; 
      y = new float [100];
    
      for (int i=0; i<x.length; i++) { 
        x[i]=random(0, 600); 
        y[i]=random(0, 600);
      }
    }
    
    void draw() { 
      background(255); 
      for (int i=0; i<x.length; i++) {
    
        y[i]=y[i]+3;
    
        fill(120);
        ellipse(x[i], y[i], 20, 20); 
        if (y[i]>600)
        {
          y[i]=0;
          x[i]=random(0, 600); 
          y[i]=random(0, 0);
        }
    
        fill(255, 0, 0);
        ellipse(xship, yship, 20, 20);
      }
    }
    
  • Line 34 should be on line 31, for a start.

    How can you tell if two circles are touching? That is, what can you say about the distance between their centers (as it relates to their radii)?

    What happens (or doesn't happen) to a grey circle when it is close enough to the red one? How can you remember that a grey one was close enough?

    When should (or shouldn't) the red circle be drawn? Hint: Did you remember something?

  • I honestly don't have any idea, which is why I posted this discussion haha.

  • Answer ✓

    Notice you don't need line 27 as line 29 is the one that takes effect.

    Notice the red dot needs to be drawn only once, so it can be outside of the for-loop. I am referring to line 33: ellipse(xship, yship, 20, 20);

    When there is a collision, you don't need to kill or delete your grey circle. Instead, you could re-init the circle exactly as what you are doing in lines 28 and 29.

    However, I am not sure what should happen to your red dot. Should it completely disappear? One needs to show the red circle again so for the overlapping algorithm to be able to execute again. You will need to provide more details.

    Kf

    final int kRADIUS=10;
    
    int n=100;
    float x[]; 
    float y[];
    
    float velY=3;
    
    
    int xship=250; 
    int yship=250; 
    
    
    void setup() { 
      size(500, 500); 
      x = new float [n]; 
      y = new float [n];
    
      for (int i=0; i<x.length; i++) { 
        x[i]=random(0, width); 
        y[i]=random(0, height);
      }
    
      noStroke();
      ellipseMode(RADIUS);
    }
    
    void draw() { 
      background(255); 
      for (int i=0; i<x.length; i++) {
    
        y[i]=y[i]+velY;
    
        if (checkOverlapping(x[i], y[i])==true) {
          x[i]=random(0, width); 
          y[i]=random(0, 0);
        }
    
        fill(120);
        ellipse(x[i], y[i], kRADIUS, kRADIUS); 
        if (y[i]>height)
        {
          //y[i]=0;
          x[i]=random(0, width); 
          y[i]=random(0, 0);
        }
      }
    
      fill(255, 0, 0);
      ellipse(xship, yship, kRADIUS, kRADIUS);
    }
    
    boolean checkOverlapping(float xx, float yy) {
      return (dist(xship, yship, xx, yy)<2*kRADIUS) ;
    }
    
  • That is perfect, thank you! I know what to do now, I can take it from here.

Sign In or Register to comment.