這可能使用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
:
- 由半徑平移(z分量0)
- 旋轉到適當的角度
- 翻譯z方向
注:變換是在它們發生在transforms
列表相反的順序施加。
另外,您可以編寫可與缸使用的座標參數,並編寫相應的x
,y
和z
值的幫手:
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();
}
太感謝你了!你會在這裏爲我的進一步問題? – LucasPG
@LucasPG如果這是一個關於這個答案的問題,請寫評論。如果這是一個新問題:我通常每天回答幾個問題。如果你寫了一個問題,它寫得夠好,有趣,沒有足夠的質量答案,我知道答案,我可能會發布它......但也有其他相當能幹的用戶誰可以回答你的問題。 – fabian
你好。在我的應用程序中,用戶放置所有線索值。我已經統計了圓周運動的半徑,同時我也有XY平面上的速度和Z軸上的速度。我如何將所有這3個值放到您的animateSphere函數中以使運動與用戶值兼容? – LucasPG