【问题标题】:Camera doesnt rotate x axis using javafx相机不使用 javafx 旋转 x 轴
【发布时间】:2022-11-29 05:50:59
【问题描述】:

我最近一直在使用 javafx,当我这样做时,我在我的代码中发现了一个问题,但花了很多时间在上面(主要是谷歌搜索)我找不到解决我的问题的方法。

当我尝试使用 x 轴旋转相机但不是做我想要发生的事情而是它围绕 (0,0,0) 旋转时,就会出现问题。我不确定它是否确实围绕 (0,0,0) 旋转,但这是我能找到的解决方案。我的相机开始盯着一个立方体,但当向左转(x 轴加 2)时,立方体变成了一个大圆圈。向右转时,盒子会绕着圆圈向另一方向移动。将轴应用于盒子时,盒子会很好地移动。

我的代码有点乱,但我尝试的是让转弯运动,如果我向右转,盒子应该向左转,如果我向左转,盒子应该向右转,就像在现实生活中一样。

public class javafx extends Application {
    int ze = 0;
    int ye = 0;
    int xe = 0;

        PerspectiveCamera cam = new PerspectiveCamera();
//the rotation angles//
 Rotate rx = new Rotate();

        { rx.setAxis(Rotate.X_AXIS); }
        Rotate ry = new Rotate();
        { ry.setAxis(Rotate.Y_AXIS); }
        Rotate rz = new Rotate();
        { rz.setAxis(Rotate.Z_AXIS); }
  

        int xt = 0;
        int yt = 0;
        int one;
        int two;
        boolean flip = false;
    public static void addRotate(Node node, Point3D rotationAxis, 
    double angle) {
    ObservableList<Transform> transforms = node.getTransforms();
    try {
        for (Transform t : transforms) {
            rotationAxis = t.inverseDeltaTransform(rotationAxis);
           
        }
    } catch (NonInvertibleTransformException ex) {
        throw new IllegalStateException(ex);
    }
    
    transforms.add(new Rotate(angle, rotationAxis));
}
      
    
    

   public void start(Stage stage) {
 
      Box cube = new Box();  
     
       
    
      cam.getTransforms().addAll(rx, rz, ry);
     
      cube.setDepth(100.0);
      cube.setHeight(100.0);
      cube.setWidth(200.0);
      
      cube.setCullFace(CullFace.BACK);
    
      cube.setDrawMode(DrawMode.FILL);
      PhongMaterial material = new PhongMaterial();
      material.setDiffuseColor(Color.BROWN);
      cube.setMaterial(material);
    
      cube.setTranslateX(1500.0);
      cube.setTranslateY(500.0);
      cube.setTranslateZ(0.0);
    
      
      
      cam.setTranslateX(0);
      cam.setTranslateY(0);
      cam.setTranslateZ(0);
      cam.setScaleX(2);
      cam.setScaleY(2);
      cam.setScaleZ(2);

 
      Group root = new Group(cube, cam);
      Dimension dimension = 
      Toolkit.getDefaultToolkit().getScreenSize();

      double screenHeight = dimension.getHeight();
      double screenWidth = dimension.getWidth();
       
      Scene scene = new Scene(root, 0,0);
     
      stage.setFullScreen(true);
      scene.setCamera(cam);
      stage.setTitle("3d space");
      stage.setScene(scene);

     
      stage.show();
     


      stage.setOnHiding( event -> {  System.exit(0);} );
      //to get the input from a mouse vvvv  //
     ScheduledExecutorService scheduler = 
     Executors.newScheduledThreadPool(1);
     Runnable toRun;
        toRun = new Runnable() {
            public void run() {
               
                if(!flip){
                    one = MouseInfo.getPointerInfo().getLocation().x;
                    two = MouseInfo.getPointerInfo().getLocation().x;
                    flip = true;
                }else{
                    flip = false;
                 
                    if(one > MouseInfo.getPointerInfo().getLocation().x){
                        
                        xt = xt +2;
               

                        ry.setAngle(xt);

        
                        System.out.println("left");
                    }else{
                        if(one < MouseInfo.getPointerInfo().getLocation().x){
                            System.out.println("right");
                            xt = xt -2;
                            ry.setAngle(xt);

                           
                            
                            
                            
                            
                        }
                    }
                }
            }
        };
ScheduledFuture<?> handle = scheduler.scheduleAtFixedRate(toRun, 1, 1, TimeUnit.MILLISECONDS);
 
   
 
 
    
   }
   public static void main(String args[]){
      launch(args);
   }
}

【问题讨论】:

  • 1. 请修复代码中的缩进。这样读起来真的很难。 2. 不得从后台线程更改场景图。这可能不是您问题的原因,但是当您这样做时,没有正确定义任何结果。 3.我不知道你所说的“围绕(0,0,0)旋转”是什么意思。在 3 维几何中,没有绕点旋转这样的东西;只围绕一个轴。
  • 老实说,它只是行不通,但看起来它围绕某个点旋转
  • 同样,“围绕一个点旋转”没有任何意义。
  • 它绕着某根绳子转了一圈

标签: java javafx rotation


【解决方案1】:

答案是cam.set.pivot(222)到它环绕的区域

【讨论】:

    猜你喜欢
    • 2017-07-23
    • 2018-08-03
    • 2012-07-23
    • 2016-09-22
    • 2017-04-22
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    相关资源
    最近更新 更多