Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (488)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple Mouse Question  (Read 1192 times)
0 Members and 1 Guest are viewing this topic.
Offline jeffchuck

Innocent Bystander




Java games rock!


« Posted 2005-01-05 05:28:11 »

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?
Offline Serethos

Junior Member




Java games rock!


« Reply #1 - Posted 2005-01-05 07:11:38 »

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 .
Offline KONI

Junior Member




Java games rock!


« Reply #2 - Posted 2005-01-05 11:24:22 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2005-01-05 17:57:26 »

Boths ways would work. Serethos' would be a bit slow and KONI's would eat some ram Smiley

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.

弾幕 ☆ @mahonnaiseblog
Offline Serethos

Junior Member




Java games rock!


« Reply #4 - Posted 2005-01-06 08:12:05 »

@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) ?
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #5 - Posted 2005-01-06 08:53:07 »

>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.

弾幕 ☆ @mahonnaiseblog
Offline 69_Jeremy_69

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-01-06 19:23:04 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (17 views)
2014-08-28 18:26:30

CopyableCougar4 (26 views)
2014-08-22 19:31:30

atombrot (39 views)
2014-08-19 09:29:53

Tekkerue (36 views)
2014-08-16 06:45:27

Tekkerue (33 views)
2014-08-16 06:22:17

Tekkerue (22 views)
2014-08-16 06:20:21

Tekkerue (33 views)
2014-08-16 06:12:11

Rayexar (69 views)
2014-08-11 02:49:23

BurntPizza (46 views)
2014-08-09 21:09:32

BurntPizza (37 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!