繁体   English   中英

3D 使用鼠标在 JavaFX 中移动相机

[英]3D Camera movement in JavaFX using mouse

我已经设法使用鼠标拖动来移动相机。 但是,问题是一旦我移动了相机并松开鼠标,相机就会返回原点。

我希望相机保留在更改后的 position 中,这样当我再次使用鼠标移动相机时,它会从 position 移动到任何地方而不是从原点移动。

package application;
    
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Sphere;


public class Main extends Application {
    
    private static final int WIDTH = 900;
    private static final int HEIGHT = 600;  

    //Tracks drag starting point for x and y
    private double anchorX, anchorY;
    
    @Override
    public void start(Stage primaryStage) {
        Sphere sphere = new Sphere(50);
        
        Group group = new Group();
        group.getChildren().add(sphere);
     
        Camera camera = new PerspectiveCamera();
        Scene scene = new Scene(group, WIDTH, HEIGHT);
        scene.setFill(Color.SILVER);
        scene.setCamera(camera);
     
        sphere.setTranslateX(WIDTH / 2);
        sphere.setTranslateY(HEIGHT / 2);
        
        initMouseControl(scene, camera, primaryStage, sphere);
     
        primaryStage.setTitle("Solar System Simulator");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    private void initMouseControl(Scene scene, Camera camera, Stage stage, Sphere sphere) {

           scene.setOnMousePressed(event -> {
                //Save start points
                anchorX = event.getSceneX();
                anchorY = event.getSceneY();
           });
         
           scene.setOnMouseDragged(event -> {
                camera.setTranslateY(anchorY - event.getSceneY());
                camera.setTranslateX(anchorX - event.getSceneX());
           });
           
           stage.addEventHandler(ScrollEvent.SCROLL, event -> {
               sphere.setTranslateZ(sphere.getTranslateZ() + event.getDeltaY());
           });
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}

我尝试使用谷歌搜索来寻找解决方案,但在 javafx 的相机移动中找不到太多

我不知道 Google 能在多大程度上帮助您解决逻辑问题。 我没有进一步分析您的代码,但至少每次按下鼠标按钮时,您的相机 position 都会按设计重置为零。 您的更改不是累积的。

首先,您的场景没有启用深度缓冲区。 这些构造函数在场景实例Scene(Parent root,double width,double height,boolean depthBuffer)Scene(Parent root,double width,double height,boolean depthBuffer,SceneAntialiasing antiAliasing)中启用 884285388 功能,depthBufer boolean 设置为 true。 第二,如果你想翻译一个节点; 翻译它得到它的当前坐标,然后添加新的坐标。 这将避免重置坐标。 我将拖动的结果除以 10 因为它带来的值太高了

    public class Main extends Application {

    private static final int WIDTH = 900;
    private static final int HEIGHT = 600;

    //Tracks drag starting point for x and y
    private double anchorX, anchorY;

    @Override
    public void start(Stage primaryStage) {
        Sphere sphere = new Sphere(50);

        Group group = new Group();
        group.getChildren().add(sphere);

        Camera camera = new PerspectiveCamera();
        // for 3d  Scene constructor must have zbuffer= true and SceneAntialiasing
        Scene scene = new Scene(group, WIDTH, HEIGHT, true, SceneAntialiasing.BALANCED);
        scene.setFill(Color.SILVER);
        scene.setCamera(camera);

        sphere.setTranslateX(WIDTH / 2);
        sphere.setTranslateY(HEIGHT / 2);

        initMouseControl(scene, camera, primaryStage, sphere);

        primaryStage.setTitle("Solar System Simulator");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void initMouseControl(Scene scene, Camera camera, Stage stage, Sphere sphere) {

        scene.setOnMousePressed(event -> {
            //Save start points
            anchorX = event.getSceneX();
            anchorY = event.getSceneY();
        });
        // translating camera from its current coords pluss event 
        scene.setOnMouseDragged(event -> {
            camera.setTranslateY(camera.getTranslateY() + ((anchorY - event.getSceneY()) / 10));
            camera.setTranslateX(camera.getTranslateX() + ((anchorX - event.getSceneX()) / 10));
        });

        stage.addEventHandler(ScrollEvent.SCROLL, event -> {
            sphere.setTranslateZ(sphere.getTranslateZ() + event.getDeltaY());
        });
    }

    public static void main(String[] args) {
        launch(args);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM