sceneObject.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  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 "platform/platform.h"
  27. #include "scene/sceneObject.h"
  28. #include "platform/profiler.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/engineAPI.h"
  31. #include "console/simPersistID.h"
  32. #include "sim/netConnection.h"
  33. #include "core/stream/bitStream.h"
  34. #include "scene/sceneManager.h"
  35. #include "scene/sceneTracker.h"
  36. #include "scene/sceneRenderState.h"
  37. #include "scene/zones/sceneZoneSpace.h"
  38. #include "collision/extrudedPolyList.h"
  39. #include "collision/earlyOutPolyList.h"
  40. #include "collision/optimizedPolyList.h"
  41. #include "math/mPolyhedron.h"
  42. #include "gfx/bitmap/gBitmap.h"
  43. #include "math/util/frustum.h"
  44. #include "math/mathIO.h"
  45. #include "math/mTransform.h"
  46. #include "T3D/gameBase/gameProcess.h"
  47. #include "T3D/gameBase/gameConnection.h"
  48. #include "T3D/accumulationVolume.h"
  49. IMPLEMENT_CONOBJECT(SceneObject);
  50. ConsoleDocClass( SceneObject,
  51. "@brief A networkable object that exists in the 3D world.\n\n"
  52. "The SceneObject class provides the foundation for 3D objects in the Engine. It "
  53. "exposes the functionality for:\n\n"
  54. "<ul><li>Position, rotation and scale within the world.</li>"
  55. "<li>Working with a scene graph (in the Zone and Portal sections), allowing efficient "
  56. "and robust rendering of the game scene.</li>"
  57. "<li>Various helper functions, including functions to get bounding information "
  58. "and momentum/velocity.</li>"
  59. "<li>Mounting one SceneObject to another.</li>"
  60. "<li>An interface for collision detection, as well as ray casting.</li>"
  61. "<li>Lighting. SceneObjects can register lights both at lightmap generation "
  62. "time, and dynamic lights at runtime (for special effects, such as from flame "
  63. "or a projectile, or from an explosion).</li></ul>\n\n"
  64. "You do not typically work with SceneObjects themselves. The SceneObject provides a reference "
  65. "within the game world (the scene), but does not render to the client on its own. The "
  66. "same is true of collision detection beyond that of the bounding box. Instead you "
  67. "use one of the many classes that derrive from SceneObject, such as TSStatic.\n\n"
  68. "@section SceneObject_Hiding Difference Between setHidden() and isRenderEnabled\n\n"
  69. "When it comes time to decide if a SceneObject should render or not, there are two "
  70. "methods that can stop the SceneObject from rendering at all. You need to be aware of "
  71. "the differences between these two methods as they impact how the SceneObject is networked "
  72. "from the server to the client.\n\n"
  73. "The first method of manually controlling if a SceneObject is rendered is through its "
  74. "SceneObject::isRenderEnabled property. When set to false the SceneObject is considered invisible but "
  75. "still present within the scene. This means it still takes part in collisions and continues "
  76. "to be networked.\n\n"
  77. "The second method is using the setHidden() method. This will actually remove a SceneObject "
  78. "from the scene and it will no longer be networked from the server to the cleint. Any client-side "
  79. "ghost of the object will be deleted as the server no longer considers the object to be in scope.\n\n"
  80. "@ingroup gameObjects\n"
  81. );
  82. #ifdef TORQUE_TOOLS
  83. extern bool gEditingMission;
  84. #endif
  85. Signal< void( SceneObject* ) > SceneObject::smSceneObjectAdd;
  86. Signal< void( SceneObject* ) > SceneObject::smSceneObjectRemove;
  87. //-----------------------------------------------------------------------------
  88. SceneObject::SceneObject()
  89. {
  90. mContainer = 0;
  91. mTypeMask = DefaultObjectType;
  92. mCollisionCount = 0;
  93. mGlobalBounds = false;
  94. mObjScale.set(1,1,1);
  95. mObjToWorld.identity();
  96. mWorldToObj.identity();
  97. mObjBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  98. mWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  99. mWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  100. mRenderObjToWorld.identity();
  101. mRenderWorldToObj.identity();
  102. mRenderWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  103. mRenderWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  104. mContainerSeqKey = 0;
  105. mBinRefHead = NULL;
  106. mSceneManager = NULL;
  107. mNumCurrZones = 0;
  108. mZoneRefHead = NULL;
  109. mZoneRefDirty = false;
  110. mBinMinX = 0xFFFFFFFF;
  111. mBinMaxX = 0xFFFFFFFF;
  112. mBinMinY = 0xFFFFFFFF;
  113. mBinMaxY = 0xFFFFFFFF;
  114. mLightPlugin = NULL;
  115. mMount.object = NULL;
  116. mMount.link = NULL;
  117. mMount.list = NULL;
  118. mMount.node = -1;
  119. mMount.xfm = MatrixF::Identity;
  120. mMountPID = NULL;
  121. mSceneObjectLinks = NULL;
  122. mObjectFlags.set( RenderEnabledFlag | SelectionEnabledFlag );
  123. mIsScopeAlways = false;
  124. mAccuTex = NULL;
  125. mSelectionFlags = 0;
  126. mPathfindingIgnore = false;
  127. mGameObjectAssetId = StringTable->insert("");
  128. mDirtyGameObject = false;
  129. }
  130. //-----------------------------------------------------------------------------
  131. SceneObject::~SceneObject()
  132. {
  133. AssertFatal( mZoneRefHead == NULL && mBinRefHead == NULL,
  134. "SceneObject::~SceneObject - Object still linked in reference lists!");
  135. AssertFatal( !mSceneObjectLinks,
  136. "SceneObject::~SceneObject() - object is still linked to SceneTrackers" );
  137. mAccuTex = NULL;
  138. unlink();
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool SceneObject::castRayRendered(const Point3F &start, const Point3F &end, RayInfo *info)
  142. {
  143. // By default, all ray checking against the rendered mesh will be passed
  144. // on to the collision mesh. This saves having to define both methods
  145. // for simple objects.
  146. return castRay( start, end, info );
  147. }
  148. //-----------------------------------------------------------------------------
  149. bool SceneObject::containsPoint( const Point3F& point )
  150. {
  151. // If it's not in the AABB, then it can't be in the OBB either,
  152. // so early out.
  153. if( !mWorldBox.isContained( point ) )
  154. return false;
  155. // Transform point into object space and test it against
  156. // our object space bounding box.
  157. Point3F objPoint( 0, 0, 0 );
  158. getWorldTransform().mulP( point, &objPoint );
  159. objPoint.convolveInverse( getScale() );
  160. return ( mObjBox.isContained( objPoint ) );
  161. }
  162. //-----------------------------------------------------------------------------
  163. bool SceneObject::collideBox(const Point3F &start, const Point3F &end, RayInfo *info)
  164. {
  165. const F32 * pStart = (const F32*)start;
  166. const F32 * pEnd = (const F32*)end;
  167. const F32 * pMin = (const F32*)mObjBox.minExtents;
  168. const F32 * pMax = (const F32*)mObjBox.maxExtents;
  169. F32 maxStartTime = -1;
  170. F32 minEndTime = 1;
  171. F32 startTime;
  172. F32 endTime;
  173. // used for getting normal
  174. U32 hitIndex = 0xFFFFFFFF;
  175. U32 side;
  176. // walk the axis
  177. for(U32 i = 0; i < 3; i++)
  178. {
  179. //
  180. if(pStart[i] < pEnd[i])
  181. {
  182. if(pEnd[i] < pMin[i] || pStart[i] > pMax[i])
  183. return(false);
  184. F32 dist = pEnd[i] - pStart[i];
  185. startTime = (pStart[i] < pMin[i]) ? (pMin[i] - pStart[i]) / dist : -1;
  186. endTime = (pEnd[i] > pMax[i]) ? (pMax[i] - pStart[i]) / dist : 1;
  187. side = 1;
  188. }
  189. else
  190. {
  191. if(pStart[i] < pMin[i] || pEnd[i] > pMax[i])
  192. return(false);
  193. F32 dist = pStart[i] - pEnd[i];
  194. startTime = (pStart[i] > pMax[i]) ? (pStart[i] - pMax[i]) / dist : -1;
  195. endTime = (pEnd[i] < pMin[i]) ? (pStart[i] - pMin[i]) / dist : 1;
  196. side = 0;
  197. }
  198. //
  199. if(startTime > maxStartTime)
  200. {
  201. maxStartTime = startTime;
  202. hitIndex = i * 2 + side;
  203. }
  204. if(endTime < minEndTime)
  205. minEndTime = endTime;
  206. if(minEndTime < maxStartTime)
  207. return(false);
  208. }
  209. // fail if inside
  210. if(maxStartTime < 0.f)
  211. return(false);
  212. //
  213. static Point3F boxNormals[] = {
  214. Point3F( 1, 0, 0),
  215. Point3F(-1, 0, 0),
  216. Point3F( 0, 1, 0),
  217. Point3F( 0,-1, 0),
  218. Point3F( 0, 0, 1),
  219. Point3F( 0, 0,-1),
  220. };
  221. //
  222. AssertFatal(hitIndex != 0xFFFFFFFF, "SceneObject::collideBox");
  223. info->t = maxStartTime;
  224. info->object = this;
  225. mObjToWorld.mulV(boxNormals[hitIndex], &info->normal);
  226. info->material = 0;
  227. return(true);
  228. }
  229. //-----------------------------------------------------------------------------
  230. void SceneObject::disableCollision()
  231. {
  232. mCollisionCount++;
  233. AssertFatal(mCollisionCount < 50, "SceneObject::disableCollision called 50 times on the same object. Is this inside a circular loop?" );
  234. }
  235. //-----------------------------------------------------------------------------
  236. void SceneObject::enableCollision()
  237. {
  238. if (mCollisionCount)
  239. --mCollisionCount;
  240. }
  241. //-----------------------------------------------------------------------------
  242. bool SceneObject::onAdd()
  243. {
  244. if ( !Parent::onAdd() )
  245. return false;
  246. mIsScopeAlways = mNetFlags.test( ScopeAlways );
  247. mWorldToObj = mObjToWorld;
  248. mWorldToObj.affineInverse();
  249. resetWorldBox();
  250. setRenderTransform(mObjToWorld);
  251. resolveMountPID();
  252. smSceneObjectAdd.trigger(this);
  253. return true;
  254. }
  255. //-----------------------------------------------------------------------------
  256. void SceneObject::onRemove()
  257. {
  258. smSceneObjectRemove.trigger(this);
  259. unmount();
  260. plUnlink();
  261. Parent::onRemove();
  262. }
  263. //-----------------------------------------------------------------------------
  264. void SceneObject::addToScene()
  265. {
  266. if( mSceneManager )
  267. return;
  268. if( isClientObject() )
  269. gClientSceneGraph->addObjectToScene( this );
  270. else
  271. gServerSceneGraph->addObjectToScene( this );
  272. }
  273. //-----------------------------------------------------------------------------
  274. void SceneObject::removeFromScene()
  275. {
  276. if( !mSceneManager )
  277. return;
  278. mSceneManager->removeObjectFromScene( this );
  279. }
  280. //-----------------------------------------------------------------------------
  281. void SceneObject::onDeleteNotify( SimObject *obj )
  282. {
  283. // We are comparing memory addresses so even if obj really is not a
  284. // ProcessObject this cast shouldn't break anything.
  285. if ( obj == mAfterObject )
  286. mAfterObject = NULL;
  287. if ( obj == mMount.object )
  288. unmount();
  289. Parent::onDeleteNotify( obj );
  290. }
  291. //-----------------------------------------------------------------------------
  292. void SceneObject::inspectPostApply()
  293. {
  294. if( isServerObject() )
  295. setMaskBits( MountedMask );
  296. Parent::inspectPostApply();
  297. }
  298. //-----------------------------------------------------------------------------
  299. void SceneObject::setGlobalBounds()
  300. {
  301. mGlobalBounds = true;
  302. mObjBox.minExtents.set( -1e10, -1e10, -1e10 );
  303. mObjBox.maxExtents.set( 1e10, 1e10, 1e10 );
  304. if( mSceneManager )
  305. mSceneManager->notifyObjectDirty( this );
  306. }
  307. //-----------------------------------------------------------------------------
  308. void SceneObject::setTransform( const MatrixF& mat )
  309. {
  310. // This test is a bit expensive so turn it off in release.
  311. #ifdef TORQUE_DEBUG
  312. //AssertFatal( mat.isAffine(), "SceneObject::setTransform() - Bad transform (non affine)!" );
  313. #endif
  314. PROFILE_SCOPE( SceneObject_setTransform );
  315. // Update the transforms.
  316. mObjToWorld = mWorldToObj = mat;
  317. mWorldToObj.affineInverse();
  318. // Update the world-space AABB.
  319. resetWorldBox();
  320. // If we're in a SceneManager, sync our scene state.
  321. if( mSceneManager != NULL )
  322. mSceneManager->notifyObjectDirty( this );
  323. setRenderTransform( mat );
  324. }
  325. //-----------------------------------------------------------------------------
  326. void SceneObject::setScale( const VectorF &scale )
  327. {
  328. AssertFatal( !mIsNaN( scale ), "SceneObject::setScale() - The scale is NaN!" );
  329. // Avoid unnecessary scaling operations.
  330. if ( mObjScale.equal( scale ) )
  331. return;
  332. mObjScale = scale;
  333. setTransform(MatrixF(mObjToWorld));
  334. // Make sure that any subclasses of me get a chance to react to the
  335. // scale being changed.
  336. onScaleChanged();
  337. setMaskBits( ScaleMask );
  338. }
  339. void SceneObject::setForwardVector(VectorF newForward, VectorF upVector)
  340. {
  341. MatrixF mat = getTransform();
  342. VectorF up(0.0f, 0.0f, 1.0f);
  343. VectorF axisX;
  344. VectorF axisY = newForward;
  345. VectorF axisZ;
  346. if (upVector != VectorF::Zero)
  347. up = upVector;
  348. // Validate and normalize input:
  349. F32 lenSq;
  350. lenSq = axisY.lenSquared();
  351. if (lenSq < 0.000001f)
  352. {
  353. axisY.set(0.0f, 1.0f, 0.0f);
  354. Con::errorf("SceneObject::setForwardVector() - degenerate forward vector");
  355. }
  356. else
  357. {
  358. axisY /= mSqrt(lenSq);
  359. }
  360. lenSq = up.lenSquared();
  361. if (lenSq < 0.000001f)
  362. {
  363. up.set(0.0f, 0.0f, 1.0f);
  364. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
  365. }
  366. else
  367. {
  368. up /= mSqrt(lenSq);
  369. }
  370. if (fabsf(mDot(up, axisY)) > 0.9999f)
  371. {
  372. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
  373. // I haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
  374. F32 tmp = up.x;
  375. up.x = -up.y;
  376. up.y = up.z;
  377. up.z = tmp;
  378. }
  379. // construct the remaining axes:
  380. mCross(axisY, up, &axisX);
  381. mCross(axisX, axisY, &axisZ);
  382. mat.setColumn(0, axisX);
  383. mat.setColumn(1, axisY);
  384. mat.setColumn(2, axisZ);
  385. setTransform(mat);
  386. }
  387. //-----------------------------------------------------------------------------
  388. void SceneObject::resetWorldBox()
  389. {
  390. AssertFatal(mObjBox.isValidBox(), "SceneObject::resetWorldBox - Bad object box!");
  391. mWorldBox = mObjBox;
  392. mWorldBox.minExtents.convolve(mObjScale);
  393. mWorldBox.maxExtents.convolve(mObjScale);
  394. mObjToWorld.mul(mWorldBox);
  395. AssertFatal(mWorldBox.isValidBox(), "SceneObject::resetWorldBox - Bad world box!");
  396. // Create mWorldSphere from mWorldBox
  397. mWorldBox.getCenter(&mWorldSphere.center);
  398. mWorldSphere.radius = (mWorldBox.maxExtents - mWorldSphere.center).len();
  399. // Update tracker links.
  400. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  401. link = link->getNextLink() )
  402. link->update();
  403. }
  404. //-----------------------------------------------------------------------------
  405. void SceneObject::resetObjectBox()
  406. {
  407. AssertFatal( mWorldBox.isValidBox(), "SceneObject::resetObjectBox - Bad world box!" );
  408. mObjBox = mWorldBox;
  409. mWorldToObj.mul( mObjBox );
  410. Point3F objScale( mObjScale );
  411. objScale.setMax( Point3F( (F32)POINT_EPSILON, (F32)POINT_EPSILON, (F32)POINT_EPSILON ) );
  412. mObjBox.minExtents.convolveInverse( objScale );
  413. mObjBox.maxExtents.convolveInverse( objScale );
  414. AssertFatal( mObjBox.isValidBox(), "SceneObject::resetObjectBox - Bad object box!" );
  415. // Update the mWorldSphere from mWorldBox
  416. mWorldBox.getCenter( &mWorldSphere.center );
  417. mWorldSphere.radius = ( mWorldBox.maxExtents - mWorldSphere.center ).len();
  418. // Update scene managers.
  419. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  420. link = link->getNextLink() )
  421. link->update();
  422. }
  423. //-----------------------------------------------------------------------------
  424. void SceneObject::setRenderTransform(const MatrixF& mat)
  425. {
  426. PROFILE_START(SceneObj_setRenderTransform);
  427. mRenderObjToWorld = mRenderWorldToObj = mat;
  428. mRenderWorldToObj.affineInverse();
  429. AssertFatal(mObjBox.isValidBox(), "Bad object box!");
  430. resetRenderWorldBox();
  431. PROFILE_END();
  432. }
  433. //-----------------------------------------------------------------------------
  434. void SceneObject::resetRenderWorldBox()
  435. {
  436. AssertFatal( mObjBox.isValidBox(), "Bad object box!" );
  437. mRenderWorldBox = mObjBox;
  438. mRenderWorldBox.minExtents.convolve( mObjScale );
  439. mRenderWorldBox.maxExtents.convolve( mObjScale );
  440. mRenderObjToWorld.mul( mRenderWorldBox );
  441. AssertFatal( mRenderWorldBox.isValidBox(), "Bad world box!" );
  442. // Create mRenderWorldSphere from mRenderWorldBox.
  443. mRenderWorldBox.getCenter( &mRenderWorldSphere.center );
  444. mRenderWorldSphere.radius = ( mRenderWorldBox.maxExtents - mRenderWorldSphere.center ).len();
  445. }
  446. //-----------------------------------------------------------------------------
  447. void SceneObject::setHidden( bool hidden )
  448. {
  449. if( hidden != isHidden() )
  450. {
  451. // Add/remove the object from the scene. Removing it
  452. // will also cause the NetObject to go out of scope since
  453. // the container query will not find it anymore. However,
  454. // ScopeAlways objects need to be treated separately as we
  455. // do next.
  456. if( !hidden )
  457. addToScene();
  458. else
  459. removeFromScene();
  460. // ScopeAlways objects stay in scope no matter what, i.e. even
  461. // if they aren't in the scene query anymore. So, to force ghosts
  462. // to go away, we need to clear ScopeAlways while we are hidden.
  463. if( hidden && mIsScopeAlways )
  464. clearScopeAlways();
  465. else if( !hidden && mIsScopeAlways )
  466. setScopeAlways();
  467. Parent::setHidden( hidden );
  468. }
  469. }
  470. //-----------------------------------------------------------------------------
  471. void SceneObject::initPersistFields()
  472. {
  473. addGroup("GameObject");
  474. addField("GameObject", TypeGameObjectAssetPtr, Offset(mGameObjectAsset, SceneObject), "The asset Id used for the game object this entity is based on.");
  475. addField("dirtyGameObject", TypeBool, Offset(mDirtyGameObject, SceneObject), "If this entity is a GameObject, it flags if this instance delinates from the template.",
  476. AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  477. endGroup("GameObject");
  478. addGroup( "Transform" );
  479. addProtectedField( "position", TypeMatrixPosition, Offset( mObjToWorld, SceneObject ),
  480. &_setFieldPosition, &defaultProtectedGetFn,
  481. "Object world position." );
  482. addProtectedField( "rotation", TypeMatrixRotation, Offset( mObjToWorld, SceneObject ),
  483. &_setFieldRotation, &defaultProtectedGetFn,
  484. "Object world orientation." );
  485. addProtectedField( "scale", TypePoint3F, Offset( mObjScale, SceneObject ),
  486. &_setFieldScale, &defaultProtectedGetFn,
  487. "Object world scale." );
  488. endGroup( "Transform" );
  489. addGroup( "Editing" );
  490. addProtectedField( "isRenderEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  491. &_setRenderEnabled, &_getRenderEnabled,
  492. "Controls client-side rendering of the object.\n"
  493. "@see isRenderable()\n" );
  494. addProtectedField( "isSelectionEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  495. &_setSelectionEnabled, &_getSelectionEnabled,
  496. "Determines if the object may be selected from wihin the Tools.\n"
  497. "@see isSelectable()\n" );
  498. endGroup( "Editing" );
  499. addGroup( "Mounting" );
  500. addProtectedField( "mountPID", TypePID, Offset( mMountPID, SceneObject ), &_setMountPID, &defaultProtectedGetFn,
  501. "@brief PersistentID of object we are mounted to.\n\n"
  502. "Unlike the SimObjectID that is determined at run time, the PersistentID of an object is saved with the level/mission and "
  503. "may be used to form a link between objects." );
  504. addField( "mountNode", TypeS32, Offset( mMount.node, SceneObject ), "Node we are mounted to." );
  505. addField( "mountPos", TypeMatrixPosition, Offset( mMount.xfm, SceneObject ), "Position we are mounted at ( object space of our mount object )." );
  506. addField( "mountRot", TypeMatrixRotation, Offset( mMount.xfm, SceneObject ), "Rotation we are mounted at ( object space of our mount object )." );
  507. endGroup( "Mounting" );
  508. Parent::initPersistFields();
  509. }
  510. bool SceneObject::_setGameObject(void* object, const char* index, const char* data)
  511. {
  512. // Sanity!
  513. AssertFatal(data != NULL, "Cannot use a NULL asset Id.");
  514. return true; //rbI->setMeshAsset(data);
  515. }
  516. //-----------------------------------------------------------------------------
  517. bool SceneObject::_setFieldPosition( void *object, const char *index, const char *data )
  518. {
  519. SceneObject* so = static_cast<SceneObject*>( object );
  520. if ( so )
  521. {
  522. MatrixF txfm( so->getTransform() );
  523. Con::setData( TypeMatrixPosition, &txfm, 0, 1, &data );
  524. so->setTransform( txfm );
  525. }
  526. return false;
  527. }
  528. //-----------------------------------------------------------------------------
  529. bool SceneObject::_setFieldRotation( void *object, const char *index, const char *data )
  530. {
  531. SceneObject* so = static_cast<SceneObject*>( object );
  532. if ( so )
  533. {
  534. MatrixF txfm( so->getTransform() );
  535. Con::setData( TypeMatrixRotation, &txfm, 0, 1, &data );
  536. so->setTransform( txfm );
  537. }
  538. return false;
  539. }
  540. //-----------------------------------------------------------------------------
  541. bool SceneObject::_setFieldScale( void *object, const char *index, const char *data )
  542. {
  543. SceneObject* so = static_cast<SceneObject*>( object );
  544. if ( so )
  545. {
  546. Point3F scale;
  547. Con::setData( TypePoint3F, &scale, 0, 1, &data );
  548. so->setScale( scale );
  549. }
  550. return false;
  551. }
  552. //-----------------------------------------------------------------------------
  553. bool SceneObject::writeField( StringTableEntry fieldName, const char* value )
  554. {
  555. if( !Parent::writeField( fieldName, value ) )
  556. return false;
  557. static StringTableEntry sIsRenderEnabled = StringTable->insert( "isRenderEnabled" );
  558. static StringTableEntry sIsSelectionEnabled = StringTable->insert( "isSelectionEnabled" );
  559. static StringTableEntry sMountNode = StringTable->insert( "mountNode" );
  560. static StringTableEntry sMountPos = StringTable->insert( "mountPos" );
  561. static StringTableEntry sMountRot = StringTable->insert( "mountRot" );
  562. // Don't write flag fields if they are at their default values.
  563. if( fieldName == sIsRenderEnabled && dAtob( value ) )
  564. return false;
  565. else if( fieldName == sIsSelectionEnabled && dAtob( value ) )
  566. return false;
  567. else if ( mMountPID == NULL && ( fieldName == sMountNode ||
  568. fieldName == sMountPos ||
  569. fieldName == sMountRot ) )
  570. {
  571. return false;
  572. }
  573. return true;
  574. }
  575. //-----------------------------------------------------------------------------
  576. static void scopeCallback( SceneObject* obj, void* conPtr )
  577. {
  578. NetConnection* ptr = reinterpret_cast< NetConnection* >( conPtr );
  579. if( obj->isScopeable() )
  580. ptr->objectInScope(obj);
  581. }
  582. void SceneObject::onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query )
  583. {
  584. SceneManager* sceneManager = getSceneManager();
  585. GameConnection* conn = dynamic_cast<GameConnection*> (connection);
  586. if (conn && (query->visibleDistance = conn->getVisibleGhostDistance()) == 0.0f)
  587. if ((query->visibleDistance = sceneManager->getVisibleGhostDistance()) == 0.0f)
  588. query->visibleDistance = sceneManager->getVisibleDistance();
  589. // Object itself is in scope.
  590. if( this->isScopeable() )
  591. connection->objectInScope( this );
  592. // If we're mounted to something, that object is in scope too.
  593. if( isMounted() )
  594. connection->objectInScope( mMount.object );
  595. // If we're added to a scene graph, let the graph do the scene scoping.
  596. // Otherwise just put everything in the server container in scope.
  597. if( getSceneManager() )
  598. getSceneManager()->scopeScene( query, connection );
  599. else
  600. gServerContainer.findObjects( 0xFFFFFFFF, scopeCallback, connection );
  601. }
  602. //-----------------------------------------------------------------------------
  603. bool SceneObject::isRenderEnabled() const
  604. {
  605. #ifdef TORQUE_TOOLS
  606. if (gEditingMission)
  607. {
  608. AbstractClassRep *classRep = getClassRep();
  609. return (mObjectFlags.test(RenderEnabledFlag) && classRep->isRenderEnabled());
  610. }
  611. #endif
  612. return (mObjectFlags.test(RenderEnabledFlag));
  613. }
  614. //-----------------------------------------------------------------------------
  615. void SceneObject::setRenderEnabled( bool value )
  616. {
  617. if( value )
  618. mObjectFlags.set( RenderEnabledFlag );
  619. else
  620. mObjectFlags.clear( RenderEnabledFlag );
  621. setMaskBits( FlagMask );
  622. }
  623. //-----------------------------------------------------------------------------
  624. const char* SceneObject::_getRenderEnabled( void* object, const char* data )
  625. {
  626. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  627. if( obj->mObjectFlags.test( RenderEnabledFlag ) )
  628. return "1";
  629. else
  630. return "0";
  631. }
  632. //-----------------------------------------------------------------------------
  633. bool SceneObject::_setRenderEnabled( void *object, const char *index, const char *data )
  634. {
  635. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  636. obj->setRenderEnabled( dAtob( data ) );
  637. return false;
  638. }
  639. //-----------------------------------------------------------------------------
  640. bool SceneObject::isSelectionEnabled() const
  641. {
  642. AbstractClassRep *classRep = getClassRep();
  643. return ( mObjectFlags.test( SelectionEnabledFlag ) && classRep->isSelectionEnabled() );
  644. }
  645. //-----------------------------------------------------------------------------
  646. void SceneObject::setSelectionEnabled( bool value )
  647. {
  648. if( value )
  649. mObjectFlags.set( SelectionEnabledFlag );
  650. else
  651. mObjectFlags.clear( SelectionEnabledFlag );
  652. // Not synchronized on network so don't set dirty bit.
  653. }
  654. //-----------------------------------------------------------------------------
  655. const char* SceneObject::_getSelectionEnabled( void* object, const char* data )
  656. {
  657. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  658. if( obj->mObjectFlags.test( SelectionEnabledFlag ) )
  659. return "true";
  660. else
  661. return "false";
  662. }
  663. //-----------------------------------------------------------------------------
  664. bool SceneObject::_setSelectionEnabled( void *object, const char *index, const char *data )
  665. {
  666. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  667. obj->setSelectionEnabled( dAtob( data ) );
  668. return false;
  669. }
  670. //--------------------------------------------------------------------------
  671. U32 SceneObject::packUpdate( NetConnection* conn, U32 mask, BitStream* stream )
  672. {
  673. U32 retMask = Parent::packUpdate( conn, mask, stream );
  674. if ( stream->writeFlag( mask & FlagMask ) )
  675. stream->writeRangedU32( (U32)mObjectFlags, 0, getObjectFlagMax() );
  676. if ( mask & MountedMask )
  677. {
  678. if ( mMount.object )
  679. {
  680. S32 gIndex = conn->getGhostIndex( mMount.object );
  681. if ( stream->writeFlag( gIndex != -1 ) )
  682. {
  683. stream->writeFlag( true );
  684. stream->writeInt( gIndex, NetConnection::GhostIdBitSize );
  685. if ( stream->writeFlag( mMount.node != -1 ) )
  686. stream->writeInt( mMount.node, NumMountPointBits );
  687. mathWrite( *stream, mMount.xfm );
  688. }
  689. else
  690. // Will have to try again later
  691. retMask |= MountedMask;
  692. }
  693. else
  694. // Unmount if this isn't the initial packet
  695. if ( stream->writeFlag( !(mask & InitialUpdateMask) ) )
  696. stream->writeFlag( false );
  697. }
  698. else
  699. stream->writeFlag( false );
  700. return retMask;
  701. }
  702. //-----------------------------------------------------------------------------
  703. void SceneObject::unpackUpdate( NetConnection* conn, BitStream* stream )
  704. {
  705. Parent::unpackUpdate( conn, stream );
  706. // FlagMask
  707. if ( stream->readFlag() )
  708. mObjectFlags = stream->readRangedU32( 0, getObjectFlagMax() );
  709. // MountedMask
  710. if ( stream->readFlag() )
  711. {
  712. if ( stream->readFlag() )
  713. {
  714. S32 gIndex = stream->readInt( NetConnection::GhostIdBitSize );
  715. SceneObject* obj = dynamic_cast<SceneObject*>( conn->resolveGhost( gIndex ) );
  716. S32 node = -1;
  717. if ( stream->readFlag() ) // node != -1
  718. node = stream->readInt( NumMountPointBits );
  719. MatrixF xfm;
  720. mathRead( *stream, &xfm );
  721. if ( !obj )
  722. {
  723. conn->setLastError( "Invalid packet from server." );
  724. return;
  725. }
  726. obj->mountObject( this, node, xfm );
  727. }
  728. else
  729. unmount();
  730. }
  731. }
  732. //-----------------------------------------------------------------------------
  733. void SceneObject::_updateZoningState() const
  734. {
  735. if( mZoneRefDirty )
  736. {
  737. SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
  738. if( manager )
  739. manager->updateObject( const_cast< SceneObject* >( this ) );
  740. else
  741. mZoneRefDirty = false;
  742. }
  743. }
  744. //-----------------------------------------------------------------------------
  745. U32 SceneObject::_getCurrZone( const U32 index ) const
  746. {
  747. _updateZoningState();
  748. // Not the most efficient way to do this, walking the list,
  749. // but it's an uncommon call...
  750. ZoneRef* walk = mZoneRefHead;
  751. for( U32 i = 0; i < index; ++ i )
  752. {
  753. walk = walk->nextInObj;
  754. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  755. }
  756. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  757. return walk->zone;
  758. }
  759. //-----------------------------------------------------------------------------
  760. Point3F SceneObject::getPosition() const
  761. {
  762. Point3F pos;
  763. mObjToWorld.getColumn(3, &pos);
  764. return pos;
  765. }
  766. //-----------------------------------------------------------------------------
  767. Point3F SceneObject::getRenderPosition() const
  768. {
  769. Point3F pos;
  770. mRenderObjToWorld.getColumn(3, &pos);
  771. return pos;
  772. }
  773. //-----------------------------------------------------------------------------
  774. void SceneObject::setPosition(const Point3F &pos)
  775. {
  776. AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
  777. MatrixF xform = mObjToWorld;
  778. xform.setColumn(3, pos);
  779. setTransform(xform);
  780. }
  781. //-----------------------------------------------------------------------------
  782. F32 SceneObject::distanceTo(const Point3F &pnt) const
  783. {
  784. return mWorldBox.getDistanceToPoint( pnt );
  785. }
  786. //-----------------------------------------------------------------------------
  787. void SceneObject::processAfter( ProcessObject *obj )
  788. {
  789. AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
  790. mAfterObject = (SceneObject*)obj;
  791. if ( mAfterObject->mAfterObject == this )
  792. mAfterObject->mAfterObject = NULL;
  793. getProcessList()->markDirty();
  794. }
  795. //-----------------------------------------------------------------------------
  796. void SceneObject::clearProcessAfter()
  797. {
  798. mAfterObject = NULL;
  799. }
  800. //-----------------------------------------------------------------------------
  801. void SceneObject::setProcessTick( bool t )
  802. {
  803. if ( t == mProcessTick )
  804. return;
  805. if ( mProcessTick )
  806. {
  807. if ( !getMountedObjectCount() )
  808. plUnlink(); // Only unlink if there is nothing mounted to us
  809. mProcessTick = false;
  810. }
  811. else
  812. {
  813. // Just to be sure...
  814. plUnlink();
  815. getProcessList()->addObject( this );
  816. mProcessTick = true;
  817. }
  818. }
  819. //-----------------------------------------------------------------------------
  820. ProcessList* SceneObject::getProcessList() const
  821. {
  822. if ( isClientObject() )
  823. return ClientProcessList::get();
  824. else
  825. return ServerProcessList::get();
  826. }
  827. //-------------------------------------------------------------------------
  828. bool SceneObject::isMounted()
  829. {
  830. resolveMountPID();
  831. return mMount.object != NULL;
  832. }
  833. //-----------------------------------------------------------------------------
  834. S32 SceneObject::getMountedObjectCount()
  835. {
  836. S32 count = 0;
  837. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  838. count++;
  839. return count;
  840. }
  841. //-----------------------------------------------------------------------------
  842. SceneObject* SceneObject::getMountedObject(S32 idx)
  843. {
  844. if (idx >= 0) {
  845. S32 count = 0;
  846. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  847. if (count++ == idx)
  848. return itr;
  849. }
  850. return NULL;
  851. }
  852. //-----------------------------------------------------------------------------
  853. S32 SceneObject::getMountedObjectNode(S32 idx)
  854. {
  855. if (idx >= 0) {
  856. S32 count = 0;
  857. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  858. if (count++ == idx)
  859. return itr->mMount.node;
  860. }
  861. return -1;
  862. }
  863. //-----------------------------------------------------------------------------
  864. SceneObject* SceneObject::getMountNodeObject(S32 node)
  865. {
  866. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  867. if (itr->mMount.node == node)
  868. return itr;
  869. return NULL;
  870. }
  871. //-----------------------------------------------------------------------------
  872. bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
  873. {
  874. SceneObject* so = static_cast<SceneObject*>( object );
  875. if ( so )
  876. {
  877. // Unmount old object (PID reference is released even if it had been resolved yet)
  878. if ( so->mMountPID )
  879. {
  880. so->mMountPID->decRefCount();
  881. so->mMountPID = NULL;
  882. }
  883. so->unmount();
  884. // Get the new PID (new object will be mounted on demand)
  885. Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
  886. if ( so->mMountPID )
  887. so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
  888. }
  889. return false;
  890. }
  891. void SceneObject::resolveMountPID()
  892. {
  893. if ( mMountPID && !mMount.object )
  894. {
  895. SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
  896. if ( obj )
  897. obj->mountObject( this, mMount.node, mMount.xfm );
  898. }
  899. }
  900. //-----------------------------------------------------------------------------
  901. void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  902. {
  903. if ( obj->mMount.object == this )
  904. {
  905. // Already mounted to this
  906. // So update our node and xfm which may have changed.
  907. obj->mMount.node = node;
  908. obj->mMount.xfm = xfm;
  909. }
  910. else
  911. {
  912. if ( obj->mMount.object )
  913. obj->unmount();
  914. obj->mMount.object = this;
  915. obj->mMount.node = node;
  916. obj->mMount.link = mMount.list;
  917. obj->mMount.xfm = xfm;
  918. mMount.list = obj;
  919. // Assign PIDs to both objects
  920. if ( isServerObject() )
  921. {
  922. obj->getOrCreatePersistentId();
  923. if ( !obj->mMountPID )
  924. {
  925. obj->mMountPID = getOrCreatePersistentId();
  926. obj->mMountPID->incRefCount();
  927. }
  928. }
  929. obj->onMount( this, node );
  930. }
  931. }
  932. //-----------------------------------------------------------------------------
  933. void SceneObject::unmountObject( SceneObject *obj )
  934. {
  935. if ( obj->mMount.object == this )
  936. {
  937. // Find and unlink the object
  938. for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
  939. {
  940. if ( *ptr == obj )
  941. {
  942. *ptr = obj->mMount.link;
  943. break;
  944. }
  945. }
  946. obj->mMount.object = NULL;
  947. obj->mMount.link = NULL;
  948. if( obj->mMountPID != NULL ) // Only on server.
  949. {
  950. obj->mMountPID->decRefCount();
  951. obj->mMountPID = NULL;
  952. }
  953. obj->onUnmount( this, obj->mMount.node );
  954. }
  955. }
  956. //-----------------------------------------------------------------------------
  957. void SceneObject::unmount()
  958. {
  959. if (mMount.object)
  960. mMount.object->unmountObject(this);
  961. }
  962. //-----------------------------------------------------------------------------
  963. void SceneObject::onMount( SceneObject *obj, S32 node )
  964. {
  965. deleteNotify( obj );
  966. if ( !isGhost() )
  967. {
  968. setMaskBits( MountedMask );
  969. //onMount_callback( node );
  970. }
  971. }
  972. //-----------------------------------------------------------------------------
  973. void SceneObject::onUnmount( SceneObject *obj, S32 node )
  974. {
  975. clearNotify(obj);
  976. if ( !isGhost() )
  977. {
  978. setMaskBits( MountedMask );
  979. //onUnmount_callback( node );
  980. }
  981. }
  982. //-----------------------------------------------------------------------------
  983. void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  984. {
  985. MatrixF mountTransform( xfm );
  986. const Point3F &scale = getScale();
  987. Point3F position = mountTransform.getPosition();
  988. position.convolve( scale );
  989. mountTransform.setPosition( position );
  990. outMat->mul( mObjToWorld, mountTransform );
  991. }
  992. //-----------------------------------------------------------------------------
  993. void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
  994. {
  995. MatrixF mountTransform( xfm );
  996. const Point3F &scale = getScale();
  997. Point3F position = mountTransform.getPosition();
  998. position.convolve( scale );
  999. mountTransform.setPosition( position );
  1000. outMat->mul( mRenderObjToWorld, mountTransform );
  1001. }
  1002. //=============================================================================
  1003. // Console API.
  1004. //=============================================================================
  1005. // MARK: ---- Console API ----
  1006. //-----------------------------------------------------------------------------
  1007. DefineEngineMethod( SceneObject, getType, S32, (),,
  1008. "Return the type mask for this object.\n"
  1009. "@return The numeric type mask for the object." )
  1010. {
  1011. return object->getTypeMask();
  1012. }
  1013. //-----------------------------------------------------------------------------
  1014. DefineEngineMethod( SceneObject, mountObject, bool,
  1015. ( SceneObject* objB, S32 slot, TransformF txfm ), ( TransformF::Identity ),
  1016. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  1017. "@param objB Object to mount onto us\n"
  1018. "@param slot Mount slot ID\n"
  1019. "@param txfm (optional) mount offset transform\n"
  1020. "@return true if successful, false if failed (objB is not valid)" )
  1021. {
  1022. if ( objB )
  1023. {
  1024. object->mountObject( objB, slot, txfm.getMatrix() );
  1025. return true;
  1026. }
  1027. return false;
  1028. }
  1029. //-----------------------------------------------------------------------------
  1030. DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
  1031. "@brief Unmount an object from ourselves.\n\n"
  1032. "@param target object to unmount\n"
  1033. "@return true if successful, false if failed\n" )
  1034. {
  1035. if ( target )
  1036. {
  1037. object->unmountObject(target);
  1038. return true;
  1039. }
  1040. return false;
  1041. }
  1042. //-----------------------------------------------------------------------------
  1043. DefineEngineMethod( SceneObject, unmount, void, (),,
  1044. "Unmount us from the currently mounted object if any.\n" )
  1045. {
  1046. object->unmount();
  1047. }
  1048. //-----------------------------------------------------------------------------
  1049. DefineEngineMethod( SceneObject, isMounted, bool, (),,
  1050. "@brief Check if we are mounted to another object.\n\n"
  1051. "@return true if mounted to another object, false if not mounted." )
  1052. {
  1053. return object->isMounted();
  1054. }
  1055. //-----------------------------------------------------------------------------
  1056. DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
  1057. "@brief Get the object we are mounted to.\n\n"
  1058. "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
  1059. {
  1060. return object->isMounted()? object->getObjectMount()->getId(): 0;
  1061. }
  1062. //-----------------------------------------------------------------------------
  1063. DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
  1064. "Get the number of objects mounted to us.\n"
  1065. "@return the number of mounted objects." )
  1066. {
  1067. return object->getMountedObjectCount();
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
  1071. "Get the object mounted at a particular slot.\n"
  1072. "@param slot mount slot index to query\n"
  1073. "@return ID of the object mounted in the slot, or 0 if no object." )
  1074. {
  1075. SceneObject* mobj = object->getMountedObject( slot );
  1076. return mobj? mobj->getId(): 0;
  1077. }
  1078. //-----------------------------------------------------------------------------
  1079. DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
  1080. "@brief Get the mount node index of the object mounted at our given slot.\n\n"
  1081. "@param slot mount slot index to query\n"
  1082. "@return index of the mount node used by the object mounted in this slot." )
  1083. {
  1084. return object->getMountedObjectNode( slot );
  1085. }
  1086. //-----------------------------------------------------------------------------
  1087. DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
  1088. "@brief Get the object mounted at our given node index.\n\n"
  1089. "@param node mount node index to query\n"
  1090. "@return ID of the first object mounted at the node, or 0 if none found." )
  1091. {
  1092. SceneObject* mobj = object->getMountNodeObject( node );
  1093. return mobj? mobj->getId(): 0;
  1094. }
  1095. //-----------------------------------------------------------------------------
  1096. DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
  1097. "Get the object's transform.\n"
  1098. "@return the current transform of the object\n" )
  1099. {
  1100. return object->getTransform();
  1101. }
  1102. //-----------------------------------------------------------------------------
  1103. DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
  1104. "Get the object's inverse transform.\n"
  1105. "@return the inverse transform of the object\n" )
  1106. {
  1107. return object->getWorldTransform();
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
  1111. "Get the object's world position.\n"
  1112. "@return the current world position of the object\n" )
  1113. {
  1114. return object->getTransform().getPosition();
  1115. }
  1116. DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
  1117. "Set the object's world position.\n"
  1118. "@param pos the new world position of the object\n" )
  1119. {
  1120. return object->setPosition(pos);
  1121. }
  1122. //-----------------------------------------------------------------------------
  1123. DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
  1124. "Get Euler rotation of this object.\n"
  1125. "@return the orientation of the object in the form of rotations around the "
  1126. "X, Y and Z axes in degrees.\n" )
  1127. {
  1128. Point3F euler = object->getTransform().toEuler();
  1129. // Convert to degrees.
  1130. euler.x = mRadToDeg( euler.x );
  1131. euler.y = mRadToDeg( euler.y );
  1132. euler.z = mRadToDeg( euler.z );
  1133. return euler;
  1134. }
  1135. //-----------------------------------------------------------------------------
  1136. DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
  1137. "Get the direction this object is facing.\n"
  1138. "@return a vector indicating the direction this object is facing.\n"
  1139. "@note This is the object's y axis." )
  1140. {
  1141. return object->getTransform().getForwardVector();
  1142. }
  1143. //-----------------------------------------------------------------------------
  1144. DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
  1145. "Get the right vector of the object.\n"
  1146. "@return a vector indicating the right direction of this object."
  1147. "@note This is the object's x axis." )
  1148. {
  1149. return object->getTransform().getRightVector();
  1150. }
  1151. //-----------------------------------------------------------------------------
  1152. DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
  1153. "Get the up vector of the object.\n"
  1154. "@return a vector indicating the up direction of this object."
  1155. "@note This is the object's z axis." )
  1156. {
  1157. return object->getTransform().getUpVector();
  1158. }
  1159. //-----------------------------------------------------------------------------
  1160. DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
  1161. "Set the object's transform (orientation and position)."
  1162. "@param txfm object transform to set" )
  1163. {
  1164. if ( !txfm.hasRotation() )
  1165. object->setPosition( txfm.getPosition() );
  1166. else
  1167. object->setTransform( txfm.getMatrix() );
  1168. }
  1169. //-----------------------------------------------------------------------------
  1170. DefineEngineMethod( SceneObject, getScale, Point3F, (),,
  1171. "Get the object's scale.\n"
  1172. "@return object scale as a Point3F" )
  1173. {
  1174. return object->getScale();
  1175. }
  1176. //-----------------------------------------------------------------------------
  1177. DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
  1178. "Set the object's scale.\n"
  1179. "@param scale object scale to set\n" )
  1180. {
  1181. object->setScale( scale );
  1182. }
  1183. //-----------------------------------------------------------------------------
  1184. DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
  1185. "Get the object's world bounding box.\n"
  1186. "@return six fields, two Point3Fs, containing the min and max points of the "
  1187. "worldbox." )
  1188. {
  1189. return object->getWorldBox();
  1190. }
  1191. //-----------------------------------------------------------------------------
  1192. DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
  1193. "Get the center of the object's world bounding box.\n"
  1194. "@return the center of the world bounding box for this object." )
  1195. {
  1196. Point3F center;
  1197. object->getWorldBox().getCenter( &center );
  1198. return center;
  1199. }
  1200. //-----------------------------------------------------------------------------
  1201. DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
  1202. "Get the object's bounding box (relative to the object's origin).\n"
  1203. "@return six fields, two Point3Fs, containing the min and max points of the "
  1204. "objectbox." )
  1205. {
  1206. return object->getObjBox();
  1207. }
  1208. //-----------------------------------------------------------------------------
  1209. DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
  1210. "Check if this object has a global bounds set.\n"
  1211. "If global bounds are set to be true, then the object is assumed to have an "
  1212. "infinitely large bounding box for collision and rendering purposes.\n"
  1213. "@return true if the object has a global bounds." )
  1214. {
  1215. return object->isGlobalBounds();
  1216. }
  1217. DefineEngineMethod(SceneObject, setForwardVector, void, (VectorF newForward, VectorF upVector), (VectorF(0, 0, 0), VectorF(0, 0, 1)),
  1218. "Sets the forward vector of a scene object, making it face Y+ along the new vector.\n"
  1219. "@param The new forward vector to set.\n"
  1220. "@param (Optional) The up vector to use to help orient the rotation.")
  1221. {
  1222. object->setForwardVector(newForward, upVector);
  1223. }