afxPath3D.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  2. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  3. // Copyright (C) 2015 Faust Logic, Inc.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to
  7. // deal in the Software without restriction, including without limitation the
  8. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9. // sell copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. // IN THE SOFTWARE.
  22. //
  23. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  24. #include "afx/arcaneFX.h"
  25. #include "console/console.h"
  26. #include "afx/util/afxPath3D.h"
  27. afxPath3D::afxPath3D() : mStart_time(0), mNum_points(0), mLoop_type(LOOP_CONSTANT), mEnd_time(0.0f)
  28. {
  29. }
  30. afxPath3D::~afxPath3D()
  31. {
  32. }
  33. void afxPath3D::sortAll()
  34. {
  35. mCurve.sort();
  36. mCurve_parameters.sort();
  37. }
  38. void afxPath3D::setStartTime( F32 time )
  39. {
  40. mStart_time = time;
  41. }
  42. void afxPath3D::setLoopType( U32 loop_type )
  43. {
  44. mLoop_type = loop_type;
  45. }
  46. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  47. F32 afxPath3D::getEndTime()
  48. {
  49. return mEnd_time;
  50. }
  51. int afxPath3D::getNumPoints()
  52. {
  53. return mNum_points;
  54. }
  55. Point3F afxPath3D::getPointPosition( int index )
  56. {
  57. if (index < 0 || index >= mNum_points)
  58. return Point3F(0.0f, 0.0f, 0.0f);
  59. return mCurve.getPoint(index);
  60. }
  61. F32 afxPath3D::getPointTime( int index )
  62. {
  63. if (index < 0 || index >= mNum_points)
  64. return 0.0f;
  65. return mCurve_parameters.getKeyTime(index);
  66. }
  67. F32 afxPath3D::getPointParameter( int index )
  68. {
  69. if (index < 0 || index >= mNum_points)
  70. return 0.0f;
  71. return mCurve_parameters.getKeyValue(index);
  72. }
  73. Point2F afxPath3D::getParameterSegment( F32 time )
  74. {
  75. return mCurve_parameters.getSegment(time);
  76. }
  77. void afxPath3D::setPointPosition( int index, Point3F &p )
  78. {
  79. if (index < 0 || index >= mNum_points)
  80. return;
  81. mCurve.setPoint(index, p);
  82. }
  83. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  84. F32 afxPath3D::calcCurveTime( F32 time )
  85. {
  86. if( time <= mStart_time )
  87. return 0.0f;
  88. if( time <= mEnd_time )
  89. return time-mStart_time;
  90. switch( mLoop_type )
  91. {
  92. case LOOP_CYCLE :
  93. {
  94. return mFmod( time-mStart_time, mEnd_time-mStart_time );
  95. }
  96. case LOOP_OSCILLATE :
  97. {
  98. F32 t1 = time- mStart_time;
  99. F32 t2 = mEnd_time - mStart_time;
  100. if( (int)(t1/t2) % 2 ) // odd segment
  101. return t2 - mFmod( t1, t2 );
  102. else // even segment
  103. return mFmod( t1, t2 );
  104. }
  105. case LOOP_CONSTANT :
  106. default:
  107. return mEnd_time;
  108. }
  109. }
  110. Point3F afxPath3D::evaluateAtTime( F32 time )
  111. {
  112. F32 ctime = calcCurveTime( time );
  113. F32 param = mCurve_parameters.evaluate( ctime );
  114. return mCurve.evaluate(param);
  115. }
  116. Point3F afxPath3D::evaluateAtTime(F32 t0, F32 t1)
  117. {
  118. F32 ctime = calcCurveTime(t0);
  119. F32 param = mCurve_parameters.evaluate( ctime );
  120. Point3F p0 = mCurve.evaluate(param);
  121. ctime = calcCurveTime(t1);
  122. param = mCurve_parameters.evaluate( ctime );
  123. Point3F p1 = mCurve.evaluate(param);
  124. return p1-p0;
  125. }
  126. Point3F afxPath3D::evaluateTangentAtTime( F32 time )
  127. {
  128. F32 ctime = calcCurveTime( time );
  129. F32 param = mCurve_parameters.evaluate( ctime );
  130. return mCurve.evaluateTangent(param);
  131. }
  132. Point3F afxPath3D::evaluateTangentAtPoint( int index )
  133. {
  134. F32 param = mCurve_parameters.getKeyValue(index);
  135. return mCurve.evaluateTangent(param);
  136. }
  137. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  138. void afxPath3D::buildPath( int num_points, Point3F curve_points[], F32 start_time, F32 end_time )
  139. {
  140. mNum_points = num_points;
  141. // Add points to path
  142. F32 param_inc = 1.0f / (F32)(num_points - 1);
  143. F32 param = 0.0f;
  144. for( int i = 0; i < num_points; i++, param += param_inc )
  145. {
  146. if( i == num_points-1 )
  147. param = 1.0f;
  148. mCurve.addPoint( param, curve_points[i] );
  149. }
  150. mCurve.computeTangents();
  151. initPathParametersNEW( curve_points, start_time, end_time );
  152. sortAll();
  153. }
  154. void afxPath3D::buildPath( int num_points, Point3F curve_points[], F32 speed )
  155. {
  156. mNum_points = num_points;
  157. // Add points to path
  158. F32 param_inc = 1.0f / (F32)(num_points - 1);
  159. F32 param = 0.0f;
  160. for( int i = 0; i < num_points; i++, param += param_inc )
  161. {
  162. if( i == num_points-1 )
  163. param = 1.0f;
  164. mCurve.addPoint( param, curve_points[i] );
  165. }
  166. initPathParameters( curve_points, speed );
  167. sortAll();
  168. }
  169. void afxPath3D::buildPath( int num_points, Point3F curve_points[],
  170. F32 point_times[], F32 time_offset, F32 time_factor )
  171. {
  172. mNum_points = num_points;
  173. // Add points to path
  174. F32 param_inc = 1.0f / (F32)(num_points - 1);
  175. F32 param = 0.0f;
  176. for( int i = 0; i < num_points; i++, param += param_inc )
  177. {
  178. if( i == num_points-1 )
  179. param = 1.0f;
  180. mCurve.addPoint( param, curve_points[i] );
  181. mCurve_parameters.addKey( (point_times[i]+time_offset)*time_factor, param );
  182. }
  183. // Set end time
  184. mEnd_time = (point_times[num_points-1]+time_offset)*time_factor;
  185. sortAll();
  186. }
  187. void afxPath3D::buildPath( int num_points, Point3F curve_points[], Point2F curve_params[] )
  188. {
  189. mNum_points = num_points;
  190. // Add points to path
  191. F32 param_inc = 1.0f / (F32)(num_points - 1);
  192. F32 param = 0.0f;
  193. for( int i = 0; i < num_points; i++, param += param_inc )
  194. {
  195. if( i == num_points-1 )
  196. param = 1.0f;
  197. mCurve.addPoint( param, curve_points[i] );
  198. }
  199. //
  200. for (int i = 0; i < num_points; i++)
  201. mCurve_parameters.addKey( curve_params[i] );
  202. // Set end time
  203. mEnd_time = curve_params[num_points - 1].x;
  204. sortAll();
  205. }
  206. void afxPath3D::reBuildPath()
  207. {
  208. mCurve.computeTangents();
  209. sortAll();
  210. }
  211. void afxPath3D::initPathParameters( Point3F curve_points[], F32 speed )
  212. {
  213. // Compute the time for each point dependent on the speed of the character and the
  214. // distance it must travel (approximately!)
  215. int num_segments = mNum_points - 1;
  216. F32 *point_distances = new F32[num_segments];
  217. for( int i = 0; i < num_segments; i++ )
  218. {
  219. Point3F p1 = curve_points[i+1];
  220. Point3F p0 = curve_points[i];
  221. point_distances[i] = (p1-p0).len();
  222. }
  223. F32 *times = new F32[num_segments];
  224. F32 last_time = 0;//start_time;
  225. for( int i = 0; i < num_segments; i++ )
  226. {
  227. times[i] = last_time + (point_distances[i] / speed);
  228. last_time = times[i];
  229. }
  230. mCurve_parameters.addKey( 0, 0.0f );//start_time, 0.0f );
  231. F32 param_inc = 1.0f / (F32)(mNum_points - 1);
  232. F32 param = 0.0f + param_inc;
  233. for( int i = 0; i < num_segments; i++, param += param_inc )
  234. mCurve_parameters.addKey( times[i], param );
  235. // Set end time
  236. mEnd_time = times[num_segments-1];
  237. if (point_distances)
  238. delete [] point_distances;
  239. if (times)
  240. delete [] times;
  241. }
  242. void afxPath3D::initPathParametersNEW( Point3F curve_points[], F32 start_time, F32 end_time )
  243. {
  244. int num_segments = mNum_points - 1;
  245. F32 *point_distances = new F32[num_segments];
  246. F32 total_distance = 0.0f;
  247. for( int i = 0; i < num_segments; i++ )
  248. {
  249. Point3F p1 = curve_points[i+1];
  250. Point3F p0 = curve_points[i];
  251. point_distances[i] = (p1-p0).len();
  252. total_distance += point_distances[i];
  253. }
  254. F32 duration = end_time - start_time;
  255. F32 time = 0.0f; //start_time;
  256. mCurve_parameters.addKey( time, 0.0f );
  257. F32 param_inc = 1.0f / (F32)(mNum_points - 1);
  258. F32 param = 0.0f + param_inc;
  259. for( int i=0; i < num_segments; i++, param += param_inc )
  260. {
  261. time += (point_distances[i]/total_distance) * duration;
  262. mCurve_parameters.addKey( time, param );
  263. }
  264. // Set end time ????
  265. //end_time = time;
  266. mStart_time = start_time;
  267. mEnd_time = end_time;
  268. if (point_distances)
  269. delete [] point_distances;
  270. }
  271. void afxPath3D::print()
  272. {
  273. // curve.print();
  274. mCurve_parameters.print();
  275. }
  276. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//