Jump to content


Photo

Script: Snap To An Isometric Grid


  • Please log in to reply
8 replies to this topic

#1 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 12 April 2007 - 04:13 PM

Since this has been asked several times in the Q&A forum, I'd post this script here so that I (or someone) can easily quote it in the future.
This is an isometric version of the in-built action "snap to a grid" (or move_snap() in GML.) Hope this is useful to someone, no credit is necessary.

The script
// Script: move_snap_isometric(hsnap, vsnap)
// Purpose: snaps the instance to an isometric grid.
// Inputs:
//  argument0 = the horizontal snapping value, i.e. the width of the grid.
//  argument1 = the vertical snapping value, i.e. the height of the grid.
// Example:
//  move_snap_isometric(32, 16);
var m, n;
m = round(y/argument1 - x/argument0);
n = round(y/argument1 + x/argument0);
x = (n - m)/2*argument0;
y = (n + m)/2*argument1;

Edit: An example
snap_isometric.gm6 (8KB)

How it works
m and n represents the indices of the diagonal-down and diagonal-up grid lines, respectively (see the figure below.) The former half of the script determines m and n of each line which is nearest to the given point, and the latter half calculates the position of intersect of grid lines with the given m and n.
Posted Image

Edited by torigara, 15 January 2011 - 04:40 PM.

  • 4

#2 Mamnarock

Mamnarock

    GMC Member

  • GMC Member
  • 21 posts
  • Version:GM:Studio

Posted 14 April 2007 - 12:53 AM

Is better if you upload a example.
  • 0

#3 Krisando

Krisando

    GMC Member

  • New Member
  • 1351 posts

Posted 14 April 2007 - 09:29 AM

Just what I was looking for!! :)
  • 0

#4 ChevyRay

ChevyRay

    GMC Member

  • New Member
  • 237 posts

Posted 19 April 2007 - 09:41 PM

Nice, I just figured this out fooling around, and I was going to post a script. I checked around first and found this one though, well done; basically the same thing.

I created iso_grid_x and iso_grid_y though, which basically you just enter x and y coordinates into and the grid size. iso_grid_x returns the x snapped, and iso_grid_y returns the y snapped. I'm programming isometric right now, and I found it to be a bit more flexible this way...

argument0 = x position
argument1 = y position
argument2 = grid width
argument3 = grid height


iso_grid_x
var xx,yy;
xx=round(argument1/argument3-argument0/argument2);
yy=round(argument1/argument3+argument0/argument2);
return((yy-xx)/2*argument2);
iso_grid_y
var xx,yy;
xx=round(argument1/argument3-argument0/argument2);
yy=round(argument1/argument3+argument0/argument2);
return((yy+xx)/2*argument3);

For example, to align an object to a 32x16 grid, the code would look like this:
x = iso_grid_x(x,y,32,16);
y = iso_grid_y(x,y,32,16);

Post those up if you want, no credit required, since for those ones I just revised your script instead. Cheers,

ChevyRay
  • 0

#5 KABUKISTAR

KABUKISTAR

    GMC Member

  • New Member
  • 1 posts

Posted 19 June 2007 - 08:28 AM

Alright; this is just what I was looking for. Thanks, torigara. I was looking for a way to make a cursor under the mouse that snaps to an isometric grid, a la Fallout.

// Script: move_snap_isometric_cursor(hsnap, vsnap)
// Purpose: creates a cursor that snaps to an isometric grid
// Inputs:
//  argument0 = the horizontal snapping value, i.e. the width of the grid.
//  argument1 = the vertical snapping value, i.e. the height of the grid.
// Example:
//  move_snap_isometric(64, 32);
//  then, draw a cursor at snapx, snapy
var m, n;
m = round(mouse_y/argument1 - mouse_x/argument0);
n = round(mouse_y/argument1 + mouse_x/argument0);
snapx = (n - m)/2*argument0;
snapy = (n + m)/2*argument1;

  • 0

#6 Irishsnow

Irishsnow

    GMC Member

  • New Member
  • 2 posts

Posted 17 July 2009 - 11:03 PM

I couldn't find this for a long time. I looked up guides, tutorials, etc.

This works perfectly, the way I was making my game before was extremely laggy, but now its not.=-D
  • 0

#7 oddchild

oddchild

    GMC Member

  • New Member
  • 37 posts

Posted 24 May 2011 - 04:54 AM

is there a way to cross apply this to a 2d top veiw game with non angeled lines? if so could you please show me an example
  • 0

#8 kv.v

kv.v

    GMC Member

  • New Member
  • 95 posts

Posted 18 June 2011 - 06:20 PM

thanks this really works well :)
  • 0

#9 RMcSwain

RMcSwain

    GMC Member

  • New Member
  • 5 posts
  • Version:GM8.1

Posted 03 January 2013 - 10:46 PM

Thank you for the script, helping me develop my inner programmer!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users