afxXM_Aim.cpp 7.1 KB

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