Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
0

# plot overlap

I am trying to check instances where the plot does not overlap, but the way I am implement is apparently not working. boolean check = false; if(this.x>=plot.getX()+plot.getWidth()||plot.getX()>=this.x+this.width){ check = false; } else if(this.y>=plot.getY()+plot.getDepth()||plot.getY()>=this.y+this.depth) { check = false; } else{ check = true; } return check; }

8th Apr 2020, 6:54 PM
Angelica
2 Antworten
0
You should take a look at something called AABB collision. It's basically checking if ob rectangle is inside another. You can find it in almkst every language and it's pretty simple.
8th Apr 2020, 8:38 PM
coddy
0
Which case does not work ? if they are rectangles, I tested this and it was all ok public static void main(String[] args) { var a = new Rectangle(50,50,50,50); // x,y, width,depth var b = new Rectangle(); int[][] test = // {x, xmax, result} {{120,150,0},{ 80,150,1},{ 60, 80,1},{ 40, 80,1},{ 40,150,1},{ 20, 40,0}}; for (int[] tx: test) { b.x = tx[0]; b.width = tx[1]-tx[0]; for (int[] ty: test) { // >> b.y = ty[0]; b.depth = ty[1]-ty[0]; boolean res = a.overlaps(b); String check = (res == ((tx[2]==1) && (ty[2]==1))) ? "ok": "wrong"; System.out.printf("%5b, %d, %d, %s\n", res, tx[2], ty[2], check ); } // >> } }
9th Apr 2020, 4:24 AM
zemiak