Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

  1. #1
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Hello everybody.

    I've created my own particular version of the Java FX program for the Bouncing Ball.

    I was testing the method decreaseSpeed() in order to try to understand the variation of the decrease of the rateProperty of the Timeline Animation.

    The increase of the speed is done using the UP key in the Keyboard.

    The decrease of the speed is done using the DOWN key in the Keyboard.

    The first thing I've discovered is that the Increase/Decrease of the rateProperty of the Timeline Animation is not uniform.

    For example a uniform increase/decrease (using only integers as simplification would be)

    1 -> 2 -> 3 -> 4

    4 -> 3 -> 2 -> 1

    In this example every time I would increase I would increase it by adding one.

    If I was decreasing I would decrease it by subtracting one.

    In my program the rate is a double but is not uniform.

    In my first run decrease I get:


    The current rate is 1.100000000000001
    The current rate is 1.0000000000000009
    The current rate is 0.9000000000000009
    The current rate is 0.8000000000000009
    The current rate is 0.700000000000001
    The current rate is 0.600000000000001
    The current rate is 0.500000000000001
    The current rate is 0.400000000000001
    The current rate is 0.30000000000000104
    The current rate is 0.20000000000000104
    The current rate is 0.10000000000000103


    Then I increased the rate a little, and after that I decreased it again, and I got this:


    The current rate is 1.0999999999999999
    The current rate is 0.9999999999999999
    The current rate is 0.8999999999999999
    The current rate is 0.7999999999999999
    The current rate is 0.7
    The current rate is 0.6
    The current rate is 0.5
    The current rate is 0.4
    The current rate is 0.30000000000000004
    The current rate is 0.20000000000000004
    The current rate is 0.10000000000000003


    As you can see the values are different in the two times.

    But that is not why I'm making this post.

    When I decrease the rate the Rate reaches 0 and if, after that, I try to decrease it again it stays 0.

    But, before the rateProperty gets to Zero, I always get a Negative number before it.

    So, in the next lines, there are two examples, and in both Examples I get a Negative rate Property.



    I've Increased the Rate Property before this

    The current rate is 2.8000000000000016
    The current rate is 2.7000000000000015
    The current rate is 2.6000000000000014
    The current rate is 2.5000000000000013
    The current rate is 2.4000000000000012
    The current rate is 2.300000000000001
    The current rate is 2.200000000000001
    The current rate is 2.100000000000001
    The current rate is 2.000000000000001
    The current rate is 1.9000000000000008
    The current rate is 1.8000000000000007
    The current rate is 1.7000000000000006
    The current rate is 1.6000000000000005
    The current rate is 1.5000000000000004
    The current rate is 1.4000000000000004
    The current rate is 1.3000000000000003
    The current rate is 1.2000000000000002
    The current rate is 1.1
    The current rate is 1.0
    The current rate is 0.9
    The current rate is 0.8
    The current rate is 0.7000000000000001
    The current rate is 0.6000000000000001
    The current rate is 0.5000000000000001
    The current rate is 0.40000000000000013
    The current rate is 0.30000000000000016
    The current rate is 0.20000000000000015
    The current rate is 0.10000000000000014
    The current rate is 1.3877787807814457E-16

    This is a negative current rate

    The current rate is -0.09999999999999987


    The current rate is 0.0
    The current rate is 0.0
    The current rate is 0.0

    I've Increased the Rate Property before this

    The current rate is 2.3000000000000007
    The current rate is 2.2000000000000006
    The current rate is 2.1000000000000005
    The current rate is 2.0000000000000004
    The current rate is 1.9000000000000004
    The current rate is 1.8000000000000003
    The current rate is 1.7000000000000002
    The current rate is 1.6
    The current rate is 1.5
    The current rate is 1.4
    The current rate is 1.2999999999999998
    The current rate is 1.1999999999999997
    The current rate is 1.0999999999999996
    The current rate is 0.9999999999999997
    The current rate is 0.8999999999999997
    The current rate is 0.7999999999999997
    The current rate is 0.6999999999999997
    The current rate is 0.5999999999999998
    The current rate is 0.4999999999999998
    The current rate is 0.3999999999999998
    The current rate is 0.2999999999999998
    The current rate is 0.19999999999999982
    The current rate is 0.09999999999999981

    This is a negative current rate

    The current rate is -1.942890293094024E-16


    The current rate is 0.0
    The current rate is 0.0
    The current rate is 0.0


    Can someone please explain me why this happens and if it is sort of an Overflow / Underflow ?

    Thank you,

    Rogério

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    why this happens
    Hard to say without seeing the code.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Hello Norm.

    Here you have the code.

    This is the code for the class BallPaneOldVersion:


     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.Section15dot12BouncingBall.OldVersion;
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
    import javafx.animation.KeyFrame;
    import javafx.animation.Timeline;
    import javafx.beans.property.DoubleProperty;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Circle;
    import javafx.util.Duration;
     
     
     
     
    public class BallPaneOldVersion extends Pane {
     
        /*
        Data fields
        */
     
        public final double RADIUS = 20;
     
        private double x = RADIUS;
     
        private double y = RADIUS;
     
        private double dx = 1;
     
        private double dy = 1;
     
        private Circle circle = new Circle(x, y, RADIUS);
     
        private Timeline animation;
     
     
        /*
        No-arg Constructor
        */
     
        public BallPaneOldVersion() {
     
            circle.setFill( Color.GREEN );
     
            getChildren().clear();
     
            getChildren().add( circle );
     
            /*
            Create an
            animation for
            moving the ball
            */
     
            animation = new Timeline( new KeyFrame( Duration.millis( 50 ), e -> moveBall() ) ); //end of animation = new Timeline();
     
            animation.setCycleCount( Timeline.INDEFINITE );
     
            animation.play();
     
        } //end of the constructor public BallPane()
     
     
        public void play() {
     
            animation.play();
     
        }
     
     
        public void pause() {
     
            animation.pause();
     
        }
     
     
        public void increaseSpeed() {
     
            double currentRate;
     
            currentRate = rateProperty().doubleValue();
     
     
            //System.out.println( "The current rate before increase is " + currentRate + "\nIncreasing Speed" );
     
     
            animation.setRate( animation.getRate() + 0.1 );
     
     
        }
     
     
        public void decreaseSpeed() {
     
            double currentRate;
     
     
            currentRate = rateProperty().doubleValue();
     
     
            System.out.println( "The current rate before decrease is " + currentRate + "\nDecreasing Speed" );
     
     
            animation.setRate( 
            animation.getRate() > 0 ? animation.getRate() - 0.1 : 0 );
     
     
        }
     
     
     
     
     
        public DoubleProperty rateProperty() {
     
            return animation.rateProperty();
     
        }
     
     
        protected void moveBall() {
     
     
            /*
            Check boundaries
            */
     
            if( x < RADIUS || x > getWidth() - RADIUS ) {
     
                dx *= -1; //Change ball move direction
     
            }
     
     
            if( y < RADIUS || y > getHeight() - RADIUS ) {
     
                dy *= -1; //Change ball move direction
     
            }
     
     
            /*
            Adjust ball position
            */
     
     
     
            x += dx;
     
            y += dy;
     
            circle.setCenterX( x );
     
            circle.setCenterY( y );
     
     
     
        } //end of the method protected void moveBall()
     
     
    } //end of the class BallPaneOldVersion


    And this is the code for the class BounceBallControlOldVersion


     
     
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package Chapter15.Section15dot12BouncingBall.OldVersion;
     
    /**
     *
     * @author Rogerio Biscaia
     */
     
     
     
    import javafx.application.Application;
    import javafx.stage.Stage;
    import javafx.scene.Scene;
    import javafx.scene.input.KeyCode;
     
     
     
     
    public class BounceBallControlOldVersion extends Application {
     
     
        @Override
        public void start(Stage primaryStage) throws Exception {
     
     
            BallPaneOldVersion ballPane = new BallPaneOldVersion(); //Create a ball pane
     
     
            /*
            Pause and resume animation
            */
     
            ballPane.setOnMousePressed( e -> ballPane.pause() );
     
            ballPane.setOnMouseReleased( e -> ballPane.play() );
     
     
            /*
            Increase and decrease animation
            */
     
            ballPane.setOnKeyPressed( e -> { 
     
                if( e.getCode() == KeyCode.UP ) {
     
                    ballPane.increaseSpeed();
     
                } else if( e.getCode() == KeyCode.DOWN ) {
     
                    ballPane.decreaseSpeed();
     
                }
     
     
            } ); //end of  ballPane.setOnKeyPressed( e -> {} )
     
     
            /*
            Create a scene and
            place it in the stage
            */
     
            Scene scene = new Scene(ballPane, 250, 150);
     
            primaryStage.setTitle( "BounceBallControl" ); //Set the stage title
     
            primaryStage.setScene(scene); //Place the scene in the stage
     
            primaryStage.show(); //Display the stage
     
     
            /*
            Must request
            focus after the
            primary stage is displayed
            */
     
            ballPane.requestFocus();
     
     
     
        } //end of the method public void start(Stage primaryStage)
     
     
     
     
        /*
        The main method is only needed for the IDE with
        limited JavaFX support.
        Not needed for running from the
        command line
        */
     
        public static void main(String[] args) {
     
            Application.launch(args);
     
        }
     
     
     
     
    } //end of the class BounceBallControlOldVersion


    Thank you, Norm.

    Rogério

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    I changed the debug output to make it smaller. Here is the output I get when decreasing then increasing.

    rate before decrease 0.5 rate after decrease 0.4
    rate before decrease 0.4 rate after decrease 0.30000000000000004
    rate before decrease 0.30000000000000004 rate after decrease 0.20000000000000004
    rate before decrease 0.20000000000000004 rate after decrease 0.10000000000000003
    rate before decrease 0.10000000000000003 rate after decrease 0.0
    rate before decrease 0.0 rate after decrease 0.0

    The issue is in the code used in the decrease logic. It needs to only look at the first decimal place. A double can not exactly hold the values you are trying to give it, See the many 0s after the tenth position followed by a digit (eg 4 and 3 above). The code needs to strip any values to the right of the tenth position. One way is to multiply the double by 10 and cast it to an int to remove the following values: test = (int)(10*rate) - gives 0 if the rate is less than .1

    I added this to speed up testing:
            animation.setRate(0.5);   //<<<<<<<<<<<<<< debug >>>>  closer to 0
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (September 9th, 2024)

  6. #5
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Hello Norm.

    Thank you for your answer.

    I've already solved that problem using a different approach.

    If the rate is greater than 0.1 it decreases it by 0.1, else it sets the rate as 0.0.

    That allowed me to solve another problem.

    I call that problem the "Wormhole" problem.

    Before, when I decreased the rateProperty to 0.0 the ball would stop.

    But after that, when I increased the rate the Ball would continue to move from a different position in the pane and not continue to move from the position where it was "frozen" before.

    It is like if the ball would stop "frozen" at a certain position, it was swallowed by a Wormhole and it appears later moving in a different position.

    Now the ball only moves if the program is running and the rate is greater than 0.1.

    Now the ball continues moving from the previous position where it was "frozen".

    Thank you again.

    Rogério

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    It looks like the system queues calls to the moveBall method and then executes the calls all at one time when the value of rate increases from 0.
    Add a print statement to the moveBall method to see it happening.

    If the rate is greater than 0.1 it decreases it by 0.1, else it sets the rate as 0.0.
    That would change the rate from 0.10000000000000003 to 0.00000000000000003 instead of to 0.0 because floating point values are not exact like integers.
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (September 10th, 2024)

  9. #7
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Hello Norm.

    First of all thank you for your previous answer.

    I did not want to ignore your answer.

    I think I understood your previous answers.

    As for your last answer of yesterday, yes, the Timeline is calling the moveBall method every 50 milliseconds.

    I will add these extra print statements in my New Version.

    Thank you again, Norm

    Rogério

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    the Timeline is calling the moveBall method every 50 milliseconds.
    I do not think that is true, Add a print statement to the moveBall method that prints the new value of x and y and prints the time the method was called.
    The print out will show that moveBall is not called when rate = 0 and then is called many times with no intervening time when then rate changes from 0.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (September 11th, 2024)

  12. #9
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Hello Norm.

    Yes you are Right.

    There are several times that the x and y are updated in the same millisecond.

    For example in the date:

    2024-09-11 16:40:19.778

    778 is the milliseconds.

    The x and y were updated 5 times.




    The x and y were updated
    New x is 123.0 and new y is 99.0
    2024-09-11 16:40:05.450
    The x and y were updated
    New x is 124.0 and new y is 100.0
    2024-09-11 16:40:05.474
    The current rate before decrease is -0.09999999999999987
    Decreasing Speed
    The current rate after decrease is 0.0
    The current rate before decrease is 0.0
    Decreasing Speed
    The current rate after decrease is 0.0
    The current rate before decrease is 0.0
    Decreasing Speed
    The current rate after decrease is 0.0
    The current rate before decrease is 0.0
    Decreasing Speed
    The current rate after decrease is 0.0



    Before this, I increased the rate



    The x and y were updated
    New x is 125.0 and new y is 101.0
    2024-09-11 16:40:19.778
    The x and y were updated
    New x is 126.0 and new y is 102.0
    2024-09-11 16:40:19.778
    The x and y were updated
    New x is 127.0 and new y is 103.0
    2024-09-11 16:40:19.778
    The x and y were updated
    New x is 128.0 and new y is 104.0
    2024-09-11 16:40:19.778
    The x and y were updated
    New x is 129.0 and new y is 105.0
    2024-09-11 16:40:19.778



    The x and y were updated
    New x is 130.0 and new y is 106.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 131.0 and new y is 107.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 132.0 and new y is 108.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 133.0 and new y is 109.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 134.0 and new y is 110.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 135.0 and new y is 111.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 136.0 and new y is 112.0
    2024-09-11 16:40:19.779
    The x and y were updated
    New x is 137.0 and new y is 113.0
    2024-09-11 16:40:19.779


    The x and y were updated
    New x is 138.0 and new y is 114.0
    2024-09-11 16:40:19.780



    So, if I understood well when the rate decreases to 0, the program makes a lot of updates to the x and y, but sort of pauses the execution of those updates.

    When the rate increases from 0 the program makes all the updates of x and y that were previously sort of paused, but makes it all at once.

    Thank you, Norm

    Rogério

  13. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    when the rate decreases to 0, the program makes a lot of updates to the x and y, {\
    No updates are made to x and y until the moveBall method is called. The calls to moveBall are saved(queued) until the rate goes positive and then all the calls to moveBall are made at once.

    Note: The debug output would be clearer if the time of the change was on the same line as the change.

    The x and y were updated New x is 137.0 and new y is 113.0 at 2024-09-11 16:40:19.779

    or minimum:
    New x 137.0, y is 113.0 at 16:40:19.779
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    Rogerm (September 19th, 2024)

  15. #11
    Member
    Join Date
    Apr 2024
    Posts
    39
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: When I decrease the Speed of a Timeline animation I get a Negative rateProperty

    Thank you, Norm.

Similar Threads

  1. Speed Converter
    By dsouzan in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 4th, 2023, 02:04 AM
  2. [SOLVED] Code speed problem
    By Gasis in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 13th, 2014, 07:53 AM
  3. how to do TimeLine daigram
    By skuskusas in forum Java Theory & Questions
    Replies: 1
    Last Post: August 21st, 2013, 08:06 AM
  4. Blender file with animation, how to import OBJ(w/ animation) into Java?
    By cr80expert5 in forum Object Oriented Programming
    Replies: 0
    Last Post: May 12th, 2011, 03:11 AM
  5. Need...more...speed!
    By nPeep in forum Java Networking
    Replies: 8
    Last Post: September 6th, 2010, 11:07 AM