Isometry mouse mapping (screen to worldcoordinates)

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?

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.

My mathbackground is insufficient. Can you give me a linkt or description waht i have to do?

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

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);
  }
}

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.


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);
    }   		
}

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/