physicalZone.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "T3D/physicalZone.h"
  27. #include "core/stream/bitStream.h"
  28. #include "collision/boxConvex.h"
  29. #include "collision/clippedPolyList.h"
  30. #include "console/consoleTypes.h"
  31. #include "math/mathIO.h"
  32. #include "scene/sceneRenderState.h"
  33. #include "T3D/trigger.h"
  34. #include "gfx/gfxTransformSaver.h"
  35. #include "renderInstance/renderPassManager.h"
  36. #include "gfx/gfxDrawUtil.h"
  37. #include "console/engineAPI.h"
  38. //#include "console/engineTypes.h"
  39. #include "sim/netConnection.h"
  40. IMPLEMENT_CO_NETOBJECT_V1(PhysicalZone);
  41. ConsoleDocClass( PhysicalZone,
  42. "@brief Physical Zones are areas that modify the player's gravity and/or velocity and/or applied force.\n\n"
  43. "The datablock properties determine how the physics, velocity and applied forces affect a player who enters this zone.\n"
  44. "@tsexample\n"
  45. "new PhysicalZone(Team1JumpPad) {\n"
  46. "velocityMod = \"1\";"
  47. "gravityMod = \"0\";\n"
  48. "appliedForce = \"0 0 20000\";\n"
  49. "polyhedron = \"0.0000000 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000 0.0000000 -1.0000000 0.0000000 0.0000000 0.0000000 1.0000000\";\n"
  50. "position = \"273.559 -166.371 249.856\";\n"
  51. "rotation = \"0 0 1 13.0216\";\n"
  52. "scale = \"8 4.95 28.31\";\n"
  53. "isRenderEnabled = \"true\";\n"
  54. "canSaveDynamicFields = \"1\";\n"
  55. "enabled = \"1\";\n"
  56. "};\n"
  57. "@endtsexample\n\n"
  58. "@ingroup enviroMisc\n"
  59. );
  60. bool PhysicalZone::smRenderPZones = false;
  61. DefineEngineMethod(PhysicalZone, activate, void, (),, "Activate the physical zone's effects.\n"
  62. "@tsexample\n"
  63. "// Activate effects for a specific physical zone.\n"
  64. "%thisPhysicalZone.activate();\n"
  65. "@endtsexample\n"
  66. "@ingroup Datablocks\n"
  67. )
  68. {
  69. if (object->isClientObject())
  70. return;
  71. object->activate();
  72. }
  73. DefineEngineMethod(PhysicalZone, deactivate, void, (),, "Deactivate the physical zone's effects.\n"
  74. "@tsexample\n"
  75. "// Deactivate effects for a specific physical zone.\n"
  76. "%thisPhysicalZone.deactivate();\n"
  77. "@endtsexample\n"
  78. "@ingroup Datablocks\n"
  79. )
  80. {
  81. if (object->isClientObject())
  82. return;
  83. object->deactivate();
  84. }
  85. //--------------------------------------------------------------------------
  86. //--------------------------------------
  87. //
  88. PhysicalZone::PhysicalZone()
  89. {
  90. mNetFlags.set(Ghostable | ScopeAlways);
  91. mTypeMask |= PhysicalZoneObjectType;
  92. mVelocityMod = 1.0f;
  93. mGravityMod = 1.0f;
  94. mAppliedForce.set(0, 0, 0);
  95. mConvexList = new Convex;
  96. mActive = true;
  97. force_type = VECTOR;
  98. force_mag = 0.0f;
  99. orient_force = false;
  100. fade_amt = 1.0f;
  101. }
  102. PhysicalZone::~PhysicalZone()
  103. {
  104. delete mConvexList;
  105. mConvexList = NULL;
  106. }
  107. ImplementEnumType( PhysicalZone_ForceType, "Possible physical zone force types.\n" "@ingroup PhysicalZone\n\n" )
  108. { PhysicalZone::VECTOR, "vector", "..." },
  109. { PhysicalZone::SPHERICAL, "spherical", "..." },
  110. { PhysicalZone::CYLINDRICAL, "cylindrical", "..." },
  111. // aliases
  112. { PhysicalZone::SPHERICAL, "sphere", "..." },
  113. { PhysicalZone::CYLINDRICAL, "cylinder", "..." },
  114. EndImplementEnumType;
  115. //--------------------------------------------------------------------------
  116. void PhysicalZone::consoleInit()
  117. {
  118. Con::addVariable( "$PhysicalZone::renderZones", TypeBool, &smRenderPZones, "If true, a box will render around the location of all PhysicalZones.\n"
  119. "@ingroup EnviroMisc\n");
  120. }
  121. void PhysicalZone::initPersistFields()
  122. {
  123. addGroup("Misc");
  124. addField("velocityMod", TypeF32, Offset(mVelocityMod, PhysicalZone), "Multiply velocity of objects entering zone by this value every tick.");
  125. addField("gravityMod", TypeF32, Offset(mGravityMod, PhysicalZone), "Gravity in PhysicalZone. Multiplies against standard gravity.");
  126. addField("appliedForce", TypePoint3F, Offset(mAppliedForce, PhysicalZone), "Three-element floating point value representing forces in three axes to apply to objects entering PhysicalZone.");
  127. addField("polyhedron", TypeTriggerPolyhedron, Offset(mPolyhedron, PhysicalZone),
  128. "The polyhedron type is really a quadrilateral and consists of a corner"
  129. "point followed by three vectors representing the edges extending from the corner." );
  130. endGroup("Misc");
  131. addGroup("AFX");
  132. addField("forceType", TYPEID<PhysicalZone::ForceType>(), Offset(force_type, PhysicalZone));
  133. addField("orientForce", TypeBool, Offset(orient_force, PhysicalZone));
  134. endGroup("AFX");
  135. Parent::initPersistFields();
  136. }
  137. //--------------------------------------------------------------------------
  138. bool PhysicalZone::onAdd()
  139. {
  140. if(!Parent::onAdd())
  141. return false;
  142. if (mVelocityMod < -40.0f || mVelocityMod > 40.0f) {
  143. Con::errorf("PhysicalZone: velocity mod out of range. [-40, 40]");
  144. mVelocityMod = mVelocityMod < -40.0f ? -40.0f : 40.0f;
  145. }
  146. if (mGravityMod < -40.0f || mGravityMod > 40.0f) {
  147. Con::errorf("PhysicalZone: GravityMod out of range. [-40, 40]");
  148. mGravityMod = mGravityMod < -40.0f ? -40.0f : 40.0f;
  149. }
  150. static const char* coordString[] = { "x", "y", "z" };
  151. F32* p = mAppliedForce;
  152. for (U32 i = 0; i < 3; i++) {
  153. if (p[i] < -40000.0f || p[i] > 40000.0f) {
  154. Con::errorf("PhysicalZone: applied force: %s out of range. [-40000, 40000]", coordString[i]);
  155. p[i] = p[i] < -40000.0f ? -40000.0f : 40000.0f;
  156. }
  157. }
  158. Polyhedron temp = mPolyhedron;
  159. setPolyhedron(temp);
  160. switch (force_type)
  161. {
  162. case SPHERICAL:
  163. force_mag = mAppliedForce.magnitudeSafe();
  164. break;
  165. case CYLINDRICAL:
  166. {
  167. Point3F force_vec = mAppliedForce;
  168. force_vec.z = 0.0;
  169. force_mag = force_vec.magnitudeSafe();
  170. }
  171. break;
  172. }
  173. addToScene();
  174. return true;
  175. }
  176. void PhysicalZone::onRemove()
  177. {
  178. mConvexList->nukeList();
  179. removeFromScene();
  180. Parent::onRemove();
  181. }
  182. void PhysicalZone::inspectPostApply()
  183. {
  184. Parent::inspectPostApply();
  185. setPolyhedron(mPolyhedron);
  186. setMaskBits(PolyhedronMask | MoveMask | SettingsMask | FadeMask);
  187. }
  188. //------------------------------------------------------------------------------
  189. void PhysicalZone::setTransform(const MatrixF & mat)
  190. {
  191. Parent::setTransform(mat);
  192. MatrixF base(true);
  193. base.scale(Point3F(1.0/mObjScale.x,
  194. 1.0/mObjScale.y,
  195. 1.0/mObjScale.z));
  196. base.mul(mWorldToObj);
  197. mClippedList.setBaseTransform(base);
  198. if (isServerObject())
  199. setMaskBits(MoveMask);
  200. }
  201. void PhysicalZone::prepRenderImage( SceneRenderState *state )
  202. {
  203. // only render if selected or render flag is set
  204. if ( !smRenderPZones && !isSelected() )
  205. return;
  206. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  207. ri->renderDelegate.bind( this, &PhysicalZone::renderObject );
  208. ri->type = RenderPassManager::RIT_Editor;
  209. ri->defaultKey = 0;
  210. ri->defaultKey2 = 0;
  211. state->getRenderPass()->addInst( ri );
  212. }
  213. void PhysicalZone::renderObject(ObjectRenderInst *ri,
  214. SceneRenderState *state,
  215. BaseMatInstance *overrideMat)
  216. {
  217. if (overrideMat)
  218. return;
  219. GFXStateBlockDesc desc;
  220. desc.setZReadWrite(true, false);
  221. desc.setBlend(true);
  222. desc.setCullMode(GFXCullNone);
  223. GFXTransformSaver saver;
  224. GFXDrawUtil *drawer = GFX->getDrawUtil();
  225. Point3F start = getBoxCenter();
  226. Box3F obb = mObjBox; //object bounding box
  227. F32 baseForce = 10000; //roughly the ammount of force needed to push a player back as it walks into a zone. (used for visual scaling)
  228. Point3F forceDir = getForce(&start);
  229. F32 forceLen = forceDir.len()/ baseForce;
  230. forceDir.normalizeSafe();
  231. ColorI guideCol = LinearColorF(mFabs(forceDir.x), mFabs(forceDir.y), mFabs(forceDir.z), 0.125).toColorI();
  232. if (force_type == VECTOR)
  233. {
  234. Point3F endPos = start + (forceDir * mMax(forceLen,0.75f));
  235. drawer->drawArrow(desc, start, endPos, guideCol, 0.05f);
  236. }
  237. MatrixF mat = getRenderTransform();
  238. mat.scale(getScale());
  239. GFX->multWorld(mat);
  240. start = obb.getCenter();
  241. if (force_type == VECTOR)
  242. {
  243. drawer->drawPolyhedron(desc, mPolyhedron, ColorI(0, 255, 0, 45));
  244. }
  245. else if (force_type == SPHERICAL)
  246. {
  247. F32 rad = obb.getBoundingSphere().radius/ 2;
  248. drawer->drawSphere(desc, rad, start, ColorI(0, 255, 0, 45));
  249. rad = (rad + (mAppliedForce.most() / baseForce))/2;
  250. desc.setFillModeWireframe();
  251. drawer->drawSphere(desc, rad, start, ColorI(0, 0, 255, 255));
  252. }
  253. else
  254. {
  255. Point3F bottomPos = start;
  256. bottomPos.z -= obb.len_z() / 2;
  257. Point3F topPos = start;
  258. topPos.z += obb.len_z() / 2;
  259. F32 rad = obb.len_x() / 2;
  260. drawer->drawCylinder(desc, bottomPos, topPos, rad, ColorI(0, 255, 0, 45));
  261. Point3F force_vec = mAppliedForce; //raw relative-applied force here as oposed to derived
  262. F32 hieght = (force_vec.z / baseForce);
  263. if (force_vec.z<0)
  264. bottomPos.z = (bottomPos.z + hieght)/2;
  265. else
  266. topPos.z = (topPos.z + hieght) / 2;
  267. if (force_vec.x > force_vec.y)
  268. rad = (rad + (force_vec.x / baseForce)) / 2;
  269. else
  270. rad = (rad + (force_vec.y / baseForce)) / 2;
  271. desc.setFillModeWireframe();
  272. drawer->drawCylinder(desc, bottomPos, topPos, rad, guideCol);
  273. }
  274. desc.setFillModeWireframe();
  275. drawer->drawPolyhedron(desc, mPolyhedron, ColorI::BLACK);
  276. }
  277. //--------------------------------------------------------------------------
  278. U32 PhysicalZone::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  279. {
  280. U32 i;
  281. U32 retMask = Parent::packUpdate(con, mask, stream);
  282. if (stream->writeFlag(mask & PolyhedronMask))
  283. {
  284. // Write the polyhedron
  285. stream->write(mPolyhedron.mPointList.size());
  286. for (i = 0; i < mPolyhedron.mPointList.size(); i++)
  287. mathWrite(*stream, mPolyhedron.mPointList[i]);
  288. stream->write(mPolyhedron.mPlaneList.size());
  289. for (i = 0; i < mPolyhedron.mPlaneList.size(); i++)
  290. mathWrite(*stream, mPolyhedron.mPlaneList[i]);
  291. stream->write(mPolyhedron.mEdgeList.size());
  292. for (i = 0; i < mPolyhedron.mEdgeList.size(); i++) {
  293. const Polyhedron::Edge& rEdge = mPolyhedron.mEdgeList[i];
  294. stream->write(rEdge.face[0]);
  295. stream->write(rEdge.face[1]);
  296. stream->write(rEdge.vertex[0]);
  297. stream->write(rEdge.vertex[1]);
  298. }
  299. }
  300. if (stream->writeFlag(mask & MoveMask))
  301. {
  302. stream->writeAffineTransform(mObjToWorld);
  303. mathWrite(*stream, mObjScale);
  304. }
  305. if (stream->writeFlag(mask & SettingsMask))
  306. {
  307. stream->write(mVelocityMod);
  308. stream->write(mGravityMod);
  309. mathWrite(*stream, mAppliedForce);
  310. stream->writeInt(force_type, FORCE_TYPE_BITS);
  311. stream->writeFlag(orient_force);
  312. }
  313. if (stream->writeFlag(mask & FadeMask))
  314. {
  315. U8 fade_byte = (U8)(fade_amt*255.0f);
  316. stream->write(fade_byte);
  317. }
  318. stream->writeFlag(mActive);
  319. return retMask;
  320. }
  321. void PhysicalZone::unpackUpdate(NetConnection* con, BitStream* stream)
  322. {
  323. Parent::unpackUpdate(con, stream);
  324. bool new_ph = false;
  325. if (stream->readFlag()) // PolyhedronMask
  326. {
  327. U32 i, size;
  328. Polyhedron tempPH;
  329. // Read the polyhedron
  330. stream->read(&size);
  331. tempPH.mPointList.setSize(size);
  332. for (i = 0; i < tempPH.mPointList.size(); i++)
  333. mathRead(*stream, &tempPH.mPointList[i]);
  334. stream->read(&size);
  335. tempPH.mPlaneList.setSize(size);
  336. for (i = 0; i < tempPH.mPlaneList.size(); i++)
  337. mathRead(*stream, &tempPH.mPlaneList[i]);
  338. stream->read(&size);
  339. tempPH.mEdgeList.setSize(size);
  340. for (i = 0; i < tempPH.mEdgeList.size(); i++) {
  341. Polyhedron::Edge& rEdge = tempPH.mEdgeList[i];
  342. stream->read(&rEdge.face[0]);
  343. stream->read(&rEdge.face[1]);
  344. stream->read(&rEdge.vertex[0]);
  345. stream->read(&rEdge.vertex[1]);
  346. }
  347. setPolyhedron(tempPH);
  348. new_ph = true;
  349. }
  350. if (stream->readFlag()) // MoveMask
  351. {
  352. MatrixF temp;
  353. stream->readAffineTransform(&temp);
  354. Point3F tempScale;
  355. mathRead(*stream, &tempScale);
  356. //if (!new_ph)
  357. //{
  358. // Polyhedron rPolyhedron = mPolyhedron;
  359. // setPolyhedron(rPolyhedron);
  360. //}
  361. setScale(tempScale);
  362. setTransform(temp);
  363. }
  364. if (stream->readFlag()) //SettingsMask
  365. {
  366. stream->read(&mVelocityMod);
  367. stream->read(&mGravityMod);
  368. mathRead(*stream, &mAppliedForce);
  369. force_type = stream->readInt(FORCE_TYPE_BITS); // AFX
  370. orient_force = stream->readFlag(); // AFX
  371. }
  372. if (stream->readFlag()) //FadeMask
  373. {
  374. U8 fade_byte;
  375. stream->read(&fade_byte);
  376. fade_amt = ((F32)fade_byte)/255.0f;
  377. }
  378. else
  379. fade_amt = 1.0f;
  380. mActive = stream->readFlag();
  381. }
  382. //--------------------------------------------------------------------------
  383. void PhysicalZone::setPolyhedron(const Polyhedron& rPolyhedron)
  384. {
  385. mPolyhedron = rPolyhedron;
  386. if (mPolyhedron.mPointList.size() != 0) {
  387. mObjBox.minExtents.set(1e10, 1e10, 1e10);
  388. mObjBox.maxExtents.set(-1e10, -1e10, -1e10);
  389. for (U32 i = 0; i < mPolyhedron.mPointList.size(); i++) {
  390. mObjBox.minExtents.setMin(mPolyhedron.mPointList[i]);
  391. mObjBox.maxExtents.setMax(mPolyhedron.mPointList[i]);
  392. }
  393. } else {
  394. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  395. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  396. }
  397. MatrixF xform = getTransform();
  398. setTransform(xform);
  399. mClippedList.clear();
  400. mClippedList.mPlaneList = mPolyhedron.mPlaneList;
  401. MatrixF base(true);
  402. base.scale(Point3F(1.0/mObjScale.x,
  403. 1.0/mObjScale.y,
  404. 1.0/mObjScale.z));
  405. base.mul(mWorldToObj);
  406. mClippedList.setBaseTransform(base);
  407. }
  408. //--------------------------------------------------------------------------
  409. void PhysicalZone::buildConvex(const Box3F& box, Convex* convex)
  410. {
  411. // These should really come out of a pool
  412. mConvexList->collectGarbage();
  413. Box3F realBox = box;
  414. mWorldToObj.mul(realBox);
  415. realBox.minExtents.convolveInverse(mObjScale);
  416. realBox.maxExtents.convolveInverse(mObjScale);
  417. if (realBox.isOverlapped(getObjBox()) == false)
  418. return;
  419. // Just return a box convex for the entire shape...
  420. Convex* cc = 0;
  421. CollisionWorkingList& wl = convex->getWorkingList();
  422. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
  423. if (itr->mConvex->getType() == BoxConvexType &&
  424. itr->mConvex->getObject() == this) {
  425. cc = itr->mConvex;
  426. break;
  427. }
  428. }
  429. if (cc)
  430. return;
  431. // Create a new convex.
  432. BoxConvex* cp = new BoxConvex;
  433. mConvexList->registerObject(cp);
  434. convex->addToWorkingList(cp);
  435. cp->init(this);
  436. mObjBox.getCenter(&cp->mCenter);
  437. cp->mSize.x = mObjBox.len_x() / 2.0f;
  438. cp->mSize.y = mObjBox.len_y() / 2.0f;
  439. cp->mSize.z = mObjBox.len_z() / 2.0f;
  440. }
  441. bool PhysicalZone::testObject(SceneObject* enter)
  442. {
  443. // TODO: This doesn't look like it's testing against the polyhedron at
  444. // all. And whats the point of building a convex if no collision methods
  445. // are implemented?
  446. if (mPolyhedron.mPointList.size() == 0)
  447. return false;
  448. mClippedList.clear();
  449. SphereF sphere;
  450. sphere.center = (mWorldBox.minExtents + mWorldBox.maxExtents) * 0.5;
  451. VectorF bv = mWorldBox.maxExtents - sphere.center;
  452. sphere.radius = bv.len();
  453. enter->buildPolyList(PLC_Collision, &mClippedList, mWorldBox, sphere);
  454. return mClippedList.isEmpty() == false;
  455. }
  456. bool PhysicalZone::testBox( const Box3F &box ) const
  457. {
  458. return mWorldBox.isOverlapped( box );
  459. }
  460. void PhysicalZone::activate()
  461. {
  462. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::open()");
  463. if (mActive != true)
  464. setMaskBits(ActiveMask);
  465. mActive = true;
  466. }
  467. void PhysicalZone::deactivate()
  468. {
  469. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::close()");
  470. if (mActive != false)
  471. setMaskBits(ActiveMask);
  472. mActive = false;
  473. }
  474. void PhysicalZone::onStaticModified(const char* slotName, const char*newValue)
  475. {
  476. if (dStricmp(slotName, "appliedForce") == 0 || dStricmp(slotName, "forceType") == 0)
  477. {
  478. switch (force_type)
  479. {
  480. case SPHERICAL:
  481. force_mag = mAppliedForce.magnitudeSafe();
  482. break;
  483. case CYLINDRICAL:
  484. {
  485. Point3F force_vec = mAppliedForce;
  486. force_vec.z = 0.0;
  487. force_mag = force_vec.magnitudeSafe();
  488. }
  489. break;
  490. }
  491. }
  492. }
  493. const Point3F& PhysicalZone::getForce(const Point3F* center) const
  494. {
  495. static Point3F force_vec;
  496. if (force_type == VECTOR)
  497. {
  498. if (orient_force)
  499. {
  500. getTransform().mulV(mAppliedForce, &force_vec);
  501. force_vec *= fade_amt;
  502. return force_vec;
  503. }
  504. force_vec = mAppliedForce;
  505. force_vec *= fade_amt;
  506. return force_vec;
  507. }
  508. if (!center)
  509. {
  510. force_vec.zero();
  511. return force_vec;
  512. }
  513. if (force_type == SPHERICAL)
  514. {
  515. force_vec = *center - getPosition();
  516. force_vec.normalizeSafe();
  517. force_vec *= force_mag*fade_amt;
  518. return force_vec;
  519. }
  520. if (orient_force)
  521. {
  522. force_vec = *center - getPosition();
  523. getWorldTransform().mulV(force_vec);
  524. force_vec.z = 0.0f;
  525. force_vec.normalizeSafe();
  526. force_vec *= force_mag;
  527. force_vec.z = mAppliedForce.z;
  528. getTransform().mulV(force_vec);
  529. force_vec *= fade_amt;
  530. return force_vec;
  531. }
  532. force_vec = *center - getPosition();
  533. force_vec.z = 0.0f;
  534. force_vec.normalizeSafe();
  535. force_vec *= force_mag;
  536. force_vec *= fade_amt;
  537. return force_vec;
  538. }
  539. bool PhysicalZone::isExcludedObject(SceneObject* obj) const
  540. {
  541. for (S32 i = 0; i < excluded_objects.size(); i++)
  542. if (excluded_objects[i] == obj)
  543. return true;
  544. return false;
  545. }
  546. void PhysicalZone::registerExcludedObject(SceneObject* obj)
  547. {
  548. if (isExcludedObject(obj))
  549. return;
  550. excluded_objects.push_back(obj);
  551. setMaskBits(FadeMask);
  552. }
  553. void PhysicalZone::unregisterExcludedObject(SceneObject* obj)
  554. {
  555. for (S32 i = 0; i < excluded_objects.size(); i++)
  556. if (excluded_objects[i] == obj)
  557. {
  558. excluded_objects.erase(i);
  559. setMaskBits(FadeMask);
  560. return;
  561. }
  562. }