afxXM_Aim.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/mathUtils.h"
  26. #include "afx/afxEffectWrapper.h"
  27. #include "afx/afxChoreographer.h"
  28. #include "afx/xm/afxXfmMod.h"
  29. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  30. class afxXM_AimData : public afxXM_WeightedBaseData
  31. {
  32. typedef afxXM_WeightedBaseData Parent;
  33. public:
  34. bool aim_z_only;
  35. public:
  36. /*C*/ afxXM_AimData();
  37. /*C*/ afxXM_AimData(const afxXM_AimData&, bool = false);
  38. void packData(BitStream* stream);
  39. void unpackData(BitStream* stream);
  40. virtual bool allowSubstitutions() const { return true; }
  41. static void initPersistFields();
  42. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  43. DECLARE_CONOBJECT(afxXM_AimData);
  44. DECLARE_CATEGORY("AFX");
  45. };
  46. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  47. class afxConstraint;
  48. class afxXM_Aim_weighted : public afxXM_WeightedBase
  49. {
  50. typedef afxXM_WeightedBase Parent;
  51. public:
  52. /*C*/ afxXM_Aim_weighted(afxXM_AimData*, afxEffectWrapper*);
  53. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  54. };
  55. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  56. class afxXM_Aim_weighted_z : public afxXM_WeightedBase
  57. {
  58. typedef afxXM_WeightedBase Parent;
  59. public:
  60. /*C*/ afxXM_Aim_weighted_z(afxXM_AimData*, afxEffectWrapper*);
  61. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  62. };
  63. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  64. class afxXM_Aim_fixed : public afxXM_Base
  65. {
  66. typedef afxXM_Base Parent;
  67. public:
  68. /*C*/ afxXM_Aim_fixed(afxXM_AimData*, afxEffectWrapper*);
  69. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  70. };
  71. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  72. class afxXM_Aim_fixed_z : public afxXM_Base
  73. {
  74. typedef afxXM_Base Parent;
  75. public:
  76. /*C*/ afxXM_Aim_fixed_z(afxXM_AimData*, afxEffectWrapper*);
  77. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  78. };
  79. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  80. IMPLEMENT_CO_DATABLOCK_V1(afxXM_AimData);
  81. ConsoleDocClass( afxXM_AimData,
  82. "@brief An xmod datablock.\n\n"
  83. "@ingroup afxXMods\n"
  84. "@ingroup AFX\n"
  85. "@ingroup Datablocks\n"
  86. );
  87. afxXM_AimData::afxXM_AimData()
  88. {
  89. aim_z_only = false;
  90. }
  91. afxXM_AimData::afxXM_AimData(const afxXM_AimData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  92. {
  93. aim_z_only = other.aim_z_only;
  94. }
  95. void afxXM_AimData::initPersistFields()
  96. {
  97. docsURL;
  98. addField("aimZOnly", TypeBool, Offset(aim_z_only, afxXM_AimData),
  99. "...");
  100. Parent::initPersistFields();
  101. }
  102. void afxXM_AimData::packData(BitStream* stream)
  103. {
  104. Parent::packData(stream);
  105. stream->writeFlag(aim_z_only);
  106. }
  107. void afxXM_AimData::unpackData(BitStream* stream)
  108. {
  109. Parent::unpackData(stream);
  110. aim_z_only = stream->readFlag();
  111. }
  112. afxXM_Base* afxXM_AimData::create(afxEffectWrapper* fx, bool on_server)
  113. {
  114. afxXM_AimData* datablock = this;
  115. if (getSubstitutionCount() > 0)
  116. {
  117. datablock = new afxXM_AimData(*this, true);
  118. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  119. }
  120. if (datablock->aim_z_only)
  121. {
  122. if (datablock->hasFixedWeight())
  123. return new afxXM_Aim_fixed_z(datablock, fx);
  124. else
  125. return new afxXM_Aim_weighted_z(datablock, fx);
  126. }
  127. else
  128. {
  129. if (datablock->hasFixedWeight())
  130. return new afxXM_Aim_fixed(datablock, fx);
  131. else
  132. return new afxXM_Aim_weighted(datablock, fx);
  133. }
  134. }
  135. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  136. afxXM_Aim_weighted::afxXM_Aim_weighted(afxXM_AimData* db, afxEffectWrapper* fxw)
  137. : afxXM_WeightedBase(db, fxw)
  138. {
  139. }
  140. void afxXM_Aim_weighted::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  141. {
  142. VectorF line_of_sight = params.pos2 - params.pos;
  143. line_of_sight.normalize();
  144. F32 wt_factor = calc_weight_factor(elapsed);
  145. QuatF qt_ori_incoming(params.ori);
  146. MatrixF ori_outgoing = MathUtils::createOrientFromDir(line_of_sight);
  147. QuatF qt_ori_outgoing(ori_outgoing);
  148. QuatF qt_ori = qt_ori_incoming.slerp(qt_ori_outgoing, wt_factor);
  149. qt_ori.setMatrix(&params.ori);
  150. }
  151. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  152. afxXM_Aim_weighted_z::afxXM_Aim_weighted_z(afxXM_AimData* db, afxEffectWrapper* fxw)
  153. : afxXM_WeightedBase(db, fxw)
  154. {
  155. }
  156. void afxXM_Aim_weighted_z::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  157. {
  158. Point3F aim_at_pos = params.pos2;
  159. aim_at_pos.z = params.pos.z;
  160. VectorF line_of_sight = aim_at_pos - params.pos;
  161. line_of_sight.normalize();
  162. F32 wt_factor = calc_weight_factor(elapsed);
  163. QuatF qt_ori_incoming(params.ori);
  164. MatrixF ori_outgoing = MathUtils::createOrientFromDir(line_of_sight);
  165. QuatF qt_ori_outgoing( ori_outgoing );
  166. QuatF qt_ori = qt_ori_incoming.slerp(qt_ori_outgoing, wt_factor);
  167. qt_ori.setMatrix(&params.ori);
  168. }
  169. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  170. afxXM_Aim_fixed::afxXM_Aim_fixed(afxXM_AimData* db, afxEffectWrapper* fxw)
  171. : afxXM_Base(db, fxw)
  172. {
  173. }
  174. void afxXM_Aim_fixed::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  175. {
  176. VectorF line_of_sight = params.pos2 - params.pos;
  177. line_of_sight.normalize();
  178. params.ori = MathUtils::createOrientFromDir(line_of_sight);
  179. }
  180. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  181. afxXM_Aim_fixed_z::afxXM_Aim_fixed_z(afxXM_AimData* db, afxEffectWrapper* fxw)
  182. : afxXM_Base(db, fxw)
  183. {
  184. }
  185. void afxXM_Aim_fixed_z::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  186. {
  187. Point3F aim_at_pos = params.pos2;
  188. aim_at_pos.z = params.pos.z;
  189. VectorF line_of_sight = aim_at_pos - params.pos;
  190. line_of_sight.normalize();
  191. params.ori = MathUtils::createOrientFromDir(line_of_sight);
  192. }
  193. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//