physicalZone.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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. return retMask;
  274. }
  275. void PhysicalZone::unpackUpdate(NetConnection* con, BitStream* stream)
  276. {
  277. Parent::unpackUpdate(con, stream);
  278. bool new_ph = false;
  279. if (stream->readFlag()) // PolyhedronMask
  280. {
  281. U32 i, size;
  282. Polyhedron tempPH;
  283. // Read the polyhedron
  284. stream->read(&size);
  285. tempPH.pointList.setSize(size);
  286. for (i = 0; i < tempPH.pointList.size(); i++)
  287. mathRead(*stream, &tempPH.pointList[i]);
  288. stream->read(&size);
  289. tempPH.planeList.setSize(size);
  290. for (i = 0; i < tempPH.planeList.size(); i++)
  291. mathRead(*stream, &tempPH.planeList[i]);
  292. stream->read(&size);
  293. tempPH.edgeList.setSize(size);
  294. for (i = 0; i < tempPH.edgeList.size(); i++) {
  295. Polyhedron::Edge& rEdge = tempPH.edgeList[i];
  296. stream->read(&rEdge.face[0]);
  297. stream->read(&rEdge.face[1]);
  298. stream->read(&rEdge.vertex[0]);
  299. stream->read(&rEdge.vertex[1]);
  300. }
  301. setPolyhedron(tempPH);
  302. new_ph = true;
  303. }
  304. if (stream->readFlag()) // MoveMask
  305. {
  306. MatrixF temp;
  307. stream->readAffineTransform(&temp);
  308. Point3F tempScale;
  309. mathRead(*stream, &tempScale);
  310. //if (!new_ph)
  311. //{
  312. // Polyhedron rPolyhedron = mPolyhedron;
  313. // setPolyhedron(rPolyhedron);
  314. //}
  315. setScale(tempScale);
  316. setTransform(temp);
  317. }
  318. if (stream->readFlag()) //SettingsMask
  319. {
  320. stream->read(&mVelocityMod);
  321. stream->read(&mGravityMod);
  322. mathRead(*stream, &mAppliedForce);
  323. force_type = stream->readInt(FORCE_TYPE_BITS); // AFX
  324. orient_force = stream->readFlag(); // AFX
  325. }
  326. if (stream->readFlag()) //FadeMask
  327. {
  328. U8 fade_byte;
  329. stream->read(&fade_byte);
  330. fade_amt = ((F32)fade_byte)/255.0f;
  331. }
  332. else
  333. fade_amt = 1.0f;
  334. mActive = stream->readFlag();
  335. }
  336. //--------------------------------------------------------------------------
  337. void PhysicalZone::setPolyhedron(const Polyhedron& rPolyhedron)
  338. {
  339. mPolyhedron = rPolyhedron;
  340. if (mPolyhedron.pointList.size() != 0) {
  341. mObjBox.minExtents.set(1e10, 1e10, 1e10);
  342. mObjBox.maxExtents.set(-1e10, -1e10, -1e10);
  343. for (U32 i = 0; i < mPolyhedron.pointList.size(); i++) {
  344. mObjBox.minExtents.setMin(mPolyhedron.pointList[i]);
  345. mObjBox.maxExtents.setMax(mPolyhedron.pointList[i]);
  346. }
  347. } else {
  348. mObjBox.minExtents.set(-0.5, -0.5, -0.5);
  349. mObjBox.maxExtents.set( 0.5, 0.5, 0.5);
  350. }
  351. MatrixF xform = getTransform();
  352. setTransform(xform);
  353. mClippedList.clear();
  354. mClippedList.mPlaneList = mPolyhedron.planeList;
  355. MatrixF base(true);
  356. base.scale(Point3F(1.0/mObjScale.x,
  357. 1.0/mObjScale.y,
  358. 1.0/mObjScale.z));
  359. base.mul(mWorldToObj);
  360. mClippedList.setBaseTransform(base);
  361. }
  362. //--------------------------------------------------------------------------
  363. void PhysicalZone::buildConvex(const Box3F& box, Convex* convex)
  364. {
  365. // These should really come out of a pool
  366. mConvexList->collectGarbage();
  367. Box3F realBox = box;
  368. mWorldToObj.mul(realBox);
  369. realBox.minExtents.convolveInverse(mObjScale);
  370. realBox.maxExtents.convolveInverse(mObjScale);
  371. if (realBox.isOverlapped(getObjBox()) == false)
  372. return;
  373. // Just return a box convex for the entire shape...
  374. Convex* cc = 0;
  375. CollisionWorkingList& wl = convex->getWorkingList();
  376. for (CollisionWorkingList* itr = wl.wLink.mNext; itr != &wl; itr = itr->wLink.mNext) {
  377. if (itr->mConvex->getType() == BoxConvexType &&
  378. itr->mConvex->getObject() == this) {
  379. cc = itr->mConvex;
  380. break;
  381. }
  382. }
  383. if (cc)
  384. return;
  385. // Create a new convex.
  386. BoxConvex* cp = new BoxConvex;
  387. mConvexList->registerObject(cp);
  388. convex->addToWorkingList(cp);
  389. cp->init(this);
  390. mObjBox.getCenter(&cp->mCenter);
  391. cp->mSize.x = mObjBox.len_x() / 2.0f;
  392. cp->mSize.y = mObjBox.len_y() / 2.0f;
  393. cp->mSize.z = mObjBox.len_z() / 2.0f;
  394. }
  395. bool PhysicalZone::testObject(SceneObject* enter)
  396. {
  397. // TODO: This doesn't look like it's testing against the polyhedron at
  398. // all. And whats the point of building a convex if no collision methods
  399. // are implemented?
  400. if (mPolyhedron.pointList.size() == 0)
  401. return false;
  402. mClippedList.clear();
  403. SphereF sphere;
  404. sphere.center = (mWorldBox.minExtents + mWorldBox.maxExtents) * 0.5;
  405. VectorF bv = mWorldBox.maxExtents - sphere.center;
  406. sphere.radius = bv.len();
  407. enter->buildPolyList(PLC_Collision, &mClippedList, mWorldBox, sphere);
  408. return mClippedList.isEmpty() == false;
  409. }
  410. bool PhysicalZone::testBox( const Box3F &box ) const
  411. {
  412. return mWorldBox.isOverlapped( box );
  413. }
  414. void PhysicalZone::activate()
  415. {
  416. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::open()");
  417. if (mActive != true)
  418. setMaskBits(ActiveMask);
  419. mActive = true;
  420. }
  421. void PhysicalZone::deactivate()
  422. {
  423. AssertFatal(isServerObject(), "Client objects not allowed in ForceFieldInstance::close()");
  424. if (mActive != false)
  425. setMaskBits(ActiveMask);
  426. mActive = false;
  427. }
  428. void PhysicalZone::onStaticModified(const char* slotName, const char*newValue)
  429. {
  430. if (dStricmp(slotName, "appliedForce") == 0 || dStricmp(slotName, "forceType") == 0)
  431. {
  432. switch (force_type)
  433. {
  434. case SPHERICAL:
  435. force_mag = mAppliedForce.magnitudeSafe();
  436. break;
  437. case CYLINDRICAL:
  438. {
  439. Point3F force_vec = mAppliedForce;
  440. force_vec.z = 0.0;
  441. force_mag = force_vec.magnitudeSafe();
  442. }
  443. break;
  444. }
  445. }
  446. }
  447. const Point3F& PhysicalZone::getForce(const Point3F* center) const
  448. {
  449. static Point3F force_vec;
  450. if (force_type == VECTOR)
  451. {
  452. if (orient_force)
  453. {
  454. getTransform().mulV(mAppliedForce, &force_vec);
  455. force_vec *= fade_amt;
  456. return force_vec;
  457. }
  458. force_vec = mAppliedForce;
  459. force_vec *= fade_amt;
  460. return force_vec;
  461. }
  462. if (!center)
  463. {
  464. force_vec.zero();
  465. return force_vec;
  466. }
  467. if (force_type == SPHERICAL)
  468. {
  469. force_vec = *center - getPosition();
  470. force_vec.normalizeSafe();
  471. force_vec *= force_mag*fade_amt;
  472. return force_vec;
  473. }
  474. if (orient_force)
  475. {
  476. force_vec = *center - getPosition();
  477. getWorldTransform().mulV(force_vec);
  478. force_vec.z = 0.0f;
  479. force_vec.normalizeSafe();
  480. force_vec *= force_mag;
  481. force_vec.z = mAppliedForce.z;
  482. getTransform().mulV(force_vec);
  483. force_vec *= fade_amt;
  484. return force_vec;
  485. }
  486. force_vec = *center - getPosition();
  487. force_vec.z = 0.0f;
  488. force_vec.normalizeSafe();
  489. force_vec *= force_mag;
  490. force_vec *= fade_amt;
  491. return force_vec;
  492. }
  493. bool PhysicalZone::isExcludedObject(SceneObject* obj) const
  494. {
  495. for (S32 i = 0; i < excluded_objects.size(); i++)
  496. if (excluded_objects[i] == obj)
  497. return true;
  498. return false;
  499. }
  500. void PhysicalZone::registerExcludedObject(SceneObject* obj)
  501. {
  502. if (isExcludedObject(obj))
  503. return;
  504. excluded_objects.push_back(obj);
  505. setMaskBits(FadeMask);
  506. }
  507. void PhysicalZone::unregisterExcludedObject(SceneObject* obj)
  508. {
  509. for (S32 i = 0; i < excluded_objects.size(); i++)
  510. if (excluded_objects[i] == obj)
  511. {
  512. excluded_objects.erase(i);
  513. setMaskBits(FadeMask);
  514. return;
  515. }
  516. }