Faced a problem, I can not catch the collision of two rectangles. I tried to do this with intersects() , but it requires two rectangles inside the method, and I only need to catch a collision with one rectangle. Here is what I write:

  Iterator<Enemy> i = enemy.iterator(); while(i.hasNext()) { Enemy e = i.next(); if(((Ball) ball).getRect().intersects((e.getRect()))) { i.remove(); } } 

I don’t know what it wants, it should work, in the lessons on YouTube I saw that one object was used there and everything was fine ... What was the problem with?

UPDATE:

It means so. Enemy and Ball are two classes; a score is a bullet that must kill the enemy, respectively, enemi is the enemy that the ball must kill. I need to catch the collision of these two objects, trying to catch using intersects , which requires two rectangles, although in the video tutorial that I watched, the tutorial's narrator uses only one class object, the second is used to call the intersect, as in my code example. So the question is, how does its intersect differ from mine? Here is his lesson. Writing a 2D game on java - Part 7.

The error that gives me when I start to compile

The method intersects (Rect, Rect) in the type of the Rect (Rect)

UPDATE 2:

I found a way that solves my problem, maybe someone will come in handy. I almost completely modified the code, here’s what it looks like:

 private void testCollision() { Iterator<Enemy> i = enemy.iterator(); Iterator<Ball> b = ball.iterator(); while(i.hasNext() && b.hasNext()) { Enemy enemies = i.next(); Ball balls = b.next(); if (Math.abs(balls.x - enemies.x) <= (balls.width + enemies.width) / 2f && Math.abs(balls.y - enemies.y) <= (balls.height + enemies.height) / 2f) { i.remove(); b.remove(); } } } 

balls.x - enemies.x - the central border of our object.

UPDATE 3:

I open the topic again because I had a problem

"you need to return the iterator again to the first element, because it has already been driven by the previous pass to the last and hasNext () and returns false."

This is a quote from one person who tried to explain to me how to correct a mistake, but was never able to explain. Can someone tell me how to do this? The code I use is the same.

After reading this quote several times - my brain broke

  • four
    think about your question: here is what useful information it carries for a person who does not know what you are doing and what you are trying to do: 1) you need to catch a collision of 2 rectangles 2) you do not need to catch a collision of 2 rectangles, but you need to catch collision of the 1st rectangle with incomprehensible than 3) you have Enemy and Ball classes / niterfaces and we do not know what they are. If you think logically, the collision of 2 rectangles means that at least 1 point of rectangle A lies on one of the sides of rectangle B or vice versa. - jmu
  • 3
    Yes, @dajver, if we talk about a question, then you need to specify what exactly does not work . 1. does not compile or 2. throws an exception or 3. I expect that i.remove will execute N times, but it is called M times. 4. etc. Something like that. - avp
  • one
    more code - Viacheslav
  • one
    docs.oracle.com/javase/6/docs/api/java/awt/ ... maybe you need this method? - Viacheslav
  • one
    Well, the only thing I can tell you is, look for a method called intersect, or once again carefully review this video tutorial, maybe a third-party library is used there - Viacheslav

2 answers 2

@dajver , You write that compilation error:

 The method intersects(Rect, Rect) in the type Rect is not applicable for the arguments (Rect) 

This means that the Rect.intersects () method wants two args (both Rect types).

Apparently you need to write (if we are looking for the intersection of a rectangular bullet with a rectangular enemy):

 if( ((Ball)ball).getRect().intersects(e.getRect(),((Ball)ball).getRect()) ) { i.remove(); break; // это если одна пуля может уложить только одного врага } 
  • If I write as you said, then I get The static method intersects (Rect, Rect) from the type Rect should be accessed in a static way - dajver
  • one
    Yeah ... he's static. Then try while (...) {... Rect ballr = (Ball) ball.getRect (); if (Rect.intersects (balr, e.getRect ())) {...}} - avp

you would put an error, most likely ball has no getRect method.

continuing to read thoughts : make this method in Ball, returning the rectangle of the described ball or inscribed.


I dare to assume that x, y is the center of the ball. A rect is the coordinates of the upper left and right lower points.

hence there must be something like:

 public Rect getRect() { new Rect(x - width/2, y - height / 2, x + width/2, y + height / 2) } 
  • there is a public Rect getRect () {return new Rect (x, y, 27, 40); } - dajver
  • one
    So what does not work, while () falls? - avp
  • but there is no error, everything works as it should, but nothing comes across - dajver
  • 3
    again, wearing a telepathic helmet I update the answer. - Alex Kapustin
  • one
    @ dajver, i.remove () work (and your required number of times)? - avp