• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Help with isometric grid calculations

L

Liam Harper

Guest
I'm working on an isometric grid and I thought I'd cracked the maths but I've hit a wall. I'm using draw_line_width() to draw a simple Cartesian grid in the centre of the screen:

Code:
global.GridLeft = (room_width / 2) - ((global.CellWidth * global.NoOfXCells) / 2);
global.GridTop = (room_height / 2) - ((global.CellHeight * global.NoOfYCells) / 2);

// Draw vertical lines

draw_line_width(
    global.GridLeft + (i * global.CellWidth),
    global.GridTop,
    global.GridLeft + (i * global.CellWidth),
    global.GridTop + (global.CellHeight * global.NoOfYCells),
    1
);

// Draw horizontal lines

draw_line_width(
    global.GridLeft,
    global.GridTop + (i * global.CellHeight),
    global.GridLeft + (global.CellWidth * global.NoOfXCells),
    global.GridTop + (i * global.CellHeight),
    1
);
This works fine, but when I try to convert these coordinates to isometric coordinates, I can't get the grid centred on the screen. Instead it's off to the left and bottom of the screen and I can't get my head around why!

Code:
// Cartesian to isometric script
/// @function iso_start_end(cart_x1, cart_y1, cart_x2, cart_y2, array)
/// @param cart_x1
/// @param cart_x2
/// @param cart_y1
/// @param cart_y2
/// @param array

cart_x1 = argument0;
cart_y1 = argument1;
cart_x2 = argument2;
cart_y2 = argument3;
array = argument4;

array[0] = cart_x1 - cart_y1;
array[1] = (cart_x1 + cart_y1) / 2;
array[2] = cart_x2 - cart_y2;
array[3] = (cart_x2 + cart_y2) / 2;

return array;
Code:
// Draw lines with isometric coordinates

isometric = iso_start_end(
    global.GridLeft + (i * global.CellWidth),
    global.GridTop,
    global.GridLeft + (i * global.CellWidth),
    global.GridTop + (global.CellHeight * global.NoOfYCells),
    isometric
);

draw_line_width(isometric[0], isometric[1], isometric[2], isometric[3], 1);

isometric = iso_start_end(
    global.GridLeft,
    global.GridTop + (i * global.CellHeight),
    global.GridLeft + (global.CellWidth * global.NoOfXCells),
    global.GridTop + (i * global.CellHeight),
    isometric
);

draw_line_width(isometric[0], isometric[1], isometric[2], isometric[3], 1);
How do I get the grid to start drawing in the centre?
 

Attachments

TheouAegis

Member
Because you're confusing what your grid is representing with how it is projected. You are thinking of the top left corner of your flat grid as being (0,0), but (0,0) is the top left corner of the screen, not your little collection of squares. An isometric projection rotates around (0,0) on the screen, so if you want the rotational axis to not be based on that, then you need to add constant horizontal and vertical offsets to the projected result. In your case, that's global.GridLeft and global.GridRight.
 
Top