View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

Simple Simulation Using Morphic Step

Written By: Jason Sirichoke (12/04/04)

Related Topics: Morph, step, simulation, Sim-Tech, animation, discrete event, random, M5

The basis of this Case was the Sim-Tech game designed for the CS2340 Fall 2004 class. This game was a derivative of the well known computer game, The Sims, where the user creates people and watches them interact with their environment and other people in the game. A requirement for this project was to have some sort of simulation where the user can sit back and watch the characters interact with each other and move around without having to constantly click the mouse and guide their every action.

In general, there are two approaches to simulation:
• Continuous: Mathematical model of a complex system, describing changes over fixed time intervals
• Discrete: Behavioral model of individual interacting objects. Time may change in fixed steps or jump to the time of the next interesting event.

The approach used for this project was a discrete simulation where each person in the game world was an object within the simulation.

Simulation Tools in Squeak:
Squeak offers a variety of ways to implement simulation:
• Simulation.cs: a change set that can be imported into Squeak which contains the Simulation, SimulationObject and DelayedEvent classes.
• Morphic step: a way for Morphs to update their appearance periodically. Anywhere that a thread would normally be used, one can consider polling the Morphic step instead.

Simulation in Sim-Tech was designed using the Morphic step approach. A standalone class was designed to handle the simulation. During each step, the list of people existing in the game world was polled with a random number between 0 and 1. The number represents the probability that the person would perform a certain action. If the number was within a certain range, the person performed either an interaction with another person/thing, moved to another place on the grid or did nothing at all. The speed of the simulation could either be sped up or slowed down by increasing or decreasing the time between each interval

The main advantage of using the Morphic step approach is that it is relatively easy to code, essentially being one large while loop where each object in concern is polled for the correct action. Unlike Simulation, it is a part of the Morph class and requires no special imports or change sets. The speed of the animation could also be changed with relative ease which can be advantageous when programming something like Sim-Tech where one may want to provide the user option to change the level of simulation speed without any major overhaul in the code. For a small environment with relatively few objects to poll, Morphic step is probably the most logical choice.

Morphic step’s main disadvantage lies in the nature of how it must be executed. Since it is a feature of the Morph class, the Morph must be open on the screen in order for the simulation stepping to be active. This would not be much of an obstacle if, for example, you were trying to display a simple clock that would always be on the screen. However, in a case of a program like Sim-Tech where objects are considered to be active even if they cannot be seen, this can be a killer if the designer chooses each person to take care of their own simulation. Although this may seem like the better approach since it is probably more efficient and allows for ease of a unique schedule by varying the time between each step for each person, the end result will be that only the people in the current people are performing actions while the rest of the world is frozen. This is entirely unrealistic and defeats the purpose of having simulation.

Another wrinkle in the Morphic stepping feature is that the Morph automatically begins to step as soon as it is drawn on screen. If simulation is supposed to be running at all times, this is of little concern. However, in the case of Sim-Tech this is not desirable since the user needs to interact with the world and is supposed to be able to control when simulation happens and when the world is static.

Some Solutions:
The approach used to counter the “off-screen” problem described in the previous section was to have a Morph always open in the world with the sole purpose of handling simulation. It was then passed in a list of objects that needed to be addressed during the simulation and at each step it went through the list and performed the appropriate actions. This is by no means a fool-proof way to do it, but it works for a small environment with only a limited number of objects in concern. It is probably not advisable to apply this method if the program must deal with a large number of objects during simulation since it could be extremely slow and could possibly produce errors in the animation. What if it was already time for the next step and the current step was still trying to process the list of objects? Problems may arise in efficiency and consistency of the program.

Controlling whether stepping is active or not can be achieved in many ways. For this project the class which handled Simulation was given a Boolean variable which was controlled by a user button and initialized to false. At the beginning of the step method a simple check was made to this variable. If it was false, it meant that the user had not requested simulation and the step function simple returned right there. If the variable was true, the function began the step process.

Link to this Page