dazza_bo
Member
I've been playing around with isometric lately and learning a lot. One thing I haven't been able to figure out though is snapping to an isometric grid. I'm trying to get a sprite to follow my mouse cursor and snap to the iso grid as I move the cursor around.
I have tried a few different suggestions I have found in these forums and elsewhere but can't get it to work right.
I found this post on the forum which kinda works but it's not snapping exactly to the grid, it's offset:
My code still uses 64w x 32h for the tile dimensions though. Any ideas on what I'd need to change?
I have tried a few different suggestions I have found in these forums and elsewhere but can't get it to work right.
I found this post on the forum which kinda works but it's not snapping exactly to the grid, it's offset:
I assume the problem is where TheouAegis says "As long as sprites origins are centered in the middle of the diamonds" but my sprites are centred at the top of the diamonds:If I'm understanding things correctly, then you want an isometric grid snap. As long as sprites origins are centered in the middle of the diamonds and mouse_x and mouse_y are greater than 0, then you can use
x = (mouse_x & ~31) + ((mouse_y >> 4 & 1) << 5);
y = (mouse_y & ~15) + ((mouse_x >> 5 & 1) << 4);
This is with no other restrictions (other than a positive mouse_x and mouse_y), so half the sprite would draw outside the room when on the edges. You'd need to instate some other preventative conditions to keep that from happening.
The formula above in slower-but-more-flexible terms:
var tile_half_width = sprite_width/2;
var tile_half_height= sprite_height/2;
var xx = mouse_x div tile_half_width;
var yy = mouse_y div tile_half_height;
x = (xx + (yy & 1)) * tile_half_width;
y = (yy + (xx & 1)) * tile_half_height;
I haven't figured out a working 2-line formula for negative coordinates yet and am starting to think I never will, so restrict your game to positive x and y coordinates.
My code still uses 64w x 32h for the tile dimensions though. Any ideas on what I'd need to change?