physicalZone.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. setPolyhedron(mPolyhedron);
  185. Parent::inspectPostApply();
  186. }
  187. //------------------------------------------------------------------------------
  188. void PhysicalZone::setTransform(const MatrixF & mat)
  189. {
  190. Parent::setTransform(mat);
  191. MatrixF base(true);
  192. base.scale(Point3F(1.0/mObjScale.x,
  193. 1.0/mObjScale.y,
  194. 1.0/mObjScale.z));
  195. base.mul(mWorldToObj);
  196. mClippedList.setBaseTransform(base);
  197. if (isServerObject())
  198. setMaskBits(MoveMask);
  199. }
  200. void PhysicalZone::prepRenderImage( SceneRenderState *state )
  201. {
  202. // only render if selected or render flag is set
  203. if ( !smRenderPZones && !isSelected() )
  204. return;
  205. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  206. ri->renderDelegate.bind( this, &PhysicalZone::renderObject );
  207. ri->type = RenderPassManager::RIT_Editor;
  208. ri->defaultKey = 0;
  209. ri->defaultKey2 = 0;
  210. state->getRenderPass()->addInst( ri );
  211. }
  212. void PhysicalZone::renderObject( ObjectRenderInst *ri,
  213. SceneRenderState *state,
  214. BaseMatInstance *overrideMat )
  215. {
  216. if (overrideMat)
  217. return;
  218. GFXStateBlockDesc desc;
  219. desc.setZReadWrite( true, false );
  220. desc.setBlend( true );
  221. desc.setCullMode( GFXCullNone );
  222. GFXTransformSaver saver;
  223. MatrixF mat = getRenderTransform();
  224. mat.scale( getScale() );
  225. GFX->multWorld( mat );
  226. GFXDrawUtil *drawer = GFX->getDrawUtil();
  227. drawer->drawPolyhedron( desc, mPolyhedron, ColorI( 0, 255, 0, 45 ) );
  228. desc.setFillModeWireframe();
  229. drawer->drawPolyhedron( desc, mPolyhedron, ColorI::BLACK );
  230. }
  231. //--------------------------------------------------------------------------
  232. U32 PhysicalZone::packUpdate(NetConnection* con, U32 mask, BitStream* stream)
  233. {
  234. U32 i;
  235. U32 retMask = Parent::packUpdate(con, mask, stream);
  236. if (stream->writeFlag(mask & PolyhedronMask))
  237. {
  238. // Write the polyhedron
  239. stream->write(mPolyhedron.pointList.size());
  240. for (i = 0; i < mPolyhedron.pointList.size(); i++)
  241. mathWrite(*stream, mPolyhedron.pointList[i]);
  242. stream->write(mPolyhedron.planeList.size());
  243. for (i = 0; i < mPolyhedron.planeList.size(); i++)
  244. mathWrite(*stream, mPolyhedron.planeList[i]);
  245. stream->write(mPolyhedron.edgeList.size());
  246. for (i = 0; i < mPolyhedron.edgeList.size(); i++) {
  247. const Polyhedron::Edge& rEdge = mPolyhedron.edgeList[i];
  248. stream->write(rEdge.face[0]);
  249. stream->write(rEdge.face[1]);
  250. stream->write(rEdge.vertex[0]);
  251. stream->write(rEdge.vertex[1]);
  252. }
  253. }
  254. if (stream->writeFlag(mask & MoveMask))
  255. {
  256. stream->writeAffineTransform(mObjToWorld);
  257. mathWrite(*stream, mObjScale);
  258. }
  259. if (stream->writeFlag(mask & SettingsMask))
  260. {
  261. stream->write(mVelocityMod);
  262. stream->write(mGravityMod);
  263. mathWrite(*stream, mAppliedForce);
  264. stream->writeInt(force_type, FORCE_TYPE_BITS);
  265. stream->writeFlag(orient_force);
  266. }
  267. if (stream->writeFlag(mask & FadeMask))
  268. {
  269. U8 fade_byte = (U8)(fade_amt*255.0f);
  270. stream->write(fade_byte);
  271. }
  272. stream->writeFlag(mActive);
  273. // AFX CODE BLOCK (enhanced-physical-zone)(pz-opt) >>
  274. return retMask;
  275. }
  276. void PhysicalZone::unpackUpdate(NetConnection* con, BitStream* stream)
  277. {
  278. Parent::unpackUpdate(con, stream);
  279. bool new_ph = false;
  280. if (stream->readFlag()) // PolyhedronMask
  281. {
  282. U32 i, size;
  283. Polyhedron tempPH;
  284. // Read the polyhedron
  285. stream->read(&size);
  286. tempPH.pointList.setSize(size);
  287. for (i = 0; i < tempPH.pointList.size(); i++)
  288. mathRead(*stream, &tempPH.pointList[i]);
  289. stream->read(&size);
  290. tempPH.planeList.setSize(size);
  291. for (i = 0; i < tempPH.planeList.size(); i++)
  292. mathRead(*stream, &tempPH.planeList[i]);
  293. stream->read(&size);
  294. tempPH.edgeList.setSize(size);
  295. for (i = 0; i < tempPH.edgeList.size(); i++) {
  296. Polyhedron::Edge& rEdge = tempPH.edgeList[i];
  297. stream->read(&rEdge.face[0]);
  298. stream->read(&rEdge.face[1]);
  299. stream->read(&rEdge.vertex[0]);
  300. stream->read(&rEdge.vertex[1]);
  301. }
  302. setPolyhedron(tempPH);
  303. new_ph = true;
  304. }
  305. if (stream->readFlag()) // MoveMask
  306. {
  307. MatrixF temp;
  308. stream->readAffineTransform(&temp);
  309. Point3F tempScale;
  310. mathRead(*stream, &tempScale);
  311. //if (!new_ph)
  312. //{
  313. // Polyhedron rPolyhedron = mPolyhedron;
  314. // setPolyhedron(rPolyhedron);
  315. //}
  316. setScale(tempScale);
  317. setTransform(temp);
  318. }
  319. if (stream->readFlag()) //SettingsMask
  320. {
  321. stream->read(&mVelocityMod);
  322. stream->read(&mGravityMod);
  323. mathRead(*stream, &mAppliedForce);
  324. force_type = stream->readInt(FORCE_TYPE_BITS); // AFX
  325. orient_force = stream->readFlag(); // AFX
  326. }
  327. if (stream->readFlag()) //FadeMask
  328. {
  329. U8 fade_byte;
  330. stream->read(&fade_byte);
  331. fade_amt = ((F32)fade_byte)/255.0f;
  332. }
  333. else
  334. fade_amt = 1.0f;
  335. mActive = stream->readFlag();
  336. }
  337. //--------------------------------------------------------------------------
  338. void PhysicalZone::setPolyhedron(const Polyhedron& rPolyhedron)
  339. {
  340. mPolyhedron = rPolyhedron;
  341. if (mPolyhedron.pointList.size() != 0) {
  342. mObjBox.minExtents.set(1e10, 1e10, 1e10);
  343. mObjBox.maxExtents.set(-1e10, -1e10, -1e10);
  344. for (U32 i = 0; i < mPolyhedron.pointList.size(); i++) {
  345. mObjBox.minExtents.setMin(mPolyhedron.pointList[i]);
  346. mObjBox.maxExtents.setMax(mPolyhedron.pointList[i]);
  347. }
  348. } else {
  349. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  350. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  351. }
  352. MatrixF xform = getTransform();
  353. setTransform(xform);
  354. mClippedList.clear();
  355. mClippedList.mPlaneList = mPolyhedron.planeList;
  356. MatrixF base(true);
  357. base.scale(Point3F(1.0/mObjScale.x,
  358. 1.0/mObjScale.y,
  359. 1.0/mObjScale.z));
  360. base.mul(mWorldToObj);
  361. mClippedList.setBaseTransform(base);
  362. }
  363. //--------------------------------------------------------------------------
  364. void PhysicalZone::buildConvex(const Box3F& box, Convex* convex)
  365. {
  366. // These should really come out of a pool
  367. mConvexList->collectGarbage();
  368. Box3F realBox = box;
  369. mWorldToObj.mul(realBox);
  370. realBox.minExtents.convolveInverse(mObjScale);
  371. realBox.maxExtents.convolveInverse(mObjScale);
  372. if (realBox.isOverlapped(getObjBox()) == false)
  373. return;
  374. // Just return a box convex for the entire shape...
  375. Convex* cc = 0;
  376. CollisionWorkingList& wl = convex->getWorkingList();
  377. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
  378. if (itr->mConvex->getType() == BoxConvexType &&
  379. itr->mConvex->getObject() == this) {
  380. cc = itr->mConvex;
  381. break;
  382. }
  383. }
  384. if (cc)
  385. return;
  386. // Create a new convex.
  387. BoxConvex* cp = new BoxConvex;
  388. mConvexList->registerObject(cp);
  389. convex->addToWorkingList(cp);
  390. cp->init(this);
  391. mObjBox.getCenter(&cp->mCenter);
  392. cp->mSize.x = mObjBox.len_x() / 2.0f;
  393. cp->mSize.y = mObjBox.len_y() / 2.0f;
  394. cp->mSize.z = mObjBox.len_z() / 2.0f;
  395. }
  396. bool PhysicalZone::testObject(SceneObject* enter)
  397. {
  398. // TODO: This doesn't look like it's testing against the polyhedron at
  399. // all. And whats the point of building a convex if no collision methods
  400. // are implemented?
  401. if (mPolyhedron.pointList.size() == 0)
  402. return false;
  403. mClippedList.clear();
  404. SphereF sphere;
  405. sphere.center = (mWorldBox.minExtents + mWorldBox.maxExtents) * 0.5;
  406. VectorF bv = mWorldBox.maxExtents - sphere.center;
  407. sphere.radius = bv.len();
  408. enter->buildPolyList(PLC_Collision, &mClippedList, mWorldBox, sphere);
  409. return mClippedList.isEmpty() == false;
  410. }
  411. bool PhysicalZone::testBox( const Box3F &box ) const
  412. {
  413. return mWorldBox.isOverlapped( box );
  414. }
  415. void PhysicalZone::activate()
  416. {
  417. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::open()");
  418. if (mActive != true)
  419. setMaskBits(ActiveMask);
  420. mActive = true;
  421. }
  422. void PhysicalZone::deactivate()
  423. {
  424. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::close()");
  425. if (mActive != false)
  426. setMaskBits(ActiveMask);
  427. mActive = false;
  428. }
  429. void PhysicalZone::onStaticModified(const char* slotName, const char*newValue)
  430. {
  431. if (dStricmp(slotName, "appliedForce") == 0 || dStricmp(slotName, "forceType") == 0)
  432. {
  433. switch (force_type)
  434. {
  435. case SPHERICAL:
  436. force_mag = mAppliedForce.magnitudeSafe();
  437. break;
  438. case CYLINDRICAL:
  439. {
  440. Point3F force_vec = mAppliedForce;
  441. force_vec.z = 0.0;
  442. force_mag = force_vec.magnitudeSafe();
  443. }
  444. break;
  445. }
  446. }
  447. }
  448. const Point3F& PhysicalZone::getForce(const Point3F* center) const
  449. {
  450. static Point3F force_vec;
  451. if (force_type == VECTOR)
  452. {
  453. if (orient_force)
  454. {
  455. getTransform().mulV(mAppliedForce, &force_vec);
  456. force_vec *= fade_amt;
  457. return force_vec;
  458. }
  459. force_vec = mAppliedForce;
  460. force_vec *= fade_amt;
  461. return force_vec;
  462. }
  463. if (!center)
  464. {
  465. force_vec.zero();
  466. return force_vec;
  467. }
  468. if (force_type == SPHERICAL)
  469. {
  470. force_vec = *center - getPosition();
  471. force_vec.normalizeSafe();
  472. force_vec *= force_mag*fade_amt;
  473. return force_vec;
  474. }
  475. if (orient_force)
  476. {
  477. force_vec = *center - getPosition();
  478. getWorldTransform().mulV(force_vec);
  479. force_vec.z = 0.0f;
  480. force_vec.normalizeSafe();
  481. force_vec *= force_mag;
  482. force_vec.z = mAppliedForce.z;
  483. getTransform().mulV(force_vec);
  484. force_vec *= fade_amt;
  485. return force_vec;
  486. }
  487. force_vec = *center - getPosition();
  488. force_vec.z = 0.0f;
  489. force_vec.normalizeSafe();
  490. force_vec *= force_mag;
  491. force_vec *= fade_amt;
  492. return force_vec;
  493. }
  494. bool PhysicalZone::isExcludedObject(SceneObject* obj) const
  495. {
  496. for (S32 i = 0; i < excluded_objects.size(); i++)
  497. if (excluded_objects[i] == obj)
  498. return true;
  499. return false;
  500. }
  501. void PhysicalZone::registerExcludedObject(SceneObject* obj)
  502. {
  503. if (isExcludedObject(obj))
  504. return;
  505. excluded_objects.push_back(obj);
  506. setMaskBits(FadeMask);
  507. }
  508. void PhysicalZone::unregisterExcludedObject(SceneObject* obj)
  509. {
  510. for (S32 i = 0; i < excluded_objects.size(); i++)
  511. if (excluded_objects[i] == obj)
  512. {
  513. excluded_objects.erase(i);
  514. setMaskBits(FadeMask);
  515. return;
  516. }
  517. }