2

When I execute this block of Code:

    Point p = new Point(1,1);
    Rectangle r = new Rectangle(0 ,0 , 10 ,10);

    if(p.intersects(r)){
        System.out.println("Collided");
    }

It never prints Collided. Why is this?

Both Objects are

org.newdawn.slick.geom

Implementations, not standard Java ones

Oliver-R
  • 163
  • 1
  • 9
  • Why do you think it should? – Sotirios Delimanolis Sep 08 '15 at 16:32
  • My assumption was that the intersects method returns true when two Shapes intersect. Is this not the case? – Oliver-R Sep 08 '15 at 16:58
  • [This](https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection) might be of some help, it explains the basic theory behind collision detection. In your case your Point doesn't have any sides (obviously). My guess would be, that the method doesn't have anything to compare against the rectangle. -> Did you try switching the rectangle and the point? – redxef Sep 08 '15 at 17:36

1 Answers1

2

That is a good question. To find out why I made some research

Point p = new Point(0,0);
System.out.println(p.closed()); // TRUE
System.out.println(p.getPointCount()); // 1, logically

And then I search in the Slick2d repo in the Shape class. I think these lines are the reason why it is not working:

    boolean result = false;
    float points[] = getPoints();  // Get all points of our shape
    int length = points.length;  // count them. here length==1

    if (!closed()) {
        length -= 2;  // as we see the point is a closed shape, here length=-1
    }
    for(int i=0;i<length;i+=2) {  // Does not even enter the complicated work because length == -1
        // Complicated thing to test if intersect with a lot off points
    }
    return result; // here return false

Here is the justification of your problem. I don't know if it's a bug or if it is the will of the developpers. You can still set up an issue.

As a solution I would advice to build a Rectangle(x,y,1,1) and intersect with it. This does the work as you want because it is a 4points shape

RPresle
  • 2,436
  • 3
  • 24
  • 28