behavior mark green school of creative media. introduction we’ve done the easy part geometry is...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/1.jpg)
Behavior
Mark Green
School of Creative Media
![Page 2: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/2.jpg)
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
![Page 3: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/3.jpg)
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
![Page 4: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/4.jpg)
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
![Page 5: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/5.jpg)
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
![Page 6: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/6.jpg)
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
![Page 7: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/7.jpg)
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
![Page 8: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/8.jpg)
Event Response
Geometry
Event handler
Event handler
Event handler
Event handler
Tick event
Trigger event (user)
Collision event
Designer event
![Page 9: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/9.jpg)
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??
![Page 10: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/10.jpg)
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
![Page 11: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/11.jpg)
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
![Page 12: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/12.jpg)
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
![Page 13: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/13.jpg)
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
![Page 14: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/14.jpg)
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
![Page 15: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/15.jpg)
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
![Page 16: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/16.jpg)
Details
SE
Script C++ Objects
Environment
![Page 17: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/17.jpg)
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
![Page 18: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/18.jpg)
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
![Page 19: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/19.jpg)
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
![Page 20: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/20.jpg)
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
![Page 21: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/21.jpg)
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)
![Page 22: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/22.jpg)
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
![Page 23: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/23.jpg)
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
![Page 24: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/24.jpg)
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?
![Page 25: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/25.jpg)
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
![Page 26: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/26.jpg)
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
![Page 27: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/27.jpg)
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
![Page 28: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/28.jpg)
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
![Page 29: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/29.jpg)
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
![Page 30: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/30.jpg)
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
![Page 31: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/31.jpg)
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
![Page 32: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/32.jpg)
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?
![Page 33: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/33.jpg)
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 ;
![Page 34: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/34.jpg)
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
![Page 35: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/35.jpg)
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
![Page 36: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/36.jpg)
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
![Page 37: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/37.jpg)
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
![Page 38: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/38.jpg)
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
![Page 39: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/39.jpg)
Actions: Control Flow
Similar to if statements in most programming languages
expressions can have comparison operators:==
!=
<
<=
>
>=
![Page 40: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/40.jpg)
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
![Page 41: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/41.jpg)
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
![Page 42: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/42.jpg)
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
![Page 43: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/43.jpg)
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
![Page 44: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/44.jpg)
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
![Page 45: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/45.jpg)
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
![Page 46: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/46.jpg)
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
![Page 47: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/47.jpg)
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
![Page 48: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/48.jpg)
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
![Page 49: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/49.jpg)
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
![Page 50: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/50.jpg)
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
![Page 51: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/51.jpg)
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
![Page 52: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/52.jpg)
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
![Page 53: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/53.jpg)
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
![Page 54: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/54.jpg)
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
![Page 55: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/55.jpg)
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
![Page 56: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/56.jpg)
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
![Page 57: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/57.jpg)
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
![Page 58: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/58.jpg)
Exampleon stop
set state, 0;triggerAt “test”, “start”, 10;
endon start
set state, 1;triggerAt “test”, “stop”, 10;
endon tick
select state;…
end
![Page 59: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/59.jpg)
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
![Page 60: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/60.jpg)
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
![Page 61: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/61.jpg)
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
![Page 62: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/62.jpg)
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
![Page 63: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/63.jpg)
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
![Page 64: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/64.jpg)
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
![Page 65: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/65.jpg)
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
![Page 66: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/66.jpg)
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
![Page 67: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/67.jpg)
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
![Page 68: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/68.jpg)
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
![Page 69: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/69.jpg)
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
![Page 70: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/70.jpg)
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
![Page 71: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/71.jpg)
Example: Plane
![Page 72: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/72.jpg)
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
![Page 73: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/73.jpg)
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
![Page 74: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/74.jpg)
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
![Page 75: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/75.jpg)
Transformations
Object
Mesh Mesh Mesh
Transformation
Transformation Transformation Transformation
![Page 76: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/76.jpg)
Example: Propeller Object
object propadd mesh georgeon tick
spiny 0.2;endon first
translate 0, 1.1, 0;end
end
![Page 77: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/77.jpg)
Example: Plane Object
object planeadd mesh fredadd object propon first
translate 5, 0, 0;rotz 3.14;
endend
![Page 78: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/78.jpg)
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
![Page 79: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/79.jpg)
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
![Page 80: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/80.jpg)
Example: Model Object
object modeladd object planeon tick
spinz 0.01;end
end
![Page 81: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/81.jpg)
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
![Page 82: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/82.jpg)
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
![Page 83: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/83.jpg)
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
![Page 84: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/84.jpg)
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
![Page 85: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/85.jpg)
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
![Page 86: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/86.jpg)
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
![Page 87: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/87.jpg)
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
![Page 88: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/88.jpg)
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
![Page 89: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/89.jpg)
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
![Page 90: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/90.jpg)
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
![Page 91: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/91.jpg)
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
![Page 92: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/92.jpg)
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
![Page 93: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/93.jpg)
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
![Page 94: 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](https://reader036.vdocuments.net/reader036/viewer/2022062518/5697bf6f1a28abf838c7d204/html5/thumbnails/94.jpg)
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;