1

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.

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.

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).

here is the code that deals with the above text:

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) -> {
            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) -> {
            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);
    }

}

the image I used is: enter image description here

José Pereda
  • 44,311
  • 7
  • 104
  • 132
Tom
  • 343
  • 3
  • 12
  • This doesn’t compile, as there is no import statement for CuboidMesh. Also, it would be useful if you made `allFacesv2.png` available. – VGR Dec 05 '16 at 01:21
  • implementation is from: https://github.com/Birdasaur/FXyz-DeepSpaceBranch – Tom Dec 05 '16 at 08:16
  • 1
    If you rotate 360º, you will always end up looking at the same face, won't you? – José Pereda Dec 05 '16 at 13:46
  • @Tom You should use the official FXyz3D.org repo. My repo holds changes I make to test wacky stuff before rolling it into the main branch. – Birdasaur Mar 15 '17 at 20:02

0 Answers0