1
\$\begingroup\$

Using GameMaker Language, I'm basing off their Adavnced Examples. How can i convert the current mouse position into a tile position?

The mouse coordinates are 0,0 from top-left

This is the function when it draws. I think this is what needs reversing. I'm tragically bad at Maths, so I'm sorry in advance.

/// DrawLevel()
var xx,yy,destx,desty, base_x,base_y,current_row_x,current_row_y,tile,o,sp, m,h,offy, tileset;

TileRowDX = 68;
TileRowDY = 34;
TileColumnDX = -68;
TileColumnDY = 34;

MapX = 0;
MapY = 0;
MapDrawX = 11;
MapDrawY = 11;

/// Draw the complete isometric level
tileset = global.tiles;


// End point in map drawing (map coordinates)
destx = MapX+MapDrawX;
desty = MapY+MapDrawY;

// Start drawing position - center in the middle of the screen - and just above a little
base_x = surface_get_width(application_surface)/2;
base_y = 150;

// X and Y loop to print all cells.
for( xx=destx-1;xx>=MapX;xx--) {
    current_row_x = base_x;
    current_row_y = base_y;
    for( yy=MapY;yy<desty;yy++)
    {
        // First draw the column of tiles in this cell, bottom to top.
        offy=0;        
        for(var h=0;h<global.mapMaxLayers;h++){
            m = Map[h];         // get 2D map grid from level array (Z axis)
            tile = m[# xx,yy];  // get tile from 2D grid (x,y axis)
            if( tile>=0 ){
                // if we have a tile, then draw it.
                draw_sprite(tileset,tile,current_row_x,current_row_y-offy);
            }
            offy+=27;
        }

        // Next, get the object in this cell, and draw it on top. 
        // If allowing height for the player, you'd need to add on 27 for each block up.
        o = Objects[# xx,yy];
        if( o!=0 ){
            // Get the object sprite to draw (could also do frames etc)
            sp=o.Sprite;

            // get a 0 to 1 value into the isometric cell.
            var dx = (1.0/15.0) * ((o.x&15));
            var dy = (1.0/15.0) * ((o.y&15));

            // Move to top left of Isometrix grid cell
            var ox = current_row_x - TileRowDX;
            var oy = current_row_y - TileRowDY;

            // Now convert into an isometric offset on X and Y
            ox = ox + (dy*TileRowDX);                   //Y axis first
            oy = oy + (dy*TileRowDY);
            ox = ox - (dx*TileColumnDX);                // now X axis
            oy = oy - (dx*TileColumnDY);

            // and draw this sprite
            draw_sprite(sp,0,ox,oy);

        }

        // Move down and right
        current_row_x += TileRowDX;      
        current_row_y += TileRowDY;    
    }
    // Back up to top, and move left and down.
    base_x += TileColumnDX;
    base_y += TileColumnDY;

}

I'm sure this is just mostly a mathematics question!

\$\endgroup\$
2
  • \$\begingroup\$ I can't comment on the code, but have you seen clintbellanger.net/articles/isometric_math - it goes into the math a bit and talks about transforming the screen to an isometric grid and back. Lots more stuff about tiles here: www-cs-students.stanford.edu/~amitp/gameprog.html#tiles If you figure it out on your own, don't forget you can answer your own question. I'd love to know how you solved it. My cousin is working with game-maker. \$\endgroup\$ – Ape-inago Mar 22 '16 at 3:13
  • \$\begingroup\$ Does game maker have a built-in matrix or something like that. \$\endgroup\$ – Bálint Apr 21 '16 at 9:57
1
\$\begingroup\$
isoX = cartX - cartY;
isoY = (cartX + cartY) / 2;

I don't know game maker's langauge, so I can only give you a pseudocode.

This only works correctly, if your "north" is facing to the top right direction

If you don't have objects, then you maybe need to create 2 separate methods to get tge X and the Y coordinate.

\$\endgroup\$
4
  • \$\begingroup\$ I will check this out. Assuming you meant to put 2DToIso(point). My north is top left, would you mind adjusting if possible? Thanks! \$\endgroup\$ – Joshua4550 Mar 22 '16 at 9:13
  • \$\begingroup\$ Yoh could try to simply negate the x coordinate \$\endgroup\$ – Bálint Mar 22 '16 at 9:15
  • \$\begingroup\$ This doesn't seem close. Are you sure you're trying to convert mouse pixels into isometric grid numbers? And not the other way around? \$\endgroup\$ – Joshua4550 Mar 22 '16 at 9:19
  • \$\begingroup\$ Sorry, this is the other way around, I fix it \$\endgroup\$ – Bálint Mar 22 '16 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.