JavaFX - 旋转我的立方体会将其移出相机,如何防止这种情况发生?
JavaFX- Rotating my cube moves it off camera, how do I prevent this from happening?
不久前我才开始用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);
}
}
所以基本上我的模型在旋转,但也在离开屏幕。我希望它留在原地并在设定的轴上旋转。我该怎么做
这是一个绕 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,您往往会得到更少的帮助。例如,根据您提供的代码,我无法准确地告诉您您在实施过程中做错了什么。
不久前我才开始用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);
}
}
所以基本上我的模型在旋转,但也在离开屏幕。我希望它留在原地并在设定的轴上旋转。我该怎么做
这是一个绕 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,您往往会得到更少的帮助。例如,根据您提供的代码,我无法准确地告诉您您在实施过程中做错了什么。