75
Programming in SecondLife Michael Grobe May 2007 (from work done while at Kan-ed 2005-2006)

Programming in SecondLife

  • Upload
    cleave

  • View
    54

  • Download
    0

Embed Size (px)

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

Citation preview

Page 1: Programming in SecondLife

Programming in SecondLife

Michael GrobeMay 2007

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

Page 2: Programming in SecondLife

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

Page 3: Programming in SecondLife

3

Shared (Multi-User) Virtual Environments

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

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

venues

Page 4: Programming in SecondLife

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…

Page 5: Programming in SecondLife

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…

Page 6: Programming in SecondLife

6

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

-- Manchester Guardian

May 9, 2009.

Page 7: Programming in SecondLife

7

Morris welcome center at secondlife.com

Page 8: Programming in SecondLife

8

Morris building area ready for scripting

Page 9: Programming in SecondLife

9

A social gathering

Page 10: Programming in SecondLife

10

Chance encounter

Page 11: Programming in SecondLife

11

My new avatar

Page 12: Programming in SecondLife

12

Building in progress

Page 13: Programming in SecondLife

13

Tour d’Eiffel?

Page 14: Programming in SecondLife

14

Barn and silo exterior

Page 15: Programming in SecondLife

15

Barn interior

Page 16: Programming in SecondLife

16

House interior

Page 17: Programming in SecondLife

17

Large arboreal maze

Page 18: Programming in SecondLife

18

Large breakfast table

Page 19: Programming in SecondLife

19

Kinetic sculpture

Page 20: Programming in SecondLife

20

Fabricated object

Page 21: Programming in SecondLife

21

Metaphysical bookcase

Page 22: Programming in SecondLife

22

Lower shelf

Page 23: Programming in SecondLife

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).

Page 24: Programming in SecondLife

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

Page 25: Programming in SecondLife

25

Object menu/General Tab

Page 26: Programming in SecondLife

26

Object menu/Object Tab

Page 27: Programming in SecondLife

27

Object menu/Content Tab

Page 28: Programming in SecondLife

28

The scripting interface

Page 29: Programming in SecondLife

29

Object menu/Texture Tab

Page 30: Programming in SecondLife

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.

Page 31: Programming in SecondLife

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.

Page 32: Programming in SecondLife

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

Page 33: Programming in SecondLife

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.

Page 34: Programming in SecondLife

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.

Page 35: Programming in SecondLife

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.

Page 36: Programming in SecondLife

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}

}

Page 37: Programming in SecondLife

37

State_entry

state_entry()

{

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

counter = 0;

}

Page 38: Programming in SecondLife

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

}

Page 39: Programming in SecondLife

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); }

Page 40: Programming in SecondLife

40

Changing box size and color

Page 41: Programming in SecondLife

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}

}

Page 42: Programming in SecondLife

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.

}

Page 43: Programming in SecondLife

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.

}

Page 44: Programming in SecondLife

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. } }

Page 45: Programming in SecondLife

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. }

Page 46: Programming in SecondLife

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.

}}

Page 47: Programming in SecondLife

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>]

);

Page 48: Programming in SecondLife

48

Running particle system

Page 49: Programming in SecondLife

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

Page 50: Programming in SecondLife

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.

Page 51: Programming in SecondLife

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 >.

Page 52: Programming in SecondLife

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 );

Page 53: Programming in SecondLife

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 }}

Page 54: Programming in SecondLife

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" ); }

Page 55: Programming in SecondLife

55

Orbit script in large, orange cone

Page 56: Programming in SecondLife

56

The rider’s point of view

Page 57: Programming in SecondLife

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.

Page 58: Programming in SecondLife

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.

Page 59: Programming in SecondLife

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.

Page 60: Programming in SecondLife

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.

Page 61: Programming in 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.

Page 62: Programming in SecondLife

62

Texture for animation

Page 63: Programming in SecondLife

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); }}

Page 64: Programming in SecondLife

64

Flaming action

Page 65: Programming in SecondLife

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.

Page 66: Programming in SecondLife

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).

Page 67: Programming in SecondLife

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.

Page 68: Programming in SecondLife

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.

Page 69: Programming in SecondLife

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

Page 70: Programming in SecondLife

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.

Page 71: Programming in SecondLife

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.

Page 72: Programming in SecondLife

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)

Page 73: Programming in SecondLife

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.”

Page 74: Programming in SecondLife

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.

Page 75: Programming in SecondLife

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.