JavaFX 中抛物线轨迹的时间线

Bru*_*lia 1 java javafx

抱歉,我还是不明白。我的问题是我对物理一无所知,但我的老师分配给我这个项目。

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();
}
Run Code Online (Sandbox Code Playgroud)

我处于停滞状态。请帮助我

tra*_*god 5

在 a 中KeyValue,第一个参数应该是 a WritableValue,例如circle.centerXProperty(),它表示初始坐标,例如x。第二个参数应该是类型兼容的值,在本例中是x射弹应移动的坐标。随着时间线的播放,WritableValue将会相应更新。添加第二个KeyValue来驱动y坐标。

\n\n

在此处看到的第一个示例中,三个实例KeyValue将图形从其初始位置移动到目标位置,该位置size沿每个坐标轴相距单位。在此相关示例中,图形从点移动p1p2

\n\n

在下面的示例中, aCircle平行于和x之间的轴移动。同时,它平行于抛物线y = \xe2\x80\x934( x \xe2\x80\x93 ½) 2 + 1 定义的轴并遵循该抛物线,其顶点为 (½, 1) x在 0 和 1 处截距。根据 API 的要求,此实现对单位正方形上的抛物线路径进行建模。您可以通过更改关键帧中的高宽比来更改仰角,例如100500Circley300100curve()curve()curve()

\n\n
KeyValue xKV = new KeyValue(c.centerXProperty(), 200);\nKeyValue yKV = new KeyValue(c.centerYProperty(), 0, new Interpolator() {\xe2\x80\xa6});\n
Run Code Online (Sandbox Code Playgroud)\n\n

图像

\n\n
import javafx.animation.Interpolator;\nimport javafx.animation.KeyFrame;\nimport javafx.animation.KeyValue;\nimport javafx.animation.Timeline;\nimport javafx.application.Application;\nimport javafx.scene.Group;\nimport javafx.scene.Scene;\nimport javafx.scene.paint.Color;\nimport javafx.scene.shape.Circle;\nimport javafx.scene.shape.Line;\nimport javafx.stage.Stage;\nimport javafx.util.Duration;\n\n/**\n * @see /sf/answers/2662227851/\n */\npublic class Test extends Application {\n\n    @Override\n    public void start(Stage primaryStage) {\n        primaryStage.setTitle("Test");\n        Group group = new Group();\n        Scene scene = new Scene(group, 600, 350);\n        scene.setFill(Color.BLACK);\n        primaryStage.setScene(scene);\n        primaryStage.show();\n        Circle c = new Circle(100, 300, 16, Color.AQUA);\n        Line l = new Line(100, 300, 500, 300);\n        l.setStroke(Color.AQUA);\n        group.getChildren().addAll(c, l);\n        final Timeline timeline = new Timeline();\n        timeline.setCycleCount(Timeline.INDEFINITE);\n        timeline.setAutoReverse(false);\n        KeyValue xKV = new KeyValue(c.centerXProperty(), 500);\n        KeyValue yKV = new KeyValue(c.centerYProperty(), 100, new Interpolator() {\n            @Override\n            protected double curve(double t) {\n                return -4 * (t - .5) * (t - .5) + 1;\n            }\n        });\n        KeyFrame xKF = new KeyFrame(Duration.millis(2000), xKV);\n        KeyFrame yKF = new KeyFrame(Duration.millis(2000), yKV);\n        timeline.getKeyFrames().addAll(xKF, yKF);\n        timeline.play();\n    }\n\n    public static void main(String[] args) {\n        launch(args);\n    }\n\n}\n
Run Code Online (Sandbox Code Playgroud)\n