JavaFX中抛物线轨迹的时间线 [英] Timeline for parabolic trajectory in JavaFX

查看:244
本文介绍了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是时间,但我不知道它的
值或者它与KeyFrame持续时间的值是否相同? * /
double x2 = x + voX * t;
double y2 = y + v0Y * t - 0.5 * gravity * t * t;
线l =新线(x,y,x2,y2);
l.setStroke(Color.BLACK);
group.getChildren()。addAll(c,l);
final时间轴时间轴=新时间轴();
KeyValue xKV = new KeyValue(c.centerXProperty(),x2);
KeyValue yKV = new KeyValue(c.centerYProperty(),y2,new Interpolator(){
@Override
//也许我需要分裂,而不是曲线(?)
受保护的双曲线(双t){
//这应该是轨迹的公式
返回Math.tan(角度)* x * - (重力/(2 *速度* Math.cos(角度))) * 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

解决方案

  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
* /
公共类测试扩展应用程序{

@Override
public void start(Stage primaryStage){
primaryStage.setTitle(Test);
Group group = new Group();
场景场景=新场景(组,600,350);
scene.setFill(Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
Circle c = new Circle(100,300,16,Color.AQUA);
线l =新线(100,300,500,300);
l.setStroke(Color.AQUA);
group.getChildren()。addAll(c,l);
final时间轴时间轴=新时间轴();
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);
}

}


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, e.g. circle.centerXProperty(), which represents the initial coordinate, say x. The second parameter should be a type compatible value, in this case the x coordinate toward which the projectile should move. As the timeline plays, the WritableValue will be updated accordingly. Add a second KeyValue to drive the y coordinate.

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. In this related example, a figure moves form point p1 to p2.

In the example below, a Circle moves parallel to the x axis between 100 and 500. 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. You can change the angle of elevation by changing the ratio of height to width in the keys frames, e.g.

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

}

这篇关于JavaFX中抛物线轨迹的时间线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆