简体   繁体   English

JavaFX中抛物线轨迹的时间线

[英]Timeline for parabolic trajectory in JavaFX

I'm sorry, but I continue not understanding. 抱歉,我仍然不明白。 My problem is I know nothing about physics but my teacher assigned to me this project. 我的问题是我对物理学一无所知,但我的老师指派给我这个项目。

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();
}

I'm at a standstill. 我停滞不前。 Please, help meeee 请帮忙

In a KeyValue , the first parameter should be a WritableValue , eg circle.centerXProperty() , which represents the initial coordinate, say x . KeyValue ,第一个参数应该是WritableValue ,例如circle.centerXProperty() ,它表示初始坐标,例如x The second parameter should be a type compatible value, in this case the x coordinate toward which the projectile should move. 第二个参数应该是类型兼容的值,在这种情况下,射弹应该向其移动的x坐标。 As the timeline plays, the WritableValue will be updated accordingly. 随着时间轴播放, WritableValue将相应地更新。 Add a second KeyValue to drive the y coordinate. 添加第二个KeyValue来驱动y坐标。

In the first example seen here , three instances of KeyValue move a figure from it's initial position to its destination position, which is size units away along each coordinate axis. 此处看到的第一个示例中, KeyValue三个实例将图形从其初始位置移动到其目标位置,该目标位置是沿每个坐标轴的size单位。 In this related example , a figure moves form point p1 to p2 . 在此相关示例中 ,图形从点p1移至p2

In the example below, a Circle moves parallel to the x axis between 100 and 500 . 在下面的示例中, Circle100500之间平行于x轴移动。 At the same time, that same Circle moves parallel to the y axis between 300 and 100 following the curve() defined by the parabola y = –4( x – ½) 2 + 1, which has vertex (½, 1) and x intercepts at 0 and 1. This implementation of curve() models a parabolic path on a unit square, as required by the curve() API. 同时,同一Circle沿抛物线y = –4( x –½) 2 + 1定义的curve()平行于y轴在300100之间移动,该curve()具有顶点(1/ 2,1 )和x在0和1处截取curve()此实现根据curve() API的要求在单位正方形上模拟了抛物线路径。 You can change the angle of elevation by changing the ratio of height to width in the keys frames, eg 您可以通过在关键帧中更改高度与宽度的比率来更改仰角,例如

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);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM