# Rotational help needed!

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:

1. 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.
2. 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?

Thanks for your time and help,
Ian

Hi ianMac,

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:

https://playcanvas.com/will/rubikscube

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:

https://playcanvas.com/will/rubikscube/editor/face.js

It rotates the face left or right using the left and right arrow keys. If you let go, the face rotates to the nearest multiple of 90 degrees.

As for number 2, I’m not sure you’re thinking about the problem in the right way here. What exactly are you trying to do?

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.

Ian

Hi Will,

here’s a link to the project - http://playcanvas.com/ianmac/KeyboardTutorial/designer/pack/d0eeb1e2-0b88-11e4-b012-22000a4a0339

Any help would be appreciated.

Thanks,

Ian

Hi Ian,

That youtube video is private so I can view it unfortunately.

Great video Ian! If only every support query was so masterfully put together. We will solve this for you today!

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.

Thanks. Now just need to get my head 'round it!

Ian

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.