Simple Mouse Question

Hello Everybody,

I know Java pretty well, and I am familiar with the basics of Swing and Graphics/Graphics2D, but I have no experience with games. I am writing a Risk style game that will have many oddly shaped regions. The part I am having trouble with is knowing when the mouse is over a particular region of the map. Should I use something like a MouseMotionListener? How would I figure out which region the mouse is moved into?

the easiest way would be to design a “Polygon” object in shape of you region. each time you click on the screen you can use the polygon’s internal contains() methods for checking if its a specified region .

I don’t know if this is just plain stupid but what came in my mind would be to have a lookup array of int[][] where you can check the x,y coords and get the id back of the region you clicked on.

This would look like the following:

1 1 1 2 2 3
1 1 1 1 2 3
1 1 1 2 3 3
1 1 2 2 3 4

so when you click on 0,0 it would give you zone 1 p.ex.

Boths ways would work. Serethos’ would be a bit slow and KONI’s would eat some ram :slight_smile:

I would use polys as Serethos suggested, but I would use a two tiered check. First determine which bounding rects you hit then check those polys and as soon as you got a positive hit for the contains method stop checking.

The benefit of that method is that it’s easy to do and it’s also pretty fast because you can exclude the vast majority of the polys for the indepth check.

So basically… create an array with all Polys and create an array with bounding rectangles (same size/same order - just use getBounds()). If a click occours, step through the rectangle array and do the contains check… if positive do the same with the poly (same index)… if positive return the current index and if nothing was hit return -1.

@onyx
good that you point to the performance question. in many game articles and books i read that for 2D games-collision detection java-shapes are used.
a year ago i compared the speed of the contains() method
to manually checking each side of the shape. my result was that the manual check was much faster (…for simple shapes).
do you think its a (good) common practise to use java-shapes for collision detection (especially pointing to action games) ?

do you think its a (good) common practise to use
java-shapes for collision detection (especially pointing to
action games) ?

If you’re using only circles and rectangles you’re pretty save with those methods from the AWT. The poly checks are pretty slow, because it’s one size fits all algo for concave and convex polys (convex polys are cheaper to check). Well, usually polys aren’t needed anyways.

As a rule of thumb… just write it plain and simple. If it’s too slow for your taste profile and take care about the time consuming spots.


I just checked the Polygon source. They actually already do that bounds check before doing the in depth test (the bounding rectangle gets cached). So even the simple way of checking (just poly.contains) should be fast enough. You can also get everything cached first by requesting the bounding rectangle for each poly once at the beginning.

So basically like Serethos said. Create the polys… call getBounds once for each poly (without storing the result) and then just loop through em, do a poly.contains() and if it’s positive stop.

Hi Jeff, I created a Risk style game as well. I used the following method to define maps. I currently have 4 different maps. It takes some time to set up. But there is not large amount of RAM used or long access times. I currently get 54 fps.

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=cluebies;action=display;num=1105044939

But since I assume its turn based, as another dev mentioned to me in another post. You may want to look at re-active renderring rather then active renderring for a risk game. So far I’m using active, but I may change that soon, but so far so good my renderring seems ok.

Actually I would really like to port the project over to LWJGL. And try the same thing with maybe a 3D map.