I wanted to update this thread again for anyone who finds it or is having trouble completing their own slide puzzle. I’ve lifted the Slide Puzzle setup I have in my game and ported it into an isolated project with some of the bells and whistles missing, but core functionality is there. This doesn’t use any line tracing, instead it just compares vectors in relative space.
You can download the project files, add to your Unreal Projects Folder, and take a look at all the Blueprints/Actors in the Slider_Puzzle folder:
Demo notes:
- The blueprints are mostly commented as it goes through logic/functions. Some of the comments may have been lost in the porting process, so if anything is unclear leave a comment here!
- In the demo map, you can see the Slider_Puzzle_AutoGenerate (the puzzle itself) and a Slide_Puzzle_Controller_New (the controller trigger). Note that on the Puzzle_Controller, it has a variable that has been set to reference the Slider_Puzzle in level. This is how the player controls communicate with the Slider Puzzle actor.
- Select the Grid Size (2x2, 3x3, 4x4, 5x5) on the Slider Puzzle_Auto_Generate and it will spawn the blocks automatically. Be sure to also set the appropriate texture so the numbers match up properly on the tiles. You can add your own Textures/Photos and it will splice them up automatically. I included a sample texture photo of my hometown to demonstrate.
- At game start, it will randomize the blocks and cut up the assigned texture. Refer to the Array of Structs 2x2UVData, 3x3UVData, etc. and the Puzzle Master Material for how this is achieved.
- The randomized blocks are checked for parity (I found this here https://www.cs.bham.ac.uk/~mdr/teaching/modules04/java2/TilesSolvability.html by brilliant Mark Ryan). If an unsolveable puzzle is generated, it will re-randomized the blocks and check again. This is honestly the most important element of this whole thing, otherwise you can end up with broken puzzles. Look at the CheckInversions function in the Puzzle actor for this.
- Since the blocks spawn from the bottom up, the solution is numbers go bottom to top, left to right (i.e 1 in the bottom left corner, missing block in the top right corner).
Give it a whirl and let me know what you think! It’s not fancy but it’s got all the basic logic involved in creating your own puzzles!