对不起,但我还是不理解。我的问题是我对物理一无所知,但我的老师给我这个项目。
private void shoot() {
Group group = new Group();
double angle = cannon.getRotate();
double speed = slider.getValue();
double x = cannon.getLayoutX();
double y = cannon.getLayoutY();
double v0X = Math.cos(angle)*speed;
double voY = Math.sin(angle)*speed;
Circle c = new Circle(x, y, 8, Color.BLACK);
/*t is the time, but I don't know its
value or has it the same value of the KeyFrame duration? */
double x2 = x + voX*t;
double y2 = y + v0Y * t - 0.5 * gravity * t * t;
Line l = new Line(x, y, x2, y2);
l.setStroke(Color.BLACK);
group.getChildren().addAll(c, l);
final Timeline timeline = new Timeline();
KeyValue xKV = new KeyValue(c.centerXProperty(), x2);
KeyValue yKV = new KeyValue(c.centerYProperty(), y2 , new Interpolator() {
@Override
//Maybe I need I splite, not a curve (?)
protected double curve(double t) {
//thisshould be trajectory's formula
return Math.tan(angle) * x*-(gravity/(2*speed*Math.cos(angle)))*x*x;
}
});
KeyFrame xKF = new KeyFrame(Duration.millis(2000), xKV);
KeyFrame yKF = new KeyFrame(Duration.millis(2000), yKV);
timeline.getKeyFrames().addAll(xKF, yKF);
timeline.play();
}
我处于停滞状态。请帮助meeee
答案 0 :(得分:3)
在KeyValue
中,第一个参数应为WritableValue
,例如代表初始坐标的circle.centerXProperty()
,表示x
。第二个参数应该是类型兼容值,在这种情况下是弹丸应移动的x
坐标。随着时间轴的播放,WritableValue
将相应更新。添加第二个KeyValue
以驱动y
坐标。
在第一个看到here的示例中,KeyValue
的三个实例将一个数字从其初始位置移动到目标位置,该位置沿着每个坐标轴size
个单位。在此相关example中,数字从表格p1
移动到p2
。
在下面的示例中,Circle
与x
和100
之间的500
轴平行移动。同时,在抛物线 y定义的curve()
之后,同一Circle
在y
和300
之间平行于100
轴移动< / em> = -4( x - ½) 2 + 1,其顶点(½,1)和 x 截取为0且1.根据curve()
API的要求,curve()
的这种实现模拟了单位正方形上的抛物线路径。您可以通过更改关键帧中高度与宽度的比例来更改高程角度,例如
KeyValue xKV = new KeyValue(c.centerXProperty(), 200);
KeyValue yKV = new KeyValue(c.centerYProperty(), 0, new Interpolator() {…});
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
* @see https://stackoverflow.com/a/38031826/230513
*/
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Test");
Group group = new Group();
Scene scene = new Scene(group, 600, 350);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
Circle c = new Circle(100, 300, 16, Color.AQUA);
Line l = new Line(100, 300, 500, 300);
l.setStroke(Color.AQUA);
group.getChildren().addAll(c, l);
final Timeline timeline = new Timeline();
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.setAutoReverse(false);
KeyValue xKV = new KeyValue(c.centerXProperty(), 500);
KeyValue yKV = new KeyValue(c.centerYProperty(), 100, new Interpolator() {
@Override
protected double curve(double t) {
return -4 * (t - .5) * (t - .5) + 1;
}
});
KeyFrame xKF = new KeyFrame(Duration.millis(2000), xKV);
KeyFrame yKF = new KeyFrame(Duration.millis(2000), yKV);
timeline.getKeyFrames().addAll(xKF, yKF);
timeline.play();
}
public static void main(String[] args) {
launch(args);
}
}