Torque3D Documentation / _generateds / afxXM_Force.cpp

afxXM_Force.cpp

Engine/source/afx/forces/afxXM_Force.cpp

More...

Classes:

Public Functions

ConsoleDocClass(afxXM_ForceData , "@brief An xmod <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">datablock.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxExperimental\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxXMods\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Datablocks\n</a>" )

Detailed Description

Public Functions

ConsoleDocClass(afxXM_ForceData , "@brief An xmod <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">datablock.\n\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxExperimental\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">afxXMods\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">AFX\n</a>" "@ingroup <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Datablocks\n</a>" )

IMPLEMENT_CO_DATABLOCK_V1(afxXM_ForceData )

  1
  2
  3//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  4// Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  5// Copyright (C) 2015 Faust Logic, Inc.
  6//
  7// Permission is hereby granted, free of charge, to any person obtaining a copy
  8// of this software and associated documentation files (the "Software"), to
  9// deal in the Software without restriction, including without limitation the
 10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 11// sell copies of the Software, and to permit persons to whom the Software is
 12// furnished to do so, subject to the following conditions:
 13//
 14// The above copyright notice and this permission notice shall be included in
 15// all copies or substantial portions of the Software.
 16//
 17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 22// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 23// IN THE SOFTWARE.
 24//
 25//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 26
 27#include "afx/arcaneFX.h"
 28
 29#include "math/mathIO.h"
 30#include "math/mathUtils.h"
 31
 32#include "afx/afxEffectWrapper.h"
 33#include "afx/afxChoreographer.h"
 34#include "afx/xm/afxXfmMod.h"
 35
 36#include "afx/afxEffectDefs.h"
 37#include "afx/forces/afxForce.h"
 38#include "afx/forces/afxForceSet.h"
 39
 40//#define ECHO_DEBUG_INFO
 41
 42//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 43
 44class afxXM_ForceData : public afxXM_WeightedBaseData, public afxEffectDefs
 45{
 46  typedef afxXM_WeightedBaseData Parent;
 47
 48public:
 49  StringTableEntry  force_set_name;
 50  F32               update_dt;
 51
 52public:
 53  /*C*/         afxXM_ForceData();
 54  /*C*/         afxXM_ForceData(const afxXM_ForceData&, bool = false);
 55
 56  void          packData(BitStream* stream);
 57  void          unpackData(BitStream* stream);
 58
 59   bool         preload(bool server, String &errorStr);
 60
 61  virtual bool  allowSubstitutions() const { return true; }
 62
 63  static void   initPersistFields();
 64
 65  afxXM_Base*   create(afxEffectWrapper* fx, bool on_server);
 66
 67  DECLARE_CONOBJECT(afxXM_ForceData);
 68  DECLARE_CATEGORY("AFX");
 69};
 70
 71class afxXM_Force : public afxXM_WeightedBase, public afxEffectDefs
 72{
 73  typedef afxXM_WeightedBase Parent;
 74
 75  afxForceSet*  force_set;
 76
 77  Point3F       pos_local;
 78  Point3F       velocity;
 79
 80  bool          first;
 81
 82  F32           mass;
 83  F32           mass_inverse;
 84
 85  afxXM_ForceData* db;
 86
 87public:
 88  /*C*/         afxXM_Force(afxXM_ForceData*, afxEffectWrapper*);
 89
 90  virtual void  start(F32 timestamp);
 91  virtual void  updateParams(F32 dt, F32 elapsed, afxXM_Params& params);
 92};
 93
 94//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
 95
 96IMPLEMENT_CO_DATABLOCK_V1(afxXM_ForceData);
 97
 98ConsoleDocClass( afxXM_ForceData,
 99   "@brief An xmod datablock.\n\n"
100
101   "@ingroup afxExperimental\n"
102   "@ingroup afxXMods\n"
103   "@ingroup AFX\n"
104   "@ingroup Datablocks\n"
105);
106
107afxXM_ForceData::afxXM_ForceData()
108{
109  force_set_name = ST_NULLSTRING;
110  update_dt = -1.0f;
111}
112
113afxXM_ForceData::afxXM_ForceData(const afxXM_ForceData& other, bool temp_clone) : afxXM_WeightedBaseData(other, temp_clone)
114{
115  force_set_name = other.force_set_name;
116  update_dt = other.update_dt;
117}
118
119
120void afxXM_ForceData::initPersistFields()
121{
122  addField("forceSetName",    TypeString, Offset(force_set_name, afxXM_ForceData),
123    "...");
124  addField("updateDT",        TypeF32,    Offset(update_dt, afxXM_ForceData),
125    "...");
126
127  Parent::initPersistFields();
128}
129
130void afxXM_ForceData::packData(BitStream* stream)
131{
132  Parent::packData(stream);
133 
134  stream->writeString(force_set_name);
135  if (stream->writeFlag(update_dt < 0.0f))
136    stream->write(update_dt);
137}
138
139void afxXM_ForceData::unpackData(BitStream* stream)
140{
141  Parent::unpackData(stream);
142  
143  force_set_name = stream->readSTString();
144  if (stream->readFlag())
145    stream->read(&update_dt);
146  else
147    update_dt = -1.0f;
148}
149
150bool afxXM_ForceData::preload(bool server, String &errorStr)
151{
152  if(!Parent::preload(server, errorStr))
153    return false;
154  
155  return true;
156}
157
158afxXM_Base* afxXM_ForceData::create(afxEffectWrapper* fx, bool on_server)
159{
160  afxXM_ForceData* datablock = this;
161
162  if (getSubstitutionCount() > 0)
163  {
164    datablock = new afxXM_ForceData(*this, true);
165    this->performSubstitutions(datablock, fx->getChoreographer(), fx->getGroupIndex());
166  }
167
168  return new afxXM_Force(datablock, fx);
169}
170
171//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//
172
173afxXM_Force::afxXM_Force(afxXM_ForceData* db, afxEffectWrapper* fxw) 
174: afxXM_WeightedBase(db, fxw) 
175{ 
176  this->db = db; 
177
178  force_set = 0;
179
180  pos_local.zero();
181  velocity.zero();
182
183  mass = 1.0f;
184  mass_inverse = 1.0f;
185
186  first = true;
187}
188
189void afxXM_Force::start(F32 timestamp)
190{
191  Parent::start(timestamp);
192
193  afxForceSetMgr* force_set_mgr = fx_wrapper->getChoreographer()->getForceSetMgr();
194  force_set = (force_set_mgr) ? force_set_mgr->findForceSet(db->force_set_name) : 0;
195  if (!force_set)
196  {
197    Con::errorf(ConsoleLogEntry::General,
198           "afxXM_Force::start() -- unable to find afxForceSet %s", db->force_set_name);
199    return;
200  }
201
202  mass = fx_wrapper->getMass();
203
204  // compute mass_inverse safely
205  mass_inverse = (mass > 0.0001f) ? (1.0f/mass) : 1.0f/0.0001f;
206
207  F32 update_dt = (db->update_dt < 0.0f) ? 1.0f/30.0f : db->update_dt;
208  if (force_set->getUpdateDT() > update_dt)
209    force_set->setUpdateDT(update_dt);
210}
211
212// JTF Note: answer these questions?
213// Can mass be removed from the force and acceleration calculations?
214// XFM Weight is not accounted for (yet).
215void afxXM_Force::updateParams(F32 dt, F32 elapsed, afxXM_Params& params)
216{
217  if (!force_set)
218    return;
219
220#ifdef ECHO_DEBUG_INFO
221  Con::printf("afxXM_Force: elapsed=%f (dt=%f)", elapsed,dt);
222#endif
223
224  if (first)
225  {
226    velocity = fx_wrapper->getDirection();
227    velocity.normalizeSafe();
228    params.ori.mulP(velocity);
229    velocity *= fx_wrapper->getSpeed();
230
231#ifdef ECHO_DEBUG_INFO
232    Con::printf("INITIAL VELOCITY : %f %f %f", velocity.x, velocity.y, velocity.z);
233    Con::printf("MASS             : %f %f", mass, mass_inverse );
234#endif
235
236    first = false;
237  }
238
239  S32 num_updates = force_set->updateDT(dt);
240  if (num_updates == 0)
241  {
242    params.pos += pos_local;
243    return;
244  }
245
246  for (S32 j = 0; j < num_updates; j++)
247  {
248    Point3F F_net(0,0,0);
249    for (S32 i = 0; i < force_set->count(); i++)
250    {
251      afxForce* force = force_set->getForce(i);
252#ifdef ECHO_DEBUG_INFO
253      Point3F F = force->evaluate(params.pos+pos_local, velocity, mass);
254      F_net += F;
255      Con::printf("(%d) F %i: %f %f %f", this, i, F.x, F.y, F.z);
256#else
257      F_net += force->evaluate(params.pos+pos_local, velocity, mass);
258#endif
259    }
260
261    Point3F acceleration = F_net * mass_inverse * force_set->getUpdateDT();
262    velocity += acceleration;
263
264    pos_local += velocity;    
265  }
266  params.pos += pos_local;
267
268#ifdef ECHO_DEBUG_INFO
269  Con::printf("velocity : %f %f %f", velocity.x, velocity.y, velocity.z);
270  Con::printf("pos:       %f %f %f", params.pos.x, params.pos.y, params.pos.z);
271#endif
272}
273
274//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
275
276