Collision with moving player !!!! and block...

I WANT to disable each side separately ! or better movement of player :D thx...

code:

float s1x = 0;      // square position (move with mouse)
float s1y = 0;
float s1w = 64;     // and dimensions
float s1h = 64;

float s2x = 200;    // same for second square
float s2y = 100;
float s2w = 64;
float s2h = 64;

int left, up, down, right;
boolean goleft, goup, godown, goright;
int speed = 5;

void setup() {
  size(600, 400);
  noStroke();
}
void keyPressed() {
  if (key == 'a' || key == 'A') {
    left = 1;
  }
  if (key == 'd' || key == 'D') {
    right = 1;
  }
  if (key == 'w' || key == 'W') {
    up = 1;
  }
  if (key == 's' || key == 'S') {
    down = 1;
  }
}
void keyReleased() {
  if (key == 'a' || key == 'A') {
    left = 0;
  }
  if (key == 'd' || key == 'D') {
    right = 0;
  }
  if (key == 'w' || key=='W') {
    up = 0;
  }
  if (key == 's' || key == 'S') {
    down = 0;
  }
}

void draw() {
  goleft = true;
  godown = true;
  goright = true;
  goup = true;
  background(255);
  rectRect(s1x, s1y, s1w, s1h, s2x, s2y, s2w, s2h);
  rect(s2x, s2y, s2w, s2h);
  fill(0, 150);
  rect(s1x, s1y, s1w, s1h);
  if (goup)s1y -= up * speed;
  if (godown)s1y += down * speed; 
  if (goleft)s1x -= left * speed;
  if (goright)s1x += right * speed;
}

void rectRect(float r1x, float r1y, float r1w, float r1h, float r2x, float r2y, float r2w, float r2h) {
  if (r1x + r1w >= r2x &&  
    r1x <= r2x + r2w &&  
    r1y + r1h >= r2y && 
    r1y <= r2y + r2h) {
    if (r1x + r1w > r2x && r1x <= r2x + r2w && r2y >= r1y-r1h) { // && r2y < r1y
      println("DOWN HIT!!!");
      godown = false;
    }
    if (r1x + r1w > r2x && r1x < r2x + r2w && r2y <= r1y-r1h) { // && r2y < r1y
      println("DOWN HIT!!!");
      goup = false;
    }

    if (r1y+r1h > r2y && r1y < r2y + r2h && r2x >= r1x) { // && r2y < r1y
      println("LEFT HIT!!!");
      goright = false;
    }

    if (r1y+r1h > r2y && r1y < r2y + r2h && r2x+r2w <= r1x+r1h) { // && r2y < r1y
      println("RIGHT HIT!!!");
      goleft = false;
    }
  }
}

Answers

  • please don't post duplicates. AND DON'T SHOUT.

  • BUT i dont have answer....

  • perhaps the question is not clear enough?

  • if (r1x + r1w > r2x && r1x <= r2x + r2w && r2y >= r1y-r1h) { // && r2y < r1y
      println("DOWN HIT!!!");
      godown = false;
    }
    if (r1x + r1w > r2x && r1x < r2x + r2w && r2y <= r1y-r1h) { // && r2y < r1y
      println("DOWN HIT!!!");
      goup = false;
    }
    

    DOWN HIT twice?

  • edited October 2017

    Now its work, but when i have speed > 1 ... player going to the rect....

    float s1x = 0;      // square position (move with mouse)
    float s1y = 0;
    float s1w = 64;     // and dimensions
    float s1h = 64;
    
    float s2x = 200;    // same for second square
    float s2y = 100;
    float s2w = 64;
    float s2h = 64;
    
    int left, up, down, right;
    boolean goleft, goup, godown, goright;
    int speed = 1;
    
    void setup() {
      size(600, 400);
      noStroke();
    }
    void keyPressed() {
      if (key == 'a' || key == 'A') {
        left = 1;
      }
      if (key == 'd' || key == 'D') {
        right = 1;
      }
      if (key == 'w' || key == 'W') {
        up = 1;
      }
      if (key == 's' || key == 'S') {
        down = 1;
      }
    }
    void keyReleased() {
      if (key == 'a' || key == 'A') {
        left = 0;
      }
      if (key == 'd' || key == 'D') {
        right = 0;
      }
      if (key == 'w' || key=='W') {
        up = 0;
      }
      if (key == 's' || key == 'S') {
        down = 0;
      }
    }
    
    void draw() {
      goleft = true;
      godown = true;
      goright = true;
      goup = true;
      background(255);
      rectRect(s1x, s1y, s1w, s1h, s2x, s2y, s2w, s2h);
      rect(s2x, s2y, s2w, s2h);
      fill(0, 150);
      rect(s1x, s1y, s1w, s1h);
      if (goup)s1y -= up * speed;
      if (godown)s1y += down * speed; 
      if (goleft)s1x -= left * speed;
      if (goright)s1x += right * speed;
    }
    
    void rectRect(float r1x, float r1y, float r1w, float r1h, float r2x, float r2y, float r2w, float r2h) {
      if (r1x + r1w >= r2x &&  
        r1x <= r2x + r2w &&  
        r1y + r1h >= r2y && 
        r1y <= r2y + r2h) {
          if (r1x + r1w > r2x && r1x < r2x + r2w && r2y > r1y-r1h) { // && r2y < r1y
            println("DOWN HIT!!!");
            godown = false;
          }
          if (r1x + r1w > r2x && r1x < r2x + r2w && r2y <= r1y-r1h) { // && r2y < r1y
            println("UP HIT!!!");
            goup = false;
          }
    
          if (r1y+r1h > r2y && r1y < r2y + r2h && r2x >= r1x) { // && r2y < r1y
            println("LEFT HIT!!!");
            goright = false;
          }
    
          if (r1y+r1h > r2y && r1y < r2y + r2h && r2x+r2w <= r1x+r1h) { // && r2y < r1y
            println("RIGHT HIT!!!");
            goleft = false;
        }
      }
    }
    
  • do you need to check for <= rather than just < (and >= rather than >) in all the conditions?

  • i want to off one side of move :D

Sign In or Register to comment.