Simply fit pieces into a grid :?

F

Fenix18

Guest
Hi guys, I'm new here, new to game maker too, as to programming;
I'm making a simple Jigsaw Puzzle, and having some doubts. You may find it easy as ****, but as I say, I'm new haha;
I create a obj that holds a grid, and the obj's for the pieces. What I'm having trouble is fiting this pieces in their correct position in the grid.
Some tips or examples, anything that may help? I appreciate so much, and I'm looking forward to be able to help you guys too :)
Thanks ~

Mod Edit: topic moved here from Off Topic.
 
Last edited by a moderator:
P

PandaPenguin

Guest
an easy way to "snap" something to a grid:
lets assume your grid is 32x32
Code:
x = floor(x / 32) * 32
y = floor(y / 32) * 32
dividing your position by the grid size will give you the "section" your object is on on the grid (floor() cuts off unnecessary decimals)
multiplying it with the grid size again will give you the "snapped" position of your object on the grid
 

amusudan

Lousiest of Potatoes
an easy way to "snap" something to a grid:
lets assume your grid is 32x32
Code:
x = floor(x / 32) * 32
y = floor(y / 32) * 32
dividing your position by the grid size will give you the "section" your object is on on the grid (floor() cuts off unnecessary decimals)
multiplying it with the grid size again will give you the "snapped" position of your object on the grid
You can use div too, does the same thing

Code:
x = x div 32 * 32
y = y div 32 * 32
 

NightFrost

Member
When you say "correct position" do you mean the position where the piece belongs in the puzzle? For that, you simply create a cheat sheet grid for the game that says which position each piece belongs to. When the player tries to put down a piece, you check from that grid if it is over correct position. If so, you allow putting it down.

EDIT: or even easier, number the pieces in order from zero upwards. For example a fifteen piece puzzle

00 01 02 03 04
05 06 07 08 09
10 11 12 13 14


Piece 13, for example, is in the correct position only when it's x and y positions give the result of y * 5 + x = 13.
 
Last edited:
F

Fenix18

Guest
Are your pieces jigsaw shaped or just squares? Do you want the piece to snap into place when placed in the correct position?
-For now, they are just squares. Yes, is that what I want. I have a obj_grid with a grid like sprite.
 

sp202

Member
So you have two design choices; only let the piece snap onto the grid if it's in the correct position or let the piece snap onto the grid anyway.

Assuming you want the second; create the instances in order of their position much like @NightFrost has done and assign them their position id. When you place them on the grid which can be done using @PandaPenguin's method, you'd calculate their position on the grid and assign their position_id to the respective ds_grid slot.

Each step you'd compare the current ds_grid configuration to the original one, when they match up, the puzzle has been solved. Apologies for the convoluted explanation but hopefully it makes sense.
 
F

Fenix18

Guest
You guys are awesome!

I am migrating the puzzle to Construct 2, as I find it easyer and more conventional for this type of project... and cheaper (unless here in Brazil).
But I'm gonna stick here with you, so much to learn!

Thank you (;
 
  • Like
Reactions: 607
Top