Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
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  
  Isometry mouse mapping (screen to worldcoordinates)  (Read 6259 times)
0 Members and 1 Guest are viewing this topic.
Offline Axion

Junior Newbie





« Posted 2007-06-17 16:01:27 »

I create a leveleditor for an isometric rpg. At this point i have draw the loaded level and now i need to know over which tile my mouse are. I read several pages on the internet about this issues and the preferred way to do is to creat a set of tiles which are the mouse maps. If i hite with my cursor a special color on the tile i know im over the tile.

is that the best way to do this?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #1 - Posted 2007-06-17 16:38:54 »

I've never seen the point of the imagemap approach - the required maths to do a few line classifications is simple and doesn't introduce precision problems. IMHO the image map method is really just a hack which dates back to when memory access was cheaper and cpu cycles were more valuable.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Axion

Junior Newbie





« Reply #2 - Posted 2007-06-19 09:27:20 »

My mathbackground is insufficient. Can you give me a linkt or description waht i have to do?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline noblemaster

« JGO Spiffy Duke »


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #3 - Posted 2007-06-19 09:31:46 »

For a flat isometric tile map (pixel coordinates to col/row and vice versa):

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
import java.awt.Dimension;
import java.awt.Point;
import java.awt.geom.Point2D;

/**
 * Iso utility.
 *
 * <pre>
 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 *
 *       |----| isoTileWidth (pixels)
 *              _    
 *         /\   |
 *        /  \  | isoTileHeight (pixels)
 *        \  /  |
 *         \/   |
 *              -
 *
 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 *
 *             isoWidth (pixels)
 *           |--------|
 *
 *                |-> isoX (pixels)
 *    ------------------------
 *   |                        | _               _
 *   |   (row) 0 /\ 0 (col)   | |               |
 *   |          /  \          | v               |
 *   | (row) 1 /\  /\ 1 (col) |                 |
 *   |        /  \/  \        | isoY (pixels)   | isoHeight (pixels)
 *   |        \  /\  /        |                 |
 *   |         \/  \/         |                 |
 *   |          \  /          |                 |
 *   |           \/           |                 -
 *   |                        |
 *    ------------------------
 *
 * xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 * </pre>
 *
 * @author Christoph Aschwanden
 * @since April 8, 2007
 */

public final class IsoUtil {

  /**
   * Constructor.
   */

  private IsoUtil() {
    // prevents instantiation.
  }
 
  /**
   * Returns the x/y coordinate for the given column. The middle of the column/row
   * will be assumed for x/y calcuation!
   *
   * @param col  The column.
   * @param row  The row.
   * @param isoTileWidth  The iso width.
   * @param isoTileHeight  The iso height.
   * @return  The x/y coordinate.
   */

  public static Point getIsoXY(int col, int row, int isoTileWidth, int isoTileHeight) {
    return getIsoXY((float)(col + 0.5), (float)(row + 0.5), isoTileWidth, isoTileHeight);
  }
 
  /**
   * Returns the x/y coordinate for the given column.
   *
   * @param col  The column. Column 0 is from [0, 1). Etc.
   * @param row  The row. Row 0 is from [0, 1). Etc.
   * @param isoTileWidth  The iso width.
   * @param isoTileHeight  The iso height.
   * @return  The x/y coordinate.
   */

  public static Point getIsoXY(float col, float row, int isoTileWidth, int isoTileHeight) {
    int isoHalfWidth = isoTileWidth / 2;
    int isoHalfHeight = isoTileHeight / 2;
    int isoX = (int)((col - row) * isoHalfWidth);
    int isoY = (int)((col + row) * isoHalfHeight);
    return new Point(isoX, isoY);
  }
 
  /**
   * Returns the row and column for x and y.
   *
   * @param isoX  X position.
   * @param isoY  Y position.
   * @param isoTileWidth  The iso width.
   * @param isoTileHeight  The iso height.
   * @return  The row and column. Column 0 is any where from [0, 1). Ditto row.
   */

  public static Point2D.Float getColRow(int isoX, int isoY, int isoTileWidth, int isoTileHeight) {
    float col = (((float)isoX) / ((float)isoTileWidth)) + (((float)isoY) / ((float)isoTileHeight));
    float row = (((float)isoY) / ((float)isoTileHeight)) - ((float)isoX) / ((float)isoTileWidth);
    return new Point2D.Float(col, row);
  }
 
  /**
   * Returns the size in pixels.
   *
   * @param numCols  The number of rows.
   * @param numRows  The number of columns.
   * @param isoTileWidth  The iso width.
   * @param isoTileHeight  The iso height.
   * @return  The size.
   */

  public static Dimension getIsoSize(int numCols, int numRows, int isoTileWidth, int isoTileHeight) {
    int width = ((numCols + numRows) * isoTileWidth) / 2;
    int height = ((numCols + numRows) * isoTileHeight) / 2;
    return new Dimension(width, height);
  }
}

Offline Axion

Junior Newbie





« Reply #4 - Posted 2007-06-19 15:58:46 »

Thx for sharing your sourcecode. I implement my mouseListener on top for your source. I hope i unterstand it right. My Mouseclass highlightes the current tile where my mouse are over. I think it works great.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
import java.awt.geom.Point2D;
import java.awt.event.*;
import java.awt.*;
import javax.swing.JComponent;
 
public class Mouse implements MouseMotionListener{

   private Point2D.Float point;
   
    public Mouse() {
    }

   public void mouseDragged(MouseEvent e) {
   }
   
      public void mouseMoved(MouseEvent e) {
         JComponent c = (JComponent) e.getSource();
         this.point = IsoUtil.getColRow(e.getX(),e.getY(),44,44);  
         c.repaint();
      }
   
      public void paint(Graphics g){
         int tmpX = (int)point.getX() * 22;
         int tmpY = (int)point.getX() * 22;
         tmpX -= (int)point.getY() * 22;
         tmpY += (int)point.getY() * 22;
         if(point.getY() < 0){
            tmpY -= 22;
         }else{          
            tmpX -= 22;
         }
         paintRaute(g,tmpX, tmpY);
      }    
         
    private void paintRaute(Graphics g, int x, int y){
       g.setColor(Color.black);
       g.drawLine(x+(44 / 2), y, x+44, y+(44/2));
       g.drawLine(x+44, y+(44/2), x+(44 / 2), y+44);
       g.drawLine(x+(44 / 2), y+44, x, y+(44/2));
       g.drawLine(x, y+(44/2), x+(44 / 2), y);
    }        
}
Offline noblemaster

« JGO Spiffy Duke »


Medals: 20
Projects: 10


Age of Conquest makes your day!


« Reply #5 - Posted 2007-06-19 18:29:50 »

1  
this.point = IsoUtil.getColRow(e.getX(),e.getY(),44,44);


yes, that's basically correct...
Also have a look at (section 2): http://www.noblemaster.com/public/

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.

SHC (23 views)
2014-11-25 12:00:59

SHC (22 views)
2014-11-25 11:53:45

Norakomi (19 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (67 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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