Problem hitbox, collision between 2 rectangles

Hi, I'm trying to have 2 rectangles which can collide each other. I have something that seems good to me but when the first one goes on the second, the image can go through the second but nothing happend until the middle of the image. I thought it was a problem with my coords but they are accurate so i'm pretty lost now.

Here you can take a look on this sample of my project : http://paste2.org/w4G3etCZ

Have a good day :)

Tagged:

Answers

  • Google rectangle collision detection or

    collision detection

    In the forum or the web

    It has been done

    Iirc the idea is to check if they overlap and then just take the opposite

  • Using not which is !

  • I already followed this, my formula comes from there

  • until the middle of the image

    your collision detection depends on the image mode you are using. you saying this makes me think you are using one image mode but the collision detection (which looks ok for CORNER mode) is coded for another.

    https://processing.org/reference/imageMode_.html

    we need the whole of your code to fix it. posting tiny samples seems to have become a thing here recently. and it really doesn't help us. runnable examples make things much easier.

  • Ok i'll watch, but my co is a bit long and not everything is usefull so shall I give you the whole thing ?

  • Post the entire Code here

  • edited June 2016

    [Link removed due to phishing warnings]

  • Firefox is giving me this warning

    This web page at paf.im has been reported as a deceptive site and has been blocked based on your security preferences.

    Just post the code

  • I have the same thing but no problem on it.

    It's too big and I have files and many tabs

  • try and write a smaller example featuring just the bits that are wrong and post that.

    actually, here's a start using rectangles

    int x1, y1, w1, h1;
    int x2, y2, w2, h2;
    
    void setup() {
      size(200, 200);
      rectMode(CENTER);
      x1 = width / 2;
      y1 = height / 2;
      h1 = 30;
      w1 = 30;
      h2 = 50;
      w2 = 30;
    }
    
    void draw() {
      background(255);
      stroke(0);
      rect(x1, y1, w1, h1); 
    
      x2 = mouseX;
      y2 = mouseY;
      if (collision()) {
        fill(255, 0, 0);  // red
      } else {
        noFill();
      }
      rect(x2, y2, w2, h2); 
    }
    
    boolean collision() {
      if ((x2 >= x1 + w1) || (x2 + w2 <= x1) || (y2 >= y1 + h1) || (y2 + h2 <= y1)) {
        return false;
      }
      return true;
    }
    

    NOTE the rectMode(CENTER);

    it shows something similar to what you describe. change it to rectMode(CORNER) and it's fine (but the rectangles are offset a bit - the mouse pointer is now top left of the moving rectangle, not in the centre)

  • edited June 2016

    @koogs doesn't work try moving mouse from bottom right corner. Ignore this my mistake.

    The collision method in the original code depends on whether the [x,y] coordinates represent the centres or the top-left corners of the rectangles.

    It would be better to have a generic box-box collision functions where the appropriate rectangle coordinates are passed as attributes.

  • edited June 2016

    i know it doesn't work:

    it shows something similar to what you describe.

    ie the bug. gotta reproduce it before you fix it!

    this was my attempt at a small example to get over the 'It's too big and I have files and many tabs' excuse

  • This demonstrates what I mean. It has a single method to detect collision between two rectangles. The parameters represent the top-left and bottom-right corners of the rectangle, as long as the values passed to the function represent these values it doesn't matter how you store the actual reactangle data.

    // RECTMODE must be CORNER or CENTER;
    int RECTMODE = CORNER;
    int x1, y1, w1, h1;
    int x2, y2, w2, h2;
    boolean collides;
    
    void setup() {
      size(200, 200);
      rectMode(RECTMODE);
      x1 = width / 2;
      y1 = height / 2;
      h1 = 30;
      w1 = 30;
      h2 = 50;
      w2 = 30;
    }
    
    void draw() {
      background(255);
      stroke(0);
      x2 = mouseX;
      y2 = mouseY;
    
      // Call the box_bow collision detertor modifiying the parameters so that it gets the ones its expects
      if (RECTMODE == CORNER)
        collides = box_box(x1, y1, x1+w1, y1+h1, x2, y2, x2+w2, y2+h2);
      else if (RECTMODE == CENTER)
        collides = box_box(x1-w1/2, y1-h1/2, x1+w1/2, y1+h1/2, x2-w2/2, y2-h2/2, x2+w2/2, y2+h2/2);
    
      if (collides) {
        fill(255, 0, 0);  // red
      } else {
        noFill();
      }
      rect(x1, y1, w1, h1); 
      rect(x2, y2, w2, h2);
    }
    
    /**
     * Determine whether two boxes intersect.
     * The boxes are represented by the top-left and bottom-right corner coordinates. 
     * 
     * [ax0, ay0]/[ax1, ay1] top-left and bottom-right corners of rectangle A
     * [bx0, by0]/[bx1, by1] top-left and bottom-right corners of rectangle B
     */
    public boolean box_box(float ax0, float ay0, float ax1, float ay1, float bx0, float by0, float bx1, float by1) {
      float topA = min(ay0, ay1);
      float botA = max(ay0, ay1);
      float leftA = min(ax0, ax1);
      float rightA = max(ax0, ax1);
      float topB = min(by0, by1);
      float botB = max(by0, by1);
      float leftB = min(bx0, bx1);
      float rightB = max(bx0, bx1);
    
      return !(botA <= topB  || botB <= topA || rightA <= leftB || rightB <= leftA);
    }
    
Sign In or Register to comment.