JavaFX:编写一个鼠标摄像头:无法围绕自身旋转

时间:2018-04-04 15:07:37

标签: camera javafx-8

我一直试图在JavaFX上编写一个mouselook相机...

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.PointLight;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class Example extends Application {
    private static Sphere sphere, sphere2, sphere3, sphere4;
    private static PerspectiveCamera camera;

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

    @Override
    public void start(Stage stage) throws Exception {
        // locate random objects
        sphere = new Sphere( 150 );

        sphere.setTranslateX( 0 );
        sphere.setTranslateY( 0 );
        sphere.setTranslateZ( 0 );

        sphere.setMaterial( new PhongMaterial( Color.RED ) );


        sphere2  = new Sphere( 150 );

        sphere2.setTranslateX( 0 );
        sphere2.setTranslateY( 0 );
        sphere2.setTranslateZ( 500 );

        sphere2.setMaterial( new PhongMaterial( Color.GREEN ) );

        sphere3  = new Sphere( 150 );

        sphere3.setTranslateX( 500 );
        sphere3.setTranslateY( 0 );
        sphere3.setTranslateZ( 0 );

        sphere3.setMaterial( new PhongMaterial( Color.BLUE ) );


        sphere4  = new Sphere( 150 );

        sphere4.setTranslateX( 500 );
        sphere4.setTranslateY( 0 );
        sphere4.setTranslateZ( 500 );

        sphere4.setMaterial( new PhongMaterial( Color.YELLOW ) );

        // locate the camera
        camera = new PerspectiveCamera( false );
        camera.setTranslateX( 250 );
        camera.setTranslateY( -200 );
        camera.setTranslateZ( 250 );


        // create a font of light
        PointLight light = new PointLight();

        light.setTranslateX( 250 );
        light.setTranslateY( 200 );
        light.setTranslateZ( 0 );

        Group root = new Group( sphere, sphere2, sphere3, sphere4,  light );
        Scene scene = new Scene( root, 800, 600, true);

        scene.setFill(Color.SKYBLUE);
        scene.setCamera( camera );
        scene.setOnMouseMoved( new MouseLook() );

        stage.setScene(scene);
        stage.setTitle("Mouselook rotation");
        stage.show();
    }

    static class MouseLook implements EventHandler <MouseEvent> {
        private static Rotate rotation;
        private static int oldX, newX;
        private static boolean alreadyMoved = false;

        @Override
        public void handle(MouseEvent event) {
            if ( alreadyMoved ) {
                newX = (int) event.getScreenX();

                if ( oldX < newX ) { // if mouse moved to right
                    rotation = new Rotate( 10.0,
                            // camera rotates around its location
                            camera.getTranslateX(), camera.getTranslateY(), camera.getTranslateZ(),
                            Rotate.Y_AXIS );


                } else if ( oldX > newX ) { // if mouse moved to left
                    rotation = new Rotate( -10.0,
                            // camera rotates around its location
                            camera.getTranslateX(), camera.getTranslateY(), camera.getTranslateZ(),
                            Rotate.Y_AXIS );

                }
                camera.getTransforms().addAll( rotation );

                oldX = newX;
            } else {
                oldX = (int) event.getScreenX();
                alreadyMoved = true;
            }
        }
    }
}

问题是我使用相机位置作为枢轴,但它会围绕场景的4个球体中的一个进行平移。

Camera movement

我已经尝试将相机位置设置为枢轴点,但它与我使用的值无关,似乎枢轴永远不会改变。

1 个答案:

答案 0 :(得分:0)

谢谢您的提问。我也想在javafx中实现鼠标外观。我玩过您的代码,这对我有用。我将PerspectiveCamera更改为true,并使用了setNearClip的{​​{1}}和setFarClip属性,以确保可以看到球体。

Camera