Rubics cube
A Rubik’s cube scripting example
I have recently came to an idea of a Rubic’s cube as a nice scripting example.
Rubik® and Rubik’s Cube® are registered trademarks throughout the world of Seven Towns Limited. Seven Towns Limited is the exclusive licensee of copyright in the Rubik’s Cube puzzle. This post regards to Unity3d cube mechanism scripting example, not the game itself.
My idea of scripting the cube movement is based on selecting the relevant rows of cubes, placing them in rotation container, rotating container and putting it back from the container to the cube.
In my example I made use of the fact that the same script can run cubes of different sizes and you can also set it here to any integer starting from 2.
People cope with with Rubic’s with at various levels of ease and the script lets them to train on small sizes to slowly work out the algoritmh and then move to bigger sizes.
After setting the size you need to reset the cube in order to apply the new size.
You can adjust the view of the cube by dragging the mouse over the application window and scrolling the mouse in order to zoom in/out.
All the meshes are created dynamically and uv-mapped, and there is a possibility to add to the difficulty level by texturing the individual tiles or the whole sides of the cube. When the tiles are individually mappped (with arrows in this case), then – for the inside tiles – not only the color but also the rotation matters. When the sides are mappped with picture, then – for the inside tiles – not only the color and rotation, but also the strict position matters.
The settings and textures can be customized by image and xml data held in separate data folder.
In the web version there are also two php scripts for communication with mysql databases for reading and storing the scores.
In the standalone versions the scores are stored on the local computer in PlayerPrefs.
The above download is obviously targeted at Unity3d enthusiast interested in this game setup. In order to try to play the game just click the picture on the top so that opens the link to the WebPlayer or download any of the standalone versions below:
The deployments contains a customizable xml file pointing to the texture. By the modification of this you can easily put your own logo or custom graphics on the cube.
I have also utilised an interesting Unity3d ability to pick a localization based on the user’s system language with the Application.systemLanguage method. The default GUI language is English, but on the detection of Polish, the application reads that from the attached XML file.
The script could of course be also applied to other geometric regular and non-regular forms like pyramids.
This is a cube mechanism script. A far more challenging would be to create a general solving algoritmh for n-cube, that would be able to find the optimal solution in minimal number of steps. I have found some considerations on that subject here: http://kociemba.org/cube.htm
and there: http://www.wrongway.org
25 Comments
Leave a Reply
You must be logged in to post a comment.
I tried to modify your project, but I get the problem, in the meshes I do not find the right design, you can share files blender or its untity complete ..? thank you
And that is right, you are not supposed to, because the meshes are procedural and are generated inside the code. In other words, they are built triangle by triangle, as described here:
http://docs.unity3d.com/Documentation/ScriptReference/Mesh-triangles.html
That way the only limit to the size – whether 2x2x2 or 10x10x10 or anything is the machine’s performance.
mmmm… i already try that but doesn’t working, for colour it’s okay but for number i don’t think it is working…?? i try modify your cube to be sudokube (numeric cube).. i’ve working many times but it doesn’t working… any advice…??
You are right, I have just opened that and made some settings in the inspector, because it didn’t work they way I intended. And I have reuploaded again, so check again and let me know. Basicaly it should work straightaway once you open the scene and enter the play mode.
really …?? are you reaupload again..?? yap.. i’m still try modified your project.. that’s why i’m asking “do you have blender file” because it’s dificult too find the right meshes… i’m still try to modified your project.. your project helping me a lot.. thank you… 😀
Yes the upload is updated now. So when you start the playmode, the default playable 3x3x3 cube should appear. But don’t look for any meshes there, because there aren’t any because the mesh is procedurally generated so can only appear at runtime or playmode.
Chà o Phú, sau khi há»c xog 2 năm tại FPT Poeitlchnyc, em có thể liên thông những trÆ°á»ng Äại há»c có cùng ngà nh và nháºn tuyển liên thông em nhé.
yap… i know already about meshes it was procedurally generated so can only appear at runtime or playmode…. mmm are you sure about reupload the new file..?? the file, it’s only 1.2 MB but when i open the file it’s only 0 KB…. file download “Download Unity3d package (40387) |uploaded 2012-12-16” is it right.?? i’m sorry if i made a mistake..
Yes, I now am sure, because I’ve tested that.
I have Unity 4.2 Pro but this should work the same in the free version. I’ve created a new project, then imported this as the custom package, opened the included scene, entered the playmode and the 3x3x3 cube has appeared!. No, wait a minute, still there is something wrong with it.
I’m sorry if I made a mistake.. link download is active.. but when the file is downloaded, file is corrupted… i try download it bunch of times… but the it’s still same i can’t open it… my unity version is 4.1.2 but thinks ther is no problem about that…
I suggested to try another server.. like google drive, dropbox or mediafire to reupload the file… really i can download it… file is corrupted.. thanks before.. 🙂
Something went wrong on some WordPress or WordPress plugins update, but looks like I have fixed that, and should work fine again.
I think there is no difference between the first and the new one…? maybe main camera always move when mouse is moved…
I wanna ask you something:
1. if you wanna put some number 1 to 9 in some cube, what vector you want to change to put that number.?
2. for algorithm, what do you suggested as solver..?? I’ve been try genetic algoritm and that’s quite good as solver.
1. There is a tree texture there – replace that with your own numbers texture and you will have numbers.
2. There is no solver there, just a function that checks if the cube is solved after each move, but I know there are cube solving algorithms and not much more – to be frank.
Can I just say what a comfort to find an individual who
truly knows what they’re talking about over the internet. You definitely
know how to bring an issue to light and make it important.
More and more people should look at this and understand this side of your story.
I was surprised you are not more popular since you surely have the gift.
How can I change the mouse control to swipe control sir?
Thank you.
Hello blogger, i must say you have very interesting posts here.
Your website should go viral. You need initial traffic only.
How to get it? Search for; Mertiso’s tips go viral
I’m seeing Purple Triangles in Unity3d 2017 – maybe a mesh disappeared?
Is it possible to amend the License on the scripts? I am willing to but the license for commercial purposes. Also, JavaScript will soon be obsolete in Unity – do you mind me making a C# version? A-lot of the features are broken in the newer engines. Would I be under the same License if I fixed and Ported this?
Thank in advance.
Purple triangles – mean a material/shader problem. I wouldn’t mind. It would also be great to make it to work on touches. Possibly If I get some time (and motivation) – I would get back to it sometime.
Thanks! – I will link to your site. I have finished the conversion to C#… it was a long day. I solved the problem – had to add the shader in the Graphics settings for standalone. Now I’m trying to optimize for mobile.
Good. I agree.
To be clear, do we have the equivalent to an MIT – License with regards to the Rubiks Cube code?
Sorry I replied to myself:
To be clear, do we have the equivalent to an MIT – License with regards to the Rubiks Cube code?
I am not sure what you mean.
But I have assumed at the time at publishing it that Rubiks Cube is one thing – and I lave put the MIT licence hare – and the Rubiks Cube game idea itself is the second thing, which I have not sorted out – in fact. But I have seen a number of Rubiks cube apps on the web, and haven’t seen any licensing references there.
Okay, so the cube javascript is MIT? I didn’t see it but thanks for the update!