physicalZone.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  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. //Default up a basic square
  102. Point3F vecs[3] = { Point3F(1.0, 0.0, 0.0),
  103. Point3F(0.0, -1.0, 0.0),
  104. Point3F(0.0, 0.0, 1.0) };
  105. mPolyhedron = Polyhedron(Point3F(-0.5, 0.5, 0.0), vecs);
  106. }
  107. PhysicalZone::~PhysicalZone()
  108. {
  109. delete mConvexList;
  110. mConvexList = NULL;
  111. }
  112. ImplementEnumType( PhysicalZone_ForceType, "Possible physical zone force types.\n" "@ingroup PhysicalZone\n\n" )
  113. { PhysicalZone::VECTOR, "vector", "..." },
  114. { PhysicalZone::SPHERICAL, "spherical", "..." },
  115. { PhysicalZone::CYLINDRICAL, "cylindrical", "..." },
  116. // aliases
  117. { PhysicalZone::SPHERICAL, "sphere", "..." },
  118. { PhysicalZone::CYLINDRICAL, "cylinder", "..." },
  119. EndImplementEnumType;
  120. //--------------------------------------------------------------------------
  121. void PhysicalZone::consoleInit()
  122. {
  123. Con::addVariable( "$PhysicalZone::renderZones", TypeBool, &smRenderPZones, "If true, a box will render around the location of all PhysicalZones.\n"
  124. "@ingroup EnviroMisc\n");
  125. }
  126. void PhysicalZone::initPersistFields()
  127. {
  128. docsURL;
  129. addGroup("Misc");
  130. addField("velocityMod", TypeF32, Offset(mVelocityMod, PhysicalZone), "Multiply velocity of objects entering zone by this value every tick.");
  131. addField("gravityMod", TypeF32, Offset(mGravityMod, PhysicalZone), "Gravity in PhysicalZone. Multiplies against standard gravity.");
  132. addField("appliedForce", TypePoint3F, Offset(mAppliedForce, PhysicalZone), "Three-element floating point value representing forces in three axes to apply to objects entering PhysicalZone.");
  133. addField("polyhedron", TypeTriggerPolyhedron, Offset(mPolyhedron, PhysicalZone),
  134. "The polyhedron type is really a quadrilateral and consists of a corner"
  135. "point followed by three vectors representing the edges extending from the corner." );
  136. endGroup("Misc");
  137. addGroup("AFX");
  138. addField("forceType", TYPEID<PhysicalZone::ForceType>(), Offset(force_type, PhysicalZone));
  139. addField("orientForce", TypeBool, Offset(orient_force, PhysicalZone));
  140. endGroup("AFX");
  141. Parent::initPersistFields();
  142. }
  143. //--------------------------------------------------------------------------
  144. bool PhysicalZone::onAdd()
  145. {
  146. if(!Parent::onAdd())
  147. return false;
  148. if (mVelocityMod < -40.0f || mVelocityMod > 40.0f) {
  149. Con::errorf("PhysicalZone: velocity mod out of range. [-40, 40]");
  150. mVelocityMod = mVelocityMod < -40.0f ? -40.0f : 40.0f;
  151. }
  152. if (mGravityMod < -40.0f || mGravityMod > 40.0f) {
  153. Con::errorf("PhysicalZone: GravityMod out of range. [-40, 40]");
  154. mGravityMod = mGravityMod < -40.0f ? -40.0f : 40.0f;
  155. }
  156. static const char* coordString[] = { "x", "y", "z" };
  157. F32* p = mAppliedForce;
  158. for (U32 i = 0; i < 3; i++) {
  159. if (p[i] < -40000.0f || p[i] > 40000.0f) {
  160. Con::errorf("PhysicalZone: applied force: %s out of range. [-40000, 40000]", coordString[i]);
  161. p[i] = p[i] < -40000.0f ? -40000.0f : 40000.0f;
  162. }
  163. }
  164. Polyhedron temp = mPolyhedron;
  165. setPolyhedron(temp);
  166. switch (force_type)
  167. {
  168. case SPHERICAL:
  169. force_mag = mAppliedForce.magnitudeSafe();
  170. break;
  171. case CYLINDRICAL:
  172. {
  173. Point3F force_vec = mAppliedForce;
  174. force_vec.z = 0.0;
  175. force_mag = force_vec.magnitudeSafe();
  176. }
  177. break;
  178. }
  179. addToScene();
  180. return true;
  181. }
  182. void PhysicalZone::onRemove()
  183. {
  184. mConvexList->nukeList();
  185. removeFromScene();
  186. Parent::onRemove();
  187. }
  188. void PhysicalZone::inspectPostApply()
  189. {
  190. Parent::inspectPostApply();
  191. setPolyhedron(mPolyhedron);
  192. setMaskBits(PolyhedronMask | MoveMask | SettingsMask | FadeMask);
  193. }
  194. //------------------------------------------------------------------------------
  195. void PhysicalZone::setTransform(const MatrixF & mat)
  196. {
  197. Parent::setTransform(mat);
  198. MatrixF base(true);
  199. base.scale(Point3F(1.0/mObjScale.x,
  200. 1.0/mObjScale.y,
  201. 1.0/mObjScale.z));
  202. base.mul(mWorldToObj);
  203. mClippedList.setBaseTransform(base);
  204. if (isServerObject())
  205. setMaskBits(MoveMask);
  206. }
  207. void PhysicalZone::prepRenderImage( SceneRenderState *state )
  208. {
  209. // only render if selected or render flag is set
  210. if ( !smRenderPZones && !isSelected() )
  211. return;
  212. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  213. ri->renderDelegate.bind( this, &PhysicalZone::renderObject );
  214. ri->type = RenderPassManager::RIT_Editor;
  215. ri->defaultKey = 0;
  216. ri->defaultKey2 = 0;
  217. state->getRenderPass()->addInst( ri );
  218. }
  219. void PhysicalZone::renderObject(ObjectRenderInst *ri,
  220. SceneRenderState *state,
  221. BaseMatInstance *overrideMat)
  222. {
  223. if (overrideMat)
  224. return;
  225. GFXStateBlockDesc desc;
  226. desc.setZReadWrite(true, false);
  227. desc.setBlend(true);
  228. desc.setCullMode(GFXCullNone);
  229. GFXTransformSaver saver;
  230. GFXDrawUtil *drawer = GFX->getDrawUtil();
  231. Point3F start = getBoxCenter();
  232. Box3F obb = mObjBox; //object bounding box
  233. F32 baseForce = 10000; //roughly the ammount of force needed to push a player back as it walks into a zone. (used for visual scaling)
  234. Point3F forceDir = getForce(&start);
  235. F32 forceLen = forceDir.len()/ baseForce;
  236. forceDir.normalizeSafe();
  237. ColorI guideCol = LinearColorF(mFabs(forceDir.x), mFabs(forceDir.y), mFabs(forceDir.z), 0.125).toColorI();
  238. if (force_type == VECTOR)
  239. {
  240. Point3F endPos = start + (forceDir * mMax(forceLen,0.75f));
  241. drawer->drawArrow(desc, start, endPos, guideCol, 0.05f);
  242. }
  243. MatrixF mat = getRenderTransform();
  244. mat.scale(getScale());
  245. GFX->multWorld(mat);
  246. start = obb.getCenter();
  247. if (force_type == VECTOR)
  248. {
  249. drawer->drawPolyhedron(desc, mPolyhedron, ColorI(0, 255, 0, 45));
  250. }
  251. else if (force_type == SPHERICAL)
  252. {
  253. F32 rad = obb.getBoundingSphere().radius/ 2;
  254. drawer->drawSphere(desc, rad, start, ColorI(0, 255, 0, 45));
  255. rad = (rad + (mAppliedForce.most() / baseForce))/2;
  256. desc.setFillModeWireframe();
  257. drawer->drawSphere(desc, rad, start, ColorI(0, 0, 255, 255));
  258. }
  259. else
  260. {
  261. Point3F bottomPos = start;
  262. bottomPos.z -= obb.len_z() / 2;
  263. Point3F topPos = start;
  264. topPos.z += obb.len_z() / 2;
  265. F32 rad = obb.len_x() / 2;
  266. drawer->drawCylinder(desc, bottomPos, topPos, rad, ColorI(0, 255, 0, 45));
  267. Point3F force_vec = mAppliedForce; //raw relative-applied force here as oposed to derived
  268. F32 hieght = (force_vec.z / baseForce);
  269. if (force_vec.z<0)
  270. bottomPos.z = (bottomPos.z + hieght)/2;
  271. else
  272. topPos.z = (topPos.z + hieght) / 2;
  273. if (force_vec.x > force_vec.y)
  274. rad = (rad + (force_vec.x / baseForce)) / 2;
  275. else
  276. rad = (rad + (force_vec.y / baseForce)) / 2;
  277. desc.setFillModeWireframe();
  278. drawer->drawCylinder(desc, bottomPos, topPos, rad, guideCol);
  279. }
  280. desc.setFillModeWireframe();
  281. drawer->drawPolyhedron(desc, mPolyhedron, ColorI::BLACK);
  282. }
  283. //--------------------------------------------------------------------------
  284. U32 PhysicalZone::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  285. {
  286. U32 i;
  287. U32 retMask = Parent::packUpdate(con, mask, stream);
  288. if (stream->writeFlag(mask & PolyhedronMask))
  289. {
  290. // Write the polyhedron
  291. stream->write(mPolyhedron.mPointList.size());
  292. for (i = 0; i < mPolyhedron.mPointList.size(); i++)
  293. mathWrite(*stream, mPolyhedron.mPointList[i]);
  294. stream->write(mPolyhedron.mPlaneList.size());
  295. for (i = 0; i < mPolyhedron.mPlaneList.size(); i++)
  296. mathWrite(*stream, mPolyhedron.mPlaneList[i]);
  297. stream->write(mPolyhedron.mEdgeList.size());
  298. for (i = 0; i < mPolyhedron.mEdgeList.size(); i++) {
  299. const Polyhedron::Edge& rEdge = mPolyhedron.mEdgeList[i];
  300. stream->write(rEdge.face[0]);
  301. stream->write(rEdge.face[1]);
  302. stream->write(rEdge.vertex[0]);
  303. stream->write(rEdge.vertex[1]);
  304. }
  305. }
  306. if (stream->writeFlag(mask & MoveMask))
  307. {
  308. stream->writeAffineTransform(mObjToWorld);
  309. mathWrite(*stream, mObjScale);
  310. }
  311. if (stream->writeFlag(mask & SettingsMask))
  312. {
  313. stream->write(mVelocityMod);
  314. stream->write(mGravityMod);
  315. mathWrite(*stream, mAppliedForce);
  316. stream->writeInt(force_type, FORCE_TYPE_BITS);
  317. stream->writeFlag(orient_force);
  318. }
  319. if (stream->writeFlag(mask & FadeMask))
  320. {
  321. U8 fade_byte = (U8)(fade_amt*255.0f);
  322. stream->write(fade_byte);
  323. }
  324. stream->writeFlag(mActive);
  325. return retMask;
  326. }
  327. void PhysicalZone::unpackUpdate(NetConnection* con, BitStream* stream)
  328. {
  329. Parent::unpackUpdate(con, stream);
  330. bool new_ph = false;
  331. if (stream->readFlag()) // PolyhedronMask
  332. {
  333. U32 i, size;
  334. Polyhedron tempPH;
  335. // Read the polyhedron
  336. stream->read(&size);
  337. tempPH.mPointList.setSize(size);
  338. for (i = 0; i < tempPH.mPointList.size(); i++)
  339. mathRead(*stream, &tempPH.mPointList[i]);
  340. stream->read(&size);
  341. tempPH.mPlaneList.setSize(size);
  342. for (i = 0; i < tempPH.mPlaneList.size(); i++)
  343. mathRead(*stream, &tempPH.mPlaneList[i]);
  344. stream->read(&size);
  345. tempPH.mEdgeList.setSize(size);
  346. for (i = 0; i < tempPH.mEdgeList.size(); i++) {
  347. Polyhedron::Edge& rEdge = tempPH.mEdgeList[i];
  348. stream->read(&rEdge.face[0]);
  349. stream->read(&rEdge.face[1]);
  350. stream->read(&rEdge.vertex[0]);
  351. stream->read(&rEdge.vertex[1]);
  352. }
  353. setPolyhedron(tempPH);
  354. new_ph = true;
  355. }
  356. if (stream->readFlag()) // MoveMask
  357. {
  358. MatrixF temp;
  359. stream->readAffineTransform(&temp);
  360. Point3F tempScale;
  361. mathRead(*stream, &tempScale);
  362. //if (!new_ph)
  363. //{
  364. // Polyhedron rPolyhedron = mPolyhedron;
  365. // setPolyhedron(rPolyhedron);
  366. //}
  367. setScale(tempScale);
  368. setTransform(temp);
  369. }
  370. if (stream->readFlag()) //SettingsMask
  371. {
  372. stream->read(&mVelocityMod);
  373. stream->read(&mGravityMod);
  374. mathRead(*stream, &mAppliedForce);
  375. force_type = stream->readInt(FORCE_TYPE_BITS); // AFX
  376. orient_force = stream->readFlag(); // AFX
  377. }
  378. if (stream->readFlag()) //FadeMask
  379. {
  380. U8 fade_byte;
  381. stream->read(&fade_byte);
  382. fade_amt = ((F32)fade_byte)/255.0f;
  383. }
  384. else
  385. fade_amt = 1.0f;
  386. mActive = stream->readFlag();
  387. }
  388. //--------------------------------------------------------------------------
  389. void PhysicalZone::setPolyhedron(const Polyhedron& rPolyhedron)
  390. {
  391. mPolyhedron = rPolyhedron;
  392. if (mPolyhedron.mPointList.size() != 0) {
  393. mObjBox.minExtents.set(1e10, 1e10, 1e10);
  394. mObjBox.maxExtents.set(-1e10, -1e10, -1e10);
  395. for (U32 i = 0; i < mPolyhedron.mPointList.size(); i++) {
  396. mObjBox.minExtents.setMin(mPolyhedron.mPointList[i]);
  397. mObjBox.maxExtents.setMax(mPolyhedron.mPointList[i]);
  398. }
  399. } else {
  400. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  401. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  402. }
  403. MatrixF xform = getTransform();
  404. setTransform(xform);
  405. mClippedList.clear();
  406. mClippedList.mPlaneList = mPolyhedron.mPlaneList;
  407. MatrixF base(true);
  408. base.scale(Point3F(1.0/mObjScale.x,
  409. 1.0/mObjScale.y,
  410. 1.0/mObjScale.z));
  411. base.mul(mWorldToObj);
  412. mClippedList.setBaseTransform(base);
  413. }
  414. //--------------------------------------------------------------------------
  415. void PhysicalZone::buildConvex(const Box3F& box, Convex* convex)
  416. {
  417. // These should really come out of a pool
  418. mConvexList->collectGarbage();
  419. Box3F realBox = box;
  420. mWorldToObj.mul(realBox);
  421. realBox.minExtents.convolveInverse(mObjScale);
  422. realBox.maxExtents.convolveInverse(mObjScale);
  423. if (realBox.isOverlapped(getObjBox()) == false)
  424. return;
  425. // Just return a box convex for the entire shape...
  426. Convex* cc = 0;
  427. CollisionWorkingList& wl = convex->getWorkingList();
  428. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
  429. if (itr->mConvex->getType() == BoxConvexType &&
  430. itr->mConvex->getObject() == this) {
  431. cc = itr->mConvex;
  432. break;
  433. }
  434. }
  435. if (cc)
  436. return;
  437. // Create a new convex.
  438. BoxConvex* cp = new BoxConvex;
  439. mConvexList->registerObject(cp);
  440. convex->addToWorkingList(cp);
  441. cp->init(this);
  442. mObjBox.getCenter(&cp->mCenter);
  443. cp->mSize.x = mObjBox.len_x() / 2.0f;
  444. cp->mSize.y = mObjBox.len_y() / 2.0f;
  445. cp->mSize.z = mObjBox.len_z() / 2.0f;
  446. }
  447. bool PhysicalZone::testObject(SceneObject* enter)
  448. {
  449. // TODO: This doesn't look like it's testing against the polyhedron at
  450. // all. And whats the point of building a convex if no collision methods
  451. // are implemented?
  452. if (mPolyhedron.mPointList.size() == 0)
  453. return false;
  454. mClippedList.clear();
  455. SphereF sphere;
  456. sphere.center = (mWorldBox.minExtents + mWorldBox.maxExtents) * 0.5;
  457. VectorF bv = mWorldBox.maxExtents - sphere.center;
  458. sphere.radius = bv.len();
  459. enter->buildPolyList(PLC_Collision, &mClippedList, mWorldBox, sphere);
  460. return mClippedList.isEmpty() == false;
  461. }
  462. bool PhysicalZone::testBox( const Box3F &box ) const
  463. {
  464. return mWorldBox.isOverlapped( box );
  465. }
  466. void PhysicalZone::activate()
  467. {
  468. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::open()");
  469. if (mActive != true)
  470. setMaskBits(ActiveMask);
  471. mActive = true;
  472. }
  473. void PhysicalZone::deactivate()
  474. {
  475. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::close()");
  476. if (mActive != false)
  477. setMaskBits(ActiveMask);
  478. mActive = false;
  479. }
  480. void PhysicalZone::onStaticModified(const char* slotName, const char*newValue)
  481. {
  482. if (dStricmp(slotName, "appliedForce") == 0 || dStricmp(slotName, "forceType") == 0)
  483. {
  484. switch (force_type)
  485. {
  486. case SPHERICAL:
  487. force_mag = mAppliedForce.magnitudeSafe();
  488. break;
  489. case CYLINDRICAL:
  490. {
  491. Point3F force_vec = mAppliedForce;
  492. force_vec.z = 0.0;
  493. force_mag = force_vec.magnitudeSafe();
  494. }
  495. break;
  496. }
  497. }
  498. }
  499. const Point3F& PhysicalZone::getForce(const Point3F* center) const
  500. {
  501. static Point3F force_vec;
  502. if (force_type == VECTOR)
  503. {
  504. if (orient_force)
  505. {
  506. getTransform().mulV(mAppliedForce, &force_vec);
  507. force_vec *= fade_amt;
  508. return force_vec;
  509. }
  510. force_vec = mAppliedForce;
  511. force_vec *= fade_amt;
  512. return force_vec;
  513. }
  514. if (!center)
  515. {
  516. force_vec.zero();
  517. return force_vec;
  518. }
  519. if (force_type == SPHERICAL)
  520. {
  521. force_vec = *center - getPosition();
  522. force_vec.normalizeSafe();
  523. force_vec *= force_mag*fade_amt;
  524. return force_vec;
  525. }
  526. if (orient_force)
  527. {
  528. force_vec = *center - getPosition();
  529. getWorldTransform().mulV(force_vec);
  530. force_vec.z = 0.0f;
  531. force_vec.normalizeSafe();
  532. force_vec *= force_mag;
  533. force_vec.z = mAppliedForce.z;
  534. getTransform().mulV(force_vec);
  535. force_vec *= fade_amt;
  536. return force_vec;
  537. }
  538. force_vec = *center - getPosition();
  539. force_vec.z = 0.0f;
  540. force_vec.normalizeSafe();
  541. force_vec *= force_mag;
  542. force_vec *= fade_amt;
  543. return force_vec;
  544. }
  545. bool PhysicalZone::isExcludedObject(SceneObject* obj) const
  546. {
  547. for (S32 i = 0; i < excluded_objects.size(); i++)
  548. if (excluded_objects[i] == obj)
  549. return true;
  550. return false;
  551. }
  552. void PhysicalZone::registerExcludedObject(SceneObject* obj)
  553. {
  554. if (isExcludedObject(obj))
  555. return;
  556. excluded_objects.push_back(obj);
  557. setMaskBits(FadeMask);
  558. }
  559. void PhysicalZone::unregisterExcludedObject(SceneObject* obj)
  560. {
  561. for (S32 i = 0; i < excluded_objects.size(); i++)
  562. if (excluded_objects[i] == obj)
  563. {
  564. excluded_objects.erase(i);
  565. setMaskBits(FadeMask);
  566. return;
  567. }
  568. }