javaprocessingpacman

How to return any point within the borders of a rectangle in processing?


I'm new to java, fyi. I'm using processing and I'm making a snake game. My next step is to make the food refresh to a new position once the snake eats it. The issue is that the "snake head" needs to be completely aligned with the "food" rectangle to refresh; basically, the mouse needs to be over the exact center of the food instead of just touching it. How can I adjust it so that as soon as the mouse touches the "food" rectangle it refreshes? I've try messing around with rectMode(), but that got weird.lol This is what I have so far, and forgive me if the code is trash. Like I said, brand new to java.

class Snake {

    //variables
    int len;
    int wid;
    int xcord;
    int ycord;

    //constructor
    Snake(int x,int y, int len, int wid) {
        this.len = len;
        this.wid = wid;
        this.xcord = x;
        this.ycord = y;
        rect(xcord, ycord, len, wid);
    }

    //clear screen
    void  update() {
        background(255);
        rect(mouseX, mouseY, len, wid);
    }    
}

class Food {

    //variables
    int xcord;
    int ycord;

    //constructor
    Food() {
        this.xcord = int(random(width));
        this.ycord = int(random(height));
        rect(xcord, ycord, 10, 10);
    }

    //update food position. This seems like the issue code block
    void update() {
        if( mouseX == xcord && mouseY == ycord) {
            xcord = int(random(width));
            ycord = int(random(height));
        }
    }

    //display food
    void displayFood() {
        rect(xcord, ycord, 10, 10);
    }
}

Snake s;
Food f;
void setup() {
background(255);
    s = new Snake(mouseX, mouseY, 10, 10);
    f = new Food();
}

void draw() {
    s.update();
    f.update();
    f.displayFood();
}

Solution

  • To check for a collision of to rectangles, you've to check if the rectangles are overlapping in both dimensions.

    For each dimension there are the following possible cases (example for dimension x):

    Not overlapping:

    x1      x1+w1
      +----+
                +----+
              x2      x2+w2
    
               x1      x1+w1
                 +----+
      +----+
    x2      x2+w2
    

    Overlapping

    x1                x1+w1
      +--------------+
           +----+
         x2      x2+w2
    
         x1      x1+w1
           +----+
      +---------------+
    x2                 x2+w2
    
    x1           x1+w1
      +---------+
           +----------+
         x2            x2+w2
    
         x1            x1+w1
           +----------+
      +----------+
    x2            x2+w2
    

    This mean, that the ranges are overlapping if

    x1 < x2+w2 AND x2 < x1+w1
    

    This leads to the following condition in the method update

    class Food {
        // [...]
    
        void update() {
    
            if( mouseX < xcord+10 && xcord < mouseX+10 &&
                mouseY < ycord+10 && ycord < mouseY+10) {
    
                xcord = int(random(width));
                ycord = int(random(height));
            }
        }
    
        // [...]
    }