Programming in SecondLife

Post on 01-Feb-2016

51 views 0 download

Tags:

description

Programming in SecondLife. Michael Grobe May 2007 (from work done while at Kan-ed 2005-2006). Introduction. This talk describes some USES of the Linden Script language (LSL). It’s not an introduction to LSL language syntax or programming in general. - PowerPoint PPT Presentation

Transcript of Programming in SecondLife

Programming in SecondLife

Michael GrobeMay 2007

(from work done while at Kan-ed2005-2006)

2

Introduction

• This talk describes some USES of the Linden Script language (LSL).

• It’s not an introduction to LSL language syntax or programming in general.

• The Web tutorial related to this talk is at:

http://people.cc.ku.edu/~grobe/intro-to-LSL

3

Shared (Multi-User) Virtual Environments

• SecondLife– Chat– Avatars– Earth-like environment– Scripting

• MUVE at Harvard (Chris Dede)– More structured educational events and

venues

4

A taxonomy of users/uses

• Avatar remodeling– Clothing– Body components: coloring, skin, etc.– Poses– Movement

• Fabrication– Domiciles (houses, mansions, castles, fortresses, etc.)– Malls – Entertainment venues (vacation spots, theatres, etc.)

• Terra-forming• Entrepreneurial activity• Scripting

All of which can be directed towards socializing and art…

5

…and/or misused…

During May 2007 English newspapers warned that SecondLife could be used for:

- identity misrepresentation - real-world money-laundering and fraud - communication among terrorists - pornography - virtual prostitution - child pornography - virtual pedophilia - virtual child prostitution, and … - don’t forget that…

6

- “Sex with animals is also increasingly popular on the site.”

-- Manchester Guardian

May 9, 2009.

7

Morris welcome center at secondlife.com

8

Morris building area ready for scripting

9

A social gathering

10

Chance encounter

11

My new avatar

12

Building in progress

13

Tour d’Eiffel?

14

Barn and silo exterior

15

Barn interior

16

House interior

17

Large arboreal maze

18

Large breakfast table

19

Kinetic sculpture

20

Fabricated object

21

Metaphysical bookcase

22

Lower shelf

23

LSL is “object oriented,” but not really…

• Almost everything in SecondLife is an object. (Atmosphere and avatars appear to get special handling.)

• Objects can be fabricated by users using the object manipulation interface.

• All scripts are associated with objects, even scripts that manipulate other objects. (To script avatar behavior you must create and object with which the avatar comes in contact (touches, approaches, attaches).

24

LSL object attributes

Objects in the SecondLife environment have many attributes:

• Access attributes– Ownership– Permissions to share, copy, modify, resell

• Appearance attributes– Primitive geometric form (cube, torus, cone, etc.)– Location, rotation– Material (wood, metal, glass, rubber, flesh, light, etc.)– Size, shape, deformation of primitive form– Surface texture and color, transparency, shininess, bumpiness,

etc

• Behavior attributes– Corporeality, permanence, physics– User-defined scripts to change other attributes

25

Object menu/General Tab

26

Object menu/Object Tab

27

Object menu/Content Tab

28

The scripting interface

29

Object menu/Texture Tab

30

The nature of the Linden “Script” language

• LSL syntax strongly resembles Java, C, etc.

• LSL is “event-driven.” The occurrence of events within the virtual world triggers LSL event processors.

• LSL is “state-oriented”. States are collections of event processors, and the processor used to process a specific event will be chosen on the basis of current object state.

31

LSL is not a “general-purpose” language

• LSL only works within the SL environment.• Almost all effects are exerted through built-in

LSL function calls (of which there are many).• It has only restricted I/O capabilities. There is

– no general-purpose graphics capability or library,– no general-purpose GUI layout facility (a’ la Swing)

• Scripts affect only the object in which they reside (or objects they have instantiated); they only react to other objects and send them messages.

• Explicit concurrency control is not supported. Only one scripted event will be in execution at a time.

32

Categories of LSL functions• Agent/Avatar: buy land or objects, sit, touch, get

permissions, control camera, attach objects, start animations, etc.

• Communication: send messages to agents and other scripts

• Collision: react to contact with other objects

• Detection: find “information about objects/agents detected by sensors, touch, and collision events”

• Dynamics/Movement: control object location and apply physical forces/torques

• Inventory: manage the agent’s “inventory”• Link: link objects together and control the assembly• List manipulation

• Math: common functions plus vector and rotation data types

33

Categories of LSL functions (cont.)• Particle: build and control “particle systems”• Primitive (“prims”): manipulate various primitive attributes

• Sensor: invoke sensors to identify the presence of agents and/or objects

• Sound• String

• Target: change object attributes relative to another object (the target) or location.

• Texture: apply and manipulate textures mapped on object surfaces

• Time• Transformation: rotate, scale, translate objects

• Vehicle: create and manipulate vehicles (with physics)• World: interact with time, land, weather, land ownership,

etc.

34

Script to change object color and size

Basic structure

• Define global variables

• Define each event processor to be executed within each state

• The startup state is “default” and it’s usually the only state used.

35

Structure of the default state

The default state in the example script willhave sub-scripts for two events:• “state-entry” processor in the default state

will be executed whenever the object starts up or is moved in-world from Inventory.

• “touch-start” will be executed whenever the object is “Touched”. (If the object is not touched, this code will never execute.

36

Script skeleton

integer counter;

default // the default state.{

state_entry() // execute on start-up{

o o o}

touch_start( integer total_number){

o o o}

}

37

State_entry

state_entry()

{

llSay( 0, "Hello, Avatar! Touch to change color and size.");

counter = 0;

}

38

touch_start outline

touch_start{

// choose three random RGB color components // between 0. and 1.0.

// combine those color components into a vector and // use that vector to set object color.

// choose a random number between 0. and 10 for// use as a scale factor, and set object scale

}

39

touch_start texttouch_start( integer total_number)

{ // do these instructions when the object is touched. counter = counter + 1;

// choose three random RGB color components between 0. and 1.0. float redness = llFrand( 1.0 );

float greenness = llFrand( 1.0 ); float blueness = llFrand( 1.0 );

// combine color components into a vector and use that vector // to set object color. vector prim_color = < redness, greenness, blueness >; llSetColor( prim_color, ALL_SIDES ); // set color to new color.

// choose a random number between 0. and 10. to set scale factor. float new_scale = llFrand( 10.0 ) + 1.0; llSetScale( < new_scale, new_scale, new_scale > ); // set new scale.

llSay( 0, "Touched by angel number " + (string)counter); }

40

Changing box size and color

41

Script to change an object “automatically”integer counter;integer second;

default // the default state.{

state_entry() // execute on start-up{

o o o}

touch_start( integer total_number) // execute on touch{

o o o}

timer( integer total_number) // execute at each timer event{

o o o}

}

42

The touch_start event

touch_start( integer total_number) { counter = counter + 1;

llSay( 0, "Touched by angel number " + (string)counter);

llSetTimerEvent( 2 ); // create a "timer event" every 2 seconds.

}

43

The timer event

timer() // do this on every timer event{

// add one to the “second” counter.

// choose three random RGB color components // between 0. and 1.0.

// combine those color components into a vector and // use that vector to set object color.

// choose a random number between 0. and 10 for// use as a scale factor, and set object scale

if ( second > 19 ) // then wrap things up.

}

44

timer() // do these instructions every time the timer event occurs. { second++;

// choose three random RGB color components between 0. and 1.0. float red = llFrand( 1.0 ); float green = llFrand( 1.0 ); float blue = llFrand( 1.0 );

// combine color components into a vector and use that vector // to set object color. vector prim_color = < red, green, blue >; llSetColor( prim_color, ALL_SIDES ); // set object color to new color.

// a choose random number between 0. and 10 for use as a scale factor. float new_scale = llFrand( 10.0 ); llSetScale(< new_scale, new_scale, new_scale > ); // set object scale.

if ( second > 19 ) // then time to wrap this up. { // turn object black, print "resting" message, and reset object.... llSetColor( < 0, 0, 0 >, ALL_SIDES );

llSay( 0, "Object now resting and resetting script." ); llResetScript(); // return object to ready state. } }

45

Script to change object positions

state_entry() { llSay( 0, "Hello, Avatar! Touch to change position."); counter = 0; startPosition = llGetPos(); }

touch_start(integer total_number) { counter = counter + 1;

llSay( 0, "Touched by angel number " + (string)counter);

llSetTimerEvent( 1 ); // arrange for a "timer event" every second. }

46

timer() // do these instructions every time the timer event occurs.{

second++;

// choose three random distances between 0. and 10.0.float X_distance = llFrand( 10.0 ); float Y_distance = llFrand( 10.0 );float Z_distance = llFrand( 10.0 );

// combine these distance components into a vector and use it to increment the// starting position and reposition the object.vector increment = < X_distance, Y_distance, Z_distance >; vector newPosition = startPosition + increment;llSetPos( newPosition ); // reposition object.

if ( second > 19 ) // then time to wrap this up. { // move object back to starting position...

while ( llVecDist( llGetPos(), startPosition ) > 0.001) { llSetPos( startPosition ); }

llSay( 0, "Object now resting and resetting script." );llResetScript(); // return object to ready state.

}}

47

Particle systemsIt is possible to arrange for an object to generate and emit "particles“

under script control.

For example, an object can be set up to emit bubbles, sparks, fire, etc.

To see this in action, insert a call to llParticleSystem() in touch_event() and another call to turn it off when the object has been returned to its starting position and the script is about to be reset.

The first call should like this one from the Wiki (with params in pairs):

llParticleSystem ( [

PSYS_PART_FLAGS, PSYS_PART_WIND_MASK | PSYS_PART_EMISSIVE_MASK,

PSYS_SRC_PATTERN, PSYS_SRC_PATTERN_EXPLODE,

PSYS_PART_START_COLOR, <1,0,0>]

);

48

Running particle system

49

Rotations in LSL

• There are several ways to represent rotational movement in LSL.

• The Euler representation uses a (3-part) vector where each component specifies movement in one of 3 dimensions.

• The components of a vector, V, holding an Euler rotation, is composed of 3 angular components describing:

Component Movement in plane Around axis Known as --------- ----------------- ------------ -------- V.x Y-Z X roll

V.y X-Z Y pitch

V.z X-Y Z yaw

50

Quaternions

• While the Euler representation is fairly intuitive and is supported within LSL, rotational movments may also be represented as "quaternions."

• Quaternions are implemented within LSL via a built-in variable type, called a "rotation," which is composed of 4 components, x, y, z, and s, all of type float.

• The first 3 components may be thought of as defining an axis around which rotation occurs, while the 4th describes the “amount” of rotation.

51

Using quaternions

• A rotation can be defined using an Euler representation and then converted to quaternion representation, via a call to llEuler2Rot().

• For example, the Euler vector < 0, 10, 0 > defines a rotation of 10 degrees around the Y axis, and can be converted to quaternion form using the call

llEuler2Rot( < 0, 10 * DEG_TO_RAD, 0 >)

yielding a value of

< 0.00000, 0.08716, 0.00000, 0.99619 >.

52

Adjusting position

• When the rotation describing the "current orientation" of an object is known, a new orientation can be derived simply by multiplying one rotation by another.

• Suppose you want to adjust position by 10 degrees around the Y-axis. Here is a rotation to do that:

rotation Y_10 = llEuler2Rot( < 0, 10 * DEG_TO_RAD, 0 > );

• which can then be used to make the adjustment of the “current position/rotation”:

rotation newRotation = llGetRot() * Y_10;

llSetRot( newRotation );

53

Script to place object in orbit

vector rotationCenter; // point to rotate around.

default{

state_entry() {

llSay( 0, "Hello, Avatar!");vector startPoint = llGetPos();rotationCenter = startPoint + < 3, 3, 3 >;

}

touch_start( integer total_number) {

o o o }}

54

touch_start touch_start( integer total_number) { llSay( 0, "Touched." );

// Define a "rotation" of 10 degrees around the z-axis. rotation Z_15 = llEuler2Rot( < 0, 0, 15 * DEG_TO_RAD > );

integer i; for( i = 1; i < 100; i++ ) // limit simulation time in case of { // unexpected behavior. vector currentPosition = llGetPos();

vector currentOffset = currentPosition - rotationCenter;

// rotate the offset vector in the X-Y plane around the distant point . vector rotatedOffset = currentOffset * Z_15; vector newPosition = rotationCenter + rotatedOffset;

llSetPos( newPosition ); } llSay( 0, "Orbiting stopped" ); }

55

Orbit script in large, orange cone

56

The rider’s point of view

57

Physics in SecondLife

• LSL includes the ability to simulate some of the physical laws governing energy, gravity, torque, etc. as they affect objects in-world.

• This ability derives from a set of built-in functions that implements the effects of these laws.

• Physical laws will apply to objects if their “Physics” attribute is checked on the Object tab of the object manipulation menu, or under script control.

58

Physics exampleYou can set physics "on" using the function call

llSetStatus( 1, TRUE )

and then apply a constant upward vertical force using the command

llSetForce( < 0.0, 0.0, Z_force>, FALSE )

on the object.

This function call sets up a force of:

0.0 Newtons along the positive X-axis, 0.0 Newtons along the positive Y-axis, and Z_force Newtons along the positive Z-axis.

The object would eventually go “off-world”, if the force was greater than the force of gravity and not reduced. Otherwise when the force ceases, the object will fall back to earth, and role, or bounce off objects.

59

Simulating artificial life

Karl Sims used a physics engine to simulate the movements of collections of “body parts” and force generators, controlled by neural nets.

He then used genetic algorithms to “evolve” new structures and control networks, “creatures”, particularly organisms that could move under their own power: walk, hop, swim, etc.

60

Physics for creature evolution?

There exist several physics modeling packages:

Open Dynamics Engine, DynaMechs, Newton Game Dynamics.

Small scale versions of creature “evolution” MIGHT be possible in Second Life…

…or perhaps, behavior in SecondLife could be simulated outside of SecondLife to build objects with more effective behavior inside SecondLife.

61

Texture manipulation

It is possible to create texture files that hold multiple individual images that may be used as animation frames.

When a series of such frames is displayed upon the surface(s) of an object many different effects are possible.

For example, an object may be made to appear as if it is rotating, burning, simmering, etc. even though it is not.

62

Texture for animation

63

Texture animation scriptThis script will simulate a flame using the texture

animation just presented, if it is applied to transparent panels intersecting one-another orthogonally:

default{ state_entry() { llSetStatus(STATUS_PHANTOM,TRUE); llSetTexture("lit_texture", ALL_SIDES); llSetTextureAnim (ANIM_ON | LOOP,

ALL_SIDES, 4, 4, 0, 0, 15.0); }}

64

Flaming action

65

Instantiating objects from within scripts

“Rezzing" is the process of creating an object.

Users, acting through their clients/agents can create objects.

However, it is not possible for a script to create an object in this fashion.

Scripts can only "instantiate" (create instances of) objects already created and residing in the inventory (“Contents” tab) of the object in which the script is located.

66

The flamingo gun

The flamingo gun must be “attached” to the avatar’s right hand, and “shoots” spheres in the mouselook direction.

“Shooting” here means they are rezed from inventory and given a momentary acceleration in the direction of the avatar’s gaze.

The spheres have their own physics, controlled by the SL physics engine; they will roll, bounce and collide with objects.

Shooting induces recoil in the gun which is transferred to the shooter (and must be adjusted to avoid excessive backwards movement).

67

Link_setsLSL provides a set of functions for linking and managing sets of primitive objects, sometimes called "link_sets."

Primitive objects that are linked together will behave like a single object in some ways. For example:

- they will move as a single object if repositioned or made physical,

- editing commands will apply to the collection, and

- they can also be set up to communicate with one another and pass certain event information among themselves.

Linked sets could possibly by used to evolve movingobjects a la Sims.

68

Objects from the virtual environment

• Shared Multi-user Virtual Environments allow us to visit the virtual world and fashion objects….

• …but what if we could bring things back?

• …or transfer them to other virtual worlds? The Guardian reports that Linden Labs is working on transfering avatars to other virtual systems. (ANSI standard avatars? OSI Layer 8?)

Bringing things back would require the ability to construct objects in the real world from descriptions built in the virtual world, typically using “machine tools,” some of which already exist.

69

Some (subtractive) machine tools

• Laser cutters cut out 2-D shapes to be used to build 3-D structures

• Sign cutters cut out 2-D shapes in metal used for electrical connections,

• Milling machines move rotating cutting bits in 3-D patterns to define precision (metal) parts

• Waterjet cutters cut plate metal for use as dies or components

70

Additive tools

• Vacuum forming machines (like shrink-wrappers)

• Injection molding for plastic parts (containing various additives)

• “3-D printing” employs techniques similar to laser or ink-jet printing to build 3-D objects in layers:– Shoot the surface of a rising pool of liquid polymer

with a laser to solidify the heated region– Spray portions of the top layer of a rising pile of of

powder with a binder.

71

Fabricating electronic circuitry

• The tools described so far can be used to build (somewhat klunky) user-defined circuits.

• There is ongoing research in 3-D printing of electronic circuitry, as well as in…

• building electronic circuitry from user-defined specifications of behavior.

72

Personal fabricators

• Suppose you could combine these kinds of tools, and drive them from your Personal Computer.

• You would then have a “Personal Fabricator”.

• “We are now on the threshold of a digital revolution in fabrication.” (Gershefeld, 2005)

• “… digitizing fabrication in the same way that communication and computation were earlier digitized.” (Gershenfeld, 2005)

73

Effects of Personal Fabrication

• “Personal fabrication will bring the programmability of the digital worlds we’ve created to the physical world we inhabit.”

• “….the killer app for personal fabrication is fulfilling individual desires rather than merely meeting mass-market needs.”

• PF will redefine “literacy.”

74

Literacy

• “The common understanding of “literacy” has narrowed down to reading and writing, but when the term emerged in the Renaissance it had a much broader meaning as a mastery of the available means of expression. However, physical fabrication was thrown out as an “illiberal art,” pursued for mere commercial gain.

• “..literacy means knowing how to communicate by using all the representations that information can take.” Including fabrication, of course…

• …and now we can add the representations within virtual realities and simulation of physical realities.

75

Additional information

• SecondLife: www.secondlife.com

• Grobe, Michael, Using the Linden Script Language,http://people.ku.edu/~grobe/intro-to-LSL, 2005.

• Sims, Karl, Evolving Virtual Creatures, Computer Graphics, SIGGRAPH ’94 Proceedings, July 1994.

• Gershenfeld, Neil, Fab: The coming revolution on your desktop-from personal computers to personal fabrication, Basic Books, New York, 2005.

• Chaumont, Nicolas, et al., Evolving virtual creatures and catapults, Artificial Life 13 (2007), MIT Press.