Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaFX:旋转对象而不旋转轴_Javafx - Fatal编程技术网

JavaFX:旋转对象而不旋转轴

JavaFX:旋转对象而不旋转轴,javafx,Javafx,为了进行Rybi项目的立方体游戏,我必须围绕特定的轴旋转立方体,所以问题是如果我围绕一个轴旋转一个对象,例如Y轴90度,那么如果我再次围绕x轴旋转,旋转将沿着Z轴的方向,因为x轴的方向是Z 下面,有一段代码演示了我刚才向您介绍的相同情况 有没有办法按照我的愿望做事 public class RybiCube extends Application { final Group root = new Group(); final PerspectiveCamera camera =

为了进行Rybi项目的立方体游戏,我必须围绕特定的轴旋转立方体,所以问题是如果我围绕一个轴旋转一个对象,例如Y轴90度,那么如果我再次围绕x轴旋转,旋转将沿着Z轴的方向,因为x轴的方向是Z

下面,有一段代码演示了我刚才向您介绍的相同情况

有没有办法按照我的愿望做事

public class RybiCube extends Application {

    final Group root = new Group();
    final PerspectiveCamera camera = new PerspectiveCamera(true);
    final XformCamera cameraXform = new XformCamera();
    public static final double CAMERA_INITIAL_DISTANCE = -1000;
    public static final double CAMERA_NEAR_CLIP = 0.1;
    public static final double CAMERA_FAR_CLIP = 10000.0;

    public void init(Stage primaryStage) {

        Box box = new Box();
        box.setHeight(70);
        box.setWidth(200);
        box.setDepth(70);
        //box.setRotationAxis(Rotate.Y_AXIS);
        //box.setRotate(80);
        box.getTransforms().add(new Rotate(90,Rotate.Y_AXIS));
        box.getTransforms().add(new Rotate(45,Rotate.X_AXIS));


        PhongMaterial material = new PhongMaterial();
        material.setDiffuseColor(Color.ORANGE);
        material.setSpecularColor(Color.BLACK);
        box.setMaterial(material);

        root.getChildren().add(box);
        buildCamera();
        Scene scene = new Scene(root, 600, 600, true);
        primaryStage.setScene(scene);
        scene.setCamera(camera);
        ///
        primaryStage.setResizable(false);
        scene.setFill(Color.rgb(0, 0,0,0.5));
        primaryStage.setScene(scene);        
    }

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

    public void start(Stage primaryStage)
    {
        init(primaryStage);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}
class XformCamera extends Group {

    final Translate tr = new Translate(0.0, 0.0, 0.0);
    final Rotate rx = new Rotate(00, Rotate.X_AXIS);
    final Rotate ry = new Rotate(10, Rotate.Y_AXIS);
    final Rotate rz = new Rotate(0, Rotate.Z_AXIS);

    public XformCamera() {
        super();
        this.getTransforms().addAll(tr, rx, ry, rz);
    }

}

您只需将逆变换应用于轴(使用
inverseDeltaTransform
):


您只需将逆变换应用于轴(使用
inverseDeltaTransform
):


以下代码可能对您的应用程序有用,它基于三维方向

import javafx.scene.Group;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class XformOrientation extends Group {

    private static final int SIZE = 5;
    private Affine affine;

    private final List<Double> averageRoll = new ArrayList();
    private final List<Double> averagePitch = new ArrayList();
    private final List<Double> averageYaw = new ArrayList();

    public XformOrientation() {
        super();
        affine = new Affine();
        this.getTransforms().add(affine);
    }

    /**
     * angles in degrees
     * @param roll
     * @param pitch
     * @param yaw
     */
    public void processEvent(double roll, double pitch, double yaw) {
        double avYaw = average(averageYaw, Math.toRadians(yaw));
        double avPitch = average(averagePitch, Math.toRadians(pitch));
        double avRoll = average(averageRoll, Math.toRadians(roll));
        matrixRotateNode(avRoll, avPitch, avYaw);
    }

    private void matrixRotateNode(double roll, double pitch, double yaw) {
        double mxx = Math.cos(pitch) * Math.cos(yaw);
        double mxy = Math.cos(roll) * Math.sin(pitch) +
                Math.cos(pitch) * Math.sin(roll) * Math.sin(yaw);
        double mxz = Math.sin(pitch) * Math.sin(roll) -
                Math.cos(pitch) * Math.cos(roll) * Math.sin(yaw);
        double myx = -Math.cos(yaw) * Math.sin(pitch);
        double myy = Math.cos(pitch) * Math.cos(roll) -
                Math.sin(pitch) * Math.sin(roll) * Math.sin(yaw);
        double myz = Math.cos(pitch) * Math.sin(roll) +
                Math.cos(roll) * Math.sin(pitch) * Math.sin(yaw);
        double mzx = Math.sin(yaw);
        double mzy = -Math.cos(yaw) * Math.sin(roll);
        double mzz = Math.cos(roll) * Math.cos(yaw);

        affine.setToTransform(mxx, mxy, mxz, 0,
                myx, myy, myz, 0,
                mzx, mzy, mzz, 0);
    }

    private double average(List<Double> list, double value) {
        while (list.size() > SIZE) {
            list.remove(0);
        }
        list.add(value);

        return list.stream()
                .collect(Collectors.averagingDouble(d -> d));
    }

}

以下代码可能对您的应用程序有用,它基于三维方向

import javafx.scene.Group;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class XformOrientation extends Group {

    private static final int SIZE = 5;
    private Affine affine;

    private final List<Double> averageRoll = new ArrayList();
    private final List<Double> averagePitch = new ArrayList();
    private final List<Double> averageYaw = new ArrayList();

    public XformOrientation() {
        super();
        affine = new Affine();
        this.getTransforms().add(affine);
    }

    /**
     * angles in degrees
     * @param roll
     * @param pitch
     * @param yaw
     */
    public void processEvent(double roll, double pitch, double yaw) {
        double avYaw = average(averageYaw, Math.toRadians(yaw));
        double avPitch = average(averagePitch, Math.toRadians(pitch));
        double avRoll = average(averageRoll, Math.toRadians(roll));
        matrixRotateNode(avRoll, avPitch, avYaw);
    }

    private void matrixRotateNode(double roll, double pitch, double yaw) {
        double mxx = Math.cos(pitch) * Math.cos(yaw);
        double mxy = Math.cos(roll) * Math.sin(pitch) +
                Math.cos(pitch) * Math.sin(roll) * Math.sin(yaw);
        double mxz = Math.sin(pitch) * Math.sin(roll) -
                Math.cos(pitch) * Math.cos(roll) * Math.sin(yaw);
        double myx = -Math.cos(yaw) * Math.sin(pitch);
        double myy = Math.cos(pitch) * Math.cos(roll) -
                Math.sin(pitch) * Math.sin(roll) * Math.sin(yaw);
        double myz = Math.cos(pitch) * Math.sin(roll) +
                Math.cos(roll) * Math.sin(pitch) * Math.sin(yaw);
        double mzx = Math.sin(yaw);
        double mzy = -Math.cos(yaw) * Math.sin(roll);
        double mzz = Math.cos(roll) * Math.cos(yaw);

        affine.setToTransform(mxx, mxy, mxz, 0,
                myx, myy, myz, 0,
                mzx, mzy, mzz, 0);
    }

    private double average(List<Double> list, double value) {
        while (list.size() > SIZE) {
            list.remove(0);
        }
        list.add(value);

        return list.stream()
                .collect(Collectors.averagingDouble(d -> d));
    }

}
import javafx.scene.Group;
import javafx.scene.transform.Affine;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class XformOrientation extends Group {

    private static final int SIZE = 5;
    private Affine affine;

    private final List<Double> averageRoll = new ArrayList();
    private final List<Double> averagePitch = new ArrayList();
    private final List<Double> averageYaw = new ArrayList();

    public XformOrientation() {
        super();
        affine = new Affine();
        this.getTransforms().add(affine);
    }

    /**
     * angles in degrees
     * @param roll
     * @param pitch
     * @param yaw
     */
    public void processEvent(double roll, double pitch, double yaw) {
        double avYaw = average(averageYaw, Math.toRadians(yaw));
        double avPitch = average(averagePitch, Math.toRadians(pitch));
        double avRoll = average(averageRoll, Math.toRadians(roll));
        matrixRotateNode(avRoll, avPitch, avYaw);
    }

    private void matrixRotateNode(double roll, double pitch, double yaw) {
        double mxx = Math.cos(pitch) * Math.cos(yaw);
        double mxy = Math.cos(roll) * Math.sin(pitch) +
                Math.cos(pitch) * Math.sin(roll) * Math.sin(yaw);
        double mxz = Math.sin(pitch) * Math.sin(roll) -
                Math.cos(pitch) * Math.cos(roll) * Math.sin(yaw);
        double myx = -Math.cos(yaw) * Math.sin(pitch);
        double myy = Math.cos(pitch) * Math.cos(roll) -
                Math.sin(pitch) * Math.sin(roll) * Math.sin(yaw);
        double myz = Math.cos(pitch) * Math.sin(roll) +
                Math.cos(roll) * Math.sin(pitch) * Math.sin(yaw);
        double mzx = Math.sin(yaw);
        double mzy = -Math.cos(yaw) * Math.sin(roll);
        double mzz = Math.cos(roll) * Math.cos(yaw);

        affine.setToTransform(mxx, mxy, mxz, 0,
                myx, myy, myz, 0,
                mzx, mzy, mzz, 0);
    }

    private double average(List<Double> list, double value) {
        while (list.size() > SIZE) {
            list.remove(0);
        }
        list.add(value);

        return list.stream()
                .collect(Collectors.averagingDouble(d -> d));
    }

}
    private final XformOrientation world = new XformOrientation();

    PhongMaterial whiteMaterial = new PhongMaterial();
    whiteMaterial.setDiffuseColor(Color.WHITE);
    whiteMaterial.setSpecularColor(Color.LIGHTBLUE);
    Box box = new Box(400, 200, 100);
    box.setMaterial(whiteMaterial);

    world.getChildren().addAll(box);

    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            Platform.runLater(() -> {
                world.processEvent(getRoll(), getPitch(), getYaw());
            });
        }
    };
    getAhrsInfo().rollProperty().addListener((observable, oldValue, newValue) -> runnable.run());
    getAhrsInfo().pitchProperty().addListener((observable, oldValue, newValue) -> runnable.run());
    getAhrsInfo().yawProperty().addListener((observable, oldValue, newValue) -> runnable.run());