behavior mark green school of creative media. introduction we’ve done the easy part geometry is...
TRANSCRIPT
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 fromWhat 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 handlerIn a completely general model we would allow any program code at this pointThe environment designer would provide the program code required to respond to the eventBut we are trying to avoid programming
Event Response
Geometry
Event handler
Event handler
Event handler
Event handler
Tick event
Trigger event (user)
Collision event
Designer event
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 programmerAction name followed by list of parameters, all on the same lineParameters can be numbers, text strings, expressions and sometimes namesThe 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 actionsEach event handler becomes list of actions which call the appropriate actionsCurrently programmer provided objects must be part of SEIn the future they could be dynamically loaded
Details
SE
Script C++ Objects
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 deviceA line in the se.map file describes it:trigger trigger_name device(button)
Trigger_name is the name used in the script for the triggerDevice 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 filterCould 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 handlerHave actions that serve as event filtersIf 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 immediatelyThis is the approach that we will useWill 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 valuesset 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 -8if Locx > 8;
set Locx, 0;
endif
if Locx < -8;
set Locx, 0;
endif
Actions: Transformations
Transformations allow us to do some basic behavior, simple motionsHave the three standard transformations: translate, scale and rotateHave both the absolute and relative formsRelative form adds to the current transformation, absolute form overwrites pervious transformation
Actions: Transformations
Why do we have two forms of transformations?
There are two main uses of transformations: Animations and autonomous motions, typically
applied in each update, want these to be relative Actions triggered by an occasional event, for
example a user pressing a button, or the object going out of range, use absolute transformation
Actions: Transformations
For translate there are two actions: translate x, y, z; move dx, dy, dz;
Translate is the absolute form, moves the the location giveMove gives a displacement to the objectThe parameters can be either real numbers or variables
Actions: Transformations
For rotate have the following actions: rotx angle; roty angle; rotz angle; spinx dangle; spiny dangle; spinz dangle;
Spin is the relative version
Actions: Transformations
For scale we have the following actions: scale sx, sy, sz; grow dsx, dsy, dsz;
In this case grow is the relative transformation and scale is the absolute transformation
Actions: Transformations
If we want an object to spin about its z axis we need to have the rotation occur in each update
Use the following event handler:on tick
spinz 0.1;
end
Actions: Filtering
Filtering controls the execution of event handlers
Determines whether the event handler should be used, how much of it should be used
One way of controlling what is executed, the if is the other way
Actions: Filtering
There are two filtering actions, both test a single variable: select variable selectinv variable
The first version checks to see if a variable exists and its valueIf the variable doesn’t exist, or its value is zero the event handler is terminated
Actions: Filtering
If the variable has a non-zero value, the event handler continues
Selectinv is the opposite, if the variable has a non-zero value the event handler is terminated, otherwise execution is continued
By setting variables we can control which event handlers are executed
Actions: Event Scheduling
The last set of basic actionsCause an event to occur, either in the current object or another objectThe trigger action causes the event to occur now:trigger object, event;
First parameter is the name of the object, second parameter is the event
Actions: Event Scheduling
The triggered event is immediately executed, before the statements after trigger are executedThe triggerAt action causes the event to be executed at some point in the future:triggerAt object, event, when;
The third parameter is the time when the event will be executed
Actions: Event Triggering
Time is measured in seconds from the current timeExample:triggerAt “fred”, “explode”, 5;
Causes fred to execute its explode event handler 5 seconds from nowSeconds provides a time unit that is independent of update rate
Example
Triggers and selection can be combined to provide some interesting behaviors
Look at an example based on random motion
Start with a simple event handler that moves the object a small amount in each update
Object will follow an erratic random path
Example
Random action used to produce random values for variables:random variable, lower, upper;
Generates a random number between lower and upper and assigns it to the variable
We generate a small random displacement to move the object in the x and y directions
Example on tick
random dx, -0.1, 0.1;random dy, -0.1, 0.1;move dx, dy, 0;if Locx > 8;
set Locx, 0;endigif Locx < -8;
set Locx, 0;endifif Locy > 8;
set Locy, 0;endifif Locy < -8;
set Locy, 0;endif
end
Example
This gives a small amount of random motion, but the object is constantly in motion
We would like it to start and stop, provide a bit more interest
We can use a variable and two events for this
Example
Variable, state, controls execution of tick event handler, switch its value between 0 and 1
Two events are used to switch the value of state: start: sets state to 1, triggers stop after 10 sec. stop: sets state to 0, triggers start after 10 sec
Exampleon stop
set state, 0;triggerAt “test”, “start”, 10;
endon start
set state, 1;triggerAt “test”, “stop”, 10;
endon tick
select state;…
end
Example
How do we get this started?
The first event is sent at the beginning of execution, it is only sent once
Can use this for initialization
Our first event handler sets state to 1 and then schedules a stop event 10 seconds in the future
State Machine
What we have produced is a state machine
a state machine uses one or more variables to encode its state, in our case we have: state = 0: no motion state = 1: random motion
there are also rules for state transitions, in our case every 10 seconds it changes from motion to no-motion and then back again
State Machine
A state machine is a powerful way of organising behaviour
it is conceptually simple, but its simplicity makes it easy to work with
state machines can have many states, and the transition rules can be quite complex
SE allows us to do simple state machines with a few states quite easily
Example
The previous example had random motion, and since each step was small the object stayed in a small area
this motion might be okay for an insect, but doesn’t look very purposeful
we need motion that is more goal directed, straight line motion, not as random
Example
Look at a simple kinematics based model
assign a motion vector or velocity to the object
on each update, velocity added to current position (move does this)
if we hit boundary, bounce back
this can all be done in the tick event handler
Exampleon tick
move dx, dy, 0;if Locx > 8;
set dx, -dx;endifif Locx < -8;
set dx, -dx;endifif Locy > 8;
set dy, -dy;endifif Locy < -8;
set dy, -dy;endif
end
on firstrandom dx, -0.1, 0.1;random dy, -0.1, 0.1;random dt, 10, 20;triggerAt “test”, “change”, dt;
end
Example
The first event handler initialises the motion
it produces the initial values for dx and dy
it also sends the event change at some random point in the future
the variable dt contains a random time between 10 and 20 seconds
this becomes time of the change event
Example
One of the problems with this type of motion is that it can easily get into a loop
the object keeps repeating the same motion
we need to break this up so there is more variety in the motion
the change event handler generates a new random velocity and then reschedules itself for some point in the future
Example
on changerandom dx, -0.1, 0.1;random dy, -0.1, 0.1;print "change direction”;print dx, dy;random dt, 10, 20;triggerAt “test”, “change”, dt;
end
Discussion
We now have some reasonable motions
we could also change the objects orientation when we change its velocity
we could add some possibility for the user to interact with the object
when a key is pressed it changes directions, or stops moving until another key is pressed
Hierarchical Modeling
One of the most powerful modelling tools
build complex objects from simpler ones
this can help us with behaviour as well, we can move the parts with respect to each other
all our behaviors so far involved moving the objects, they didn’t change shape, we can change that now
Hierarchical Modeling
To see how this works we will look at a simple example, a plane
plane consists of two parts: propeller body
the propeller turns as the plane moves in a circular path
Example: Plane
Example: Plane
The model is built from two cubes, we scale them to get the propeller and body shape
this would normally be done in a modeller, but we don’t need it for this example
the main issue is structuring the model and getting the transformations right
this isn’t trivial
Example: Plane
There are two places where we can do transformations: at the mesh level with a copy command at the object level in a first event handler
scaling to get the shape is done at the mesh level, this only applies to the mesh, not the subparts
Example: Plane
The positioning transformations are done in the first event handlersthey then apply to all of the sub objects in the modelour model consists of two parts, the propeller is the child of the plane bodythis all becomes the child of the model that moves the plane
Transformations
Object
Mesh Mesh Mesh
Transformation
Transformation Transformation Transformation
Example: Propeller Object
object propadd mesh georgeon tick
spiny 0.2;endon first
translate 0, 1.1, 0;end
end
Example: Plane Object
object planeadd mesh fredadd object propon first
translate 5, 0, 0;rotz 3.14;
endend
Transformations
Both the prop and plane objects use mesh transformations to scale the cubes
The prop object uses a first transformation to position the propeller, this really isn’t necessary, could be done at the mesh level
The plane object uses first transformations to position the plane and orient it in the right direction
Transformations
The first transformations are applied to the object, thus they are applied to everything inside the object
This includes the propeller, which will now be positioned and oriented correctly and move with the plane
If this was done at the mesh level the propeller wouldn’t be moved
Example: Model Object
object modeladd object planeon tick
spinz 0.01;end
end
Transformations
The final level of the model is the model objectThis object spins the plane on its flight pathWhy do we need this object? Why not spin the plane object?This is due to the order of transformations, the plane would rotate about its z axis and not make a circle
Transformations
If the object is located at the origin (its mesh location is (0,0,0) all object transformations are about the object’s center
The order of transformations is: Scale Rotate z, then y, then x Translate
Transformations
In the plane object we translate the plane away from the origin, the radius of the circle it travels in
This is the last transformation in the plane, so any spin occurs before it
Thus a spin at the plane level only rotates it about its local axis
Example
Lets add one more thing to this example
Let the user control the roll of the plane, rotation about its long axis, which is the y axis
Pressing the a key rotates in one direction, pressing the s key rotates in the opposite direction
New Plane Objectobject plane
add mesh fredadd object propon first
translate 5, 0, 0;rotz 3.14;
endon one
spiny 0.1;endon two
spiny -0.1;end
end
Discussion
We have managed to construct some non-trivial behaviorsObjects have autonomous motions and the user can interact with themThe event based model seems to work reasonably wellWith an expanded set of actions we might bet more interesting motion
Example
Return to the plane example, want the plane to start and stopWhen it stops the rotation of the propeller slows, and the plane gradually stops rotatingFor start the opposite occursThe rotation rate will be variable, we need to replace the constant in spiny and spinz by a variable
Example
But were does the spin rate come from?We need to calculate it, we need an expressionFor propeller use proprate, so in the tick event handler we will have:Spiny proprate;
Need another event handler to adjust proprate
Example
on speedupselect state;eval proprate = proprate*1.1;if proprate > 0.2;
set state, 0;endiftriggerAt “prop”, “speedup”, 0.1;
endon 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 problemTime, we don’t treat time correctlyFrequency of the tick event depends upon processor speedOn fast computers propeller spins faster, objects move fasterAppearance and behavior depend on processor speed
Discussion
We would like things to look roughly the same on all processorsThis means that rotation amount must depend upon the time difference between tick eventsAt 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;