再稍稍修改一下邻近检测器,把它的中心位置向右移了2米:
DEF comeCloser ProximitySensor {
center 2 0 0
size 4 4 4
}
这样你就可以从左边(方块那一边)走进球体(视点不跳),但不能从右边(圆锥那一边)走近它(视点跳转)。
总之,ProximitySensor能够检测用户是否进入或离开检测器指定的空间区域,典型用法是当用户走进房间时开启灯光,当用户离开时关闭灯光,从而建立功能丰富的“智能”空间。
#VRML V2.0 utf8
DEF view1 Viewpoint {
position 0 0 20
description "view1"
}
DEF view2 Viewpoint {
position 0 0 20
description "view2"
}
Group {
children [
DEF box Transform {
translation 5 0 0
children [
Shape {
appearance Appearance {
material Material {
diffuseColor 1 0 0
}
}
geometry Box {}
}
]
}
DEF sphere Transform {
translation 0 0 0
children [
Shape {
appearance Appearance {
material Material {
diffuseColor 0 1 0
}
}
geometry Sphere {}
}
DEF comeCloser ProximitrySensor {
center 0 0 0
size 4 4 4
}
]
}
DEF cone Transform {
translation -5 0 0
children [
Shape {
appearance Appearance {
material Material {
diffuseColor 0 0 1
}
}
geometry Cone {}
}
]
}
]#end of Group children
}
DEF comeCloserScript Script {
eventIn SFBool enterProximitySensorIsActive
eventOut SFBool bindView2
url "javascript :
function enterProximitySensorIsActive(active) {
bindView2=TRUE;
}"
}
ROUTE comeCloser.isActive TO comeCloserScript.enterProximitySensorIsActive
ROUTE comeCloserScript.bindView2 TO view2.set_bind
ROUTE comeCloser.isActive TO comeCloserScript.enterProximitySensorIsActive
ROUTE comeCloserScript.bindView2 TO view2.set_bind
DEF comeCloserScript Script {
eventIn SFBool enterProximitySensorIsActive
eventOut SFBool bindView2
url " javascript :
function enterProximitySensorIsActive (active) {
bindView2=TRUE;
} "
}
DEF sphere Transform {
translation 0 0 0
children [
Shape {....}
DEF comeClose ProximitySensor {
center 0 0 0
size 4 4 4
}
]
}
这里稍作介绍。ProximitySensor节点共有三个外露域(exposedField)和五个出事件(eventOut).出事件我们已经熟悉,是节点状态发生改变时用来通知其它节点的,这里的出事件isActive 用于ProximitySensor通报自己已被激活。enterTime和exitTime通报用户(代表用户的用户化身或指示器)进入和退出ProximitySensor检测区的时刻。若用户已在检测器之内,则当用户的位置或方位发生变化时,送出position_changed和orientation_changed出事件。这五个出事件联合起来,就定义了邻近检测器的功能。外露域则集域(Field)、入事件(eventIn)和出事件(eventOut)三者的功能于一身,也就是说,它既象域一样描述了节点的当前状态,又可以作为入事件由其它节点修改这种状态,并作为出事件把这种改变通知其它节点。这里的enabled外露域是布尔型的,用于ProximitySensor的启用和停用,center和size定义形为长方体的邻近检测区。
我们的出发点是第一节中建造的境界helloworld,它是由方块、球体和圆柱这三个物体构成的静态世界,现在在球体周围增加一个邻近检测区:
ProximitySensor {
exposedField SFVec3f center 0 0 0
exposedField SFVec3f size 0 0 0
exposedField SFBool enabled TRUE
eventOut SFBool isActive
eventOut SFVec3f position_changed
eventOut SFRotation orientation_changed
eventOut SFTime enterTime
eventOut SFTime exitTime
}