<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE X3D PUBLIC "ISO//Web3D//DTD X3D 3.0//EN" "http://www.web3d.org/specifications/x3d-3.0.dtd">
<X3D profile='Immersive' version='3.0 xmlns:xsd='http://www.w3.org/2001/XMLSchema-instance' xsd:noNamespaceSchemaLocation =' http://www.web3d.org/specifications/x3d-3.0.xsd '>
<head>
<meta name='titlecontent='RelativeProximitySensorPrototype.x3d'/>
<meta name='descriptioncontent='Paired object-to-object collision detection using proximity sensor design pattern, implemented as a reusable prototype node.'/>
<meta name='creatorcontent='Don Brutzman and MV4204 class'/>
<meta name='createdcontent='3 September 2004'/>
<meta name='modifiedcontent='14 January 2014'/>
<meta name='subjectcontent='Object-to-object collision detection'/>
<meta name='identifiercontent=' https://savage.nps.edu/Savage/Tools/Animation/RelativeProximitySensorPrototype.x3d '/>
<meta name='generatorcontent='X3D-Edit 3.2, https://savage.nps.edu/X3D-Edit'/>
<meta name='licensecontent=' ../../license.html'/>
</head>
<!--

Index for ProtoDeclare definition : RelativeProximitySensor
-->
<Scene>
<ProtoDeclare name='RelativeProximitySensorappinfo='RelativeProximitySensor measures paired object-to-object collision detection'>
<ProtoInterface>
<field name='descriptiontype='SFStringaccessType='inputOutput'
 appinfo='describe the purpose of this sensor'/>

<field name='locationPrimarytype='SFVec3fvalue='0 0 0accessType='initializeOnly'
 appinfo='where is the primary object'/>

<field name='set_locationPrimarytype='SFVec3faccessType='inputOnly'
 appinfo='update location of the primary object'/>

<field name='locationSecondarytype='SFVec3fvalue='0 0 0accessType='initializeOnly'
 appinfo='where is the secondary object'/>

<field name='set_locationSecondarytype='SFVec3faccessType='inputOnly'
 appinfo='update location of the secondary object'/>

<field name='proximityRangeThresholdtype='SFFloatvalue='10accessType='initializeOnly'
 appinfo='distance for detecting object-to-object collision'/>

<field name='set_proximityRangeThresholdtype='SFFloataccessType='inputOnly'
 appinfo='change threshold distance for detecting collision'/>

<field name='isInRangetype='SFBoolaccessType='outputOnly'
 appinfo='is object-to-object distance less than proximityRangeThreshold?'/>

<field name='isInRangeTimetype='SFTimeaccessType='outputOnly'
 appinfo='when did object-to-object range meet detection criteria?'/>

<field name='enabledtype='SFBoolvalue='trueaccessType='initializeOnly'
 appinfo='whether sensor is active'/>

<field name='set_enabledtype='SFBoolaccessType='inputOnly'/>
</ProtoInterface>
<ProtoBody>
<Group>
<Script>
<field name='descriptiontype='SFStringaccessType='inputOutput'
 appinfo='describe the purpose of this sensor'/>

<field name='locationPrimarytype='SFVec3faccessType='initializeOnly'/>
<field name='set_locationPrimarytype='SFVec3faccessType='inputOnly'/>
<field name='locationSecondarytype='SFVec3faccessType='initializeOnly'/>
<field name='set_locationSecondarytype='SFVec3faccessType='inputOnly'/>
<field name='proximityRangeThresholdtype='SFFloataccessType='initializeOnly'/>
<field name='set_proximityRangeThresholdtype='SFFloataccessType='inputOnly'/>
<field name='isInRangetype='SFBoolaccessType='outputOnly'/>
<field name='isInRangeTimetype='SFTimeaccessType='outputOnly'/>
<field name='enabledtype='SFBoolaccessType='initializeOnly'/>
<field name='set_enabledtype='SFBoolaccessType='inputOnly'/>
<!-- local Script variables -->
<field name='priorDistancetype='SFFloatvalue='-1accessType='initializeOnly'/>
<field name='newDistancetype='SFFloatvalue='-1accessType='initializeOnly'/>
<field name='traceEnabledtype='SFBoolvalue='trueaccessType='initializeOnly'/>
<IS>
<connect nodeField='descriptionprotoField='description'/>
<connect nodeField='locationPrimaryprotoField='locationPrimary'/>
<connect nodeField='set_locationPrimaryprotoField='set_locationPrimary'/>
<connect nodeField='locationSecondaryprotoField='locationSecondary'/>
<connect nodeField='set_locationSecondaryprotoField='set_locationSecondary'/>
<connect nodeField='proximityRangeThresholdprotoField='proximityRangeThreshold'/>
<connect nodeField='set_proximityRangeThresholdprotoField='set_proximityRangeThreshold'/>
<connect nodeField='isInRangeprotoField='isInRange'/>
<connect nodeField='isInRangeTimeprotoField='isInRangeTime'/>
<connect nodeField='enabledprotoField='enabled'/>
<connect nodeField='set_enabledprotoField='set_enabled'/>
</IS>
<![CDATA[
            
ecmascript:

function initialize ()
{
	tracePrint ('initialize(), description=' + description + ', enabled=' + enabled +
		', traceEnabled=' + traceEnabled);
	if (!enabled) return;
	// sensor is enabled, so force initial proximity condition
	priorDistance = distance (locationPrimary, locationSecondary);
	if      (priorDistance > proximityRangeThreshold)
        {
		isInRange     = false;
                isInRangeTime = -1;
        }
	else	
        {
        	isInRange     = true;
                isInRangeTime = 0; // TODO change to current timestamp
	}
	tracePrint ('isInRange=' + isInRange + ', distance=' + priorDistance +
		', proximityRangeThreshold=' + proximityRangeThreshold);
}
function distance (p1, p2)
{
	return Math.sqrt (
		(p2.x - p1.x) * (p2.x - p1.x) +
		(p2.y - p1.y) * (p2.y - p1.y) +
		(p2.z - p1.z) * (p2.z - p1.z));
}
function computeProximity (value, timestamp) // triggering value is unused since it comes from different sources
{
	newDistance = distance (locationPrimary, locationSecondary);
//	tracePrint ('newDistance=' + newDistance);
	// test if proximity gained
	if      ((  newDistance <  proximityRangeThreshold) &&
	         (priorDistance >= proximityRangeThreshold))
	{
		isInRange     = true;
		isInRangeTime = timestamp;
		tracePrint ('isInRange=' + isInRange + ', newDistance=' + newDistance);
	}
	// test if proximity lost
	else if ((  newDistance >  proximityRangeThreshold) &&
	         (priorDistance <= proximityRangeThreshold))
	{
		isInRange     = false;
		isInRangeTime = timestamp;
		tracePrint ('isInRange=' + isInRange + ', newDistance=' + newDistance);
	}
	priorDistance = newDistance;
}
function set_description (newDescription)
{
    description = newDescription;
    tracePrint ('description=' + description);
}
function set_locationPrimary (value, timestamp)
{
	locationPrimary = value;
	if (enabled) computeProximity (value, timestamp);
}
function set_locationSecondary (value, timestamp)
{
	locationSecondary = value;
	if (enabled) computeProximity (value, timestamp);
}
function set_proximityRangeThreshold (value, timestamp)
{
	proximityRangeThreshold = value;
}
function set_enabled (value, timestamp)
{
	enabled = value;
	initialize (timestamp);
}
function tracePrint (outputString)
{
	if (traceEnabled) Browser.print ('[RelativeProximitySensor] ' + outputString + '\n');
}
function alwaysPrint (outputString)
{
	Browser.print ('[RelativeProximitySensor] ' + outputString + '\n');
}

          
]]>
</Script>
</Group>
</ProtoBody>
</ProtoDeclare>
<!-- ====================================== -->
<!-- Example use -->
<Anchor description='RelativeProximitySensor Exampleparameter='"target=_blank"'
  url=' "RelativeProximitySensorExample.x3d" "https://savage.nps.edu/Savage/Tools/Animation/RelativeProximitySensorExample.x3d" "RelativeProximitySensorExample.wrl" "https://savage.nps.edu/Savage/Tools/Animation/RelativeProximitySensorExample.wrl" '>
<Shape>
<Text string='"RelativeProximitySensorPrototype" "defines a prototype" "" "Click text to see" "RelativeProximitySensorExample scene"'>
<FontStyle justify='"MIDDLE" "MIDDLE"size='0.7'/>
</Text>
<Appearance>
<Material diffuseColor='1 1 0.2'/>
</Appearance>
</Shape>
</Anchor>
</Scene>
</X3D>
<!--

Index for ProtoDeclare definition : RelativeProximitySensor
-->

<!-- Color key: <X3dNode field='value'/> matches <XmlElement attribute='value'/>
(Light blue background: behavior node) (Grey background: inserted documentation) (Magenta background: X3D Extensibility)
    <Prototype name='ProtoName'> <field name='fieldName'/> </Prototype> -->

<!-- Additional help information about X3D scenes: X3D Resources, X3D Scene Authoring Hints and X3D Tooltips -->