Search code examples
java3dlibgdx

Rotation of a sphere with certain velocity. (360 degrees in 24 hours)


I am trying to create a 3D model of solar system. For this purposes i have chosen LibGDX Framework. Currently i am puzzling over, how to rotate the earth model with certain velocity. I want it to rotate with 360 degrees in 24 hours. I made some attempts, to rotate it to some tiny delta each frame, so that in sum it gives 360 degrees after one minute (or hour).
So my question, what would be the most convenient way to achieve it?


Solution

  • If you're using delta time from the game engine to move it each frame, there will be a probable build-up of error over time. For any long-running animation (more than a minute or so), you should use elapsed time that is calculated by subtracting the start time from the current time. Then use the elapsed time to get the current animation state. Then it will never have any error relative to the start time.

    I also like to divide out the time it takes for an animation to repeat before converting to a float to get the animation progress. That way we don't lose precision as time goes on.

    For example:

    long startTime;
    long animationRepeatTime = 24 * 60 * 60; // one rotation per 24 hours
    
    @Override
    public void start() {
        //...
    
        startTime = System.currentTimeMillis();
    }
    
    @Override
    public void render() {
        // ...
    
        long now = System.currentTimeMillis();
        long elapsed = now - startTime;
        long animationElapsed = elapsed % animationRepeatTime;
        float animationProgress = (float)animationElapsed / (float)animationRepeatTime;
    
        // Use animationProgress to get the current animation state.
        // For a rotating planet:
        float degreesRotation = animationProgress * 360f;
        // use degreesRotation to rotate the planet model.
    }