Stick Figure Walking NetLogo Model

Produced for the book series "Artificial Intelligence";

Author: W. J. Teahan; Publisher: Ventus Publishing Aps, Denmark.

powered by NetLogo

view/download model file: Stick-Figure-Walking.nlogo

WHAT IS IT?

This model shows how to get a stick figure to walk across the screen.


THE INTERFACE

The Interface buttons are defined as follows:

- setup: This resets the animation by placing the stick figure at the left of the environment.
- go-once: The stick figure moves across the screen once from left to right.
- go: The stick figure repeatedly walks across the screen from left to right.

The Interface sliders and switches are defined as follows:

- forward-movement-increment: This is how far the stick figure moves foward before the next frame is drawn.
- clear-display-in-between: If this is set to Off, all the frames will be drawn without the previous one being cleared first.


HOW IT WORKS

It uses six versions of the stick figure to perform basic key-frame animation. There are six configurations of the stick figure that correspond to the six frames used to define the walking motion. These six frames are displayed one after the other, and then the animation repeats. i.e. The frame sequence is 1,2,3,4,5,6,1,2,3,4,5,6,1,...

The stick figure is drawn using two turtle breeds - one for the head, and the other for the body and limbs. This is done using NetLogo turtle drawing commands rather than using link agents.


HOW TO USE IT

You can't really use it for anything, except for changing the values in the sliders to create different sized mazes. Press the setup button first, then if you want to run the animation sequence once, then press the go once button. If you want the animation sequence to run continuously, press the go button. Changing the forward-movement-increment slider changes the speed of the movement if the clear-display-in-between switch is set to On. If set to Off, it draws all the frames.


WHAT IS ITS PURPOSE?

Its purpose is to show how to do basic key-frame animation, and also show how changing a single variable that specifies the forward movement of the stick figure (e.g. the forward-movement-increment variable) can yield a surprising range of movements.

A further purpose is to illustrate how movement is fundamental to an agent in an environment and can be used to distinguish its type of behaviour. The movement of the stick figure's body & head (a manifestation of its embodiment) can also be decomposed down to movement of individual parts of its body & head.


THINGS TO TRY

Try changing the forward-movement-increment slider to see what happens to the animation. Try doing this dynamically, for example, by changing the slider during the middle of an animation. This will result in the stick figure seeming to slow down to a walk or speed up to a run. If you set the increment to negative, it will start moving backwards. If you set the increment to -1, you can also get the stick figure to moonwalk.


EXTENDING THE MODEL

Try using a different set of frames to get the stick figure to walk/run/jump/crawl in different ways.


RELATED MODELS

See the Stick Figure Animation model.


CREDITS AND REFERENCES

To refer to this model in publications, please use:

Teahan, W. J. (2010). Stick Figure Walking NetLogo model.
Artificial Intelligence. Ventus Publishing Aps.


PROCEDURES

; Stick Figure Walking model.
;
; Shows a stick figure walking (pretty badly).
; I am sure someone could do better...
; Anyone wants to create a Star Wars Clone Warrior Walking model???
;
; Copyright 2010 William John Teahan. All Rights Reserved.
;

breed [paths path] ;; turtles that draw the limbs and body of the stick
                   ;; figure as a jointed path
breed [circle-paths circle-path] ;; for drawing the head

to setup
  clear-all
  
  create-paths 5 [initialize-paths] ;; agents to draw paths for four limbs
                                    ;; (two legs, two arms), plus body
  set-default-shape paths "circle"  ;; this is the shape of the turtle for
                                    ;; drawing the path

  ifelse forward-movement-increment > 0
    [draw-stick-figure -56 0 [160 7 170 7] [190 7 200 7] [165 7 130 5]
                             [195 7 130 5] [0 0 0 12]]
;;else
    [draw-stick-figure  56 0 [160 7 170 7] [190 7 200 7] [165 7 130 5]
                             [195 7 130 5] [0 0 0 12]]  
end

to go
  let frame 1
  let xpos forward-movement-increment
  ifelse forward-movement-increment > 0
    [set xpos xpos - 56]
;;else
    [set xpos xpos + 56]

  while [xpos > -57 and xpos < 57]
  [

    if clear-display-in-between
      [clear-drawing]

    let front-leg item (frame mod 6) [[160 7 170 7] [170 7 180 7] [180 7 190 7] [190 7 200 7] [180 7 190 7] [170 7 180 7]]
    let back-leg  item (frame mod 6) [[190 7 200 7] [180 7 190 7] [170 7 180 7] [160 7 170 7] [170 7 180 7] [180 7 190 7]]
    let front-arm item (frame mod 6) [[165 7 130 5] [173 7 125 5] [187 7 125 5] [195 7 130 5] [187 7 125 5] [173 7 125 5]]
    let back-arm  item (frame mod 6) [[195 7 130 5] [187 7 125 5] [173 7 125 5] [165 7 130 5] [173 7 125 5] [187 7 125 5]]

    let body [0 0 0 12] ;; body does not change oriention

    draw-stick-figure xpos 0 front-leg back-leg front-arm back-arm body

    set frame frame + 1
    set xpos xpos + forward-movement-increment
    tick
  ]
end

to draw-limb [limb-color top-x top-y specs]
  ;; draws a limb for the stick figure according to the specifications specs
  ;; top of limb is at top-x,top-y
  ;; middle joint is drawn at heading [item 0 specs] with length [item 1 specs]
  ;; bottom of limb is drawn at heading [item 2 specs] from the joint with length [item 3 specs]

  let dir-to-joint item 0 specs
  let joint-length item 1 specs
  let dir-to-bot item 2 specs
  let bot-length item 3 specs

  pen-up
  set color limb-color
  set size 1.5
  setxy top-x top-y
  pen-down
  stamp
  if joint-length > 0 [
    set heading dir-to-joint
    forward joint-length
    stamp
  ]
  set heading dir-to-bot
  forward bot-length
  stamp
  pen-up

  ;;set path (list top-x top-y dir-to-joint joint-length dir-to-bot bot-length)
end

to draw-stick-figure [xpos ypos front-leg back-leg front-arm back-arm body]
  ;; draw the stick figure in the following order so that the front leg and arms
  ;; appear in front of the back leg and arms
  ask path 0 [draw-limb gray xpos ypos back-leg]        ;; back leg
  ask path 1 [draw-limb gray xpos ypos + 12 back-arm]   ;; back arm
  ask path 2 [draw-limb white xpos ypos front-leg]      ;; front leg
  ask path 3 [draw-limb white xpos ypos body]           ;; body
  ask path 4 [draw-limb white xpos ypos + 12 front-arm] ;; front arm

  create-circle-paths 1 [initialize-circle-paths]
  set-default-shape circle-paths "circle 2"
  ask circle-path 5 [setxy xpos 15 set size 6 set color white stamp]
end

to initialize-paths
  pen-up
  set size 0
  set pen-size 4
  ;;set path []
end

to initialize-circle-paths
  pen-up
  set size 0
  set pen-size 8
end
;
; Copyright 2010 by William John Teahan.  All rights reserved.
;
; Permission to use, modify or redistribute this model is hereby granted,
; provided that both of the following requirements are followed:
; a) this copyright notice is included.
; b) this model will not be redistributed for profit without permission
;    from William John Teahan.
; Contact William John Teahan for appropriate licenses for redistribution for
; profit.
;
; To refer to this model in publications, please use:
;
; Teahan, W. J. (2010).  Stick Figure Walking NetLogo model.
;   Artificial Intelligence. Ventus Publishing Aps.
;