Small Steps II — Making Things

Animate with Time

Animate with Time

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Changing position and drawing again

What you will learn

Animation displays pictures in succession while changing their state a little at a time. Here, we change the circle's x position.

Try it

Example · example1.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 50;

    while (window.is_open())
    {
        x = x + 2;

        if (x > 640)
            x = 0;

        window.clear(Black);
        window.fill_circle(x, 240, 20, Yellow);
        window.show();
        sleep(0.01);
    }
}

Each iteration increases x by 2, clears the background, and draws and displays the circle. When it passes the right edge, x returns to 0.

sleep(0.01) waits about 0.01 seconds. Check that the circle moves right and reappears on the left.

Bouncing by changing direction

What you will learn

The sign of a speed value can represent direction: positive moves right and negative moves left.

Try it

Example · animation_2_example2.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 100;
    double speed = 3;

    while (window.is_open())
    {
        x = x + speed;

        if (x > 620 || x < 20)
            speed = -speed;

        window.clear(Black);
        window.fill_circle(x, 240, 20, Cyan);
        window.show();
        sleep(0.01);
    }
}

At a boundary, speed = -speed; reverses the sign. Account for the radius of 20 so the center stays between 20 and 620.

This speed is currently distance per iteration. Computers may run loops at different rates, so next we will use elapsed time.

Measuring elapsed time

What you will learn

StopWatch measures elapsed time. It starts when you create the object, and elapsed reports the seconds that have passed.

Try it

Example · stopwatch_1_example1.cpp
void small_main()
{
    StopWatch watch;

    sleep(1.0);
    print("About one second: ", watch.elapsed());

    watch.reset();
    sleep(0.5);
    print("About half a second: ", watch.elapsed());
}

After sleep(1.0), the result is about 1 second. After reset and sleep(0.5), it is about 0.5 seconds. Reading the watch does not reset it to zero; reset starts the measurement again.

It is normal for the execution environment to make the results differ from exactly 1.000 or 0.500.

Moving according to actual time

What you will learn

Distance = speed × time. Choosing a speed per second and moving by the elapsed time keeps movement consistent even when loop rates differ.

Try it

Example · stopwatch_2_example2.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x = 50;
    double speed = 200;

    StopWatch watch;

    while (window.is_open())
    {
        double dt = watch.elapsed();
        watch.reset();

        x = x + speed * dt;

        if (x > 620 || x < 20)
            speed = -speed;

        window.clear(Black);
        window.fill_circle(x, 240, 20, Yellow);
        window.show();
    }
}

dt stores the time since the previous iteration. Read it with elapsed, then reset the watch, and add speed * dt to the position.

At 200 pixels per second, the object moves 2 pixels in 0.01 seconds and 4 in 0.02 seconds. dt is not new syntax. Change speed to 100 and compare.

Exercise

Place the circle at x=320 in the center of the screen and move it along y. Reverse its direction at the top and bottom edges.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double y = 100;
    double speed = 3;

    while (window.is_open())
    {
        // Update y and bounce.

        window.clear(Black);
        window.fill_circle(320, y, 20, Green);
        window.show();
        sleep(0.01);
    }
}

Hint

Create y and speed. Reverse the sign of speed when y > 460 || y < 20.

show solution
void small_main()
{
    Window window;
    window.open(640, 480);

    double y = 100;
    double speed = 3;

    while (window.is_open())
    {
        y = y + speed;

        if (y > 460 || y < 20)
            speed = -speed;

        window.clear(Black);
        window.fill_circle(320, y, 20, Green);
        window.show();
        sleep(0.01);
    }
}
Try More (Optional)

Exercise

Move two balls with different x positions and speeds on one screen. Make both restart from the left after passing the right edge.

Exercise starter · animation_2_exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double x1 = 50;
    double x2 = 200;
    double speed1 = 2;
    double speed2 = 4;

    while (window.is_open())
    {
        // Update both balls.

        window.clear(Black);
        window.fill_circle(x1, 180, 15, Yellow);
        window.fill_circle(x2, 300, 15, Cyan);
        window.show();
        sleep(0.01);
    }
}

Hint

Create x1, x2, speed1, and speed2 separately.

show solution
void small_main()
{
    Window window;
    window.open(640, 480);

    double x1 = 50;
    double x2 = 200;
    double speed1 = 2;
    double speed2 = 4;

    while (window.is_open())
    {
        x1 = x1 + speed1;
        x2 = x2 + speed2;

        if (x1 > 655) x1 = -15;
        if (x2 > 655) x2 = -15;

        window.clear(Black);
        window.fill_circle(x1, 180, 15, Yellow);
        window.fill_circle(x2, 300, 15, Cyan);
        window.show();
        sleep(0.01);
    }
}

Exercise

Create a StopWatch and print elapsed after sleep(2.0). A result other than exactly 2.000... is normal.

Exercise starter · stopwatch_1_exercise1_starter.cpp
void small_main()
{
    StopWatch watch;

    // Wait two seconds and print the elapsed time.
}

Hint

StopWatch starts when created, so it needs no separate start call.

show solution
void small_main()
{
    StopWatch watch;

    sleep(2.0);
    print("Elapsed: ", watch.elapsed());
}

Exercise

Update the vertically moving ball from the earlier animation example to use dt and a speed of 150 pixels per second.

Exercise starter · stopwatch_2_exercise2_starter.cpp
void small_main()
{
    Window window;
    window.open(640, 480);

    double y = 100;
    double speed = 150;
    StopWatch watch;

    while (window.is_open())
    {
        // Measure dt and move using seconds.

        window.clear(Black);
        window.fill_circle(320, y, 20, Green);
        window.show();
    }
}

Hint

Each loop, use double dt = watch.elapsed(); watch.reset();, then move with y = y + speed * dt;.

show solution
void small_main()
{
    Window window;
    window.open(640, 480);

    double y = 100;
    double speed = 150;
    StopWatch watch;

    while (window.is_open())
    {
        double dt = watch.elapsed();
        watch.reset();

        y = y + speed * dt;

        if (y > 460 || y < 20)
            speed = -speed;

        window.clear(Black);
        window.fill_circle(320, y, 20, Green);
        window.show();
    }
}
← Previous lessonNext lesson →