Browse DevX
Sign up for e-mail newsletters from DevX


A Speed Guide to Virtual Driving in Second Life : Page 3

Find out how to use Linden Scripting Language to control virtual vehicles in Second Life.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Starting and Stopping the Car
Second Life calls the changed event handler whenever an avatar enters (sits in) or leaves (stands up from) the car. The changed event handler must accommodate three basic scenarios: the owner sitting down, a non-owner sitting down, or the owner standing:

changed(integer change) {

The changed event handler begins by checking to see if someone has sat down:

if (change & CHANGED_LINK) {

If someone did sit down, make sure it's the vehicle's owner.

key agent = llAvatarOnSitTarget(); if (agent) {

If the avatar sitting down is not the owner, inform them that only the owner can drive the car, and force the avatar to stand up:

if (agent != llGetOwner()) { llSay(0, not_owner_message); llUnSit(agent); llPushObject(agent, <0,0,50>, ZERO_VECTOR, FALSE); } else {

Otherwise, the avatar is the owner, so play the car_start sound:


After waiting for the sound to play, you need to turn physics on. Objects with physics enabled can fall and have force applied to them. Most objects in SL are non-physical:

llSleep(.4); llSetStatus(STATUS_PHYSICS, TRUE); llSleep(.1);

Finally, you must request permission to take the avatar's controls. When the avatar attempts to move, the car (your script) will receive those inputs:

llRequestPermissions(agent, PERMISSION_TRIGGER_ANIMATION | PERMISSION_TAKE_CONTROLS); llLoopSound("car_run",1); } } else {

When the driver stands up, stop the sound, turn off physics, and release the controls. The llTargetOmega call stops any car movement that might be in progress:

llStopSound(); llSetStatus(STATUS_PHYSICS, FALSE); llSleep(.4); llReleaseControls(); llTargetOmega(<0,0,0>,PI,0); llResetScript(); } } }

Controlling the Car
The control event handler begins by setting up a few variables that the handler needs. Because cars turn differently when in reverse, you must keep a flag that indicates the current direction. Another variable holds the direction of the angular motor:

control(key id, integer level, integer edge) { integer reverse=1; vector angular_motor;

Next, the script obtains the current speed of the car, which it uses when turning. Cars do not turn when they are not in motion:

//get current speed vector vel = llGetVel(); float speed = llVecMag(vel);

Next, the script checks each of the relevant controls, starting with the Forward control. When the user presses Forward, the script uses the linear motor to apply force to move the car forward. Also note that the script sets the reverse variable to one:

//car controls if(level & CONTROL_FWD) { llSetVehicleVectorParam( VEHICLE_LINEAR_MOTOR_DIRECTION, <forward_power,0,0>); reverse=1; }

If the user presses Back, then the script applies power in the opposite direction, and stores the fact that the car is in reverse by setting the reverse variable to -1:

if(level & CONTROL_BACK) { llSetVehicleVectorParam( VEHICLE_LINEAR_MOTOR_DIRECTION, <reverse_power,0,0>); reverse = -1; }

For a right or left turn, you use the angular motor to rotate the car in the z-coordinate by the specified angle, being sure to take into account whether the car is in reverse:

if(level & (CONTROL_RIGHT|CONTROL_ROT_RIGHT)) { angular_motor.z -= speed / turning_ratio * reverse; } if(level & (CONTROL_LEFT|CONTROL_ROT_LEFT)) { angular_motor.z += speed / turning_ratio * reverse; }

Now you can set the angular motor:

llSetVehicleVectorParam( VEHICLE_ANGULAR_MOTOR_DIRECTION, angular_motor); }

Figure 3. Car in Motion: Here's the author's avatar driving the car.
Together, these event handlers let users drive the car using the same controls they use to move their avatars. Figure 3 shows my avatar, Encog Dod, driving the car.

You can get a copy of the car object described in this article in Second Life at this SLURL.

That's all there is to creating and controlling a simple car in Second Life—but don't feel that you've covered the full extent of Second Life vehicles. Other land vehicles, such as tanks, trucks, sleds, and motorcycles are also common, as are air and sea vehicles. If you're interested in learning about other vehicle types, my book "Scripting Recipes in Second Life" covers all three types of vehicles: land, air, and sea.

Jeff Heaton is an author, college instructor, and consultant. Jeff is the author of four books and over two dozen journal and magazine articles. Jeff maintains a personal website where he publishes information about artificial intelligence, spider/bot programming, and other topics.
Thanks for your registration, follow us on our social networks to keep up-to-date