afxXM_Force.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/afxEffectDefs.h"
  31. #include "afx/forces/afxForce.h"
  32. #include "afx/forces/afxForceSet.h"
  33. //#define ECHO_DEBUG_INFO
  34. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  35. class afxXM_ForceData : public afxXM_WeightedBaseData, public afxEffectDefs
  36. {
  37. typedef afxXM_WeightedBaseData Parent;
  38. public:
  39. StringTableEntry force_set_name;
  40. F32 update_dt;
  41. public:
  42. /*C*/ afxXM_ForceData();
  43. /*C*/ afxXM_ForceData(const afxXM_ForceData&, bool = false);
  44. void packData(BitStream* stream);
  45. void unpackData(BitStream* stream);
  46. bool preload(bool server, String &errorStr);
  47. virtual bool allowSubstitutions() const { return true; }
  48. static void initPersistFields();
  49. afxXM_Base* create(afxEffectWrapper* fx, bool on_server);
  50. DECLARE_CONOBJECT(afxXM_ForceData);
  51. DECLARE_CATEGORY("AFX");
  52. };
  53. class afxXM_Force : public afxXM_WeightedBase, public afxEffectDefs
  54. {
  55. typedef afxXM_WeightedBase Parent;
  56. afxForceSet* force_set;
  57. Point3F pos_local;
  58. Point3F velocity;
  59. bool first;
  60. F32 mass;
  61. F32 mass_inverse;
  62. afxXM_ForceData* db;
  63. public:
  64. /*C*/ afxXM_Force(afxXM_ForceData*, afxEffectWrapper*);
  65. virtual void start(F32 timestamp);
  66. virtual void updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
  67. };
  68. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  69. IMPLEMENT_CO_DATABLOCK_V1(afxXM_ForceData);
  70. ConsoleDocClass( afxXM_ForceData,
  71. "@brief An xmod datablock.\n\n"
  72. "@ingroup afxExperimental\n"
  73. "@ingroup afxXMods\n"
  74. "@ingroup AFX\n"
  75. "@ingroup Datablocks\n"
  76. );
  77. afxXM_ForceData::afxXM_ForceData()
  78. {
  79. force_set_name = ST_NULLSTRING;
  80. update_dt = -1.0f;
  81. }
  82. afxXM_ForceData::afxXM_ForceData(const afxXM_ForceData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
  83. {
  84. force_set_name = other.force_set_name;
  85. update_dt = other.update_dt;
  86. }
  87. void afxXM_ForceData::initPersistFields()
  88. {
  89. addField("forceSetName", TypeString, Offset(force_set_name, afxXM_ForceData),
  90. "...");
  91. addField("updateDT", TypeF32, Offset(update_dt, afxXM_ForceData),
  92. "...");
  93. Parent::initPersistFields();
  94. }
  95. void afxXM_ForceData::packData(BitStream* stream)
  96. {
  97. Parent::packData(stream);
  98. stream->writeString(force_set_name);
  99. if (stream->writeFlag(update_dt < 0.0f))
  100. stream->write(update_dt);
  101. }
  102. void afxXM_ForceData::unpackData(BitStream* stream)
  103. {
  104. Parent::unpackData(stream);
  105. force_set_name = stream->readSTString();
  106. if (stream->readFlag())
  107. stream->read(&update_dt);
  108. else
  109. update_dt = -1.0f;
  110. }
  111. bool afxXM_ForceData::preload(bool server, String &errorStr)
  112. {
  113. if(!Parent::preload(server, errorStr))
  114. return false;
  115. return true;
  116. }
  117. afxXM_Base* afxXM_ForceData::create(afxEffectWrapper* fx, bool on_server)
  118. {
  119. afxXM_ForceData* datablock = this;
  120. if (getSubstitutionCount() > 0)
  121. {
  122. datablock = new afxXM_ForceData(*this, true);
  123. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  124. }
  125. return new afxXM_Force(datablock, fx);
  126. }
  127. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  128. afxXM_Force::afxXM_Force(afxXM_ForceData* db, afxEffectWrapper* fxw)
  129. : afxXM_WeightedBase(db, fxw)
  130. {
  131. this->db = db;
  132. force_set = 0;
  133. pos_local.zero();
  134. velocity.zero();
  135. mass = 1.0f;
  136. mass_inverse = 1.0f;
  137. first = true;
  138. }
  139. void afxXM_Force::start(F32 timestamp)
  140. {
  141. Parent::start(timestamp);
  142. afxForceSetMgr* force_set_mgr = fx_wrapper->getChoreographer()->getForceSetMgr();
  143. force_set = (force_set_mgr) ? force_set_mgr->findForceSet(db->force_set_name) : 0;
  144. if (!force_set)
  145. {
  146. Con::errorf(ConsoleLogEntry::General,
  147. "afxXM_Force::start() -- unable to find afxForceSet %s", db->force_set_name);
  148. return;
  149. }
  150. mass = fx_wrapper->getMass();
  151. // compute mass_inverse safely
  152. mass_inverse = (mass > 0.0001f) ? (1.0f/mass) : 1.0f/0.0001f;
  153. F32 update_dt = (db->update_dt < 0.0f) ? 1.0f/30.0f : db->update_dt;
  154. if (force_set->getUpdateDT() > update_dt)
  155. force_set->setUpdateDT(update_dt);
  156. }
  157. // JTF Note: answer these questions?
  158. // Can mass be removed from the force and acceleration calculations?
  159. // XFM Weight is not accounted for (yet).
  160. void afxXM_Force::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  161. {
  162. if (!force_set)
  163. return;
  164. #ifdef ECHO_DEBUG_INFO
  165. Con::printf("afxXM_Force: elapsed=%f (dt=%f)", elapsed,dt);
  166. #endif
  167. if (first)
  168. {
  169. velocity = fx_wrapper->getDirection();
  170. velocity.normalizeSafe();
  171. params.ori.mulP(velocity);
  172. velocity *= fx_wrapper->getSpeed();
  173. #ifdef ECHO_DEBUG_INFO
  174. Con::printf("INITIAL VELOCITY : %f %f %f", velocity.x, velocity.y, velocity.z);
  175. Con::printf("MASS : %f %f", mass, mass_inverse );
  176. #endif
  177. first = false;
  178. }
  179. S32 num_updates = force_set->updateDT(dt);
  180. if (num_updates == 0)
  181. {
  182. params.pos += pos_local;
  183. return;
  184. }
  185. for (S32 j = 0; j < num_updates; j++)
  186. {
  187. Point3F F_net(0,0,0);
  188. for (S32 i = 0; i < force_set->count(); i++)
  189. {
  190. afxForce* force = force_set->getForce(i);
  191. #ifdef ECHO_DEBUG_INFO
  192. Point3F F = force->evaluate(params.pos+pos_local, velocity, mass);
  193. F_net += F;
  194. Con::printf("(%d) F %i: %f %f %f", this, i, F.x, F.y, F.z);
  195. #else
  196. F_net += force->evaluate(params.pos+pos_local, velocity, mass);
  197. #endif
  198. }
  199. Point3F acceleration = F_net * mass_inverse * force_set->getUpdateDT();
  200. velocity += acceleration;
  201. pos_local += velocity;
  202. }
  203. params.pos += pos_local;
  204. #ifdef ECHO_DEBUG_INFO
  205. Con::printf("velocity : %f %f %f", velocity.x, velocity.y, velocity.z);
  206. Con::printf("pos: %f %f %f", params.pos.x, params.pos.y, params.pos.z);
  207. #endif
  208. }
  209. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//