lostluke
lostluke

Reputation: 165

Three.js Cube - Spin to random side

I have a Three.js Cube with six differnet side.

I have wrote a random number generator shich generates a number between 1-6. On a button click I want to rotate the cube to the side(number).

The fiddle from Stallion(http://jsfiddle.net/majman/6vrH8/) looks nice, but don't work for me because i don't know how much i have to roate the cube to a specific side....

My try was this:

   mesh.rotation.x =0;
     mesh.rotation.y =0;
    //face2
     mesh.rotation.x =0;
     mesh.rotation.y =1.571;
    //face3
     mesh.rotation.x =0;
     mesh.rotation.y =3.142;
     ...
     mesh.rotation.x =0;
     mesh.rotation.y =4.714;
     ...
     mesh.rotation.x =1.571;
     mesh.rotation.y =0;
     ...
     mesh.rotation.x =-1.571;
     mesh.rotation.y =0;

Then i wanted to += or -= the rotation to the above numbers. The problem is that after a few rounds mesh.rotation.x/y is sooo high (100+) that it needs to long to go to the faces.

What can i do? Here's the fiddle: https://jsfiddle.net/crinca15/2o8n3xox/

Update: I think there are misunderstandings... On click on Start-Button the cube starts rotating. On the stop-button the cube should go to a side from 1-6 (random). The face-postions i have written above.

Now i can use for (mesh.rotation.x == wanted.rotation.x; mesh.rotation.x -= 0.02)

The problem is that the cube needs along time and alot rotations to spin to the postition if the mesh.rotation-value is very high...

Upvotes: 1

Views: 478

Answers (1)

ManoDestra
ManoDestra

Reputation: 6503

Looks correct according to this, but it sounds like you're calling this code repeatedly in a loop, if the values keep increasing. Make sure that you only apply the rotations a SINGLE time, when required.

And also ensure that your x and y values remain in the range 0 <= value < Math.PI * 2.

Upvotes: 1

Related Questions