image Tutorial on State Machines and Automata

Based on the previous Tutorial Lesson on advanced variable attributes, we can use variables to design finite state behaviors. In fact, the example of the rabbit moving in a rectangluar fashion gave a glimpse of state-driven behavior.

We will futher this concept in this lesson via the following two simple examples. By tracing the characters, we can also see the resulting behavior.

There are two rabbits.
The rabbits are traced.
The state of the rabbit is 0 initially.
When the state of the rabbit is equal to 0, it moves forward 2 pixels and the s0_steps of the rabbit increases.
When the state of the rabbit is equal to 1, the angle of the rabbit increases by 3 and the s1_steps of the rabbit increases.
When the s0_steps of the rabbit is greater than or equal to 50, the state of the rabbit increments and the s0_steps of the rabbit becomes 0.
When the s1_steps of the rabbit is greater than or equal to 30, the state of the rabbit increments and the s1_steps of the rabbit becomes 0.
When the state of the rabbit is greater than 1, the state of the rabbit becomes 0.
image
In this example, there are 2 states for the rabbit: State 0 and state 1. In state 0, the rabbit will move forward 2 pixels. For each frame (moving forward 2 pixels), the variable s0_steps also increases. This variable, s0_steps, tracks the number of steps made during state 0. When the s0_steps reaches 50, the rabbit will advance to state 1. With 50 steps, the rabbit would have traveled a distance of 100 pixels.

Likewise, in state 1, the rabbit is making a right turn in increments of 3 degrees. Therefore, it will take 30 s1_steps to complete a full 90 degrees.

There are 3 rabbits.
The size of the rabbits is 3.
The rabbits are traced with green.
The state of the rabbit is initially 0.
When the state of the rabbit equals 0, the angle of the rabbit increases by 22 and the s0_step of the rabbit increases.
When the s0_step of the rabbit is equal to 4, the state of the rabbit increases and the s0_step of the rabbit becomes 0.
When the state of the rabbit equals 1, the rabbit moves forward and the s1_step of the rabbit increases.
When the s1_step of the rabbit is greater than 100, the state of the rabbit becomes 0 and the s1_step of the rabbit becomes 0.
This state machine is very similar to the one above, except that in step 0, the rabbit turns right 22 degrees at a time, for 4 steps to a complete 88 degrees. In state 1, the rabbit moves forward 1 pixel for 100 steps. The resulting image is shown here:
image


Try it yourself: How would you design other patterns?


Next page

Tutorials Home