June 30, 2007

LSL-script for a Second Life football

I have been looking into the Linden Script Language (LSL) to be able to do more cool stuff within Second Life.

Here's a small script for a football. The ball can be kicked by just walking against it. Touching it causes the ball to bounce up in the air a couple of feet. You can also talk to it by saying "kick" which causes the ball to move with a certain vector strength and direction. "High kick" makes the ball going forward up in the air.

default {
state_entry () {

llListen (0, "", "","");
// Make sure that Physical is turned on
llSetStatus (1, TRUE);
}

collision_start (integer total_number) {

// Get the vector that points from the kicker to the ball
vector diff = llGetPos() - llDetectedPos(0);

// Force the z coordinate up
diff.z = .1;


// Scale the vector to a fixed length
vector kick_vector = llVecNorm(diff)*5;

// Apply the kick vector to the ball

llApplyImpulse(kick_vector, FALSE);
}

listen (integer channel, string name, key id, string message) {
if (message == "kick") {

// Get the vector that points from the kicker to the ball
vector diff = llGetPos() - llDetectedPos(0);

// Force the z coordinate up
diff.z = .1;

// Scale the vector to a fixed length
vector kick_vector = llVecNorm(diff)*10;


// Apply the kick vector to the ball
llApplyImpulse(kick_vector, FALSE);
}
else if (message == "high kick") {
// Get the vector that points from the kicker to the ball
vector diff = llGetPos() - llDetectedPos(0);


// Set the z-coordinate high up
diff.z =500;


// Scale the vector to a fixed length
vector kick_vector = llVecNorm(diff)*25;

// Apply the kick vector to the ball

llApplyImpulse(kick_vector, FALSE);
}
}

touch_start (integer total_number) {
// Create a manual kick vector where only the z-coordinate has a value separate from zero
vector kick_vector = <0.0,0.0,0.0>;

// Apply the kick vector to the ball
llApplyImpulse(kick_vector, FALSE);
}
}


Copy/type the script into the script editor pane when creating/replacing a new script for an object. Remember you have to be in a place where scripting is allowed to modify it and see the changes in action. Second Sweden allows it and there are many more places.
























Here are a couple of reference pages to get you started:

- This article is good to get you started. I have taken some lines of code from here.
- LSL Portal
- Second Life Tutorials

Technorati tags:
, , , ,


6 comments:

Anonymous said...

Nice. I tried it out for myself and it works well. When I was playing in an open field it worked fine, but when I was playing in a user-created pitch, it kept bouncing against the sides. I'm not sure if this was just my client, or a glitch in the code/game.

Niklas Waller said...

Hi Giano,
You're so right and I am quite aware of it. The problem is that when the ball is in a user-created pitch the walls, objects etc. are treated as an avatar by the ball.

When an avatar touches the ball the ball is moved by a certain vector. The same thing happends when a wall or furniture touches the ball (which is all the time when inside a building). That's why it is impossible to play a decent indoor soccer game with this script.

It should be possible to tell the ball to only act on avatars. I'll look into it and update.

Thank's for your comment!

Anonymous said...

I'm building a futsal court inworld and am looking for a decent football. Interested in creating a script for it?What's your avatar name? Mine is Milo Milo :]

Niklas Waller said...

Hi,
I might just be. Let's meet and see what you have done and are about to create.

My Second Life avatar name is Nicolas Paravane.

Anonymous said...

nice script try to use AGENT. If you are interested on soccer sl, look for "tim stadium", its the stadium of a soccer league. Good luck ;)

Anonymous said...

there are already a great soccer system in second life called second.futbol
there u can join and create teams & national teams for free

http://maps.secondlife.com/secondlife/Jericho%20Hill/89/237/1511