afxXM_VelocityOffset.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 "math/mathIO.h"
  26. #include "math/mathUtils.h"
  27. #include "afx/afxEffectWrapper.h"
  28. #include "afx/afxChoreographer.h"
  29. #include "afx/xm/afxXfmMod.h"
  30. #include "afx/util/afxPath3D.h"
  31. #include "afx/util/afxPath.h"
  32. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  33. // VELOCITY OFFSET
  34. class afxXM_VelocityOffsetData : public afxXM_WeightedBaseData
  35. {
  36. typedef afxXM_WeightedBaseData Parent;
  37. public:
  38. F32 offset_factor;
  39. bool offset_pos2;
  40. bool normalize;
  41. public:
  42. /*C*/ afxXM_VelocityOffsetData();
  43. /*C*/ afxXM_VelocityOffsetData(const afxXM_VelocityOffsetData&, bool = false);
  44. void packData(BitStream* stream);
  45. void unpackData(BitStream* stream);
  46. virtual bool allowSubstitutions() const { return true; }
  47. static void initPersistFields();
  48. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  49. DECLARE_CONOBJECT(afxXM_VelocityOffsetData);
  50. DECLARE_CATEGORY("AFX");
  51. };
  52. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  53. class afxXM_VelocityOffset_weighted : public afxXM_WeightedBase
  54. {
  55. typedef afxXM_WeightedBase Parent;
  56. afxConstraint* cons;
  57. F32 offset_factor;
  58. bool normalize;
  59. public:
  60. /*C*/ afxXM_VelocityOffset_weighted(afxXM_VelocityOffsetData*, afxEffectWrapper*);
  61. virtual void start(F32 timestamp);
  62. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  63. };
  64. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  65. // this fixed variation is used when
  66. // the weight factors are constant.
  67. class afxXM_VelocityOffset_fixed : public afxXM_Base
  68. {
  69. typedef afxXM_Base Parent;
  70. afxConstraint* cons;
  71. F32 offset_factor;
  72. bool normalize;
  73. public:
  74. /*C*/ afxXM_VelocityOffset_fixed(afxXM_VelocityOffsetData*, afxEffectWrapper*);
  75. virtual void start(F32 timestamp);
  76. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  77. };
  78. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  79. class afxXM_VelocityOffset2_weighted : public afxXM_WeightedBase
  80. {
  81. typedef afxXM_WeightedBase Parent;
  82. afxConstraint* cons;
  83. F32 offset_factor;
  84. bool normalize;
  85. public:
  86. /*C*/ afxXM_VelocityOffset2_weighted(afxXM_VelocityOffsetData*, afxEffectWrapper*);
  87. virtual void start(F32 timestamp);
  88. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  89. };
  90. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  91. // this fixed variation is used when
  92. // the weight factors are constant.
  93. class afxXM_VelocityOffset2_fixed : public afxXM_Base
  94. {
  95. typedef afxXM_Base Parent;
  96. afxConstraint* cons;
  97. F32 offset_factor;
  98. bool normalize;
  99. public:
  100. /*C*/ afxXM_VelocityOffset2_fixed(afxXM_VelocityOffsetData*, afxEffectWrapper*);
  101. virtual void start(F32 timestamp);
  102. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  103. };
  104. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  105. // VELOCITY OFFSET
  106. IMPLEMENT_CO_DATABLOCK_V1(afxXM_VelocityOffsetData);
  107. ConsoleDocClass( afxXM_VelocityOffsetData,
  108. "@brief An xmod datablock.\n\n"
  109. "@ingroup afxXMods\n"
  110. "@ingroup AFX\n"
  111. "@ingroup Datablocks\n"
  112. );
  113. afxXM_VelocityOffsetData::afxXM_VelocityOffsetData()
  114. {
  115. offset_factor = 1.0f;
  116. offset_pos2 = false;
  117. normalize = false;
  118. }
  119. afxXM_VelocityOffsetData::afxXM_VelocityOffsetData(const afxXM_VelocityOffsetData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  120. {
  121. offset_factor = other.offset_factor;
  122. offset_pos2 = other.offset_pos2;
  123. normalize = other.normalize;
  124. }
  125. void afxXM_VelocityOffsetData::initPersistFields()
  126. {
  127. docsURL;
  128. addField("offsetFactor", TypeF32, Offset(offset_factor, afxXM_VelocityOffsetData),
  129. "...");
  130. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_VelocityOffsetData),
  131. "...");
  132. addField("normalize", TypeBool, Offset(normalize, afxXM_VelocityOffsetData),
  133. "...");
  134. Parent::initPersistFields();
  135. }
  136. void afxXM_VelocityOffsetData::packData(BitStream* stream)
  137. {
  138. Parent::packData(stream);
  139. stream->write(offset_factor);
  140. stream->write(offset_pos2);
  141. }
  142. void afxXM_VelocityOffsetData::unpackData(BitStream* stream)
  143. {
  144. Parent::unpackData(stream);
  145. stream->read(&offset_factor);
  146. stream->read(&offset_pos2);
  147. }
  148. afxXM_Base* afxXM_VelocityOffsetData::create(afxEffectWrapper* fx, bool on_server)
  149. {
  150. afxXM_VelocityOffsetData* datablock = this;
  151. if (getSubstitutionCount() > 0)
  152. {
  153. datablock = new afxXM_VelocityOffsetData(*this, true);
  154. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  155. }
  156. if (datablock->offset_pos2)
  157. {
  158. if (datablock->hasFixedWeight())
  159. return new afxXM_VelocityOffset2_fixed(datablock, fx);
  160. else
  161. return new afxXM_VelocityOffset2_weighted(datablock, fx);
  162. }
  163. else
  164. {
  165. if (datablock->hasFixedWeight())
  166. return new afxXM_VelocityOffset_fixed(datablock, fx);
  167. else
  168. return new afxXM_VelocityOffset_weighted(datablock, fx);
  169. }
  170. }
  171. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  172. afxXM_VelocityOffset_weighted::afxXM_VelocityOffset_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  173. : afxXM_WeightedBase(db, fxw)
  174. {
  175. cons = 0;
  176. offset_factor = db->offset_factor*db->getWeightFactor();
  177. normalize = db->normalize;
  178. }
  179. void afxXM_VelocityOffset_weighted::start(F32 timestamp)
  180. {
  181. Parent::start(timestamp);
  182. cons = fx_wrapper->getPosConstraint();
  183. if (!cons)
  184. Con::errorf(ConsoleLogEntry::General,
  185. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  186. }
  187. void afxXM_VelocityOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  188. {
  189. if (!cons)
  190. return;
  191. SceneObject* scene_obj = cons->getSceneObject();
  192. if (scene_obj)
  193. {
  194. Point3F vel_vec = scene_obj->getVelocity();
  195. if (!vel_vec.isZero())
  196. {
  197. if (normalize)
  198. vel_vec.normalize();
  199. F32 wt_factor = calc_weight_factor(elapsed);
  200. params.pos += vel_vec*offset_factor*wt_factor;
  201. }
  202. }
  203. }
  204. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  205. afxXM_VelocityOffset_fixed::afxXM_VelocityOffset_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  206. : afxXM_Base(db, fxw)
  207. {
  208. cons = 0;
  209. offset_factor = db->offset_factor*db->getWeightFactor();
  210. normalize = db->normalize;
  211. }
  212. void afxXM_VelocityOffset_fixed::start(F32 timestamp)
  213. {
  214. Parent::start(timestamp);
  215. cons = fx_wrapper->getPosConstraint();
  216. if (!cons)
  217. Con::errorf(ConsoleLogEntry::General,
  218. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  219. }
  220. void afxXM_VelocityOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  221. {
  222. if (!cons)
  223. return;
  224. SceneObject* scene_obj = cons->getSceneObject();
  225. if (scene_obj)
  226. {
  227. Point3F vel_vec = scene_obj->getVelocity();
  228. if (!vel_vec.isZero())
  229. {
  230. if (normalize)
  231. vel_vec.normalize(offset_factor);
  232. params.pos += vel_vec*offset_factor;
  233. }
  234. }
  235. }
  236. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  237. afxXM_VelocityOffset2_weighted::afxXM_VelocityOffset2_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  238. : afxXM_WeightedBase(db, fxw)
  239. {
  240. cons = 0;
  241. offset_factor = db->offset_factor*db->getWeightFactor();
  242. normalize = db->normalize;
  243. }
  244. void afxXM_VelocityOffset2_weighted::start(F32 timestamp)
  245. {
  246. Parent::start(timestamp);
  247. cons = fx_wrapper->getAimConstraint();
  248. if (!cons)
  249. Con::errorf(ConsoleLogEntry::General,
  250. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  251. }
  252. void afxXM_VelocityOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  253. {
  254. if (!cons)
  255. return;
  256. SceneObject* scene_obj = cons->getSceneObject();
  257. if (scene_obj)
  258. {
  259. Point3F vel_vec = scene_obj->getVelocity();
  260. if (!vel_vec.isZero())
  261. {
  262. if (normalize)
  263. vel_vec.normalize();
  264. F32 wt_factor = calc_weight_factor(elapsed);
  265. params.pos2 += vel_vec*offset_factor*wt_factor;
  266. }
  267. }
  268. }
  269. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  270. afxXM_VelocityOffset2_fixed::afxXM_VelocityOffset2_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  271. : afxXM_Base(db, fxw)
  272. {
  273. cons = 0;
  274. offset_factor = db->offset_factor*db->getWeightFactor();
  275. normalize = db->normalize;
  276. }
  277. void afxXM_VelocityOffset2_fixed::start(F32 timestamp)
  278. {
  279. Parent::start(timestamp);
  280. cons = fx_wrapper->getAimConstraint();
  281. if (!cons)
  282. Con::errorf(ConsoleLogEntry::General,
  283. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  284. }
  285. void afxXM_VelocityOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  286. {
  287. if (!cons)
  288. return;
  289. SceneObject* scene_obj = cons->getSceneObject();
  290. if (scene_obj)
  291. {
  292. Point3F vel_vec = scene_obj->getVelocity();
  293. if (!vel_vec.isZero())
  294. {
  295. if (normalize)
  296. vel_vec.normalize(offset_factor);
  297. params.pos2 += vel_vec*offset_factor;
  298. }
  299. }
  300. }
  301. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//