【问题标题】:JavaFX- Rotating my cube moves it off camera, how do I prevent this from happening?JavaFX-旋转我的立方体将其移出相机,如何防止这种情况发生?
【发布时间】:2021-04-10 12:41:43
【问题描述】:

不久前我刚刚开始使用 javaFX 学习 3D 图形,我目前正在研究旋转模型。虽然它确实旋转,但它的旋转方式并不完全符合我的想法......例如,如果我按下键向左旋转,模型在旋转时向屏幕左侧移动并最终消失屏幕。

下面是一些构建盒子和场景的代码:

   Box box = new Box(100,20,50);
   SmartGroup group = new SmartGroup();
     group.getChildren().add(box);
 
 Camera camera = new PerspectiveCamera();
 
Scene scene = new Scene(group,WIDTH,HEIGHT);
 scene.setFill(Color.SILVER);
 scene.setCamera(camera);
    
 box.translateXProperty().set(WIDTH/2);
 box.translateYProperty().set(HEIGHT/2);
 box.translateZProperty().set(-1000); 

//Here's the wrapper class I made to make the box move:

   class SmartGroup extends Group{
Rotate r;
Transform t = new Rotate();
void rotateByX(double ang){
r = new Rotate(ang, Rotate.X_AXIS);
t = t.createConcatenation(r);
this.getTransforms().clear();
this.getTransforms().addAll(t);
}
void rotateByY(double ang){
r = new Rotate(ang, Rotate.Y_AXIS);
t = t.createConcatenation(r);
this.getTransforms().clear();
this.getTransforms().addAll(t);
}
void rotateByZ(double ang){
r = new Rotate(ang, Rotate.Z_AXIS);
t = t.createConcatenation(r);
this.getTransforms().clear();
this.getTransforms().addAll(t);
}
} 

所以基本上我的模型在旋转,但也在屏幕外移动。我希望它保持原位并在设定的轴上旋转。我该怎么做

【问题讨论】:

    标签: java javafx rotation


    【解决方案1】:

    这是一个围绕 Y 轴旋转框的示例。

    该示例使用RotateTransition 进行旋转。

    旋转过渡应用于被旋转的盒子,而不是封闭的组。

    还有其他实现旋转的方法,例如您可以手动创建变换并将它们应用到动画计时器或时间轴中。

    一般来说,如果您尝试在 3D 中旋转某个物体,但它不是围绕其中心旋转,而是围绕某个较远的原点旋转,它看起来像是在远离或朝向相机移动,因为它没有围绕其旋转居中,而是在其他地方。

    要解决这个问题,您可以应用一系列变换,一个将对象平移到原点 (0,0,0),然后第二个实际旋转对象,最后第三个平移将物体旋转回原来的位置。也可以应用类似的技术来缩放对象。

    我没有做所有这些,因为我可以使用更高级别的RotateTransition 来围绕穿过对象中心的 Y 轴旋转对象。 RotateTransition 为您处理所有必要的转换,因此您无需担心制定和应用它们。但是,在其他一些使用模型中,可以使用使用多个变换的替代方法。

    import javafx.animation.Interpolator;
    import javafx.animation.RotateTransition;
    import javafx.animation.Transition;
    import javafx.application.Application;
    import javafx.scene.*;
    import javafx.scene.paint.Color;
    import javafx.scene.paint.PhongMaterial;
    import javafx.scene.shape.Box;
    import javafx.scene.transform.Rotate;
    import javafx.scene.transform.Translate;
    import javafx.stage.Stage;
    import javafx.util.Duration;
    
    public class RotatingBoxApp extends Application {
    
        public Parent createContent() {
            // Box
            Box testBox = new Box(5, 5, 5);
            testBox.setMaterial(new PhongMaterial(Color.LIMEGREEN));
    
            // Create and position camera
            PerspectiveCamera camera = new PerspectiveCamera(true);
            camera.getTransforms().addAll(
                    new Rotate(-20, Rotate.Y_AXIS),
                    new Rotate(-20, Rotate.X_AXIS),
                    new Translate(0, 0, -20)
            );
    
            // Build the Scene Graph
            Group root = new Group();
            root.getChildren().add(camera);
            root.getChildren().add(testBox);
    
            RotateTransition rotator = new RotateTransition(Duration.seconds(5), testBox);
            rotator.setAxis(Rotate.Y_AXIS);
            rotator.setFromAngle(0);
            rotator.setToAngle(360);
            rotator.setCycleCount(Transition.INDEFINITE);
            rotator.setInterpolator(Interpolator.LINEAR);
            rotator.play();
    
            // Use a SubScene       
            SubScene subScene = new SubScene(root, 300, 300);
            subScene.setFill(Color.web("#4a4f59"));
            subScene.setCamera(camera);
            Group group = new Group();
            group.getChildren().add(subScene);
    
            return group;
        }
    
        @Override
        public void start(Stage primaryStage) {
            primaryStage.setResizable(false);
            Scene scene = new Scene(createContent());
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

    一般来说,如果您需要进一步帮助解决此类问题,您通常需要提供mcve(有人可以复制和粘贴以原样运行并重现问题的最少代码)。如果没有 mcve,您往往会得到更少的帮助。例如,从您提供的代码中,我无法真正告诉您您在实现中做错了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2017-04-06
      • 2017-10-15
      • 1970-01-01
      相关资源
      最近更新 更多