简体   繁体   English

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

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

I just started learning 3D graphics with javaFX not too long ago, I am currently working on rotating the model.不久前我刚刚开始学习 3D 图形和 javaFX,我目前正在研究旋转 model。 While it does rotate, the way that it rotates is not exactly what I had in mind...For example if I press the key to rotate left the model moves towards the left hand side of the screen while it is rotating and eventually goes off screen.虽然它确实旋转,但它的旋转方式并不完全是我的想法......例如,如果我按下键向左旋转,model 在旋转时向屏幕左侧移动并最终熄灭屏幕。

Here's some of the code to construct the box and scene:下面是一些构建盒子和场景的代码:

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

So basically my model is rotating but also moving off screen.所以基本上我的 model 正在旋转但也在屏幕外移动。 I want it to stay in place and rotate on a set axis.我希望它保持原位并在设定的轴上旋转。 How would I go about doing我会怎么做 go

Here is an example which rotates a box about the Y axis.这是一个围绕 Y 轴旋转框的示例。

The example uses a RotateTransition for the rotation.该示例使用RotateTransition进行旋转。

The rotate transition is applied on the box being rotated, not an enclosing group.旋转过渡应用于被旋转的盒子,而不是封闭的组。

There are other ways to achieve rotation, for instance you could manually create transforms and apply them in an animation timer or a timeline.还有其他实现旋转的方法,例如您可以手动创建变换并将它们应用到 animation 计时器或时间轴中。

Generally, if you are trying to rotate something in 3D and it is not rotating about its center but instead some distant origin point, it will look like it is moving away or toward from the camera, because it is not rotating around its center but, instead, around somewhere else.通常,如果您尝试在 3D 中旋转某物并且它不是围绕其中心旋转而是围绕某个远处的原点旋转,它看起来就像是在远离或远离相机移动,因为它不是围绕其中心旋转但是,相反,在其他地方。

To fix that, what you can do is apply a series of transforms, one to translate the object to an origin point (0,0,0) then a second to actually rotate the object, and finally a third to translate the rotated object back to the position it was in before.要解决这个问题,您可以应用一系列转换,一个将 object 转换为原点 (0,0,0),然后再实际旋转 object,最后第三个转换旋转后的 ZA8CFDE6331BD49EB2AC96F8911到之前的 position。 A similar technique can be applied to scale an object as well.类似的技术也可以应用于缩放 object。

I didn't do all that because I could just make use of the higher level RotateTransition to rotate the object about a Y axis running through the object's center.我没有做所有这些,因为我可以使用更高级别的RotateTransition来围绕穿过对象中心的 Y 轴旋转 object。 The RotateTransition takes care of all the necessary transforms for you, so that you don't need to worry about formulating and applying them. RotateTransition为您处理所有必要的转换,因此您无需担心制定和应用它们。 However, in some other use model, the alternate approach using multiple transforms could be used.然而,在一些其他用途 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);
    }
}

In general if you want further help with issues like this, you usually need to provide an mcve (minimal code which somebody could copy and paste to run unchanged and reproduce the issue).一般来说,如果您需要进一步帮助解决此类问题,您通常需要提供一个mcve (有人可以复制和粘贴以原样运行并重现问题的最少代码)。 Without an mcve, you will tend to get less help.如果没有 mcve,您往往会得到更少的帮助。 For example, from the code you supplied, I couldn't really tell you exactly what you are doing wrong in your implementation.例如,从您提供的代码中,我无法准确地告诉您您在实现中做错了什么。

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

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