physicalZone.cpp 20 KB

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