followPath.vwf.yaml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 path following behavior
  14. ##
  15. ## @name followPath.vwf
  16. ## @namespace
  17. ---
  18. extends:
  19. http://vwf.example.com/node3.vwf
  20. properties:
  21. ## Whether the component is following a path
  22. ##
  23. ## @name followPath.vwf#followPath-following
  24. ## @property
  25. followPath-following:
  26. set: |
  27. if ( value && ! this["followPath-following"] ) { // starting
  28. if ( this["followPath-path"] ) {
  29. if ( this["followPath-path"] instanceof Array ) {
  30. if ( this["followPath-path"].length > 1 ) {
  31. this["followPath-following"] = true; // set the internal value
  32. this.future(this["followPath-tickRate"]).followPath(); // run the first step and schedule the next one
  33. this.translation = [ this["followPath-path"][0].translation[0], this["followPath-path"][0].translation[1], this["followPath-height"] ];
  34. this["followPath-lastTime"] = this.time;
  35. }
  36. }
  37. }
  38. } else if ( ! value && this["followPath-following"] ) { // stopping
  39. this["followPath-following"] = false; // set the internal value
  40. }
  41. value: false
  42. ## Path to follow
  43. ##
  44. ## @name followPath.vwf#followPath-path
  45. ## @property
  46. followPath-path:
  47. ## Index of the last path point achieved.
  48. ##
  49. ## @name followPath.vwf#followPath-lastPathIndex
  50. ## @property
  51. followPath-lastPathIndex:
  52. set: |
  53. if ( value >= 0 ) {
  54. if ( this["followPath-path"] && value < this["followPath-path"].length ) {
  55. this["followPath-lastPathIndex"] = value;
  56. this["followPath-nextPathIndex"] = value+1;
  57. if ( this["followPath-nextPathIndex"] == this["followPath-path"].length ) {
  58. this["followPath-nextPathIndex"] = 0;
  59. }
  60. } else {
  61. this["followPath-lastPathIndex"] = 0;
  62. this["followPath-nextPathIndex"] = 1;
  63. }
  64. }
  65. value: 0
  66. ## Index of the next path point to achieve.
  67. ##
  68. ## @name followPath.vwf#followPath-nextPathIndex
  69. ## @property
  70. followPath-nextPathIndex: 1
  71. ## Should the path loop.
  72. ##
  73. ## @name followPath.vwf#followPath-looping
  74. ## @property
  75. followPath-looping: true
  76. ## Height to follow path at
  77. ##
  78. ## @name followPath.vwf#followPath-height
  79. ## @property
  80. followPath-height: 1200
  81. ## Last time that followPath 'ticked.'
  82. ##
  83. ## @name followPath.vwf#followPath-lastTime
  84. ## @property
  85. followPath-lastTime: 0
  86. ## How often follow path will tick.
  87. ##
  88. ## @name followPath.vwf#followPath-tickRate
  89. ## @property
  90. followPath-tickRate: 1
  91. methods:
  92. ## Follow Path function. Updates component position and calls vwf future followPath call
  93. ##
  94. ## @name followPath.vwf#followPath
  95. ## @function
  96. ##
  97. ## @returns undefined
  98. followPath:
  99. scripts:
  100. - |
  101. this.followPath = function() { // when following path ...
  102. var time = this.time;
  103. var len = this["followPath-path"].length;
  104. var timeElasped = time - this["followPath-lastTime"];
  105. var lastPathPoint = this["followPath-path"][ this["followPath-lastPathIndex"] ];
  106. var nextPathPoint = this["followPath-path"][ this["followPath-nextPathIndex"] ];
  107. var goalPointIndex = this["followPath-nextPathIndex"] + 1;
  108. if ( goalPointIndex >= this["followPath-path"].length ) {
  109. goalPointIndex = 0;
  110. }
  111. var goalPathPoint = this["followPath-path"][ goalPointIndex ];
  112. var percentDone = timeElasped / nextPathPoint.time;
  113. if ( percentDone > 0.975 ) {
  114. percentDone = 1;
  115. }
  116. if ( ( goalPointIndex == 0 ) && ( this["followPath-looping"] == false ) ) {
  117. goalPathPoint = nextPathPoint + ( nextPathPoint - lastPathPoint );
  118. if ( percentDone == 1 ) {
  119. this[ "followPath-following" ] = false;
  120. }
  121. }
  122. function interpolate( beforeCoord, afterCoord, percent ) {
  123. return beforeCoord + percent * (afterCoord - beforeCoord);
  124. }
  125. var newX = interpolate( nextPathPoint.translation[0], goalPathPoint.translation[0], percentDone );
  126. var newY = interpolate( nextPathPoint.translation[1], goalPathPoint.translation[1], percentDone );
  127. var goal = [ newX, newY, this["followPath-height"] ];
  128. newX = interpolate( lastPathPoint.translation[0], nextPathPoint.translation[0], percentDone );
  129. newY = interpolate( lastPathPoint.translation[1], nextPathPoint.translation[1], percentDone );
  130. var zRot = [ goalPathPoint.translation[0] - newX, goalPathPoint.translation[1] - newY, 0 ];
  131. var rotation = Math.atan2( zRot[1], zRot[0] )-(Math.PI*0.5);
  132. var sinRot = Math.sin(rotation);
  133. var cosRot = Math.cos(rotation);
  134. this.transformTo([ cosRot, sinRot, 0, 0, -sinRot, cosRot, 0, 0, 0, 0, 1, 0, newX, newY, this["followPath-height"], 1], this["followPath-tickRate"]);
  135. if ( percentDone == 1 ) {
  136. this["followPath-lastPathIndex"] = this["followPath-lastPathIndex"] + 1;
  137. this["followPath-lastTime"] = time;
  138. }
  139. if ( this[ "followPath-following" ] ) {
  140. this.future( this["followPath-tickRate"] ).followPath( );
  141. }
  142. } //@ sourceURL=fly.vwf