afxXM_Force.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. docsURL;
  90. addField("forceSetName", TypeString, Offset(force_set_name, afxXM_ForceData),
  91. "...");
  92. addField("updateDT", TypeF32, Offset(update_dt, afxXM_ForceData),
  93. "...");
  94. Parent::initPersistFields();
  95. }
  96. void afxXM_ForceData::packData(BitStream* stream)
  97. {
  98. Parent::packData(stream);
  99. stream->writeString(force_set_name);
  100. if (stream->writeFlag(update_dt < 0.0f))
  101. stream->write(update_dt);
  102. }
  103. void afxXM_ForceData::unpackData(BitStream* stream)
  104. {
  105. Parent::unpackData(stream);
  106. force_set_name = stream->readSTString();
  107. if (stream->readFlag())
  108. stream->read(&update_dt);
  109. else
  110. update_dt = -1.0f;
  111. }
  112. bool afxXM_ForceData::preload(bool server, String &errorStr)
  113. {
  114. if(!Parent::preload(server, errorStr))
  115. return false;
  116. return true;
  117. }
  118. afxXM_Base* afxXM_ForceData::create(afxEffectWrapper* fx, bool on_server)
  119. {
  120. afxXM_ForceData* datablock = this;
  121. if (getSubstitutionCount() > 0)
  122. {
  123. datablock = new afxXM_ForceData(*this, true);
  124. this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
  125. }
  126. return new afxXM_Force(datablock, fx);
  127. }
  128. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
  129. afxXM_Force::afxXM_Force(afxXM_ForceData* db, afxEffectWrapper* fxw)
  130. : afxXM_WeightedBase(db, fxw)
  131. {
  132. this->db = db;
  133. force_set = 0;
  134. pos_local.zero();
  135. velocity.zero();
  136. mass = 1.0f;
  137. mass_inverse = 1.0f;
  138. first = true;
  139. }
  140. void afxXM_Force::start(F32 timestamp)
  141. {
  142. Parent::start(timestamp);
  143. afxForceSetMgr* force_set_mgr = fx_wrapper->getChoreographer()->getForceSetMgr();
  144. force_set = (force_set_mgr) ? force_set_mgr->findForceSet(db->force_set_name) : 0;
  145. if (!force_set)
  146. {
  147. Con::errorf(ConsoleLogEntry::General,
  148. "afxXM_Force::start() -- unable to find afxForceSet %s", db->force_set_name);
  149. return;
  150. }
  151. mass = fx_wrapper->getMass();
  152. // compute mass_inverse safely
  153. mass_inverse = (mass > 0.0001f) ? (1.0f/mass) : 1.0f/0.0001f;
  154. F32 update_dt = (db->update_dt < 0.0f) ? 1.0f/30.0f : db->update_dt;
  155. if (force_set->getUpdateDT() > update_dt)
  156. force_set->setUpdateDT(update_dt);
  157. }
  158. // JTF Note: answer these questions?
  159. // Can mass be removed from the force and acceleration calculations?
  160. // XFM Weight is not accounted for (yet).
  161. void afxXM_Force::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
  162. {
  163. if (!force_set)
  164. return;
  165. #ifdef ECHO_DEBUG_INFO
  166. Con::printf("afxXM_Force: elapsed=%f (dt=%f)", elapsed,dt);
  167. #endif
  168. if (first)
  169. {
  170. velocity = fx_wrapper->getDirection();
  171. velocity.normalizeSafe();
  172. params.ori.mulP(velocity);
  173. velocity *= fx_wrapper->getSpeed();
  174. #ifdef ECHO_DEBUG_INFO
  175. Con::printf("INITIAL VELOCITY : %f %f %f", velocity.x, velocity.y, velocity.z);
  176. Con::printf("MASS : %f %f", mass, mass_inverse );
  177. #endif
  178. first = false;
  179. }
  180. S32 num_updates = force_set->updateDT(dt);
  181. if (num_updates == 0)
  182. {
  183. params.pos += pos_local;
  184. return;
  185. }
  186. for (S32 j = 0; j < num_updates; j++)
  187. {
  188. Point3F F_net(0,0,0);
  189. for (S32 i = 0; i < force_set->count(); i++)
  190. {
  191. afxForce* force = force_set->getForce(i);
  192. #ifdef ECHO_DEBUG_INFO
  193. Point3F F = force->evaluate(params.pos+pos_local, velocity, mass);
  194. F_net += F;
  195. Con::printf("(%d) F %i: %f %f %f", this, i, F.x, F.y, F.z);
  196. #else
  197. F_net += force->evaluate(params.pos+pos_local, velocity, mass);
  198. #endif
  199. }
  200. Point3F acceleration = F_net * mass_inverse * force_set->getUpdateDT();
  201. velocity += acceleration;
  202. pos_local += velocity;
  203. }
  204. params.pos += pos_local;
  205. #ifdef ECHO_DEBUG_INFO
  206. Con::printf("velocity : %f %f %f", velocity.x, velocity.y, velocity.z);
  207. Con::printf("pos: %f %f %f", params.pos.x, params.pos.y, params.pos.z);
  208. #endif
  209. }
  210. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//