afxF_Gravity.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 <typeinfo>
  25. #include "afx/arcaneFX.h"
  26. #include "afx/forces/afxForce.h"
  27. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  28. class afxF_GravityData : public afxForceData
  29. {
  30. typedef afxForceData Parent;
  31. public:
  32. F32 gravity;
  33. public:
  34. /*C*/ afxF_GravityData();
  35. /*C*/ afxF_GravityData(const afxF_GravityData&, bool = false);
  36. virtual void packData(BitStream* stream);
  37. virtual void unpackData(BitStream* stream);
  38. virtual afxForceData* cloneAndPerformSubstitutions(const SimObject*, S32 index=0);
  39. static void initPersistFields();
  40. DECLARE_CONOBJECT(afxF_GravityData);
  41. DECLARE_CATEGORY("AFX");
  42. };
  43. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  44. class afxF_Gravity : public afxForce
  45. {
  46. typedef afxForce Parent;
  47. private:
  48. afxF_GravityData* mDatablock;
  49. public:
  50. /*C*/ afxF_Gravity();
  51. virtual bool onNewDataBlock(afxForceData* dptr, bool reload);
  52. virtual Point3F evaluate(Point3F pos, Point3F v, F32 mass);
  53. };
  54. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  55. // afxForceData
  56. IMPLEMENT_CO_DATABLOCK_V1(afxF_GravityData);
  57. ConsoleDocClass( afxF_GravityData,
  58. "@brief A datablock for specifiying AFX gravity forces.\n\n"
  59. "@ingroup afxExperimental\n"
  60. "@ingroup AFX\n"
  61. "@ingroup Datablocks\n"
  62. );
  63. afxF_GravityData::afxF_GravityData()
  64. {
  65. gravity = 9.807f;
  66. }
  67. afxF_GravityData::afxF_GravityData(const afxF_GravityData& other, bool temp_clone) : afxForceData(other, temp_clone)
  68. {
  69. gravity = other.gravity;
  70. }
  71. #define myOffset(field) Offset(field, afxF_GravityData)
  72. void afxF_GravityData::initPersistFields()
  73. {
  74. addField("gravity", TypeF32, myOffset(gravity),
  75. "...");
  76. Parent::initPersistFields();
  77. }
  78. void afxF_GravityData::packData(BitStream* stream)
  79. {
  80. Parent::packData(stream);
  81. stream->write(gravity);
  82. }
  83. void afxF_GravityData::unpackData(BitStream* stream)
  84. {
  85. Parent::unpackData(stream);
  86. stream->read(&gravity);
  87. }
  88. afxForceData* afxF_GravityData::cloneAndPerformSubstitutions(const SimObject* owner, S32 index)
  89. {
  90. afxF_GravityData* grav_data = this;
  91. // only clone the datablock if there are substitutions
  92. if (this->getSubstitutionCount() > 0)
  93. {
  94. // clone the datablock and perform substitutions
  95. afxF_GravityData* orig_db = this;
  96. grav_data = new afxF_GravityData(*orig_db, true);
  97. orig_db->performSubstitutions(grav_data, owner, index);
  98. }
  99. return grav_data;
  100. }
  101. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  102. afxF_Gravity::afxF_Gravity() : afxForce()
  103. {
  104. mDatablock = NULL;
  105. }
  106. bool afxF_Gravity::onNewDataBlock(afxForceData* dptr, bool reload)
  107. {
  108. mDatablock = dynamic_cast<afxF_GravityData*>(dptr);
  109. if (!mDatablock || !Parent::onNewDataBlock(dptr, reload))
  110. return false;
  111. return true;
  112. }
  113. Point3F afxF_Gravity::evaluate(Point3F pos, Point3F v, F32 mass)
  114. {
  115. return Point3F(0,0,-mDatablock->gravity)*mass;
  116. }
  117. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  118. class afxF_GravityDesc : public afxForceDesc
  119. {
  120. static afxF_GravityDesc desc;
  121. public:
  122. virtual bool testForceType(const SimDataBlock*) const;
  123. virtual afxForce* create() const { return new afxF_Gravity; }
  124. };
  125. afxF_GravityDesc afxF_GravityDesc::desc;
  126. bool afxF_GravityDesc::testForceType(const SimDataBlock* db) const
  127. {
  128. return (typeid(afxF_GravityData) == typeid(*db));
  129. }
  130. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//