afxXM_VelocityOffset.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. addField("offsetFactor", TypeF32, Offset(offset_factor, afxXM_VelocityOffsetData),
  128. "...");
  129. addField("offsetPos2", TypeBool, Offset(offset_pos2, afxXM_VelocityOffsetData),
  130. "...");
  131. addField("normalize", TypeBool, Offset(normalize, afxXM_VelocityOffsetData),
  132. "...");
  133. Parent::initPersistFields();
  134. }
  135. void afxXM_VelocityOffsetData::packData(BitStream* stream)
  136. {
  137. Parent::packData(stream);
  138. stream->write(offset_factor);
  139. stream->write(offset_pos2);
  140. }
  141. void afxXM_VelocityOffsetData::unpackData(BitStream* stream)
  142. {
  143. Parent::unpackData(stream);
  144. stream->read(&offset_factor);
  145. stream->read(&offset_pos2);
  146. }
  147. afxXM_Base* afxXM_VelocityOffsetData::create(afxEffectWrapper* fx, bool on_server)
  148. {
  149. afxXM_VelocityOffsetData* datablock = this;
  150. if (getSubstitutionCount() > 0)
  151. {
  152. datablock = new afxXM_VelocityOffsetData(*this, true);
  153. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  154. }
  155. if (datablock->offset_pos2)
  156. {
  157. if (datablock->hasFixedWeight())
  158. return new afxXM_VelocityOffset2_fixed(datablock, fx);
  159. else
  160. return new afxXM_VelocityOffset2_weighted(datablock, fx);
  161. }
  162. else
  163. {
  164. if (datablock->hasFixedWeight())
  165. return new afxXM_VelocityOffset_fixed(datablock, fx);
  166. else
  167. return new afxXM_VelocityOffset_weighted(datablock, fx);
  168. }
  169. }
  170. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  171. afxXM_VelocityOffset_weighted::afxXM_VelocityOffset_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  172. : afxXM_WeightedBase(db, fxw)
  173. {
  174. cons = 0;
  175. offset_factor = db->offset_factor*db->getWeightFactor();
  176. normalize = db->normalize;
  177. }
  178. void afxXM_VelocityOffset_weighted::start(F32 timestamp)
  179. {
  180. Parent::start(timestamp);
  181. cons = fx_wrapper->getPosConstraint();
  182. if (!cons)
  183. Con::errorf(ConsoleLogEntry::General,
  184. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  185. }
  186. void afxXM_VelocityOffset_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  187. {
  188. if (!cons)
  189. return;
  190. SceneObject* scene_obj = cons->getSceneObject();
  191. if (scene_obj)
  192. {
  193. Point3F vel_vec = scene_obj->getVelocity();
  194. if (!vel_vec.isZero())
  195. {
  196. if (normalize)
  197. vel_vec.normalize();
  198. F32 wt_factor = calc_weight_factor(elapsed);
  199. params.pos += vel_vec*offset_factor*wt_factor;
  200. }
  201. }
  202. }
  203. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  204. afxXM_VelocityOffset_fixed::afxXM_VelocityOffset_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  205. : afxXM_Base(db, fxw)
  206. {
  207. cons = 0;
  208. offset_factor = db->offset_factor*db->getWeightFactor();
  209. normalize = db->normalize;
  210. }
  211. void afxXM_VelocityOffset_fixed::start(F32 timestamp)
  212. {
  213. Parent::start(timestamp);
  214. cons = fx_wrapper->getPosConstraint();
  215. if (!cons)
  216. Con::errorf(ConsoleLogEntry::General,
  217. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  218. }
  219. void afxXM_VelocityOffset_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  220. {
  221. if (!cons)
  222. return;
  223. SceneObject* scene_obj = cons->getSceneObject();
  224. if (scene_obj)
  225. {
  226. Point3F vel_vec = scene_obj->getVelocity();
  227. if (!vel_vec.isZero())
  228. {
  229. if (normalize)
  230. vel_vec.normalize(offset_factor);
  231. params.pos += vel_vec*offset_factor;
  232. }
  233. }
  234. }
  235. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  236. afxXM_VelocityOffset2_weighted::afxXM_VelocityOffset2_weighted(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  237. : afxXM_WeightedBase(db, fxw)
  238. {
  239. cons = 0;
  240. offset_factor = db->offset_factor*db->getWeightFactor();
  241. normalize = db->normalize;
  242. }
  243. void afxXM_VelocityOffset2_weighted::start(F32 timestamp)
  244. {
  245. Parent::start(timestamp);
  246. cons = fx_wrapper->getAimConstraint();
  247. if (!cons)
  248. Con::errorf(ConsoleLogEntry::General,
  249. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  250. }
  251. void afxXM_VelocityOffset2_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  252. {
  253. if (!cons)
  254. return;
  255. SceneObject* scene_obj = cons->getSceneObject();
  256. if (scene_obj)
  257. {
  258. Point3F vel_vec = scene_obj->getVelocity();
  259. if (!vel_vec.isZero())
  260. {
  261. if (normalize)
  262. vel_vec.normalize();
  263. F32 wt_factor = calc_weight_factor(elapsed);
  264. params.pos2 += vel_vec*offset_factor*wt_factor;
  265. }
  266. }
  267. }
  268. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  269. afxXM_VelocityOffset2_fixed::afxXM_VelocityOffset2_fixed(afxXM_VelocityOffsetData* db, afxEffectWrapper* fxw)
  270. : afxXM_Base(db, fxw)
  271. {
  272. cons = 0;
  273. offset_factor = db->offset_factor*db->getWeightFactor();
  274. normalize = db->normalize;
  275. }
  276. void afxXM_VelocityOffset2_fixed::start(F32 timestamp)
  277. {
  278. Parent::start(timestamp);
  279. cons = fx_wrapper->getAimConstraint();
  280. if (!cons)
  281. Con::errorf(ConsoleLogEntry::General,
  282. "afxXM_VelocityOffset: failed to find a SceneObject derived constraint source.");
  283. }
  284. void afxXM_VelocityOffset2_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  285. {
  286. if (!cons)
  287. return;
  288. SceneObject* scene_obj = cons->getSceneObject();
  289. if (scene_obj)
  290. {
  291. Point3F vel_vec = scene_obj->getVelocity();
  292. if (!vel_vec.isZero())
  293. {
  294. if (normalize)
  295. vel_vec.normalize(offset_factor);
  296. params.pos2 += vel_vec*offset_factor;
  297. }
  298. }
  299. }
  300. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//