2016-11-21 136 views
1

我在做一个小小的JavaFX 8项目。我需要一些建议如何正确执行动画。关于该项目的一些事情。这是一个通过磁场动画带电粒子流动的程序。所有需要的值都是从用户将它们放入textField的GUI中获取的。点击按钮后,我们将转到3D场景,其中我的点显示为球体,所有值都设置完成。字段行被打印在它的目录中。JavaFX 8 3D动画

问题是如何做propper动画。我正在尝试使用我的Sphere X Y Z坐标,但找不到设置这些坐标的方法。在Z平面内的运动应该与线速度相同。并且在XY平面中的运动应该是圆形的。我可以用路径转换吗?

我的愿景是创建勾画动画,通过路径绘制球体。如果进一步打勾,则下一个球体将被绘制新的坐标,其由平移向量计算。

回答

1

这可能使用PathTransition?不,路径在javafx中是二维的,但是您需要3D运动。

请注意,由于角度的计算有点复杂,因此球坐标系不是一个很好的坐标系来描述这样的运动。

更适合的坐标系将是圆柱体坐标。

您可以使用几种变换和动画使用Timeline动画那些尽管实现这种动作:

private static void animateSphere(Sphere sphere) { 
    Rotate rot = new Rotate(); 
    Translate radiusTranslate = new Translate(50, 0, 0); 
    Translate zMovement = new Translate(); 

    sphere.getTransforms().setAll(zMovement, rot, radiusTranslate); 
    Timeline tl = new Timeline(
      new KeyFrame(Duration.ZERO, 
         new KeyValue(zMovement.zProperty(), 0d), 
         new KeyValue(rot.angleProperty(), 0d)), 
      new KeyFrame(Duration.seconds(4), 
         new KeyValue(zMovement.zProperty(), 900d, Interpolator.LINEAR), 
         new KeyValue(rot.angleProperty(), 720, Interpolator.LINEAR)) 
    ); 
    tl.setCycleCount(Timeline.INDEFINITE); 
    tl.play(); 
} 

@Override 
public void start(Stage primaryStage) { 
    Sphere sphere = new Sphere(30); 

    Pane root = new Pane(sphere); 

    Scene scene = new Scene(root, 400, 400, true); 
    PerspectiveCamera camera = new PerspectiveCamera(); 
    camera.setTranslateZ(-10); 
    camera.setTranslateX(-500); 
    camera.setTranslateY(-200); 
    camera.setRotationAxis(new Point3D(0, 1, 0)); 
    camera.setRotate(45); 
    scene.setCamera(camera); 

    animateSphere(sphere); 

    primaryStage.setScene(scene); 
    primaryStage.show(); 
} 

你的运动是螺旋运动,因此下列转换的组合将appropritately移动Sphere

  1. 由半径平移(z分量0)
  2. 旋转到适当的角度
  3. 翻译z方向

注:变换是在它们发生在transforms列表相反的顺序施加。


另外,您可以编写可与缸使用的坐标参数,并编写相应的xyz值的帮手:

public class CylinderCoordinateAdapter { 

    private final DoubleProperty theta = new SimpleDoubleProperty(); 
    private final DoubleProperty radius = new SimpleDoubleProperty(); 
    private final DoubleProperty h = new SimpleDoubleProperty(); 

    private static final Point3D DEFAULT_AXIS = new Point3D(0, 0, 1); 
    private Point3D axis2; 
    private Point3D axis3; 

    private final ObjectProperty<Point3D> axis = new SimpleObjectProperty<Point3D>() { 

     @Override 
     public void set(Point3D newValue) { 
      newValue = (newValue == null || newValue.equals(Point3D.ZERO)) ? DEFAULT_AXIS : newValue.normalize(); 

      // find first value ortogonal to axis with z = 0 
      axis2 = newValue.getX() == 0 && newValue.getY() == 0 ? new Point3D(1, 0, 0) : new Point3D(-newValue.getY(), newValue.getX(), 0).normalize(); 

      // find axis ortogonal to the other 2 
      axis3 = newValue.crossProduct(axis2); 
      super.set(newValue); 
     } 
    }; 

    public CylinderCoordinateAdapter(WritableValue<Number> x, WritableValue<Number> y, WritableValue<Number> z) { 
     Objects.requireNonNull(x); 
     Objects.requireNonNull(y); 
     Objects.requireNonNull(z); 
     axis.set(DEFAULT_AXIS); 
     InvalidationListener listener = o -> { 
      Point3D ax = axis.get(); 
      double h = getH(); 
      double theta = getTheta(); 
      double r = getRadius(); 

      Point3D endPoint = ax.multiply(h).add(axis2.multiply(Math.cos(theta) * r)).add(axis3.multiply(Math.sin(theta) * r)); 

      x.setValue(endPoint.getX()); 
      y.setValue(endPoint.getY()); 
      z.setValue(endPoint.getZ()); 
     }; 
     theta.addListener(listener); 
     radius.addListener(listener); 
     h.addListener(listener); 
     axis.addListener(listener); 

     listener.invalidated(null); 
    } 

    public final Point3D getAxis() { 
     return this.axis.get(); 
    } 

    public final void setAxis(Point3D value) { 
     this.axis.set(value); 
    } 

    public final ObjectProperty<Point3D> axisProperty() { 
     return this.axis; 
    } 

    public final double getH() { 
     return this.h.get(); 
    } 

    public final void setH(double value) { 
     this.h.set(value); 
    } 

    public final DoubleProperty hProperty() { 
     return this.h; 
    } 

    public final double getRadius() { 
     return this.radius.get(); 
    } 

    public final void setRadius(double value) { 
     this.radius.set(value); 
    } 

    public final DoubleProperty radiusProperty() { 
     return this.radius; 
    } 

    public final double getTheta() { 
     return this.theta.get(); 
    } 

    public final void setTheta(double value) { 
     this.theta.set(value); 
    } 

    public final DoubleProperty thetaProperty() { 
     return this.theta; 
    } 

} 
private static void animateSphere(Sphere sphere) { 
    CylinderCoordinateAdapter adapter = new CylinderCoordinateAdapter(
      sphere.translateXProperty(), 
      sphere.translateYProperty(), 
      sphere.translateZProperty()); 

    adapter.setRadius(50); 

    Timeline tl = new Timeline(
      new KeyFrame(Duration.ZERO, 
         new KeyValue(adapter.hProperty(), 0d), 
         new KeyValue(adapter.thetaProperty(), 0d)), 
      new KeyFrame(Duration.seconds(4), 
         new KeyValue(adapter.hProperty(), 900d, Interpolator.LINEAR), 
         new KeyValue(adapter.thetaProperty(), Math.PI * 4, Interpolator.LINEAR)) 
    ); 
    tl.setCycleCount(Timeline.INDEFINITE); 
    tl.play(); 
} 
+0

太感谢你了!你会在这里为我的进一步问题? – LucasPG

+0

@LucasPG如果这是一个关于这个答案的问题,请写评论。如果这是一个新问题:我通常每天回答几个问题。如果你写了一个问题,它写得够好,有趣,没有足够的质量答案,我知道答案,我可能会发布它......但也有其他相当能干的用户谁可以回答你的问题。 – fabian

+0

你好。在我的应用程序中,用户放置所有线索值。我已经统计了圆周运动的半径,同时我也有XY平面上的速度和Z轴上的速度。我如何将所有这3个值放到您的animateSphere函数中以使运动与用户值兼容? – LucasPG