RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


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

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

Selecting Car Materials
The default material type for Second Life is wood. Unless the object is going to move, the material type does not really matter. Most builders simply leave the material type set to the default wood material. Probably 95 percent of the Second Life world is made of wood. You can select the "edit" option from a prim's menu to set the material type.

Here's where things get interesting.

The material you choose for the tires is very important. Any part of the vehicle that comes into contact with the ground must have its material specifically set. Second Life supports the following material types: stone, metal, glass, wood, flesh, plastic, and rubber. Really, all that a material type specifies is the mass and friction of an object.

It may seem logical that the wheels should be made from rubber, but rubber is actually a bad choice for tires in Second Life, because it has the most friction of any material. In Second Life's primitive physics system, the wheels do not actually "turn" to allow the car to move. A car with square tires could move just as easily. Think of the rubber tires as coming into contact with the ground, and the car being pushed with the parking brake on. In real life, such a car would hop along the ground in a bumpy and uncomfortable way. And if you choose rubber for your tire material type, your car will also bump along the ground, as the vehicle force overcomes the rubber resistance.

Instead of rubber, almost all land vehicles in Second Life use glass for their tire material. Glass has the least amount of resistance, so it makes sense as a wheel material type. Durability is not an issue. The glass tires will not shatter.

Understanding the Car Script
After building the simple car, you can script it. The car script begins by defining some constants that define how the car will operate. Of course Linden Scripting Language does not support "true constants." The values below could be changed, but the car script only reads their values. For your convenience, you can download the complete script or view it in Listing 1. Here are the constants that begin the script:
   //Power used to go forward (1 to 30)
   float forward_power = 15; 
   //Power used to go in reverse (-1 to -30)
   float reverse_power = -15; 
   //How sharply the vehicle turns. Less is more sharply. (.1 to 10)
   float turning_ratio = 2.0; 
   //Sit message 
   string sit_message = "Ride"; 
   //Not owner message
   string not_owner_message = 
      "You are not the owner of this vehicle ..."; 
You can change the preceding values to affect the car's handling, making it faster or slower, or affecting its turning ability.

The car has only one state, the default state. The script for that state sets up the car:

First, the script changes the "sit text." This causes the option to enter the car to be "ride" rather than the default "sit" in the Second Life menu system. Additionally, the script defines a sit target for the avatar. This prevents the avatar from sitting "inside" the seat, rather than on top of it:

         llSitTarget(<0.2,0,0.45>, ZERO_ROTATION );
The SL "camera" follows the car automatically, just as the camera automatically follows avatars. However, for the car, it's better to move the camera 5 meters above and 8 meters behind the car. This provides both a good view of the car, and the area around it:

         llSetCameraEyeOffset(<-8, 0.0, 5.0>);
         llSetCameraAtOffset(<1.0, 0.0, 2.0>);
The car uses two sounds, playing the car_start sound when the car starts up, and the car_run sound in a loop as the car runs:

Second Life vehicles move using two motors. The "linear motor" moves the vehicle forward or backward. The "angular motor" turns the car about any of the three axes. You define these motors by setting vehicle parameters.

The vehicle type is set to car. This gives Second Life a hint about what vehicle type you're creating. Setting the vehicle type also enables the two motors:

Here are some other vehicle parameter settings that you'll need:

Angular deflection is the tendency of a vehicle to move in certain directions. For example, a car will tend not to move in the z-coordinate (up and down). The angular deflection efficiency determines how effective angular deflection is. A value of 0.2 specifies angular deflection at 20 percent. This allows the car to turn fairly easily:

A value of 0.8 specifies a linear deflection power of 80 percent. The value specifies the effort it takes for the car to change its linear velocity:

Because real-car directional changes are not instantaneous, it takes this car one tenth of a second for both linear and angular deflection to kick in:

It also takes one second for the linear motor to reach full power:

The linear motor power drops off in one fifth of a second, meaning the car will not coast well:

Remember that there are two motors. The angular motor also reaches full power in one tenth of a second:

But the angular motor power drops off in one half of a second. The car will stop turning fairly quickly when the user lets up on the control:

Friction affects the car only in the y-coordinate, which is how the car moves forward and backwards. The car can fall or turn quickly:

            <1000.0, 2.0, 1000.0>);
The car rotates fairly easily in the z-coordinate, but less easily in the x and y coordinates:

            <10.0, 10.0, 1000.0>);
A car should (preferably) always stay right side up. The vertical attraction setting allows this:

These values work well for a car. However, if you were building a boat or helicopter they would need to be considerably different.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date