0

So what am trying to do is to enable the user to draw rectangles and detect collisions between rectangles.

The rectangles are added into an Array_List shapes:

ArrayList<Shape> shapes = new ArrayList<Shape>();

and for the collisions java must go through the ArrayList to detect collisions of shapes. (Any other ways to detect the collision is fine). I know that java has a ".intersects()" method for shapes but here it's more about many shapes and i don't know how to use it here.

Here is a small beginning of what i've done so far for the collisions, am ready stuck on that. Please do help?

if (currentAction == 4) {

// Create a new rectangle using x & y coordinates 

aShape = drawRectangle(drawStart.x, drawStart.y,
 e.getX(), e.getY());

//aShape added to the arraylist shapes
 shapes.add(aShape);                                                             

 for(int i=0;i<shapes.size();i++){                            
Shape s = shapes.get(i);

  //collision detection                                 

 }
  }                                 
Lana
  • 171
  • 1
  • 7
  • 18
  • There are multiple ways you might determine if two shapes collide, depending on you needs. Take a look at [this example](http://stackoverflow.com/questions/20927189/detecting-collision-of-two-sprites-that-can-rotate/20928531#20928531) for one. – MadProgrammer Mar 02 '15 at 08:42
  • @MadProgrammer In your example, two rectangles are already defined.. what is need is to detect for collisions between the rectangles that i will draw. – Lana Mar 02 '15 at 09:21
  • @MadProgrammer I just want the program to go through the array, read the coordinates of the shapes, compare them and find what shape collides. Can you please help? – Lana Mar 02 '15 at 09:28
  • The basic principles of comparing two `Shape`s doesn't change. You just need to determine how you can loop through a `List` of them and compare them. Hint, you may need a compound loop... – MadProgrammer Mar 02 '15 at 09:43
  • Thank you @MadProgrammer i've been able to do it!! :D – Lana Mar 02 '15 at 10:52

1 Answers1

0

I'll will tip you - try to read on dynamic programing and try to break the collisions to one who happen horizontally and one who happen vertically.

I hope it helps...

Shmulik Klein
  • 3,754
  • 19
  • 34
  • I can't really understand what you mean by breaking the collisions to one. What am trying to do is to find any collisions between the shapes that are stored inside the arraylist. – Lana Mar 02 '15 at 09:25
  • What I mean is that you first need to sort and then iterate the ArrayList and look for collisions in the X-axis, if you didn't find such a collision you can iterate again and check for Y-axis collision. Another solution would be to use intersect() method with each of the shapes in O(n^2) complexity. – Shmulik Klein Mar 02 '15 at 09:31
  • i did thought of the intersect() method but how to compare when the shapes are inside the array list ? – Lana Mar 02 '15 at 09:38
  • For each shape in the list, compare it with the other shapes in the list. But it is really insufficient. – Shmulik Klein Mar 02 '15 at 09:40
  • what can you suggest? – Lana Mar 02 '15 at 09:43