title: Creating a 3d dice cube in javafx and doing its 3D dice animation
body: <p>I've implemented a dice cube for a game I am developing with some partners and I am trying to create an animation for the cube.</p>
  
  <p>Up until now the only thing I am able to achieve is to Rotate the cube 360 degrees for a duration of 3000 mils I set in an axis pattern I randomly create.</p>
  
  <p>I however am not able to make a rotation on a Y axis and I am always landing on the same face of the cube(the number 4 for some reason).</p>
  
  <p>here is the code that deals with the above text:</p>
  
  <pre><code>package test;
  import javafx.application.Application;
  
  import java.util.Random;
  import javafx.animation.RotateTransition;
  import javafx.scene.DepthTest;
  import javafx.scene.Group;
  import javafx.scene.PerspectiveCamera;
  import javafx.scene.Scene;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.paint.Color;
  import javafx.scene.transform.Rotate;
  import javafx.scene.transform.Translate;
  import javafx.stage.Stage;
  import javafx.util.Duration;
  
  public class TrafoTest extends Application {
  
      final Group root = new Group();
      final XformWorld world1 = new XformWorld();
      final PerspectiveCamera camera = new PerspectiveCamera(true);
      final XformCamera cameraXform = new XformCamera();
      private static final double CAMERA_INITIAL_DISTANCE = -1000;
      private static final double CAMERA_NEAR_CLIP = 0.1;
      private static final double CAMERA_FAR_CLIP = 10000.0;
      double mousePosX, mousePosY, mouseOldX, mouseOldY, mouseDeltaX, mouseDeltaY;
  
      @Override
      public void start(Stage primaryStage) {
          root.getChildren().add(world1);
          CuboidMesh cuboid = new CuboidMesh(50f,50f,50f);
          cuboid.setTextureModeImage(getClass().getResource("/test/allFacesv2.png").toExternalForm());
          RotateTransition rt = new RotateTransition(Duration.millis(3000), cuboid);
          float number1;
          float number2;
          float number3;
          float minX = 1.0f;
          float maxX = 50.0f;
          Random rand = new Random();
          number1 = rand.nextFloat()* (maxX - minX) * minX;
          number2 = rand.nextFloat()* (maxX - minX) + minX;
          number3 = rand.nextFloat()* (maxX - minX) - minX;
          rt.setByAngle(360);
          rt.setAxis(new javafx.geometry.Point3D(number1,number2,number3));
         // rt.setCycleCount(2);
          //rt.setAutoReverse(true);
          rt.play();
          root.getChildren().addAll(cuboid);
          root.setDepthTest(DepthTest.ENABLE);
          buildCamera();
          Scene scene = new Scene(root, 800, 800, true);
          scene.setFill(Color.GREY);
          handleMouse(scene);
          primaryStage.setTitle("Transformationen");
          primaryStage.setScene(scene);
          primaryStage.show();
          scene.setCamera(camera);
      }
  
      private void buildCamera() {
          root.getChildren().add(cameraXform);
          cameraXform.getChildren().add(camera);
          camera.setNearClip(CAMERA_NEAR_CLIP);
          camera.setFarClip(CAMERA_FAR_CLIP);
          camera.setTranslateZ(CAMERA_INITIAL_DISTANCE);
      }
  
      private void handleMouse(Scene scene) {
          scene.setOnMousePressed((MouseEvent me) -&gt; {
              mousePosX = me.getSceneX();
              mousePosY = me.getSceneY();
              mouseOldX = me.getSceneX();
              mouseOldY = me.getSceneY();
              // this is done after clicking and the rotations are apearently
              // performed in coordinates that are NOT rotated with the camera.
              // (pls activate the two lines below for clicking)
              //cameraXform.rx.setAngle(-90.0);
              //cameraXform.ry.setAngle(180.0);
          });
          scene.setOnMouseDragged((MouseEvent me) -&gt; {
              mouseOldX = mousePosX;
              mouseOldY = mousePosY;
              mousePosX = me.getSceneX();
              mousePosY = me.getSceneY();
              mouseDeltaX = (mousePosX - mouseOldX);
              mouseDeltaY = (mousePosY - mouseOldY);
              if (me.isPrimaryButtonDown()) {
                  // this is done when the mouse is dragged and each rotation is
                  // performed in coordinates, that are rotated with the camera.            
                  cameraXform.ry.setAngle(cameraXform.ry.getAngle() + mouseDeltaX * 0.2);
                  cameraXform.rx.setAngle(cameraXform.rx.getAngle() - mouseDeltaY * 0.2);                
              }
          });
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  
  }
  
  class XformWorld extends Group {
  
      final Translate t = new Translate(0.0, 0.0, 0.0);
      final Rotate rx = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
      final Rotate ry = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
      final Rotate rz = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
  
      public XformWorld() {
          super();
          this.getTransforms().addAll(t, rx, ry, rz);
      }
  
  }
  
  class XformCamera extends Group {
  
      final Translate t = new Translate(0.0, 0.0, 0.0);
      final Rotate rx = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
      final Rotate ry = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
      final Rotate rz = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
  
      public XformCamera() {
          super();
          this.getTransforms().addAll(t, rx, ry, rz);
      }
  
  }
  </code></pre>
  
  <p>the image I used is: <a href="https://i.stack.imgur.com/DFv8z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DFv8z.png" alt="enter image description here"></a></p>