Search code examples
javaslick2d

Why does a Slick2d Point not intersect a Rectangle?


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


Solution

  • 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