Building Virtual Worlds

Moshell - Fall 98

Lecture 5a: VRML Scripts, Slightly

Today we're moving through Chapter 2 of the CPC Tutorial. The material in Chapters 3, 4 and 6+ is all useful, but you're big kids and can do them yourselves.... We will defer practicing with Chapter 5 until next week, as we've got some interesting work to do on today's Queries. (I do expect to lecture & demonstrate Chapter 5 as part of this upcoming lecture, however.)

Begin with Chapter 2. As it says, 2.1 and 2.2 involve creating a cone and touch-sensor and naming the touch-sensor TS1.. Do it.

Chapter 2. Step 2.3 is a bit tricky. The instructions are to click "View/Appearance" on the menu in the Main window. However if you just do this, you are likely to get a display that says "not available". This is because you haven't selected a scene element that has an Appearance node. Select View/Scene Graph, then click on world/transform/shape/appearance. This is the appearance node of the shape (cone) we're working with.

Even though we're about to work with a Java script node, don't worry - you don't have to learn Java. I will ask the Java Experts in class to identify themselves. If you subsequently get in trouble, ask one of them for advice. That's what I'll do, anyhow.
 
 When you come to step 2.7, the Java Console window must be visible to see the output. Minimize other windows until you see a window titled 'Vscp java console output.' It will display the message "_inBoolCB() called: true" and then false, etc. as you move the mouse over the cone and off again.

Click the square-box STOP button to return to editing the Java code.

After you make the changes to the Java code described in 2.9, don't forget to right-click and COMPILE the Java again. Then proceed to 2.10 and test it.

Now we get to some creative work.

Query 5.1. Add a box  to your world. See if you can figure out how to attach a touch-sensor to it, and to route the touch-sensor's "isActive" event to the Navigationinfo.headlight. When you run this world, you will be able to turn the headlight on and off by clicking on the cube.

If you get stuck, instructions are below on how to do this query. But you will learn more by fumbling around and trying to solve it yourself.

Query 5.2. Experiment with the Text node to position the word ON, in red letters, on the front of the cube. Hints for this Query are also found below.

Query 5.3. The annoying thing about this example was that the light only stays on while you hold down the mouse button. Can you figure a way to make our example into a two-button switch, by adding another cube with a green OFF label on it?

And finally:

Query 5.4: If you designed your answer to Query 5.3 in such a way that the light only goes off upon the RELEASE of the mouse-click on the OFF box, how would you improve your script so that it goes off upon the PRESS of the mouse-click on the OFF box?

If you designed your answer so that the light goes off on the PRESS of the OFF-box mouse click, I bet you already have figured out how to make it go off on the RELEASE thereof. If not, figure that out now, too.
 

Now I need to insert some text here so that you won't just look down below and read the HINTS without realizing it!


HINT for Query 5.1. Initially the box will be so big that it covers up the cone. You can see its X, Y and Z dimensions by double clicking on the Box node in the scene graph. Modify its size so that it's a cube with side length 0.5 meters.Then modify the transform in which it lives, so that its translation is +4 meters in Y direction. The cube should appear up above the cone. Name the box. Now, by analogy to the cone, construct a touch-sensor on the box. Give the sensor a name, like TS2.

In the scene graph browser, select NavigationInfo and give it a name. Now you can route the TS2.isActive message to the navigation info's headlight. Test the results.

HINT for Query 5.2. Adding a Text object to the scene is done by dragging the big T down into the window. But its transform will wind up attached to the World, and you want it attached to the Box's transform. So drag the text's transform (in the scene graph) onto the Box's transform.

Now you can work with the Text's transform to move it around - IN THE COORDINATE SYSTEM of the box. This means that if its translation is 0,0,0 then it's in the middle of the box. Translate the text by -.1, -.2, 0.4. That should move it out in front of the box. Be able to explain, interms of the cube's size, why these numbers work.

HINT for Query 5.3. I don't know of any way to do this without providing some simple logic via Java. What you obviously (?) need is a way to strip off the TRUE events only (or the FALSE events only) from an event stream. That way you can run the TRUE isActive events from your ON box to the headlight, but filter out the FALSE events which would turn it off.

The operative line is this:

if (ev.getvalue()) m.outTrue.setvalue(true);

You use the Scripts Expert again to create a Java script named TrueOnly. whose output event is named outTrue. Then you put the above line into it, as the body of the inEventsCB method (just like we did with the color selection logic in the textbook example.)

You then route the output of Box 1's sensor to this script node, and the script node's output to the headlight. You can test this first stage,  by routing Box 2's script node directly to the headlight.

The OFF box even works - almost. That is, when you click the ON box, the light comes on. When you click the off box and then release, the light goes off. The only problem is  that you can still use the OFF box to turn the light on. Why is this?

To remedy this problem you need to define another Script node type, called FalseOnly. Its key line is

(but wait - can you figure it out for yourself?)

if not, here's ONE of several possible lines.

if (!ev.getvalue()) m.outFalse.setvalue(false);


If you did all these queries, it's enough for this session. We'll give you qeries for Chapter 5 in the next Lesson Plan.

Back to the course index
Back to the course syllabus
Back to the previous lecture
Onward to the next lecture