title: JavaFX: How can I rotate a 3D-box 90-degrees with the keyboard, and then save and load the rotation using JSON?
body: <p>I have a 'javafx.scene.shape.Box' with a 'javafx.scene.shape.MeshView' shown at it's coordinates. </p>
  
  <p>I want to rotate this box 90-degrees using the keyboard (perhaps WASDQE, WS, QE and AD for the different axises). I also want to save the box-rotation to JSON and then be able to set that rotation again.</p>
  
  <p>What I've tried (the box- and meshview-properties are bound):</p>
  
  <pre><code>box.setRotate(box.getRotate + angle); // angle being +-90 and reset on &gt; 360 or &lt; -360
  box.setRotationAxis(Rotate.X_AXIS); // Axis depending on key pressed
  
  
  </code></pre>
  
  <p>This approach works for the current axis, and it works to save and load using JSON since I can set/get this value. However, with this, it seems I can not rotate all the angles, because when I change rotation-axis it forgets the previous rotation and starts from the 'default'/'starting'-rotation.</p>
  
  <p>I've also tried doing it like below:</p>
  
  <pre><code>this.setRotationAxis(this.getRotationAxis().normalize().add(Rotate.X_AXIS));
  
  </code></pre>
  
  <p>This approach results with the box rotating on multiple axis, in other words, it seems to remember previous axises, but it does not rotate the axis I just added by the angle given. The box ends up tilted.</p>
  
  <p>The last thing I've tried was to add the rotation as a transform:</p>
  
  <pre><code>
  this.getTransforms().add(new Rotate(angle, Rotate.X_AXIS));
  
  (
  also tried this with some code I found that did this with inverseDeltaTransform (where node being the box):
  
  private void addRotate(Node node, Point3D rotationAxis, double angle) {
        ObservableList&lt;Transform&gt; transforms = node.getTransforms();
        try {
            for (Transform t : transforms) {
               rotationAxis = t.inverseDeltaTransform(rotationAxis);
            }
        } catch (NonInvertibleTransformException ex) {
            throw new IllegalStateException(ex);
        }
            transforms.add(new Rotate(angle, rotationAxis));
       }
  
  )
  
  </code></pre>
  
  <p>This approach, however, leaves me with two problems.</p>
  
  <p>Problem 1:
  If i render the box, it rotates just fine, but if I render the meshview, it seems to rotate around a pivot rather then to rotate on the spot.</p>
  
  <p>Problem 2:
  If i add the rotation through transforms, how would I save the current rotation to JSON and then apply it again when I load it?
  box.getRotationAxis() and box.getRotate() will now just show the default-values as if the box is never rotated and box.getTransforms() does not seem to make this easy.</p>
  
  <p>Another question about the transforms:
  The list of transforms is building up with every rotation, would I need to clear this list to avoid memory-issues and would clearing the list affect the current rotation?</p>