Behavior
Mark Green
School of Creative Media
Introduction
We’ve done the easy part
Geometry is pretty standard, we have a
good idea of how we should do it
We can get reasonable looking
environments
Now we have the problem of making the
react
Introduction
We need to have some model of behavior
Some theory of how objects behave
May only cover a subset of behavior, but
allows us to develop design tools
Provide a way of building applications
above the programming level
Start to explore such a model
Introduction
For behavior our objects must be able to
react
What do they react to?
User actions
Other objects
Passage of time
We can view all of these as events
Objects and Events
An object will receive a stream of events, it
must know how to respond to these events
There are two parts to this:
The object declares the set of events its
interested in, it doesn’t respond to other events
How it responds to each type of event, the
actions it performs when it receives the events
Events
Each event has a name, indicates where the
events comes from
What are possible events?
Tick: one tick of the clock
Triggers: events generated by the user, pressing
a button
Collisions: when two objects collide
Designer: created by designer, passed between
objects
Event Response
What the object does when it receives and
event, event handler
In a completely general model we would
allow any program code at this point
The environment designer would provide
the program code required to respond to the
event
But we are trying to avoid programming
Event Response
Geometry
Tick event
Event handler
Trigger event (user)
Event handler
Collision event
Event handler
Designer event
Event handler
Event Response
To avoid programming we need a higher
level way of dealing with behavior
Have a set of actions that objects can
perform, these can be pre-programmed, just
need to select them, put them in event
handler
But what is this set of actions, can we
develop a universal set??
Event Response
No universal set of actions
Need to have an extendable system, be able
to add actions as system evolves
Easy for programmers to add functionality
They can easily produce one or more new
actions and have them integrated into the
system
SE Event Handlers
To make this more concrete look at event
handlers in SE
Event handlers are part of objects, they start
with the word on followed by the name of
the event
For the tick event, we have:
on tick
SE Event Handlers
Body of event handler is list of actions
performed when event occurs
List terminate by end
Basically looks the same as our other
descriptions used for geometry
The key is the actions that we put in the
event handler
SE Event Handlers
Each action starts with the name of the
action, this is decided by the programmer
Action name followed by list of parameters,
all on the same line
Parameters can be numbers, text strings,
expressions and sometimes names
The programmer defines the number of
parameters and their types
Details
Programmer provides actions in form of one
or more C++ objects (you don’t need to
worry about this)
Each object tells SE the names of the
actions is provides, number of parameters
and types of parameters
SE uses this information to process scripts,
set of legal actions and how to process them
Details
Programmer provided objects also execute
the actions
Each event handler becomes list of actions
which call the appropriate actions
Currently programmer provided objects
must be part of SE
In the future they could be dynamically
loaded
Details
Script C++ Objects
SE
Environment
Event Generation: Tick
Where do the events come from and what
are they used for?
The tick event is generated on each update,
if we are running at 60 updates/sec there
will be 60 tick events generated every
second
Used for object behavior, objects can move
and act on their own, animation
Event Generation: Tick
Example, object has propeller or wheel, on
each tick the propeller or wheel can be
rotated
Used to move objects about the
environment, don’t need other objects or
user to trigger the action
Need a set of action for moving objects,
changing their properties
Event Generation: Trigger
Trigger events are generated by the user
When they press a key or button the trigger
event is generated
An object can respond to many trigger
events
Trigger events are defined in the se.map
file, part of MRObjects
Event Generation: Trigger
A trigger corresponds to a button on a
device
A line in the se.map file describes it:
trigger trigger_name device(button)
Trigger_name is the name used in the script
for the trigger
Device is the name of the device the button
is on
Event Generation: Trigger
Button is the name of the button that
generates the trigger
For the a key on the keyboard we have:
trigger akey console(a)
For button two on a joystick we have:
trigger selection joystick(2)
Event Generation: Trigger
Do we always want objects responding to
triggers?
Probably not, may only want the selected
object to respond to the trigger
For example, the user might select an object
and then start performing some operations
on it, only wants to change the selected
object
Event Generation: Filtering
Only want to process the event if some
condition is true
This is an example of filtering, only
processing some of the events, a filtering
process determines the events to be
processed
Two places to filter: before or after the
event arrives
Event Generation: Filtering
Before event received: need to somehow
specify the filter
Could be part of the on statement, for
example:
on button1 selected
This is reasonably efficient, but not very
flexible, would need to change SE for every
new filter, how do we combine them?
Event Generation: Filtering
Event filtering as part of event handler
Have actions that serve as event filters
If the event passes the filter, continue with
the next action, otherwise exit, example:
on button1
selected
….
end
Event Generation: Filtering
Has advantage of being more flexible:
Set of filters is extendable
Can easily combine filters
Not as efficient, need to execute part of the
event handler for each event, can’t discard it
immediately
This is the approach that we will use
Will need a set of filter actions
Event Generation: Collisions
Collision events: a set of related events that
involve multiple objects
One way that objects can react to each other
A collision occurs when two objects collide,
not easy to determine
Must look at each pair of objects to see if
they intersect
Event Generation: Collisions
Must be done centrally, can’t be efficiently
done by individual objects
A central process that looks for collisions
on each update and generates the
appropriate event
Other types of proximity events, attracted or
repelled by certain objects, etc
Event Generation: Designer
Events used by the environment designer
for interaction between objects
Example: a change in one object may
trigger similar changes in other objects,
need to be able to inform the other objects
Designer event can be sent to the other
objects to trigger the change
Event Generation: Designer
Can also be used for timing, within the
same object or between objects
Example: send an event in 5 seconds
Can be used to trigger a delayed reaction
Terminate a motion after a certain period of
time
Need a set of actions to support this
Actions
Now that we have some idea of the events
that can be generated we can examine the
types of actions required
Have the following basic action categories:
Utility
Filtering
Event scheduling
transformations
Actions: Utility
Generally useful operations that don’t fit in
other categories
One example is print, useful for debugging
event handlers
Do objects need variables?
Do we need to store information in objects
that are useful for event handlers?
Actions: Utility
The print action can have any number of
operands
operand separated by commas (,) and list
terminated by ;
for example:
print “the value of x is “ , x ;
Actions: Utility
What would we use variables for?
They could be part of a filtering
mechanism, a way of indicating that an
action shouldn’t be performed
Could be used to pass information between
event handlers
Used to keep track of the state of the object
Actions: Variables
Example: variables used to keep track of
object’s location: Locx, Locy, Locz
Each time object is moved, values are
changed
Determine when the object has reached the
end of the environment: move it back to the
center or change direction of movement
Actions: Variables
Two types of values:
Real
Strings
Set action used to assign values
set variable, value;
value can be a constant, 5 or “some text”, or
its could be an expression
Actions: Variables
An action similar to set is eval
this action has the form:
eval variable = expression ;
both eval and set do basically the same
thing
set usually used to initialise variables, while
eval used to update variable values
Actions: Control Flow
If statement used for testing, there are two
different versions of the if statement:
if expression ;
actions
endif
if expression ;
actions
else
actions
endif
Actions: Control Flow
Similar to if statements in most
programming languages
expressions can have comparison operators:
==
!=
>=
Actions: Control Flow
If action used to update variables based on
current state of the environment
for example, to keep an object in an
environment with boundaries at 8 and -8
if Locx > 8;
set Locx, 0;
endif
if Locx 8;
set Locx, 0;
endig
if Locx 8;
set Locy, 0;
endif
if Locy 8; random dy, -0.1, 0.1;
set dx, -dx; random dt, 10, 20;
endif triggerAt “test”, “change”, dt;
if Locx 8;
set dy, -dy;
endif
if Locy 0.2;
set state, 0;
endif
triggerAt “prop”, “speedup”, 0.1;
end
on starttrigger
set state, 1;
set proprate, 0.01;
trigger “prop”, “speedup”;
end
Example Three
We will have a similar event handler for
slowing down
We will need similar event handlers in
plane that vary the rotation speed of the
plane
This gives us more interesting motion
Discussion
There’s one problem
Time, we don’t treat time correctly
Frequency of the tick event depends upon
processor speed
On fast computers propeller spins faster,
objects move faster
Appearance and behavior depend on
processor speed
Discussion
We would like things to look roughly the
same on all processors
This means that rotation amount must
depend upon the time difference between
tick events
At first this seems like a relatively simple
problem, we just modify the relative
transformations to take time difference into
account
Discussion
This doesn’t work!
It solves problem for tick events, but what
happens in the other events?
We use relative transformation to respond
to user events, what time value should we
use?
We need a different approach
Discussion
We can solve this problem in the following
way
On each tick event, set variable dt to the
time difference
Can now use:
eval rate = proprate * dt;
spiny rate;