0

I'm playing around with the Slick 2d Java game engine. I am trying to write some render logic where if a circle is within a square it is coloured green. Otherwise it is coloured red. You can see what I am trying to achieve in this image:

My problem is that when I use Slick2d's Shape.contains(Shape) method, it always returns false regardless of whether the circle is in the square. Whenever I use the Java AWT Rectangle.contains(Rectangle) method it will return true correctly.

Here is some (rubbish) code I'm using. I am storing coordinates as floats if that makes a difference (hence the cast to int for Java AWT's rectangle).

public boolean contains(final Entity entity) {
    Rectangle me = new Rectangle(x, y, width, height);
    Rectangle them = new Rectangle(entity.getX(), entity.getY(), entity.getWidth(), entity.getHeight());
    java.awt.Rectangle awtMe = new java.awt.Rectangle((int) x, (int) y, (int) width, (int) height);
    java.awt.Rectangle awtThem = new java.awt.Rectangle((int) entity.getX(), (int) entity.getY(), (int) entity.getWidth(), (int) entity.getHeight());
    return awtMe.contains(awtThem); // returns true correctly
    //return me.contains(them); // never returns true
}

I'm using Slick version 274. I'm pretty hopeless when it comes to game development so this has puzzled me. I have tried looking at the Slick source for Shape.contains but it's a bit above me at this stage. Any advice as to why this is happening would be appreciated.

EDIT

Ok, so it seems that when I use the Slick2d methods as follows...

return me.intersects(them) || me.contains(them);

...it works as expected. I'm still not sure why contains doesn't work on its own though.

Ben J
  • 5,811
  • 2
  • 28
  • 32
  • 1
    In the comments of Shape.intersects() (of which I don't get the calculation either), it says "if UA and UB are both between 0 and 1 then the lines intersect.", so I guess their "intersection" occurs only when the lines intersect, and not when 1 object is fully contained by the other. Very counter-intuitive indeed! – Torious Apr 14 '12 at 00:34
  • 1
    You need the separate methods because often you want to know about intersections (e.g. for collision detection) separately from containment. However, it appears to me that you're using `contains` as intended. Have you tried contacting the author, or looking at the code for the Webstart demos here?: http://slick.cokeandcode.com/static.php?page=demos – jefflunt Apr 14 '12 at 14:47

1 Answers1

1

Having the same problem, kind of. I see there is an issue on it here: https://bitbucket.org/kevglass/slick/issue/15/circle1intersects-circle2-returns-true