Saturday, December 21, 2013

A Simple Animation Exercise


   In his book, The Armchair Universe, computer scientist A.K. Dewdney reprints 5 Easy Pieces, one of his Computer Recreations columns published in Scientific American. The first easy piece was to program a moving, segmented caterpillar.
   I first programmed this easy piece in the BASIC programming language. In BASIC, the coordinates of each segment were stored in a 1-dimensional array. To compute the next cycle, the new position for the head was computed and then every segment was moved up one element of the array. Only one new movement computation had to be made per cycle of animation!
   In Scratch, I used the point towards block to simplify the animation.
   The script uses 11 sprites to create a head, a tail, and nine intervening segments. The ‘head’ selects a turn angle between 30º to the left and 30º to the right and then moves 3 steps. Every other sprite ‘points towards’ the sprite in front of it and then moves 3 steps. For example, sprite 4 points towards sprite 3 and moves 3 steps. This simple technique creates the illusion of a crawling caterpillar!       
    Here’s a screenshot of showing how the caterpillar starts in 11 segments.
    The segments soon fit loosely together since they are all travelling at the same speed but after a few reflections off the sides, the segments fit together as is shown in this screenshot.
    The project can be viewed and downloaded by clicking on this link.



No comments:

Post a Comment