Search code examples
copenglanimationglutsliding-tile-puzzle

OpenGL glut animation of 8 puzzle solving


I have solved an 8 puzzle using a BFS algorithm and stored the needed moves into an array and then converted those numbers into either 0 to say the blank space needs to move up or 1 down or 2 left or 3 right. I have no idea how i would animate the 8 squares solving the puzzle with my corresponding moves from the BFS. i know to use the Timer and how to do animation in general just not how to sequentially animate the correct square at the correct time. They always need to move 80 pixels in any given direction. if someone could point me in the right direction i would be grateful.


Solution

  • I don't get how you can write something to solve this puzzle, but then not know how to maintain a simple counter to animate a value.

    Let's say you have a frequency F which is moves per second, and time T which is the number of seconds since the animation began.

    The idea, then, is that move M begins at T*F*M and has a duration of 1 (or less, if you want pauses between moves).

    Now, you just need an interpolation function to blend the values. Linear interpolation (LERP) is easiest, but you might want a spline function to accelerate/decelerate at the end points. However you do it, the function simply takes a beginning point, an end point, and a relative position t which is between 0 and 1.

    double blend( double from, double to, double t )
    {
        // linear interp:
        return from * (t-1.0) + to * t;
    }
    

    So, you just calculate t by computing the fractional part fmod(T*F*M, 1.0). If you have a duration of less than 1, you do not change the 1.0 in that fmod call. You simply clamp it to your duration D and then divide by D.