How to make 2 moving objects collide?

edited July 2017 in Questions about Code

Hello, I'm currently making this little game where you're a ball and you must dodge other balls, and I want to make that when some ball hits you the game ends, I've read that dist() may help, but I've only seen it work with static objects and the mouse, not both being moving.

This my code:

int a=390;
int b=390;
int c=390;
int d=390;
int e=390;
int f=390;
int g=390;
int h=390;
int i=390;
int j=390;

void setup () {
size (420,420);
}

void draw () {
  background(mouseY +250,mouseY ,0);
  ellipse (mouseX,mouseY, 30,30);
    a=a-5;
    b=b-4;
    c=c-7;
    d=d-5;
    e=e-6;
    f=f-3;
    g=g-5;
    h=h-6;
    i=i-7;
    j=j-3;
    ellipse (30,a+1, 25,25);
    ellipse (75,b+2, 25,25);
    ellipse (120,c+1,25,25);
    ellipse (165,d+2,25,25);
    ellipse (210,e+1,25,25);
    ellipse (255,f+2,25,25);
    ellipse (300,g+1,25,25);
    ellipse (345,h+2,25,25);
    ellipse (390,i+1,25,25);

    if (a<=0)
    a=390;
    if (b<=0)
    b=390;
    if (c<=0)
    c=390;
    if (d<=0)
    d=390;
    if (e<=0)
    e=390;
    if (f<=0)
    f=390;
    if (g<=0)
    g=390;
    if (h<=0)
    h=390;
    if (i<=0)
    i=390;

}

Any help will be appreciated!

Tagged:

Answers

  • Edit post, highlight code, press Ctrl-o to format code.

    Have you read the collision detection post in the Common Questions part of the forum?

  • Thanks for your answer and the code thingy.

    Yeah, I've seen several options but haven't find any that suits with what I need

  • Answer ✓

    A modified version below. If you want to have multiple objects, you can use an array or an ArrayList container https://processing.org/reference/ArrayList.html and classes. If this interests you, you can check:
    https://processing.org/tutorials/arrays/
    https://processing.org/tutorials/objects/

    Kf

    int a=390;
    
    void setup () {
      size (420, 420);
    }
    
    void draw () {
      background(mouseY +250, mouseY, 0);
      ellipse (mouseX, mouseY, 30, 30);
      a=a-5;
    
      ellipse (30, a+1, 25, 25);
    
      if (dist(mouseX, mouseY, 30, a+1)<25)
        noLoop();
    
    
      if (a<=0)
        reset();
    }
    
    void reset() {
      a=390;
    }
    
    void keyReleased(){
      reset();
      loop();
    }
    
  • Thanks a lot! that was just what I needed, si all I got to do now is make a class for the balls so every single one will "end loop"?

  • Answer ✓

    This is an example using classes.

    Kf

    int a=390;
    
    int n=10;
    ArrayList<Ball> manyBalls = new ArrayList<Ball>();
    
    class Ball {
    
      int x, y, r;
      float speed;
      Ball(int xx, int  yy) {
        x=xx;
        y=yy;
        r=25;
        speed=random(2, 8);
      }
    
      void draw() {
        ellipse (x, y, 25, 25);
      }
    
      void update() {    
        checkBoundaries();
        y-=speed;
      }
    
      void checkBoundaries() {
        if (y<0)
          reset();
      }
    
      void reset() {
        y=height;
        speed=random(2, 8);
      }
    
      boolean checkContact(int xx, int yy) {
        return dist(x, y, xx, yy)<r;
      }
    }
    
    void setup () {
      size (420, 420);
      for (int i=0; i<n; i++) {
        manyBalls.add(new Ball((int)random(width), 0));
      }
    }
    
    void draw () {
      background(mouseY +250, mouseY, 0);
    
      boolean mouseHover=false;
      for (Ball b : manyBalls) {
        b.update();
        b.draw();
        mouseHover|=b.checkContact(mouseX, mouseY);
      }
    
      if (mouseHover==true)
        noLoop();
    
      ellipse (mouseX, mouseY, 30, 30);
    }
    
    
    
    void keyReleased() {
      for (Ball b : manyBalls) {
        b.reset();
      }
      loop();
    }
    
  • Wow!!! thank you very much, really, you saved me, thanks.

Sign In or Register to comment.