2016-06-25 5 views
1

申し訳ありませんが、私は理解していません。私の問題は物理学については何も知りませんが、私の教師はこのプロジェクトを私に割り当てました。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(); 
} 

私は停止しています。してください、助けてください

+0

私は(cannone_imがcannoneのImageViewのある)大砲から弾丸を撃つしようとしているが、私は問題を抱えているです。KeyValueを聖霊降臨祭と私は –

+0

に移動することはできません更新後、あなたが求めている実際の質問は何ですか? –

+0

あなたのアニメーションは50ミリ秒しか続きません... –

答えて

3

KeyValueでは、最初のパラメータはWritableValue、たとえば最初の座標を表すcircle.centerXProperty()、たとえばx。 2番目のパラメータは型互換値(この場合はx座標)でなければなりません。タイムラインが再生されると、WritableValueがそれに応じて更新されます。 2番目のKeyValueを追加してyの座標を駆動します。

hereと表示されている例では、KeyValueの3つのインスタンスは、図形をその初期位置から各座標軸に沿ってsize単位離れた目的地に移動します。これに関連して、図形はp1の書式ポイントをp2に移動します。以下の例で

Circle移動は100500x軸に平行です。同時に、その同じCircle移動し=放物線Yによって定義curve()以下300100y軸に平行-4(Xから½)頂点(½を有する + 1を、 1)とxは0と1でインターセプトします。このcurve()の実装では、curve() APIで必要とされるように、単位平方の放物線パスをモデル化します。キーフレームの高さと幅の比率を変更することによって、仰角を変更することができます。

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

} 
+0

私はInterpolator.SPLINEの使用を避けることができますか? –

+0

はい、[コンストラクタ](http://docs.oracle.com/javase/8/javafx/api/javafx/animation/KeyValue.html#KeyValue-javafx.beans.value.WritableValue-T-)があります。補間器を使用しません。 –

+0

質問を更新しました。ボタンをクリックして弾を撃つときに「シュート」すると、しばらくの間そのサークルが見えます。私は全体の曲線を見ません。 私はまた、 "circle.setTranslateX(x1); circle.setTranslateY(y1);" –

関連する問題