1

I read recently a lesson about JAVAFX, and I want to make an application to assimilate as well what I learnt. I would like to do a 2D game, where a ball is animated throught a parabolic way. I'm using for that two objects, Timeline and Interpolator. I must admit that i don't know how exactly the Interpolator works, my problem is that I want to make the animation stop when the ball arrives at the bottom of the window.

I thought about using 2 methods:

  • The first one consist of stopping the animation when the Y coordinate of the ball (Circle) is greater or equal to the windows's height, but I don't know how to do that.
  • The second one consist of stoping the animation at a special moment Tf, I got a formula to know when the ball intersects the goal, but I don't know how to define an end-time for the animation.

I would like to have a response for the both methods if possible :) Here's my Timeline code:

Timeline t1 = new Timeline();
t1.setAutoReverse(false);
t1.setCycleCount(Timeline.INDEFINITE);
t1.getKeyFrames().addAll(
    new KeyFrame(new Duration(1000 * Tf),new KeyValue(this.centerXProperty(),0,new Interpolator(){
        protected double curve(double t){
            double resultat = coeffAngleX * t;
            return - resultat;
        }
    })),
    new KeyFrame(new Duration(1000 * Tf),new KeyValue(this.centerYProperty(),0,new Interpolator(){
        protected double curve(double t) {
            double resultat = (a * t * t)
                     + (coeffAngleY * t);
            return resultat;
        }
    }))
);

Edit:

Ok, I read everything at the link that you gave me, and I understand now that the second parameter of the KeyValue is the endValue!

Now look at this example, I set the end value of (X,Y) to (500,700), but look what I got as a result in the console while printing the values of centerXProperty and centerYProperty:

Result

Here's a the new code of mine:

Timeline t1 = new Timeline();
t1.setAutoReverse(false);
t1.setCycleCount(1);

KeyValue xKvB = new KeyValue(this.centerXProperty(),xCenter);
KeyValue yKvB = new KeyValue(this.centerYProperty(),yCenter);

KeyValue xKvE = new KeyValue(this.centerXProperty(),500.0,new Interpolator(){
    protected double curve(double t){
        double resultat = coeffAngleX * t;
        System.out.println("X = " + centerXProperty().get());
        return resultat;
    }
});
KeyValue yKvE = new KeyValue(this.centerYProperty(),700.0,new Interpolator(){
   protected double curve(double t){
     double resultat = (a * t * t) + (coeffAngleY * t);
     System.out.println("Y = " + centerYProperty().get());
     return resultat;
   } 
});

KeyFrame KfB = new KeyFrame(Duration.ZERO,xKvB,yKvB);
KeyFrame KfE = new KeyFrame(Duration.seconds(1),xKvE,yKvE);


t1.getKeyFrames().addAll(KfB,KfE);
t1.play();
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Mssm
  • 717
  • 11
  • 29

2 Answers2

1

For things like this I find using an AnimationTimer more flexible than using a Timeline. With an AnimationTimer you can directly implement both of your two basic approaches. You either just call the stop method of the timer or inside your timer method you just stop advancing the ball when you have reached the end condition.

mipa
  • 10,369
  • 2
  • 16
  • 35
  • Humm yeah i see, but can i use the interpolator with this object ? my purpose is to learn using JAVAFX's objetcs , and it would be too easy to use a loop in the handle method of the AnimationTimer, is there a way to keep my structure or use the Interpolator with the AnimationTimer ? – Mssm Jul 10 '16 at 11:14
  • For what would you want to use a loop here? Maybe you misunderstood how an AnimationTimer works. In order to answer your question: yes you can of course use an Interpolator inside the AnimationTimer although it does not make much sense and is less flexible. – mipa Jul 10 '16 at 14:08
  • A loop is necessary to change x,y coordinates using a formula with an AnimationTimer, doesn't it ? – Mssm Jul 11 '16 at 20:53
  • No, you have to implement a method which implements your formula but the AnimationTimer will call that after every puls so you don't need a loop. – mipa Jul 28 '16 at 16:15
1

I want to make the animation stop when the ball arrives at the bottom of the window.

Starting from this complete example, Simply set the cycle count to 1, the default.

timeline.setCycleCount(1);

In flight:

image during

Landed:

image end

An algebraically equivalent parabolic Interpolator is seen here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I've posted a comment to you as an answer, didn't know how to put a picture and code in a comment :) – Mssm Jul 11 '16 at 12:14
  • As shown [here](http://stackoverflow.com/a/38139772/230513), your implementation of `curve()` does not map to the interval `[0…1]` onto itself; vary ratio of height to width to get different angles, as suggested [here](http://stackoverflow.com/a/38031826/230513). – trashgod Jul 11 '16 at 17:45
  • Oki i didn't know about that condition concerning mapping the interval [0..1], i advanced concerning the problem but still didn't found the right solution. – Mssm Jul 13 '16 at 15:52
  • 1- Can you first explain me what does it mean " curve() have to #map the interval [0..1] " ? i was supposing it's a function who's equals to 0 (y = 0) when x = 1 or 0, so i've used t(1-t) didn't work , then i used the example of 4 t (1-t) it worked but with another problem. – Mssm Jul 13 '16 at 15:53
  • 2- The problem was that the y coordinate of the end is always equals to the y coordinate of the start, so if i start the animation from for example the point (20,500) the end point will be (maxDistanceX,500). – Mssm Jul 13 '16 at 15:54
  • 3- If for example i put a 0° of angle , the animation will go with an angle which may be arround 60°, in case of moving only horizantally – Mssm Jul 13 '16 at 15:54
  • I checked again the formulas, this isn't the problem, i don't see why i got those problems, i'll upload a picture of my new code – Mssm Jul 13 '16 at 15:55
  • @Mouley: [plot](https://www.desmos.com/calculator) the equation in the [example](http://stackoverflow.com/a/38031826/230513) cited to see how the vertex and intercepts map `[0…1]` onto itself. – trashgod Jul 13 '16 at 16:00
  • I didn't get you, this is supposed to answer my last 3 questions ? didn't get you xD sorry – Mssm Jul 13 '16 at 16:36
  • Yes. You're trying to adjust the angle by changing `curve()`; instead, change the ratio of height/width: _tan θ_ = `height / width`. – trashgod Jul 14 '16 at 00:03