Scripting a Swinging Door, with Linking

I’ve now been talking about doors for several sections, and created a plain door and then made it slide back and forth. Why didn’t I start with a swinging, or rotating, door, the kind we are most familiar with in real life?

Although it is possible to build a swinging door with just one prim, I think the better example is done with two, and that’s what I’ll explain how to do in this section. Let’s start with a few ideas around the door, literally, and then get to the door and the necessary script.

Wall section hollowed out for door

When I built a simple textured door from a single prim, I said nothing of how to fit it into a wall. Wall sections are also often built from rectangular prims, though if they are longer than 10 meters, they must be made from multiple aligned prims. How do you make a hole in the wall in which to place the door? There are several options. Although I have done only very simple reshaping of the cube prim to turn it into a door, it is possible to twist, taper, shear, and hollow it as well. You can also cut away part of the primitive, but only in a very limited way. We could end up with something that really looks nothing like the original cube.

One way to get the wall opening is to start with a cube and then hollow it out a bit, rotate it 90 degrees top to front, and then adjust the sizes. This has the advantage of only using one prim, but it won’t always work for every door size and all heights. In addition, it’s a little tricky to get the texture right inside the hollowed out portion. (You need to use the dimensions for the outside of the wall section, not the inside.) You can see such a wall section to hold a door in the picture on the right.

An alternative method is to place two wall sections with a gap between them that is the width of the door. Then create a small section that is the width of the door and place it in the gap above the door. In the section on basic doors, I created a door that was 1.75 meters wide by 3 meters high. If my wall height is 5 meters, then I need to create a small wall section that is 1.75 meters wide and 2 meters high, where 2 meters = 5 meters (= wall height) – 3 meters (= door height). The Z position of this small wall section will be 2.5 meters bigger than Z position of the door. That is, to get from the center of the 3 meter high door to its top edge, you need to go up 1.5 meters. Then to get to the center of the 2 meter high wall section, you need to go an additional 1 meter.

Three piece wall section for door

I’ve illustrated this technique in the picture on the right and used colors to make the wall sections stand out more clearly. The side wall sections are green and the piece over the door is red. Though I used three prims this way versus only one prim the first way, this is the more versatile technique. In addition, I could potentially make the side pieces as big as 10 meters by 10 meters, so I might end up saving prims anyway. Finally, if my architectural design allowed the door to be at the end of the wall, I might only end up using two prims.

For small doors that are not rectangular and are arched, the first technique is probably the one to use. You use a circular hollowing shape and then adjust the width and height of the door to get the right partial ellipse. That’s how I built the door opening illustrated at the beginning of the sliding door scripting section.

One of the problems with the three-prim wall opening is that the three pieces are independent. If I need to move the wall 1 meter to the left, I need to move each prim individually. These is tedious. Large building projects can use hundreds if not thousands of prims, so we need a way of connecting several of them together so we can can move as many of them as possible together as solid units. This is called linking.

Linking is an essential building skill for construction in Second Life. Our two-prim swinging door example depends on it, so let’s delve into the technique, starting with linking together prims.

Think of linking as a way of gluing together prims with the ability to selectively unglue all or part of the result. To start linking, you need to be in Edit mode. There are several ways of getting into this state. The easiest is to press Ctrl-3, though I find that hard to remember. If you like buttons, click the Build button on the bottom of the SL screen, and then click the Edit button near the top of the dialog box that opens. A third method is to right-click or Cmd-click an object, and then choose Edit from the pie-shaped menu that opens.

If the prims you want to link are close together, then press and hold down the (left) mouse button and drag out a rectangle around the prims. You will see the different prims light up with white outlines as they get selected and then all will have yellow outlines when you release the mouse button. Press Ctrl-L to link. Alternatively, choose Tools > Link from the top menu bar.

Once linked, the prims will be outlined in blue except for one special one, the root prim. I’ll describe some properties of this in a bit, but note for now that it is the very first prim that got selected in the click-drag method.

Another way to link is to hold down the Shift key and then just click the prims you want to include. If you accidentally include something unintended, just Shick click it again and it will deselect. When you are done, press Ctrl-L to link. With this technique, it’s the last prim you selected that becomes the root prim. These linked-together prims now form a multi-prim object.

An object is one or more prims linked together. The center of an object is the center of the root prim. When you look at the Object tab in the Edit dialog box for one of these linked objects, the information displayed is for the linked prim. In particular, an object rotates around the center of the root prim. You really need to remember this.

To unlink an object, select it while in Edit mode and press Ctrl-Shift-L or use Tools > Unlink from the menu bar. All component prims will still be highlighted. To immediately relink, press Ctrl-L. To remove a prim from a linked object, select the whole object, press Ctrl-Shift-L, then hold down Shift and click the prim you wish to exclude. Now press Ctrl-L to relink everything else.

Linked objects can be no more than 30 meters in any dimension and you can’t link more than 255 prims together. You may just have to live with multiple linked objects in a big project. You’ll get an error message if you try to link things that are too far apart.

You can link two objects together or add a prim to a linked object using the linking techniques above. Suppose you had one object that was two prims linked together and another that had seven prims linked together. If you linked them, you would have one object with nine component prims. If you then unlinked this, you would not get your two objects back. Rather you would get the nine separate prims.

Simple rotation of a door

Be very careful when deleting a linked object because the whole and potentially very large object will be sent to your Trash in your Inventory. The SL undo facility is not very good and rather hit or miss, so if you do delete something unintentionally, you’ll need to drag it into your SL working space fromt Trash and then manually reposition it. I find I usually make this sort of mistake late at night when I’m rushing to finish building something. My analogy is that one last run on the ski slopes when you get hurt. If you find yourself getting careless while building, take a break or get some sleep.

Let’s now go back to our door. If we just rotated the door 90 degrees, we would see something similar to what is in the picture on the right. What happened? The door rotated around the vertical axis that passes through the center of the door! This is not what we want for our swinging door. We want the door to rotate around one of the side edges of the door.

Think about the wooden doors you have seen. How are they attached to their frames? Hinges! Real life doors may use, two, three, four or more hinges, but we only need one in SL.

The idea is this: we’ll create a very thin hinge and put our rotation script in it. We’ll then position the hinge so it is just on the edge of the door around which we wish to rotate. Then we’ll link the hinge to the door, making sure that the hinge is the root prim. When you click the door, that action will get transmitted to the hinge and it will rotate around it’s vertical center axis. Since the door is linked to the hinge, it to will rotate around the hinge’s center vertical axis.

Here is the variation of the sliding door script that we’ll use now to make the hinge and door rotate.

Most of this requires no variation from door to door. The delay variable holds the number of seconds to wait until the door automatically closes. If you don’t want it to do this, set delay to 0.0.

A door swings in or out, and those directions are relative to how you have placed the door. Experiment with using either 1.0 or -1.0 for direction to get the door to swing in the correct way. Note that I’ve coded this so that the door swings 90 degrees, or PI/2.0 in radians. If you make direction a bit larger or smaller than 1.0 or -1.0, the door will open wider or narrower.

Door with hinge

Now we need to create the hinge. The door is .25 meters thick and 3 meters high, so we’ll just create a single hinge that is .2 meters wide, .01 meters thick and .5 meters high. We’ll position it right on the edge of the door. Using the instructions for creating a basic door, go ahead and create the hinge. Color it black with a plain texture from the Texture editing tab. You can see my version in the picture to the right.

To position the hinge, set its Z position value to the same as the door. Setting the X and Y values will vary by where you created your door. You will want the hinge to be positioned in the middle of the depth of the door, and one-half the width of the door away from the center of the door. In my case, the door is 1.75 meters wide, so that means I would add or subtract .875 from the position value along the axis that goes along the width of the door.

Confused? Start by setting the X position value of the hinge to be the X value of the door. If that moves the hinge in line with the center of the depth of the door, then move on to setting the Y position value of the hinge equal to the Y position value of the door plus or minus .875. If this doesn’t work, reverse X and Y in the above. In the worst case you’ll get a better understanding of the arithmetic involved.

Now add the above script to the hinge. Edit the hinge and choose the Content tab. Press the New Script button. Right- or Cmd-click the name “New Script” and choose Rename. Call it “Open and Close Door” and press Enter or Return. Right- or Cmd-click the name again and choose Open. Copy and paste the above script over the existing short default script (the “Hello Avatar” one). Save the script and make sure the Running checkbox is indeed checked.

Now link the hinge to the door. In Edit mode, Shift click the door and then Shift click the hinge. Press Ctrl-L. Go out of edit mode. Click the door. It should open. Click it again. It should close. Click it again. It should open again. Wait three seconds. It should close.

If you need to adjust the direction or the delay, just right- or Cmd-click the door and choose Open. Double click the name of the script and adjust those parameters.

Finally, make sure you give the door a good descriptive name on the General tab and take a copy into your Inventory.

There are several variations you can make to this, including placing the hinge slightly inside the door. Just move it a smidgen so that it is not visible. Personally, this is what I do because I don’t want to see the hinge. The effect seems more magical, somehow.

This door will open and close for anyone. A more advanced door script might only open for certain named people or people who belong to a particular group. I would program this by adding another function that returns true or false if the person clicking or walking into the door is allowed to open it. Another option is to have a magic word that opens the door. The door needs to be listening for it, but this is also an effective way of unlocking and opening the door selectively. You can purchase fancy doors and scripts that do all this, but I think you are now well on your way to doing it yourself.

← Previous ↑ Up Next →

Print pagePDF pageEmail page