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.
contains
as intended. Have you tried contacting the author, or looking at the code for the Webstart demos here?: slick.cokeandcode.com/static.php?page=demos