fly.vwf.yaml 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. # Copyright 2012 United States Government, as represented by the Secretary of Defense, Under
  2. # Secretary of Defense (Personnel & Readiness).
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. # in compliance with the License. You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software distributed under the License
  10. # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
  11. # or implied. See the License for the specific language governing permissions and limitations under
  12. # the License.
  13. ## The component representation of a fly behavior
  14. ##
  15. ## @name fly.vwf
  16. ## @namespace
  17. ---
  18. extends:
  19. http://vwf.example.com/node3.vwf
  20. properties:
  21. ## Whether the component is flying
  22. ##
  23. ## @name fly.vwf#fly-flying
  24. ## @property
  25. fly-flying:
  26. set: |
  27. if ( value && ! this["fly-flying"] ) { // starting
  28. this["fly-flying"] = true; // set the internal value
  29. this.fly(); // run the first step and schedule the next one
  30. } else if ( ! value && this["spin-spinning"] ) { // stopping
  31. this["fly-flying"] = false; // set the internal value
  32. }
  33. value: false
  34. ## Distance on ground
  35. ##
  36. ## @name fly.vwf#fly-distanceOnGround
  37. ## @property
  38. fly-distanceOnGround: 1000
  39. ## Maximum speed on the ground
  40. ##
  41. ## @name fly.vwf#fly-speedOnGroundMax
  42. ## @property
  43. fly-speedOnGroundMax: 200
  44. ## Elevation
  45. ##
  46. ## @name fly.vwf#fly-elevation
  47. ## @property
  48. fly-elevation: 2500
  49. ## Speed in the air
  50. ##
  51. ## @name fly.vwf#fly-speedInAir
  52. ## @property
  53. fly-speedInAir: 250
  54. ## Flying radius
  55. ##
  56. ## @name fly.vwf#fly-radius
  57. ## @property
  58. fly-radius: 8000
  59. ## Flying speed
  60. ##
  61. ## @name fly.vwf#fly-speed
  62. ## @property
  63. fly-speed: 62.5
  64. ## Point index
  65. ##
  66. ## @name fly.vwf#fly-pointIndex
  67. ## @property
  68. fly-pointIndex: 0
  69. methods:
  70. ## Animation function. Updates component positions and calls vwf future fly call
  71. ##
  72. ## @name fly.vwf#fly
  73. ## @function
  74. ##
  75. ## @returns undefined
  76. fly:
  77. ## Updates speed based on whether the component is on the ground or in the air.
  78. ##
  79. ## @name fly.vwf#updateSpeed
  80. ## @function
  81. ##
  82. ## @returns undefined
  83. updateSpeed:
  84. ## Returns whether component is on the ground
  85. ##
  86. ## @name fly.vwf#isOnGround
  87. ## @function
  88. ##
  89. ## @returns undefined
  90. isOnGround:
  91. ## Returns whether component is rising
  92. ##
  93. ## @name fly.vwf#isRising
  94. ## @function
  95. ##
  96. ## @returns undefined
  97. isRising:
  98. ## Initialization function
  99. ##
  100. ## @name fly.vwf#initialize
  101. ## @function
  102. ##
  103. ## @returns undefined
  104. initializePoints:
  105. ## Calculates new interpolated positions
  106. ##
  107. ## @name fly.vwf#isOnGround
  108. ## @function
  109. ##
  110. ## @param {Array} beforeCoord
  111. ## @param {Array} afterCoord
  112. ## @param {Number} percent
  113. ##
  114. ## @returns new position
  115. interpolate:
  116. scripts:
  117. - |
  118. var index = 0;
  119. var lastTime = undefined;
  120. var onGround = true;
  121. this.isOnGround = function() {
  122. if ( this.points[ this["fly-pointIndex"] ].place == "ground" ) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. this.isRising = function() {
  128. if ( this.points[ this["fly-pointIndex"] ].place == "rising" ) {a
  129. return true;
  130. }
  131. return false;
  132. }
  133. this.updateSpeed = function() {
  134. if ( this.isOnGround() ) {
  135. if ( this["fly-speed"] < this["fly-speedOnGroundMax"] ) {
  136. this["fly-speed"] = this["fly-speed"] + this["fly-speedOnGroundMax"] / this.pointsOnGround;
  137. }
  138. if ( this["fly-speed"] >= this["fly-speedOnGroundMax"] ) {
  139. this["fly-speed"] = this["fly-speedOnGroundMax"];
  140. }
  141. } else {
  142. this["fly-speed"] = this["fly-speedInAir"];
  143. }
  144. }
  145. this.initializePoints = function(){
  146. var trans = this.transform;
  147. if ( trans ) {
  148. var initPt = this.translation;
  149. var newPoint;
  150. var distance = 0;
  151. var prevPt = undefined;
  152. this.points = [];
  153. this["fly-pointIndex"] = 0;
  154. this.pointsOnGround = 16.0;
  155. this.pointsClimbing = 24.0;
  156. this.pointsOnCircle = 36.0;
  157. this.lastTime = 0;
  158. var diffX, diffY, diffZ;
  159. var at = goog.vec.Vec3.createFromValues( trans[4], trans[5], 0 );
  160. goog.vec.Vec3.normalize( at, at );
  161. for ( var i = 0; i < this.pointsOnGround; i++ ) {
  162. newPoint = [];
  163. newPoint[0] = initPt[0] + at[0] * i * this["fly-distanceOnGround"] / this.pointsOnGround;
  164. newPoint[1] = initPt[1] + at[1] * i * this["fly-distanceOnGround"] / this.pointsOnGround;
  165. newPoint[2] = initPt[2];
  166. if ( prevPt ) {
  167. diffX = prevPt[0] - newPoint[0];
  168. diffY = prevPt[1] - newPoint[1];
  169. diffZ = prevPt[2] - newPoint[2];
  170. distance = Math.sqrt( diffX*diffX + diffY*diffY + diffZ*diffZ );
  171. }
  172. this.points.push( { point: newPoint, distance: distance, place: "ground" } );
  173. prevPt = newPoint;
  174. }
  175. var heightToTravel = this["fly-elevation"];
  176. var hypLength = heightToTravel / Math.sin( Math.PI/6 );
  177. var adjLength = Math.cos( Math.PI/6 ) * hypLength;
  178. initPt = this.points[ this.points.length-1 ].point;
  179. for ( var i = 1; i <= this.pointsClimbing; i++ ) {
  180. newPoint = [];
  181. newPoint[0] = initPt[0] + at[0] * i * adjLength / this.pointsClimbing;
  182. newPoint[1] = initPt[1] + at[1] * i * adjLength / this.pointsClimbing;
  183. newPoint[2] = initPt[2] + i * heightToTravel / this.pointsClimbing;
  184. if ( prevPt ) {
  185. diffX = prevPt[0] - newPoint[0];
  186. diffY = prevPt[1] - newPoint[1];
  187. diffZ = prevPt[2] - newPoint[2];
  188. distance = Math.sqrt( diffX*diffX + diffY*diffY + diffZ*diffZ );
  189. }
  190. this.points.push( { point: newPoint, distance: distance, place: "rising" } );
  191. prevPt = newPoint;
  192. }
  193. //prevPt = this.points[ this.points.length-1 ].point;
  194. var inc = ( 2.0 * Math.PI ) / this.pointsOnCircle;
  195. var currentAngle = Math.PI + inc;
  196. var center = [];
  197. center[0] = prevPt[0] + this["fly-radius"];;
  198. center[1] = prevPt[1];
  199. center[2] = prevPt[2];
  200. this.firstCiclePoint = this.points.length-1;
  201. for ( var i = 0; i < this.pointsOnCircle; i++ ) {
  202. newPoint = [];
  203. newPoint[0] = center[0] + this["fly-radius"] * Math.cos( currentAngle );
  204. newPoint[1] = center[1] + this["fly-radius"] * Math.sin( currentAngle );
  205. newPoint[2] = center[2];
  206. currentAngle += inc;
  207. if ( prevPt ) {
  208. diffX = prevPt[0] - newPoint[0];
  209. diffY = prevPt[1] - newPoint[1];
  210. diffZ = prevPt[2] - newPoint[2];
  211. distance = Math.sqrt( diffX*diffX + diffY*diffY + diffZ*diffZ );
  212. }
  213. this.points.push( { point: newPoint, distance: distance, place: "circling" } );
  214. prevPt = newPoint;
  215. }
  216. //var pt;
  217. //for ( var i = 0; i < this.points.length; i++ ) {
  218. // pt = this.points[i];
  219. //}
  220. }
  221. }
  222. this.interpolate = function( beforeCoord, afterCoord, percent ) {
  223. var newPos = [];
  224. newPos.push( beforeCoord[0] + percent * (afterCoord[0] - beforeCoord[0]) );
  225. newPos.push( beforeCoord[1] + percent * (afterCoord[1] - beforeCoord[1]) );
  226. newPos.push( beforeCoord[2] + percent * (afterCoord[2] - beforeCoord[2]) );
  227. return newPos;
  228. }
  229. this.fly = function() {
  230. if ( this["fly-flying"] ) { // if enabled
  231. if ( this["fly-speed"] < 63 ) {
  232. if ( !this.points ) {
  233. this.initializePoints();
  234. }
  235. this.updateSpeed();
  236. this.timeToNext = this.points[ this["fly-pointIndex"]+1 ].distance / this["fly-speed"];
  237. this.timeAtLastPoint = this.time;
  238. } else {
  239. var elapsedTime = this.time - this.timeAtLastPoint;
  240. var currentPoint = this.points[ this["fly-pointIndex"] ].point;
  241. var nextPoint = this.points[ this["fly-pointIndex"]+1 ].point;
  242. var percent = elapsedTime / this.timeToNext;
  243. var newPos = this.interpolate( currentPoint, nextPoint, percent );
  244. this.translation = newPos;
  245. if ( percent > 0.9875 ) {
  246. this["fly-pointIndex"]++;
  247. if ( this["fly-pointIndex"]+1 >= this.points.length ) {
  248. this["fly-pointIndex"] = this.firstCiclePoint;
  249. }
  250. // update rotation
  251. var goalPathPoint = this.points[ this["fly-pointIndex"]+1 ].point;
  252. var zRot = [ goalPathPoint[0] - newPos[0], goalPathPoint[1] - newPos[1], 0 ];
  253. this.rotation = [ 0, 0, 1, (Math.atan2( zRot[1], zRot[0] )-(Math.PI*0.5))*180/Math.PI ];
  254. this.updateSpeed();
  255. this.timeToNext = this.points[ this["fly-pointIndex"]+1 ].distance / this["fly-speed"];
  256. this.timeAtLastPoint = this.time;
  257. }
  258. }
  259. this.future( 0.05 ).fly(); // schedule the next step
  260. }
  261. }
  262. this.pointerClick = function() { // when clicked ...
  263. this["fly-flying"] = ! this["fly-flying"]; // ... toggle the enabled flag
  264. } //@ sourceURL=fly.vwf