繁体   English   中英

JavaFX-旋转我的立方体将其移出相机,如何防止这种情况发生?

[英]JavaFX- Rotating my cube moves it off camera, how do I prevent this from happening?

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

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

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

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

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

该示例使用RotateTransition进行旋转。

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

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

通常,如果您尝试在 3D 中旋转某物并且它不是围绕其中心旋转而是围绕某个远处的原点旋转,它看起来就像是在远离或远离相机移动,因为它不是围绕其中心旋转但是,相反,在其他地方。

要解决这个问题,您可以应用一系列转换,一个将 object 转换为原点 (0,0,0),然后再实际旋转 object,最后第三个转换旋转后的 ZA8CFDE6331BD49EB2AC96F8911到之前的 position。 类似的技术也可以应用于缩放 object。

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

qube1 qube2

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,您往往会得到更少的帮助。 例如,从您提供的代码中,我无法准确地告诉您您在实现中做错了什么。

暂无
暂无

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

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