<?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 '>
<![CDATA[
ecmascript:
var x;
var y;
var Vx;
var Vy;
var B_m;
var dt;
var blocksize;
var Vi;
var theta;
var key;
var keyValue;
var previousFraction;
var previousFractionIndex;
var blockSize;
var outputArray;
function tracePrint (outputString)
{
var traceEnabled = true;
if (traceEnabled) Browser.print ('[WaypointInterpolator]' + outputString);
}
function forcePrint (outputString)
{
Browser.print ('[WaypointInterpolator]' + outputString);
}
function initialize() {
key = new Array();
keyValue = new MFVec3f();
x = new Array();
y = new Array();
calculateTrajectory();
previousFractionIndex = -1;
previousFraction = 0;
// check key array ranges [0..1] and is monotonically increasing
// check that size of keyValue array is integer multiple of size of key array
tracePrint ('key =' + key);
tracePrint ('key.length= ' + key.length);
tracePrint ('keyValue= ' + keyValue);
tracePrint ('keyValue.length=' + keyValue.length);
blockSize = 3; //keyValue.length/key.length;
tracePrint ('blockSize=' + blockSize);
if (blockSize != Math.round(blockSize))
{
forcePrint ('*** warning: blockSize not an integer multiple. check sizes of key and keyValue');
}
if (key[0] != 0)
{
forcePrint ('*** warning: key[0] != 0');
}
if (key[key.length-1] != 1)
{
forcePrint ('*** warning: key[' + (key.length - 1) + '] != 1, reset from' + key[key.length-1] + ' to 1');
key[key.length-1] = 1;
}
for (index = 0; index < blockSize; index++)
{
if ((key[index] < 0) || (key[index] > 1))
{
forcePrint ('*** warning: key[' + index + '] =' + key[index] + ', out of range [0..1]');
}
}
// instantiate default array, later computations just update it
outputArray = new SFVec3f();
outputArray = keyValue[0];
tracePrint ('initial outputArray=' + outputArray);
}
function set_fraction (inputFloat, timestamp) {
fraction = inputFloat;
tracePrint ('previousFractionIndex=' + previousFractionIndex
+ ', fraction=' + fraction + ', previousFraction=' + previousFraction);
if (fraction < 0)
{
tracePrint ('*** illegal fraction' + fraction + ' set to 0');
fraction = 0;
previousFractionIndex = 0; // first
}
else if (fraction > 1)
{
forcePrint ('*** illegal fraction' + fraction + ' set to 1');
fraction = 1;
previousFractionIndex = blockSize - 1; // last
}
else if (previousFractionIndex == -1)
{
previousFractionIndex = 0; // first
tracePrint ('previousFractionIndex initialized for first event');
}
else if ((fraction >= previousFraction) && (fraction >= key[previousFractionIndex+1]))
{
previousFractionIndex++;
}
else if (fraction < previousFraction) // regress, or loop repeat without reaching one
{
previousFractionIndex = 0;
while ((fraction >= key[previousFractionIndex+1]) && (previousFractionIndex < blockSize))
{
previousFractionIndex++;
}
tracePrint ('reset/reincrement previousFractionIndex to' + previousFractionIndex);
}
if (fraction == 1) // use final block
{
tracePrint ('(fraction == 1)');
outputArray = keyValue[(keyValue.length -1)];
previousFractionIndex = -1; // setup for restart
tracePrint ('finished final fraction==1 block');
}
// when fraction matches index, calculate value_changed from corresponding keyValue array
else if (fraction == key[previousFractionIndex])
{
tracePrint ('(fraction == key[previousFractionIndex])');
// update outputArray - need to interpolate next
outputArray = keyValue[previousFractionIndex];
}
else {
delta = key[previousFractionIndex + 1] - key[previousFractionIndex];
differ = fraction - key[previousFractionIndex];
percentDiffer = differ / delta;
valueDelta = new SFVec3f();
for(index = 0; index < blockSize; index++) {
valueDelta[index] = keyValue[(previousFractionIndex + 1)][index] - keyValue[previousFractionIndex][index];
outputArray[index] = keyValue[previousFractionIndex][index] + valueDelta[index] * percentDiffer;
Browser.print ('valueDelta' + valueDelta[index]);
Browser.print ('perDiffer' + percentDiffer);
}
}
value_changed = outputArray;
previousFraction = fraction;
tracePrint ('value_changed=' + value_changed);
}
function set_Vi(initialVelocity, timeStamp) {
Vi = initialVelocity;
initialize(timeStamp);
}
function set_theta(angle, timeStamp) {
theta = angle;
initialize(timeStamp);
}
function calculateTrajectory() {
x[0] = 0;
y[0] = 0;
var timeKeys = new Array();
timeKeys[0] = 0.0;
//convert degree to radian
angle = Math.PI * theta / 180;
Vx = Vi * Math.cos(angle);
Vy = Vi * Math.sin(angle);
var i = 0;
do {
i = i + 1;
timeKeys[i] = timeKeys[i - 1] + dt;
Browser.print ('timeKeys' + timeKeys[i]);
x[i] = x[i - 1] + Vx * dt;
y[i] = y[i - 1] + Vy * dt;
f = B_m * Math.sqrt(Vx * Vx + Vy * Vy) * Math.exp(-y[i] / 0.0001);
Vy = Vy - 9.8 * dt - f * Vy * dt;
Vx = Vx - f * Vx * dt;
Browser.print ('Vy' + Vy);
}while(y[i] > 0);
Browser.print ('Im here' + x.length);
//interpolate to find landing point
var a = -y[i] / y[i-1];
x[i] = (x[i] + a * x[i-1]) / (1+a);
y[i] = 0;
//copy x, y values to keyValues
copyToKeyValues();
//finding keys
for(j = 0; j < timeKeys.length; j++) {
key[j] = timeKeys[j] / timeKeys[timeKeys.length - 1];
Browser.print (' ' + key[j]);
}
}
function copyToKeyValues() {
for(i = 0; i < x.length; i++) {
Browser.print ('x' + x[i]);
keyValue[i][0] = x[i];
keyValue[i][1] = y[i];
keyValue[i][2] = 0;
Browser.print ('keyValue' + i + ' ' + keyValue[i][0]);
}
}
]]>
<!-- Tag color codes: <Node DEF='idName' attribute='value'/> <Prototype name='ProtoName'> <field name='fieldName'/> </Prototype> -->