Light with switch
After much thinking and planning, I have finally implemented an initial behaviour framework. Behaviour is different to animation. But it may also have effect of animation. In this blog, I have programmed a desk lamp -- a spotlight with a switch. You can click on the switch to switch on and off the light. When you do, you can also hear a switch sound, so have your speakers turned on to enjoy the virtual simulation of light, shadow and sound.
The 3D world below includes a three-face room corner, a Vika Byske Table, a desk lamp, and 4 3D-objects on the desk. You can drag to rotate the world and view from different angle. To zoom in and out, please hold down the ALT key while dragging mouse pointer forward and back. Drag while holding down the CTRL key will pan the virtual world.
Below is the Logo program. You can copy and paste to the Logo Editor to try. Or you can download the Logo file at the end of this blog. This is a long program, but I have included comments to explain how it works.
; behaviour demonstration: light switch ; By Andy Yeh (2014) CS RESET SETBG 31 ; set black bachground ; 3 faces for a room corner RU 90 RECTANGLE FD 1 RD 90 FD 1 RECTANGLE BK 1 LT 90 FD 1 RECTANGLE HOME ; insert the Vika Byske Table WORLD "/sites/default/files/user/u3/world/vika_table.x3d RU 90 FD 0.715 RD 90 LT 90 FD 0.6 LT 90 SETSCALE 0.24 0.03 0.24 RU 90 FD 0.015 RD 90 SETMAT 14 20 CYLINDER ; base of lamp RU 90 FD 0.015 RD 90 LT 90 FD 0.06 SETSCALE 0.03 0.05 0.03 SETMAT 10 31 CYLINDER ; switch object MAKE "switch OBJECT ; store the switch object id to :switch BK 0.12 RU 90 FD 0.17 RD 90 SETSCALE 0.02 0.34 0.02 SETMAT 3 24 CYLINDER ; the pole RU 90 FD 0.17 RD 90 FD 0.06 RT 90 RU 180 SETSCALE 0.12 0.12 0.12 SETMAT 15 34 TORUS ; this torus section is for the curved part of the pole MAKE "torus WORD OBJECT "_torus SET :torus "innerradius 0.1 ; change torus size SET :torus "outerradius 0.5 ; change torus size SET :torus "angle 2 ; change torus angle to 2 in radian. 2 pi will be a full circle. RD 90 FD 0.06 RD 90 LT 90 RD 30 FD 0.05 RU 90 FD 0.01 SETSCALE 0.05 0.05 0.05 SETMAT 15 10 CYLINDER ; base for the light bulb RD 90 FD 0.06 RU 90 SETSCALE 0.2 0.2 0.2 SETMAT 14 9 CONE ; light bulb cover MAKE "cone WORD OBJECT "_cone SET :cone "bottom FALSE SET :cone "solid FALSE RD 90 FD 0.03 SETSCALE 0.1 0.1 0.1 SETMAT 2 6 BALL ; light bulb MAKE "bulb OBJECT SPOTLIGHT MAKE "light WORD OBJECT "_spotlight SET :light "radius 1 ; set spotlight's radius to 1 meter so it won't shine too far ; insert the sound effect SOUND "/sites/default/files/webfm/public/media/sound/effect/computer/mouse-doubleclick-01.wav TRUE FALSE MAKE "sound WORD OBJECT "_audioclip TOGGLE "action1 :light "on ; create an action named action1, which acts to toggle the on attribute of the spotlight TOGGLE "action2 :sound "enabled ; create an action named action2, which toggles the enabled attribute of the sound's audioclip CLICK :switch "action1 ; perform action1 when the switch object is clicked CLICK :switch "action2 ; perform action2 when the switch object is clicked CLICK :switch "action2 ; this is the tricky part. perform action2 again so the sound won't keep playing ; now put some objects on desk RESET HOME RU 90 FD 0.74 RD 90 RT 30 SETSCALE 0.05 0.05 0.05 BOX FD 0.1 BALL LT 20 BK 0.2 PYRAMID RT 50 FD 0.1 CAN
Please leave a comment and let me know what you think.
- Andy's blog
- Login or register to post comments
- 17875 reads
Comments
I love user interaction
I love the new idea to add more user interraction into my programms. Thanks for egsaple and I'll use it soon.
MATHSKID
The behaviour framework
Welcome back MathsKid. You can check out this forum post for more details about Event and Action.
https://vrmath2.net/content/behaviour-framework