2013-12-08 40 views
1

欲旋轉對象輪定製樞軸,這是它的點,所以我有這樣的代碼:如何在JavaFX中圍繞自定義透視旋轉對象?

private final EventHandler<MouseEvent> mouseEventHandler = new EventHandler<MouseEvent>() { 
     @Override 
     public void handle(MouseEvent mouseEvent) { 
      if (mouseEvent.getEventType() == MouseEvent.MOUSE_PRESSED) { 
       dragStartX = mouseEvent.getSceneX(); 
       dragStartY = mouseEvent.getSceneY(); 
       mousePosX = mouseEvent.getSceneX(); 
       mousePosY = mouseEvent.getSceneY(); 
       mouseOldX = mouseEvent.getSceneX(); 
       mouseOldY = mouseEvent.getSceneY(); 

       if (mouseEvent.isMiddleButtonDown()) { 
        pivot = mouseEvent.getPickResult().getIntersectedPoint(); 
        camForm1.rx.setPivotX(pivot.getX()); 
        camForm1.ry.setPivotY(pivot.getY()); 
        camForm1.rz.setPivotZ(pivot.getZ()); 
        System.out.println(pivot); 
       } 

      } else if (mouseEvent.getEventType() == MouseEvent.MOUSE_DRAGGED) { 

       double modifier = 1.0; 
       double modifierFactor = 0.3; 

       if (mouseEvent.isControlDown()) { 
        modifier = 0.1; 
       } 
       if (mouseEvent.isShiftDown()) { 
        modifier = 10.0; 
       } 

       mouseOldX = mousePosX; 
       mouseOldY = mousePosY; 
       mousePosX = mouseEvent.getSceneX(); 
       mousePosY = mouseEvent.getSceneY(); 
       mouseDeltaX = (mousePosX - mouseOldX); //*DELTA_MULTIPLIER; 
       mouseDeltaY = (mousePosY - mouseOldY); //*DELTA_MULTIPLIER; 

       double flip = -1.0; 

       if (mouseEvent.isPrimaryButtonDown() && mouseEvent.isSecondaryButtonDown()) { 
        camForm2.t.setX(camForm2.t.getX() + flip * mouseDeltaX * modifierFactor * modifier * 0.3); // - 
        camForm2.t.setY(camForm2.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3); // - yFlip* 
       } else if (mouseEvent.isSecondaryButtonDown()) { 
        camForm1.ry.setAngle(camForm1.ry.getAngle() - mouseDeltaX * modifierFactor * modifier * 2.0); // + yFlip* 
        camForm1.rx.setAngle(camForm1.rx.getAngle() + flip * mouseDeltaY * modifierFactor * modifier * 2.0); // - 

       } 
      } 
     } 
    }; 

camForm1camForm2和是XForm類的例子。

public class XForm extends Group { 

     public enum RotateOrder { 
      XYZ, XZY, YXZ, YZX, ZXY, ZYX 
     } 

     public Translate t = new Translate(); 
     public Translate p = new Translate(); 
     public Translate ip = new Translate(); 
     public Rotate rx = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.X_AXIS); 
     public Rotate ry = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.Y_AXIS); 
     public Rotate rz = new Rotate(0.0, 0.0, 0.0, 0.0, Rotate.Z_AXIS); 
     public Scale s = new Scale(); 

     public XForm() { 
      super(); 
      getTransforms().addAll(t, rz, ry, rx, s); 
     } 
... 
} 

但是,旋轉是圍繞點O(0,0,0)。我究竟做錯了什麼?

回答

6

自己剛剛處理了這個問題。

我不太瞭解您的代碼,但Rotate class允許設置自定義軸心點。

一個例子:

Box box = new Box(1, 1, 1); // can be any Node 
box.getTransforms().add(new Rotate(angle, pivotX, pivotY, pivotZ, Rotate.Z_AXIS)); 
+0

我試圖從(0,0,0),這是在構造函數中設置一些新的與setPivotX(pivot.getX())的'幫助'等改變支點在Y和Z軸上。你建議添加新的變換,但這並不意味着每次我改變主軸時,都會添加新的變換。而變換的數組可能非常龐大。 – Eugene

+0

如果您需要旋轉更改,請使用getTransforms()。get(rotationIndex)訪問它。畢竟,getTransforms()確實返回一個列表,並且JavaFX轉換是可變的。 –

0

創建旋轉對象

Rotate x = new Rotate(1,2,7,134); 

力與旋轉屬性定製樞軸位置

x.pivotXProperty().set(2); 
x.pivotYProperty().set(7); 
x.pivotZProperty().set(134); 

添加旋轉軸

x.axisProperty().setValue(Rotate.Y_AXIS); 

使用GET轉換

meshViews[14].getTransforms().add(x); 
meshViews[9].getTransforms().add(x);