Hi, I’m learning PlayCanvas by building (trying to build) a Rubik’s Cube type of game - very simple stuff.
I have 2 things I can’t figure out:
If I rotate a cube and release the key I want the cube to snap to the nearest 90 degrees…either back to where it started or forward to next 90 degrees.
At the start, a cube’s orientation is in agreement the world orientation. After a 90 degree rotation the local/world orientations differ. I need to reset the local axes at this point so the local axes new orientation becomes the start orientation - ie the local x,y,z axes are in agreement with the world x,y,z. Is there a simple way to do this?
Let’s start with number 1. If you think about a Rubik’s Cube, the only moving bits that matter are the center squares on each face. The surrounding cubes just move with the rotating center face. So I have modelled a Rubik’s Cube here:
But for now, I’ve ignored the edge cubes. If you look at the structure of the entity hierarchy, you’ll see I’ve arranged each face center cube to have a local y axis pointing outwards. This means that I can apply a local y rotation on any of those cubes and they will rotate correctly. The script that rotates one of the faces (the red one) is here:
Thanks Will.
I’ll have a look through the scripts over the next couple of weeks.
(Sadly I’m back at work tomorrow so don’t have as much time!)
thanks again.
Hi Ian,
Does this help: https://playcanvas.com/will/cubespin
So this turns out to be a non-trivial problem. Although I have a solution with 150 lines of script, it was a bit tricky to write.
The important bit of code is here: https://playcanvas.com/will/cubespin/editor/game.js
The main error in your code was that you were rotating around the local axes. You actually should have been rotating around the world coordinate system axes. In my code, I’m using the ‘rotate’ function which does rotate around the world X, Y and Z axes.
Hope this helps.
Well, if there’s anything confusing there, let me know. I’ve just added a cube highlighter that displays the selected row. Clicking a cube twice switches between column and row.