Why constrain doesnt work

int cols, rows;
int w = 20;

ArrayList<Cell> grid = new ArrayList<Cell>();  
ArrayList<Cell> stack = new ArrayList<Cell>();
Cell current;
Ball begain;



void setup() {
  size(820, 820,P3D);
  camera(width/2+100,height,700,width/2,height/2,-400,0,1,0);
  //ortho();
  //directionalLight(255, 0, 255, 0, 1, 0);
  
  cursor(CROSS);
 
  cols = floor(width/w);
  rows = floor(height/w);

  for (int j = 0; j < rows; j++) {
    for (int i = 0; i < cols; i++) {
      Cell cell = new Cell(i, j);
      grid.add(cell);
    }
  }
  //initialized the wall
  for (int j = 0; j < rows/2+1; j++) {
    for (int i = 0; i < cols; i++) {
      grid.get(index(i, j*2 )).wall = true;
    }
  }

  for (int j = 0; j < rows; j++) {
    for (int i = 0; i < cols/2+1; i++) {
      grid.get(index(i*2, j )).wall = true;
    }
  }

  //frameRate(5);
  current = grid.get(index(1, 1));
  begain = new Ball(1,1);
  
}



void draw() {
  background(51);
   //rotateY(PI/4);
  for (int j = 0; j < rows; j++) {
    for (int i = 0; i < cols; i++) {
      grid.get(index(i, j)).show();
    }
  }
  current.visited = true;
  //current.highlight();
  // STEP 1
  Cell next = current.checkNeighbors();
  if (next != null) {
    next.visited = true;

    // STEP 2
    stack.add(current);

    // STEP 3
    removeWalls(current, next);

    // STEP 4
    current = next;
  } else if (stack.size() > 0) {
    current = stack.remove(stack.size()-1);
  }

//  PVector temp = checkgridcell(mouseX, mouseY);
//  println(temp.x, temp.y);
//  println(grid.get(index(int(temp.x), int(temp.y))).wall);
//  if (grid.get(index(int(temp.x), int(temp.y))).wall == true
//    ) {
//    println("nope");
//  } else {
//    println("yes");
//  }
begain.checkneighbor();
begain.changepos();
begain.show();

}

 

int index(int i, int j) {
  if (i < 0 || j < 0 || i > cols-1 || j > rows-1) {
    return rows+1;
  }
  return i + j * rows;
}



void removeWalls(Cell a, Cell b) {
  int x = a.i - b.i;
  if (x == 2) {
    grid.get(index((a.i+b.i)/2, a.j)).wall = false;
  } else if (x == -2) {
    grid.get(index((a.i+b.i)/2, a.j)).wall = false;
  }
  int y = a.j - b.j;
  if (y == 2) {
    grid.get(index(a.i, (a.j+b.j)/2)).wall = false;
  } else if (y == -2) {
    grid.get(index(a.i, (a.j+b.j)/2)).wall = false;
  }
}



PVector checkgridcell(float x, float y) {

  int i = int(x/w);
  int j = int(y/w);

  PVector v = new PVector(int(i), int(j));

  return v;
}

class cell

class Cell {
  int i, j;
  boolean wall = false;
  boolean visited = false;
  
  ArrayList<Cell> neighborAstars = new ArrayList<Cell>();
  
  
  Cell(int i_, int j_) {
    i = i_;
    j = j_;
  }

  
 
  Cell checkNeighbors() {
    ArrayList<Cell> neighbors = new ArrayList<Cell>();

    Cell top    = grid.get(index(i, j-2));
    Cell right  = grid.get(index(i+2, j));
    Cell bottom = grid.get(index(i, j+2));
    Cell left   = grid.get(index(i-2, j));

    if (top != null && !top.visited && !wall) {
      neighbors.add(top);
    }
    if (right != null && !right.visited && !wall) {
      neighbors.add(right);
    }
    if (bottom != null && !bottom.visited && !wall) {
      neighbors.add(bottom);
    } 
    if (left != null && !left.visited && !wall) {
      neighbors.add(left);
    }

    if (neighbors.size() > 0) {
      int r = floor(random(0, neighbors.size()));
      
      return neighbors.get(r);
    } else {
      return null;
    }
  }
  
  
  
    
  
   
   
   void show() {
    int x = i*w;
    int y = j*w;
    stroke(0);
    if(wall){
      push(); 
      noStroke();
      fill(255);
      translate(-w,0,-w*4);
      rect(x, y, w*3, w*4);
      pop();
         
      push(); 
      fill(200);
      strokeWeight(2);        
      translate(x,y,-50);      
      box(w,w,w*1.5);
      pop();
    }
    //  if(!wall){
    //  noStroke();
    //  fill(255);
    //  rect(x, y, w, w);
    //}
    
    //if (visited) {
    //  noStroke();
    //  fill(255);
    //  rect(x, y, w, w);
    //}
  }
  
  
 
  void highlight() {
    int x = i*w;
    int y = j*w;
    noStroke();
    fill(0, 0, 255, 100);
    rect(x, y, w, w);

  }
  
  
  

}

class ball

class Ball {
  float xpos;
  float ypos;
  float speed = 5;
  float r = w/2;



  Ball(float i, float j) {
    xpos = i*w+w/2;
    ypos = j*w+w/2;
  }



  void changepos() {
  if (key == CODED&&keyPressed) {
    if (keyCode == UP) {
      ypos-=speed;
    } else if (keyCode == DOWN) {
      ypos+=speed;
    } else if (keyCode == RIGHT) {
      xpos+=speed;
    } else if  (keyCode == LEFT) {
      xpos-=speed;
    }
  }
  //if (keyCode == UP&&RIGHT) {
  //    ypos--;xpos++;
  //  } else if (keyCode == DOWN&&RIGHT) {
  //    ypos++;xpos++;
  //  } else if (keyCode == LEFT&&UP) {
  //    xpos--; ypos--;
  //  } else if  (keyCode == LEFT&&DOWN) {
  //    xpos--; ypos++;
  //  }
  


}





 **void checkneighbor() {**
**    PVector v = new PVector(xpos, ypos);**
**    PVector t = checkgridcell(v.x, v.y);**
**    Cell top    = grid.get(index(int(t.x), int(t.y-1)));**
**    Cell right  = grid.get(index(int(t.x+1), int(t.y)));**
**    Cell bottom = grid.get(index(int(t.x), int(t.y+1)));**
**    Cell left   = grid.get(index(int(t.x-1), int(t.y)));**

**    if (top.wall == true) {**
**      ypos = constrain(ypos, t.y*w,(t.y+1)*w);**
**     **
**    }**
**    if (right.wall == true) {**
**      xpos = constrain(xpos, (t.x-1)*w,(t.x+1)*w);**
**    }**
**    if (bottom.wall == true) {**
**      ypos = constrain(ypos, (t.y-1)*w,(t.y+1)*w);**
**    }**
**    if (left.wall == true) {**
**      xpos = constrain(xpos, t.x*w,(t.x+1)*w);**
**    }**
**    println(xpos,ypos,t.x,t.y);**
**  }**


strong text

  void show() {
    push();
    strokeWeight(0.11);
    fill(255, 0, 0);
    translate(xpos, ypos, -w*2);
    sphere(r);
    pop();
  }
}

the problem is happen in class ball .the constrain() in function checkneihbor donesnt work at all.
i want to make a maze game . and i try to constrain the red ball inside the wall.but when i contrl the
ball, it goes through the wall .the constrain() isnt work.

did you try to print your conditions and value

    println(top.wall,ypos, int(t.y*w),int((t.y)*w));
    if (top.wall == true) {

my other guess is that may be checkneighbor trigger too late:
when you re on the path, everything is fine so it does nothing, then on key events you move without checking conditions so you’re “on” the walls an from there neighbor are paths :thinking:

1 Like