- Home /
Need an Algorithm to solve the Geometric Puzzle
Hi there, I'm trying to make a geometric puzzle game for kids. I have done all the dragging stuffs. I'm confused on the checking part of the game. After the user has put all the pieces together, how can I check if the user is correct, it means there are many ways to complete 1 shape, like this:
I thought about using anchor point to handle this but this idea same to lame and just working at the unique cases. Here is an image with anchor point (the black asterisk):
And here is when I move the red square to the right, it meets the anchor point but not fit the gray shape.
How can I handle this problem ? Are there any algorithms for it ? Thanks
Are your shapes going to be rectangular only? Or other shapes and concave polygons too?
$$anonymous$$y shapes has circle, lozenge, triangle, halfmoon, etc, not only rectangular. Do you have any ideas ?
I'm hoping that your halfmoon etc. shapes don't have curves. If they did, it would make things twice as hard.
But if you plan on having only one solution, it's fairly easy. Just break up the final shape into random smaller shapes (in the editor) and check whether the shapes are in the required position (which we set in the editor).
thanks for your reply, but I think your solution does not work with me. $$anonymous$$y game plan is 1 shape - many solutions so if I break up the final shape into random pieces, I just having 1 solution for 1 shape. Besides that, don't worry about curved shapes and pieces because I already have a way to do with it.
Answer by ricardo_arango · Nov 12, 2014 at 05:49 PM
I would do two things:
Add snapping, so the pieces are placed in precise locations. You could use a grid of 5pixelx5pixels, or so, whatever fits your design.
Render the pieces with a replacement shader to a RenderTexture (it could be half the size or smaller : 256x256, etc), with the result being a black & white image : white for pieces, black for everything else. I would then compare the result with a "template" or "correct" image.
There are many algorithms that can do image comparison. The simplest one and which should suffice for this would be to do a pixel-by-pixel comparison. So you would take pixel [0] from the RenderTexture and subtract pixel [0] from the Template texture. If the different is zero, then they are equal.
You can add a threshold to compensate for any errors. For example, if 98% of pixels are the same, then the images are about the same, and you can then say that the pieces are covering the area correctly.
Answer by tanoshimi · Nov 09, 2014 at 08:18 AM
For some reason I can't see any image but, from your description, can't you simply create a box collider that encompasses the extent of the desired completed shape? When all the fragments lie within the collider (which you can easily check with OnTriggerStay), the puzzle is complete.
It's a great idea but how OnTriggerStay can check if pieces are inside the Collider fences ? Because I think that user can move a piece in and out the collider many times
I don't think that OnTriggerStay will work... because it will detect objects on the edge of the collider too, so even if the shapes don't fit, the OnTriggerStay will say that the shape is complete...
Hi tanoshimi, you can view the image in the order 1,2,3 as the same above
Thanks
[1]: http://i.$$anonymous$$us.com/iUIEI03vb99XA.jpg
[2]: http://i.$$anonymous$$us.com/iFxVm9bv0YVFl.jpg
[3]: http://i.$$anonymous$$us.com/iIqAjyaWxbnWS.jpg
Ok, so you know when the level starts whether a fragment is within the "completed puzzle" area, and you can keep track of each OnTriggerEnter/ OnTriggerExit after that to know at any one time which shapes are still outside. The only tricky bit is knowing which shapes are completely inside the puzzle area. You can do this a couple of ways - for your example I'd add multiple edge colliders to each side of the shape and check that they had all fired an OnTriggerEnter event.
Your answer
