The Anatomy of a Squirrel
By Mr. Zap
You are user number six hundred and thirtysix seeing this since November 2019.
This page was laste updated January 1997.
The squirrel was coded in Memetm from Immersive Systems. The squirrel demo consists of four different interacting Meme modules.
- Space - Managing the world, and the navigation
- Squirrel - Managing the squirrel, and it's behavior
- Tree - Managing the tree, and it's behavior
- Nut - Managing the nut, and it's behavior
A Meme module is an independent program. It has a creation/destruction routine, it has a message-handling routine, and it has any number of sleeping or dormant tasks that carry out the actual work.
A typical Meme module loads some geometry from disk, and sets up at one or more tasks to manage that objects behavior. A module for a spinning ball would load the ball geometry in it's creation routine, and set up a task for the spinning.
The most powerful think in Meme is the compound object. A compound object is simply a module - but it is included in the object heirarchy as if it was a geometric object.
A compound object is written such that all it's geometry isn't liked to world as you would normally have it, but to ModuleWorld. The ModuleWorld object represents the object under which this compound object is placed when it is
If you have a standalone "spinning ball" module, turning it into a compound object requires you to:
When you have done this, you can instantiate the compound object from another module with the function NewCompound. The function works identical to NewVisible but instead of refering to a geometry file, you refer to your already
compiled module. You can translate, rotate, and scale the object at your whim.
- Remove any camera declaration and rendering loop
- Link all geometry created to ModuleWorld instead of world
You now have a spinning ball object that can by used anywhere.
The ability to rescale and move the object at will, or to link it into the object heirarchy at any point, gives you lots of power. If you have written a wall-clock module, you can scale
it down and use it as a wristwatch for your avatar, if you want to.
The Compound Objects of the Squirrel demo
The geometries were created in 3D-Studio. To design the cute squirrel, I had help from my wife, who has a keen eye for animals. (If you had seen the roadkill I made for squirrel version 1.0, you would have laughed your head off
. Luckily, my wife set me straight).
Click the modules name to see the source of each module. Note that it isn't an entry for a code beauty contest and that there is old junk, testing code, and yet-to-be-used stuff in it!!
- The space module loads the floor (the mesh on which the saga takes place), sets up a rendering task and a navigation task.
Then it loads two Tree compound objects, and a Squirrel compound object at predefined locations.
A keyboard task is also set up, which monitors the keyboard for the keystrokes 'S' and 'T', which create a new Squirrel and Tree object respectively, giving them a random location and scalefactor.
- The tree module is the simplest of them all. It simply loads the tree-trunk and crown as geometries, and sets up a simple "brain" task.
The "brain" of the tree doesn't doo very much. It simply waits a random time, and then creates a Nut compound object at a random location in it's crown. Then it waits again, and creates another nut, and so on....
- The nut isn't very intelligent either. It simply has a "brain" that calculates a descendance curve towards the ground, and upon impact, a bounce. After a few bounces the nut comes to rest, and the task is put to sleep.
The squirrel is the most advanced module. It contains five tasks, each for:
- blink...the blinking of the eyes, at pseudo-random intervals (initiall active).
- brain...the "brain" of the squirrel (initially active), which "decitions" the squirrel makes.
- walk...walking along the ground, doing the up-down motion, wiggling the tail, turning the legs. (initially dormant)
- turnhead...turning the head to a predefined yaw angle (initially dormant).
- munch...eating a nut (initially dormant).
The Brain task is the interesting one - it is the "scheduler" that decides which of the three sleeping tasks to execute.
The brain selects one of four basic behaviors randomly (with equal probability), and a random timespan under which to execute this behavior. The four behaviors are:
- Sit - the squirrel does nothing. The walk, turnhead and munch tasks are dormant.
- Walk - the squirrel decides a random direction, calculates the position of the nearest tree (to avoid it), and wakes the walk task.
- Look - the squirrel calculates a random head yaw angle, and launches the turnhead task.
- FindFood/Eat - this fourth behavior has a few conditions:
The closest nut is found by scanning thru the object tree, and using messages inquiring each object if it is of the type "Nut" or not.
- If the squirrel is already holding a nut, then eat it by launching the munch task.
- If the squirrel is not holding a nut, but is close to a nut, then pick it up.
This is done by sending a pickup-message to the nut, (which stops the nut's bouncing behavior) and attach the nut to the squirrels arm.
- If the squirrel isn't close to a nut, then calculate the angle and distance to the closest nut, and launch the walk task. Note that the brain might preempt the squirrels "walk to a nut" by getting a new idea before the squirrel
actually gets to the nut!
A Multi-User Squirrel
All objects in the squirrel demo is written in a fashion that could easily make them multi-user. It is only my lack of time, and laziness, that has kept them from being so.
Essentially, the squirrel follows the thoughts in my behavior proposal in that it uses a "brain" that makes the indeterministic decitions, and an "engine" (in this case the other tasks) that deterministically carry out the
orders of the brain.
To make the squirrel fully multi-user, would simply mean to put a message layer between these two. This is fairly easy to do, but lack of time forced me not to. I started to implement it, but have only gotten as far as a bunch of declared message
structures that arn't used anywhere....
Essentially, each "decision" the brain makes would need to be communicated to the "engine" task with a message. When that is accomplished, the squirrel would be able to drive one or more remote replicas identically across the net.
All possible, but not done.
I hope this little tour of Meme programming and Virtual Squirrology has been interesting reading. If you have any comments, send me a mail.