Timeline for a parabolic trajectory in JavaFX

Sorry, but I continue to understand. My problem is that I do not know anything about physics, but my teacher assigned 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 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 froze. Please help meeee

0
source share
1 answer

KeyValue WritableValue, . circle.centerXProperty(), , x. , x, . , , WritableValue . KeyValue y.

, , KeyValue , size . p1 p2.

a Circle x 100 500. Circle y 300 100, curve(), y = -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() {…});

image

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

}
+3

Source: https://habr.com/ru/post/1652722/


All Articles