sceneObject.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008
  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. mLastXform.identity();
  97. mWorldToObj.identity();
  98. mObjBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  99. mWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  100. mWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  101. mRenderObjToWorld.identity();
  102. mRenderWorldToObj.identity();
  103. mRenderWorldBox = Box3F(Point3F(0, 0, 0), Point3F(0, 0, 0));
  104. mRenderWorldSphere = SphereF(Point3F(0, 0, 0), 0);
  105. mContainerSeqKey = 0;
  106. mSceneManager = NULL;
  107. mNumCurrZones = 0;
  108. mZoneRefHead = NULL;
  109. mZoneRefDirty = false;
  110. mLightPlugin = NULL;
  111. mMount.object = NULL;
  112. mMount.link = NULL;
  113. mMount.list = NULL;
  114. mMount.node = -1;
  115. mMount.xfm = MatrixF::Identity;
  116. mMountPID = NULL;
  117. mSceneObjectLinks = NULL;
  118. mObjectFlags.set( RenderEnabledFlag | SelectionEnabledFlag );
  119. // PATHSHAPE
  120. // init the scenegraph relationships to indicate no parent, no children, and no siblings
  121. mGraph.parent = NULL;
  122. mGraph.nextSibling = NULL;
  123. mGraph.firstChild = NULL;
  124. mGraph.objToParent.identity();
  125. // PATHSHAPE END
  126. mIsScopeAlways = false;
  127. mAccuTex = NULL;
  128. mSelectionFlags = 0;
  129. mPathfindingIgnore = false;
  130. mGameObjectAssetId = StringTable->insert("");
  131. mDirtyGameObject = false;
  132. }
  133. //-----------------------------------------------------------------------------
  134. SceneObject::~SceneObject()
  135. {
  136. AssertFatal(mContainer == NULL,
  137. "SceneObject::~SceneObject - Object still in container!");
  138. AssertFatal( mZoneRefHead == NULL,
  139. "SceneObject::~SceneObject - Object still linked in reference lists!");
  140. AssertFatal( !mSceneObjectLinks,
  141. "SceneObject::~SceneObject() - object is still linked to SceneTrackers" );
  142. mAccuTex = NULL;
  143. }
  144. //-----------------------------------------------------------------------------
  145. bool SceneObject::castRayRendered(const Point3F &start, const Point3F &end, RayInfo *info)
  146. {
  147. // By default, all ray checking against the rendered mesh will be passed
  148. // on to the collision mesh. This saves having to define both methods
  149. // for simple objects.
  150. return castRay( start, end, info );
  151. }
  152. //-----------------------------------------------------------------------------
  153. bool SceneObject::containsPoint( const Point3F& point )
  154. {
  155. // If it's not in the AABB, then it can't be in the OBB either,
  156. // so early out.
  157. if( !mWorldBox.isContained( point ) )
  158. return false;
  159. // Transform point into object space and test it against
  160. // our object space bounding box.
  161. Point3F objPoint( 0, 0, 0 );
  162. getWorldTransform().mulP( point, &objPoint );
  163. objPoint.convolveInverse( getScale() );
  164. return ( mObjBox.isContained( objPoint ) );
  165. }
  166. //-----------------------------------------------------------------------------
  167. bool SceneObject::collideBox(const Point3F &start, const Point3F &end, RayInfo *info)
  168. {
  169. const F32 * pStart = (const F32*)start;
  170. const F32 * pEnd = (const F32*)end;
  171. const F32 * pMin = (const F32*)mObjBox.minExtents;
  172. const F32 * pMax = (const F32*)mObjBox.maxExtents;
  173. F32 maxStartTime = -1;
  174. F32 minEndTime = 1;
  175. F32 startTime;
  176. F32 endTime;
  177. // used for getting normal
  178. U32 hitIndex = 0xFFFFFFFF;
  179. U32 side;
  180. // walk the axis
  181. for(U32 i = 0; i < 3; i++)
  182. {
  183. //
  184. if(pStart[i] < pEnd[i])
  185. {
  186. if(pEnd[i] < pMin[i] || pStart[i] > pMax[i])
  187. return(false);
  188. F32 dist = pEnd[i] - pStart[i];
  189. startTime = (pStart[i] < pMin[i]) ? (pMin[i] - pStart[i]) / dist : -1;
  190. endTime = (pEnd[i] > pMax[i]) ? (pMax[i] - pStart[i]) / dist : 1;
  191. side = 1;
  192. }
  193. else
  194. {
  195. if(pStart[i] < pMin[i] || pEnd[i] > pMax[i])
  196. return(false);
  197. F32 dist = pStart[i] - pEnd[i];
  198. startTime = (pStart[i] > pMax[i]) ? (pStart[i] - pMax[i]) / dist : -1;
  199. endTime = (pEnd[i] < pMin[i]) ? (pStart[i] - pMin[i]) / dist : 1;
  200. side = 0;
  201. }
  202. //
  203. if(startTime > maxStartTime)
  204. {
  205. maxStartTime = startTime;
  206. hitIndex = i * 2 + side;
  207. }
  208. if(endTime < minEndTime)
  209. minEndTime = endTime;
  210. if(minEndTime < maxStartTime)
  211. return(false);
  212. }
  213. // fail if inside
  214. if(maxStartTime < 0.f)
  215. return(false);
  216. //
  217. static Point3F boxNormals[] = {
  218. Point3F( 1, 0, 0),
  219. Point3F(-1, 0, 0),
  220. Point3F( 0, 1, 0),
  221. Point3F( 0,-1, 0),
  222. Point3F( 0, 0, 1),
  223. Point3F( 0, 0,-1),
  224. };
  225. //
  226. AssertFatal(hitIndex != 0xFFFFFFFF, "SceneObject::collideBox");
  227. info->t = maxStartTime;
  228. info->object = this;
  229. mObjToWorld.mulV(boxNormals[hitIndex], &info->normal);
  230. info->material = 0;
  231. return(true);
  232. }
  233. //-----------------------------------------------------------------------------
  234. void SceneObject::disableCollision()
  235. {
  236. for (SceneObject* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
  237. ptr->disableCollision();
  238. mCollisionCount++;
  239. AssertFatal(mCollisionCount < 50, "SceneObject::disableCollision called 50 times on the same object. Is this inside a circular loop?" );
  240. }
  241. //-----------------------------------------------------------------------------
  242. void SceneObject::enableCollision()
  243. {
  244. for (SceneObject* ptr = getMountList(); ptr; ptr = ptr->getMountLink())
  245. ptr->enableCollision();
  246. if (mCollisionCount)
  247. --mCollisionCount;
  248. }
  249. //-----------------------------------------------------------------------------
  250. bool SceneObject::onAdd()
  251. {
  252. if ( !Parent::onAdd() )
  253. return false;
  254. mIsScopeAlways = mNetFlags.test( ScopeAlways );
  255. mWorldToObj = mObjToWorld;
  256. mWorldToObj.affineInverse();
  257. resetWorldBox();
  258. setRenderTransform(mObjToWorld);
  259. resolveMountPID();
  260. smSceneObjectAdd.trigger(this);
  261. return true;
  262. }
  263. //-----------------------------------------------------------------------------
  264. void SceneObject::onRemove()
  265. {
  266. smSceneObjectRemove.trigger(this);
  267. unmount();
  268. plUnlink();
  269. Parent::onRemove();
  270. // PATHSHAPE
  271. if ( getParent() != NULL) attachToParent( NULL);
  272. // PATHSHAPE END
  273. }
  274. //-----------------------------------------------------------------------------
  275. void SceneObject::addToScene()
  276. {
  277. if( mSceneManager )
  278. return;
  279. if( isClientObject() )
  280. gClientSceneGraph->addObjectToScene( this );
  281. else
  282. gServerSceneGraph->addObjectToScene( this );
  283. }
  284. //-----------------------------------------------------------------------------
  285. void SceneObject::removeFromScene()
  286. {
  287. if( !mSceneManager )
  288. return;
  289. mSceneManager->removeObjectFromScene( this );
  290. }
  291. //-----------------------------------------------------------------------------
  292. void SceneObject::onDeleteNotify( SimObject *obj )
  293. {
  294. // We are comparing memory addresses so even if obj really is not a
  295. // ProcessObject this cast shouldn't break anything.
  296. if ( obj == mAfterObject )
  297. mAfterObject = NULL;
  298. if ( obj == mMount.object )
  299. unmount();
  300. Parent::onDeleteNotify( obj );
  301. }
  302. //-----------------------------------------------------------------------------
  303. void SceneObject::inspectPostApply()
  304. {
  305. if( isServerObject() )
  306. setMaskBits( MountedMask );
  307. Parent::inspectPostApply();
  308. }
  309. //-----------------------------------------------------------------------------
  310. void SceneObject::setGlobalBounds()
  311. {
  312. mGlobalBounds = true;
  313. mObjBox.minExtents.set( -1e10, -1e10, -1e10 );
  314. mObjBox.maxExtents.set( 1e10, 1e10, 1e10 );
  315. if( mSceneManager )
  316. mSceneManager->notifyObjectDirty( this );
  317. }
  318. //-----------------------------------------------------------------------------
  319. void SceneObject::setTransform( const MatrixF& mat )
  320. {
  321. // This test is a bit expensive so turn it off in release.
  322. #ifdef TORQUE_DEBUG
  323. //AssertFatal( mat.isAffine(), "SceneObject::setTransform() - Bad transform (non affine)!" );
  324. #endif
  325. PROFILE_SCOPE( SceneObject_setTransform );
  326. // PATHSHAPE
  327. UpdateXformChange(mat);
  328. PerformUpdatesForChildren(mat);
  329. // PATHSHAPE END
  330. // Update the transforms.
  331. mObjToWorld = mWorldToObj = mat;
  332. mWorldToObj.affineInverse();
  333. // Update the world-space AABB.
  334. resetWorldBox();
  335. // If we're in a SceneManager, sync our scene state.
  336. if( mSceneManager != NULL )
  337. mSceneManager->notifyObjectDirty( this );
  338. setRenderTransform( mat );
  339. }
  340. //-----------------------------------------------------------------------------
  341. void SceneObject::setScale( const VectorF &scale )
  342. {
  343. AssertFatal( !mIsNaN( scale ), "SceneObject::setScale() - The scale is NaN!" );
  344. // Avoid unnecessary scaling operations.
  345. if ( mObjScale.equal( scale ) )
  346. return;
  347. mObjScale = scale;
  348. setTransform(MatrixF(mObjToWorld));
  349. // Make sure that any subclasses of me get a chance to react to the
  350. // scale being changed.
  351. onScaleChanged();
  352. setMaskBits( ScaleMask );
  353. }
  354. void SceneObject::setForwardVector(VectorF newForward, VectorF upVector)
  355. {
  356. MatrixF mat = getTransform();
  357. VectorF up(0.0f, 0.0f, 1.0f);
  358. VectorF axisX;
  359. VectorF axisY = newForward;
  360. VectorF axisZ;
  361. if (upVector != VectorF::Zero)
  362. up = upVector;
  363. // Validate and normalize input:
  364. F32 lenSq;
  365. lenSq = axisY.lenSquared();
  366. if (lenSq < 0.000001f)
  367. {
  368. axisY.set(0.0f, 1.0f, 0.0f);
  369. Con::errorf("SceneObject::setForwardVector() - degenerate forward vector");
  370. }
  371. else
  372. {
  373. axisY /= mSqrt(lenSq);
  374. }
  375. lenSq = up.lenSquared();
  376. if (lenSq < 0.000001f)
  377. {
  378. up.set(0.0f, 0.0f, 1.0f);
  379. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - too small");
  380. }
  381. else
  382. {
  383. up /= mSqrt(lenSq);
  384. }
  385. if (fabsf(mDot(up, axisY)) > 0.9999f)
  386. {
  387. Con::errorf("SceneObject::setForwardVector() - degenerate up vector - same as forward");
  388. // I haven't really tested this, but i think it generates something which should be not parallel to the previous vector:
  389. F32 tmp = up.x;
  390. up.x = -up.y;
  391. up.y = up.z;
  392. up.z = tmp;
  393. }
  394. // construct the remaining axes:
  395. mCross(axisY, up, &axisX);
  396. mCross(axisX, axisY, &axisZ);
  397. mat.setColumn(0, axisX);
  398. mat.setColumn(1, axisY);
  399. mat.setColumn(2, axisZ);
  400. setTransform(mat);
  401. }
  402. //-----------------------------------------------------------------------------
  403. void SceneObject::resetWorldBox()
  404. {
  405. AssertFatal(mObjBox.isValidBox(), "SceneObject::resetWorldBox - Bad object box!");
  406. mWorldBox = mObjBox;
  407. mWorldBox.minExtents.convolve(mObjScale);
  408. mWorldBox.maxExtents.convolve(mObjScale);
  409. mObjToWorld.mul(mWorldBox);
  410. AssertFatal(mWorldBox.isValidBox(), "SceneObject::resetWorldBox - Bad world box!");
  411. // Create mWorldSphere from mWorldBox
  412. mWorldBox.getCenter(&mWorldSphere.center);
  413. mWorldSphere.radius = (mWorldBox.maxExtents - mWorldSphere.center).len();
  414. // Update tracker links.
  415. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  416. link = link->getNextLink() )
  417. link->update();
  418. }
  419. //-----------------------------------------------------------------------------
  420. void SceneObject::resetObjectBox()
  421. {
  422. AssertFatal( mWorldBox.isValidBox(), "SceneObject::resetObjectBox - Bad world box!" );
  423. mObjBox = mWorldBox;
  424. mWorldToObj.mul( mObjBox );
  425. Point3F objScale( mObjScale );
  426. objScale.setMax( Point3F( (F32)POINT_EPSILON, (F32)POINT_EPSILON, (F32)POINT_EPSILON ) );
  427. mObjBox.minExtents.convolveInverse( objScale );
  428. mObjBox.maxExtents.convolveInverse( objScale );
  429. AssertFatal( mObjBox.isValidBox(), "SceneObject::resetObjectBox - Bad object box!" );
  430. // Update the mWorldSphere from mWorldBox
  431. mWorldBox.getCenter( &mWorldSphere.center );
  432. mWorldSphere.radius = ( mWorldBox.maxExtents - mWorldSphere.center ).len();
  433. // Update scene managers.
  434. for( SceneObjectLink* link = mSceneObjectLinks; link != NULL;
  435. link = link->getNextLink() )
  436. link->update();
  437. }
  438. //-----------------------------------------------------------------------------
  439. void SceneObject::setRenderTransform(const MatrixF& mat)
  440. {
  441. PROFILE_START(SceneObj_setRenderTransform);
  442. mRenderObjToWorld = mRenderWorldToObj = mat;
  443. mRenderWorldToObj.affineInverse();
  444. AssertFatal(mObjBox.isValidBox(), "Bad object box!");
  445. resetRenderWorldBox();
  446. PROFILE_END();
  447. }
  448. //-----------------------------------------------------------------------------
  449. void SceneObject::resetRenderWorldBox()
  450. {
  451. AssertFatal( mObjBox.isValidBox(), "Bad object box!" );
  452. mRenderWorldBox = mObjBox;
  453. mRenderWorldBox.minExtents.convolve( mObjScale );
  454. mRenderWorldBox.maxExtents.convolve( mObjScale );
  455. mRenderObjToWorld.mul( mRenderWorldBox );
  456. AssertFatal( mRenderWorldBox.isValidBox(), "Bad world box!" );
  457. // Create mRenderWorldSphere from mRenderWorldBox.
  458. mRenderWorldBox.getCenter( &mRenderWorldSphere.center );
  459. mRenderWorldSphere.radius = ( mRenderWorldBox.maxExtents - mRenderWorldSphere.center ).len();
  460. }
  461. //-----------------------------------------------------------------------------
  462. void SceneObject::setHidden( bool hidden )
  463. {
  464. if( hidden != isHidden() )
  465. {
  466. // Add/remove the object from the scene. Removing it
  467. // will also cause the NetObject to go out of scope since
  468. // the container query will not find it anymore. However,
  469. // ScopeAlways objects need to be treated separately as we
  470. // do next.
  471. if( !hidden )
  472. addToScene();
  473. else
  474. removeFromScene();
  475. // ScopeAlways objects stay in scope no matter what, i.e. even
  476. // if they aren't in the scene query anymore. So, to force ghosts
  477. // to go away, we need to clear ScopeAlways while we are hidden.
  478. if( hidden && mIsScopeAlways )
  479. clearScopeAlways();
  480. else if( !hidden && mIsScopeAlways )
  481. setScopeAlways();
  482. Parent::setHidden( hidden );
  483. }
  484. }
  485. //-----------------------------------------------------------------------------
  486. void SceneObject::initPersistFields()
  487. {
  488. docsURL;
  489. //Disabled temporarily
  490. /*addGroup("GameObject");
  491. addField("GameObject", TypeGameObjectAssetPtr, Offset(mGameObjectAsset, SceneObject), "The asset Id used for the game object this entity is based on.");
  492. addField("dirtyGameObject", TypeBool, Offset(mDirtyGameObject, SceneObject), "If this entity is a GameObject, it flags if this instance delinates from the template.",
  493. AbstractClassRep::FieldFlags::FIELD_HideInInspectors);
  494. endGroup("GameObject");*/
  495. addGroup( "Transform" );
  496. addProtectedField( "position", TypeMatrixPosition, Offset( mObjToWorld, SceneObject ),
  497. &_setFieldPosition, &defaultProtectedGetFn,
  498. "Object world position." );
  499. addProtectedField( "rotation", TypeMatrixRotation, Offset( mObjToWorld, SceneObject ),
  500. &_setFieldRotation, &defaultProtectedGetFn,
  501. "Object world orientation." );
  502. addProtectedField( "scale", TypePoint3F, Offset( mObjScale, SceneObject ),
  503. &_setFieldScale, &defaultProtectedGetFn,
  504. "Object world scale." );
  505. endGroup( "Transform" );
  506. addGroup( "Editing" );
  507. addProtectedField( "isRenderEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  508. &_setRenderEnabled, &_getRenderEnabled,
  509. "Controls client-side rendering of the object.\n"
  510. "@see isRenderable()\n" );
  511. addProtectedField( "isSelectionEnabled", TypeBool, Offset( mObjectFlags, SceneObject ),
  512. &_setSelectionEnabled, &_getSelectionEnabled,
  513. "Determines if the object may be selected from wihin the Tools.\n"
  514. "@see isSelectable()\n" );
  515. endGroup( "Editing" );
  516. addGroup( "Mounting" );
  517. addProtectedField( "mountPID", TypePID, Offset( mMountPID, SceneObject ), &_setMountPID, &defaultProtectedGetFn,
  518. "@brief PersistentID of object we are mounted to.\n\n"
  519. "Unlike the SimObjectID that is determined at run time, the PersistentID of an object is saved with the level/mission and "
  520. "may be used to form a link between objects." );
  521. addField( "mountNode", TypeS32, Offset( mMount.node, SceneObject ), "Node we are mounted to." );
  522. addField( "mountPos", TypeMatrixPosition, Offset( mMount.xfm, SceneObject ), "Position we are mounted at ( object space of our mount object )." );
  523. addField( "mountRot", TypeMatrixRotation, Offset( mMount.xfm, SceneObject ), "Rotation we are mounted at ( object space of our mount object )." );
  524. endGroup( "Mounting" );
  525. Parent::initPersistFields();
  526. }
  527. bool SceneObject::_setGameObject(void* object, const char* index, const char* data)
  528. {
  529. // Sanity!
  530. AssertFatal(data != NULL, "Cannot use a NULL asset Id.");
  531. return true; //rbI->setMeshAsset(data);
  532. }
  533. //-----------------------------------------------------------------------------
  534. bool SceneObject::_setFieldPosition( void *object, const char *index, const char *data )
  535. {
  536. SceneObject* so = static_cast<SceneObject*>( object );
  537. if ( so )
  538. {
  539. MatrixF txfm( so->getTransform() );
  540. Con::setData( TypeMatrixPosition, &txfm, 0, 1, &data );
  541. so->setTransform( txfm );
  542. }
  543. return false;
  544. }
  545. //-----------------------------------------------------------------------------
  546. bool SceneObject::_setFieldRotation( void *object, const char *index, const char *data )
  547. {
  548. SceneObject* so = static_cast<SceneObject*>( object );
  549. if ( so )
  550. {
  551. MatrixF txfm( so->getTransform() );
  552. Con::setData( TypeMatrixRotation, &txfm, 0, 1, &data );
  553. so->setTransform( txfm );
  554. }
  555. return false;
  556. }
  557. //-----------------------------------------------------------------------------
  558. bool SceneObject::_setFieldScale( void *object, const char *index, const char *data )
  559. {
  560. SceneObject* so = static_cast<SceneObject*>( object );
  561. if ( so )
  562. {
  563. Point3F scale;
  564. Con::setData( TypePoint3F, &scale, 0, 1, &data );
  565. so->setScale( scale );
  566. }
  567. return false;
  568. }
  569. //-----------------------------------------------------------------------------
  570. bool SceneObject::writeField( StringTableEntry fieldName, const char* value )
  571. {
  572. if( !Parent::writeField( fieldName, value ) )
  573. return false;
  574. static StringTableEntry sIsRenderEnabled = StringTable->insert( "isRenderEnabled" );
  575. static StringTableEntry sIsSelectionEnabled = StringTable->insert( "isSelectionEnabled" );
  576. static StringTableEntry sMountNode = StringTable->insert( "mountNode" );
  577. static StringTableEntry sMountPos = StringTable->insert( "mountPos" );
  578. static StringTableEntry sMountRot = StringTable->insert( "mountRot" );
  579. // Don't write flag fields if they are at their default values.
  580. if( fieldName == sIsRenderEnabled && dAtob( value ) )
  581. return false;
  582. else if( fieldName == sIsSelectionEnabled && dAtob( value ) )
  583. return false;
  584. else if ( mMountPID == NULL && ( fieldName == sMountNode ||
  585. fieldName == sMountPos ||
  586. fieldName == sMountRot ) )
  587. {
  588. return false;
  589. }
  590. return true;
  591. }
  592. //-----------------------------------------------------------------------------
  593. static void scopeCallback( SceneObject* obj, void* conPtr )
  594. {
  595. NetConnection* ptr = reinterpret_cast< NetConnection* >( conPtr );
  596. if( obj->isScopeable() )
  597. ptr->objectInScope(obj);
  598. }
  599. void SceneObject::onCameraScopeQuery( NetConnection* connection, CameraScopeQuery* query )
  600. {
  601. SceneManager* sceneManager = getSceneManager();
  602. GameConnection* conn = dynamic_cast<GameConnection*> (connection);
  603. if (conn && (query->visibleDistance = conn->getVisibleGhostDistance()) == 0.0f)
  604. if ((query->visibleDistance = sceneManager->getVisibleGhostDistance()) == 0.0f)
  605. query->visibleDistance = sceneManager->getVisibleDistance();
  606. // Object itself is in scope.
  607. if( this->isScopeable() )
  608. connection->objectInScope( this );
  609. // If we're mounted to something, that object is in scope too.
  610. if( isMounted() )
  611. connection->objectInScope( mMount.object );
  612. // If we're added to a scene graph, let the graph do the scene scoping.
  613. // Otherwise just put everything in the server container in scope.
  614. if( getSceneManager() )
  615. getSceneManager()->scopeScene( query, connection );
  616. else
  617. gServerContainer.findObjects( 0xFFFFFFFF, scopeCallback, connection );
  618. }
  619. //-----------------------------------------------------------------------------
  620. bool SceneObject::isRenderEnabled() const
  621. {
  622. #ifdef TORQUE_TOOLS
  623. if (gEditingMission)
  624. {
  625. AbstractClassRep *classRep = getClassRep();
  626. return (mObjectFlags.test(RenderEnabledFlag) && classRep->isRenderEnabled());
  627. }
  628. #endif
  629. return (mObjectFlags.test(RenderEnabledFlag));
  630. }
  631. //-----------------------------------------------------------------------------
  632. void SceneObject::setRenderEnabled( bool value )
  633. {
  634. if( value )
  635. mObjectFlags.set( RenderEnabledFlag );
  636. else
  637. mObjectFlags.clear( RenderEnabledFlag );
  638. setMaskBits( FlagMask );
  639. }
  640. //-----------------------------------------------------------------------------
  641. const char* SceneObject::_getRenderEnabled( void* object, const char* data )
  642. {
  643. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  644. if( obj->mObjectFlags.test( RenderEnabledFlag ) )
  645. return "1";
  646. else
  647. return "0";
  648. }
  649. //-----------------------------------------------------------------------------
  650. bool SceneObject::_setRenderEnabled( void *object, const char *index, const char *data )
  651. {
  652. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  653. obj->setRenderEnabled( dAtob( data ) );
  654. return false;
  655. }
  656. //-----------------------------------------------------------------------------
  657. bool SceneObject::isSelectionEnabled() const
  658. {
  659. AbstractClassRep *classRep = getClassRep();
  660. return ( mObjectFlags.test( SelectionEnabledFlag ) && classRep->isSelectionEnabled() );
  661. }
  662. //-----------------------------------------------------------------------------
  663. void SceneObject::setSelectionEnabled( bool value )
  664. {
  665. if( value )
  666. mObjectFlags.set( SelectionEnabledFlag );
  667. else
  668. mObjectFlags.clear( SelectionEnabledFlag );
  669. // Not synchronized on network so don't set dirty bit.
  670. }
  671. //-----------------------------------------------------------------------------
  672. const char* SceneObject::_getSelectionEnabled( void* object, const char* data )
  673. {
  674. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  675. if( obj->mObjectFlags.test( SelectionEnabledFlag ) )
  676. return "true";
  677. else
  678. return "false";
  679. }
  680. //-----------------------------------------------------------------------------
  681. bool SceneObject::_setSelectionEnabled( void *object, const char *index, const char *data )
  682. {
  683. SceneObject* obj = reinterpret_cast< SceneObject* >( object );
  684. obj->setSelectionEnabled( dAtob( data ) );
  685. return false;
  686. }
  687. //--------------------------------------------------------------------------
  688. U32 SceneObject::packUpdate( NetConnection* conn, U32 mask, BitStream* stream )
  689. {
  690. U32 retMask = Parent::packUpdate( conn, mask, stream );
  691. if ( stream->writeFlag( mask & FlagMask ) )
  692. stream->writeRangedU32( (U32)mObjectFlags, 0, getObjectFlagMax() );
  693. // PATHSHAPE
  694. //Begin attachment
  695. retMask = 0; //retry mask
  696. if (stream->writeFlag(getParent() != NULL)) {
  697. stream->writeAffineTransform(mGraph.objToParent);
  698. }
  699. if (stream->writeFlag(mask & MountedMask))
  700. {
  701. // Check to see if we need to write an object ID
  702. if (stream->writeFlag(mGraph.parent)) {
  703. S32 t = conn->getGhostIndex(mGraph.parent);
  704. // Check to see if we can actually ghost this...
  705. if (t == -1) {
  706. // Cant, try again later
  707. retMask |= MountedMask;
  708. stream->writeFlag(false);
  709. }
  710. else {
  711. // Can, write it.
  712. stream->writeFlag(true);
  713. stream->writeRangedU32(U32(t), 0, NetConnection::MaxGhostCount);
  714. stream->writeAffineTransform(mGraph.objToParent);
  715. //Con::errorf("%d: sent mounted on %d", getId(), mGraph.parent->getId());
  716. }
  717. }
  718. }
  719. // End of Attachment
  720. // PATHSHAPE END
  721. if ( mask & MountedMask )
  722. {
  723. if ( mMount.object )
  724. {
  725. S32 gIndex = conn->getGhostIndex( mMount.object );
  726. if ( stream->writeFlag( gIndex != -1 ) )
  727. {
  728. stream->writeFlag( true );
  729. stream->writeInt( gIndex, NetConnection::GhostIdBitSize );
  730. if ( stream->writeFlag( mMount.node != -1 ) )
  731. stream->writeInt( mMount.node, NumMountPointBits );
  732. mathWrite( *stream, mMount.xfm );
  733. }
  734. else
  735. // Will have to try again later
  736. retMask |= MountedMask;
  737. }
  738. else
  739. // Unmount if this isn't the initial packet
  740. if ( stream->writeFlag( !(mask & InitialUpdateMask) ) )
  741. stream->writeFlag( false );
  742. }
  743. else
  744. stream->writeFlag( false );
  745. return retMask;
  746. }
  747. //-----------------------------------------------------------------------------
  748. void SceneObject::unpackUpdate( NetConnection* conn, BitStream* stream )
  749. {
  750. Parent::unpackUpdate( conn, stream );
  751. // FlagMask
  752. if ( stream->readFlag() )
  753. mObjectFlags = stream->readRangedU32( 0, getObjectFlagMax() );
  754. // PATHSHAPE
  755. // begin of attachment
  756. if (stream->readFlag())
  757. {
  758. MatrixF m;
  759. stream->readAffineTransform(&m);
  760. mGraph.objToParent = m;
  761. }
  762. if (stream->readFlag())
  763. {
  764. // Check to see if we need to read an object ID
  765. if (stream->readFlag())
  766. {
  767. // Check to see if we can actually ghost this...
  768. if (stream->readFlag())
  769. {
  770. GameBase *newParent = static_cast<GameBase*>(conn->resolveGhost(stream->readRangedU32(0, NetConnection::MaxGhostCount)));
  771. MatrixF m;
  772. stream->readAffineTransform(&m);
  773. if (getParent() != newParent)
  774. {
  775. clearProcessAfter();
  776. processAfter(newParent);
  777. }
  778. attachToParent(newParent, &m);
  779. //Con::errorf("%d: got mounted on %d", getId(), mParentObject->getId());
  780. }
  781. }
  782. else
  783. {
  784. attachToParent(NULL);
  785. }
  786. }
  787. // End of attachment
  788. // PATHSHAPE END
  789. // MountedMask
  790. if ( stream->readFlag() )
  791. {
  792. if ( stream->readFlag() )
  793. {
  794. S32 gIndex = stream->readInt( NetConnection::GhostIdBitSize );
  795. SceneObject* obj = dynamic_cast<SceneObject*>( conn->resolveGhost( gIndex ) );
  796. S32 node = -1;
  797. if ( stream->readFlag() ) // node != -1
  798. node = stream->readInt( NumMountPointBits );
  799. MatrixF xfm;
  800. mathRead( *stream, &xfm );
  801. if ( !obj )
  802. {
  803. conn->setLastError( "Invalid packet from server." );
  804. return;
  805. }
  806. obj->mountObject( this, node, xfm );
  807. }
  808. else
  809. unmount();
  810. }
  811. }
  812. //-----------------------------------------------------------------------------
  813. void SceneObject::_updateZoningState() const
  814. {
  815. if( mZoneRefDirty )
  816. {
  817. SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
  818. if( manager )
  819. manager->updateObject( const_cast< SceneObject* >( this ) );
  820. else
  821. mZoneRefDirty = false;
  822. }
  823. }
  824. //-----------------------------------------------------------------------------
  825. U32 SceneObject::getCurrZone( const U32 index ) const
  826. {
  827. _updateZoningState();
  828. // Not the most efficient way to do this, walking the list,
  829. // but it's an uncommon call...
  830. ZoneRef* walk = mZoneRefHead;
  831. for( U32 i = 0; i < index; ++ i )
  832. {
  833. walk = walk->nextInObj;
  834. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  835. }
  836. AssertFatal( walk != NULL, "SceneObject::_getCurrZone - Too few object refs!" );
  837. return walk->zone;
  838. }
  839. //-----------------------------------------------------------------------------
  840. Point3F SceneObject::getPosition() const
  841. {
  842. Point3F pos;
  843. mObjToWorld.getColumn(3, &pos);
  844. return pos;
  845. }
  846. //-----------------------------------------------------------------------------
  847. Point3F SceneObject::getRenderPosition() const
  848. {
  849. Point3F pos;
  850. mRenderObjToWorld.getColumn(3, &pos);
  851. return pos;
  852. }
  853. //-----------------------------------------------------------------------------
  854. void SceneObject::setPosition(const Point3F &pos)
  855. {
  856. AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
  857. MatrixF xform = mObjToWorld;
  858. xform.setColumn(3, pos);
  859. setTransform(xform);
  860. }
  861. //-----------------------------------------------------------------------------
  862. F32 SceneObject::distanceTo(const Point3F &pnt) const
  863. {
  864. return mWorldBox.getDistanceToPoint( pnt );
  865. }
  866. //-----------------------------------------------------------------------------
  867. void SceneObject::processAfter( ProcessObject *obj )
  868. {
  869. AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
  870. mAfterObject = (SceneObject*)obj;
  871. if ( mAfterObject->mAfterObject == this )
  872. mAfterObject->mAfterObject = NULL;
  873. getProcessList()->markDirty();
  874. }
  875. //-----------------------------------------------------------------------------
  876. void SceneObject::clearProcessAfter()
  877. {
  878. mAfterObject = NULL;
  879. }
  880. //-----------------------------------------------------------------------------
  881. void SceneObject::setProcessTick( bool t )
  882. {
  883. if ( t == mProcessTick )
  884. return;
  885. if ( mProcessTick )
  886. {
  887. if ( !getMountedObjectCount() )
  888. plUnlink(); // Only unlink if there is nothing mounted to us
  889. mProcessTick = false;
  890. }
  891. else
  892. {
  893. // Just to be sure...
  894. plUnlink();
  895. getProcessList()->addObject( this );
  896. mProcessTick = true;
  897. }
  898. }
  899. //-----------------------------------------------------------------------------
  900. ProcessList* SceneObject::getProcessList() const
  901. {
  902. if ( isClientObject() )
  903. return ClientProcessList::get();
  904. else
  905. return ServerProcessList::get();
  906. }
  907. //-------------------------------------------------------------------------
  908. bool SceneObject::isMounted()
  909. {
  910. resolveMountPID();
  911. return mMount.object != NULL;
  912. }
  913. //-----------------------------------------------------------------------------
  914. S32 SceneObject::getMountedObjectCount()
  915. {
  916. S32 count = 0;
  917. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  918. count++;
  919. return count;
  920. }
  921. //-----------------------------------------------------------------------------
  922. SceneObject* SceneObject::getMountedObject(S32 idx)
  923. {
  924. if (idx >= 0) {
  925. S32 count = 0;
  926. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  927. if (count++ == idx)
  928. return itr;
  929. }
  930. return NULL;
  931. }
  932. //-----------------------------------------------------------------------------
  933. S32 SceneObject::getMountedObjectNode(S32 idx)
  934. {
  935. if (idx >= 0) {
  936. S32 count = 0;
  937. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  938. if (count++ == idx)
  939. return itr->mMount.node;
  940. }
  941. return -1;
  942. }
  943. //-----------------------------------------------------------------------------
  944. SceneObject* SceneObject::getMountNodeObject(S32 node)
  945. {
  946. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  947. if (itr->mMount.node == node)
  948. return itr;
  949. return NULL;
  950. }
  951. //-----------------------------------------------------------------------------
  952. bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
  953. {
  954. SceneObject* so = static_cast<SceneObject*>( object );
  955. if ( so )
  956. {
  957. // Unmount old object (PID reference is released even if it had been resolved yet)
  958. if ( so->mMountPID )
  959. {
  960. so->mMountPID->decRefCount();
  961. so->mMountPID = NULL;
  962. }
  963. so->unmount();
  964. // Get the new PID (new object will be mounted on demand)
  965. Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
  966. if ( so->mMountPID )
  967. so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
  968. }
  969. return false;
  970. }
  971. void SceneObject::resolveMountPID()
  972. {
  973. if ( mMountPID && !mMount.object )
  974. {
  975. SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
  976. if ( obj )
  977. obj->mountObject( this, mMount.node, mMount.xfm );
  978. }
  979. }
  980. //-----------------------------------------------------------------------------
  981. void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  982. {
  983. if ( obj->mMount.object == this )
  984. {
  985. // Already mounted to this
  986. // So update our node and xfm which may have changed.
  987. obj->mMount.node = node;
  988. obj->mMount.xfm = xfm;
  989. }
  990. else
  991. {
  992. if ( obj->mMount.object )
  993. obj->unmount();
  994. obj->mMount.object = this;
  995. obj->mMount.node = node;
  996. obj->mMount.link = mMount.list;
  997. obj->mMount.xfm = xfm;
  998. mMount.list = obj;
  999. // Assign PIDs to both objects
  1000. if ( isServerObject() )
  1001. {
  1002. obj->getOrCreatePersistentId();
  1003. if ( !obj->mMountPID )
  1004. {
  1005. obj->mMountPID = getOrCreatePersistentId();
  1006. obj->mMountPID->incRefCount();
  1007. }
  1008. }
  1009. obj->onMount( this, node );
  1010. }
  1011. }
  1012. //-----------------------------------------------------------------------------
  1013. void SceneObject::unmountObject( SceneObject *obj )
  1014. {
  1015. if ( obj->mMount.object == this )
  1016. {
  1017. // Find and unlink the object
  1018. for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
  1019. {
  1020. if ( *ptr == obj )
  1021. {
  1022. *ptr = obj->mMount.link;
  1023. break;
  1024. }
  1025. }
  1026. obj->mMount.object = NULL;
  1027. obj->mMount.link = NULL;
  1028. if( obj->mMountPID != NULL ) // Only on server.
  1029. {
  1030. obj->mMountPID->decRefCount();
  1031. obj->mMountPID = NULL;
  1032. }
  1033. obj->onUnmount( this, obj->mMount.node );
  1034. }
  1035. }
  1036. //-----------------------------------------------------------------------------
  1037. void SceneObject::unmount()
  1038. {
  1039. if (mMount.object)
  1040. mMount.object->unmountObject(this);
  1041. }
  1042. //-----------------------------------------------------------------------------
  1043. void SceneObject::onMount( SceneObject *obj, S32 node )
  1044. {
  1045. deleteNotify( obj );
  1046. if ( !isGhost() )
  1047. {
  1048. setMaskBits( MountedMask );
  1049. //onMount_callback( node );
  1050. }
  1051. }
  1052. //-----------------------------------------------------------------------------
  1053. void SceneObject::onUnmount( SceneObject *obj, S32 node )
  1054. {
  1055. clearNotify(obj);
  1056. if ( !isGhost() )
  1057. {
  1058. setMaskBits( MountedMask );
  1059. //onUnmount_callback( node );
  1060. }
  1061. }
  1062. //-----------------------------------------------------------------------------
  1063. void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  1064. {
  1065. MatrixF mountTransform( xfm );
  1066. const Point3F &scale = getScale();
  1067. Point3F position = mountTransform.getPosition();
  1068. position.convolve( scale );
  1069. mountTransform.setPosition( position );
  1070. outMat->mul( mObjToWorld, mountTransform );
  1071. }
  1072. //-----------------------------------------------------------------------------
  1073. void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
  1074. {
  1075. MatrixF mountTransform( xfm );
  1076. const Point3F &scale = getScale();
  1077. Point3F position = mountTransform.getPosition();
  1078. position.convolve( scale );
  1079. mountTransform.setPosition( position );
  1080. outMat->mul( mRenderObjToWorld, mountTransform );
  1081. }
  1082. //=============================================================================
  1083. // Console API.
  1084. //=============================================================================
  1085. // MARK: ---- Console API ----
  1086. //-----------------------------------------------------------------------------
  1087. DefineEngineMethod( SceneObject, getType, S32, (),,
  1088. "Return the type mask for this object.\n"
  1089. "@return The numeric type mask for the object." )
  1090. {
  1091. return object->getTypeMask();
  1092. }
  1093. //-----------------------------------------------------------------------------
  1094. DefineEngineMethod( SceneObject, mountObject, bool,
  1095. ( SceneObject* objB, S32 slot, TransformF txfm ), ( TransformF::Identity ),
  1096. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  1097. "@param objB Object to mount onto us\n"
  1098. "@param slot Mount slot ID\n"
  1099. "@param txfm (optional) mount offset transform\n"
  1100. "@return true if successful, false if failed (objB is not valid)" )
  1101. {
  1102. if ( objB )
  1103. {
  1104. object->mountObject( objB, slot, txfm.getMatrix() );
  1105. return true;
  1106. }
  1107. return false;
  1108. }
  1109. //-----------------------------------------------------------------------------
  1110. DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
  1111. "@brief Unmount an object from ourselves.\n\n"
  1112. "@param target object to unmount\n"
  1113. "@return true if successful, false if failed\n" )
  1114. {
  1115. if ( target )
  1116. {
  1117. object->unmountObject(target);
  1118. return true;
  1119. }
  1120. return false;
  1121. }
  1122. //-----------------------------------------------------------------------------
  1123. DefineEngineMethod( SceneObject, unmount, void, (),,
  1124. "Unmount us from the currently mounted object if any.\n" )
  1125. {
  1126. object->unmount();
  1127. }
  1128. //-----------------------------------------------------------------------------
  1129. DefineEngineMethod( SceneObject, isMounted, bool, (),,
  1130. "@brief Check if we are mounted to another object.\n\n"
  1131. "@return true if mounted to another object, false if not mounted." )
  1132. {
  1133. return object->isMounted();
  1134. }
  1135. //-----------------------------------------------------------------------------
  1136. DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
  1137. "@brief Get the object we are mounted to.\n\n"
  1138. "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
  1139. {
  1140. return object->isMounted()? object->getObjectMount()->getId(): 0;
  1141. }
  1142. //-----------------------------------------------------------------------------
  1143. DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
  1144. "Get the number of objects mounted to us.\n"
  1145. "@return the number of mounted objects." )
  1146. {
  1147. return object->getMountedObjectCount();
  1148. }
  1149. //-----------------------------------------------------------------------------
  1150. DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
  1151. "Get the object mounted at a particular slot.\n"
  1152. "@param slot mount slot index to query\n"
  1153. "@return ID of the object mounted in the slot, or 0 if no object." )
  1154. {
  1155. SceneObject* mobj = object->getMountedObject( slot );
  1156. return mobj? mobj->getId(): 0;
  1157. }
  1158. //-----------------------------------------------------------------------------
  1159. DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
  1160. "@brief Get the mount node index of the object mounted at our given slot.\n\n"
  1161. "@param slot mount slot index to query\n"
  1162. "@return index of the mount node used by the object mounted in this slot." )
  1163. {
  1164. return object->getMountedObjectNode( slot );
  1165. }
  1166. //-----------------------------------------------------------------------------
  1167. DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
  1168. "@brief Get the object mounted at our given node index.\n\n"
  1169. "@param node mount node index to query\n"
  1170. "@return ID of the first object mounted at the node, or 0 if none found." )
  1171. {
  1172. SceneObject* mobj = object->getMountNodeObject( node );
  1173. return mobj? mobj->getId(): 0;
  1174. }
  1175. //-----------------------------------------------------------------------------
  1176. DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
  1177. "Get the object's transform.\n"
  1178. "@return the current transform of the object\n" )
  1179. {
  1180. return object->getTransform();
  1181. }
  1182. //-----------------------------------------------------------------------------
  1183. DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
  1184. "Get the object's inverse transform.\n"
  1185. "@return the inverse transform of the object\n" )
  1186. {
  1187. return object->getWorldTransform();
  1188. }
  1189. //-----------------------------------------------------------------------------
  1190. DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
  1191. "Get the object's world position.\n"
  1192. "@return the current world position of the object\n" )
  1193. {
  1194. return object->getTransform().getPosition();
  1195. }
  1196. DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
  1197. "Set the object's world position.\n"
  1198. "@param pos the new world position of the object\n" )
  1199. {
  1200. return object->setPosition(pos);
  1201. }
  1202. //-----------------------------------------------------------------------------
  1203. DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
  1204. "Get Euler rotation of this object.\n"
  1205. "@return the orientation of the object in the form of rotations around the "
  1206. "X, Y and Z axes in degrees.\n" )
  1207. {
  1208. Point3F euler = object->getTransform().toEuler();
  1209. // Convert to degrees.
  1210. euler.x = mRadToDeg( euler.x );
  1211. euler.y = mRadToDeg( euler.y );
  1212. euler.z = mRadToDeg( euler.z );
  1213. return euler;
  1214. }
  1215. DefineEngineMethod(SceneObject, setEulerRotation, void, (Point3F inRot), ,
  1216. "set Euler rotation of this object.\n"
  1217. "@set the orientation of the object in the form of rotations around the "
  1218. "X, Y and Z axes in degrees.\n")
  1219. {
  1220. MatrixF curMat = object->getTransform();
  1221. Point3F curPos = curMat.getPosition();
  1222. Point3F curScale = curMat.getScale();
  1223. EulerF inRotRad = inRot * M_PI_F / 180.0;
  1224. curMat.set(inRotRad, curPos);
  1225. curMat.scale(curScale);
  1226. object->setTransform(curMat);
  1227. }
  1228. //-----------------------------------------------------------------------------
  1229. DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
  1230. "Get the direction this object is facing.\n"
  1231. "@return a vector indicating the direction this object is facing.\n"
  1232. "@note This is the object's y axis." )
  1233. {
  1234. return object->getTransform().getForwardVector();
  1235. }
  1236. //-----------------------------------------------------------------------------
  1237. DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
  1238. "Get the right vector of the object.\n"
  1239. "@return a vector indicating the right direction of this object."
  1240. "@note This is the object's x axis." )
  1241. {
  1242. return object->getTransform().getRightVector();
  1243. }
  1244. //-----------------------------------------------------------------------------
  1245. DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
  1246. "Get the up vector of the object.\n"
  1247. "@return a vector indicating the up direction of this object."
  1248. "@note This is the object's z axis." )
  1249. {
  1250. return object->getTransform().getUpVector();
  1251. }
  1252. //-----------------------------------------------------------------------------
  1253. DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
  1254. "Set the object's transform (orientation and position)."
  1255. "@param txfm object transform to set" )
  1256. {
  1257. // PATHSHAPE
  1258. object->PerformUpdatesForChildren(txfm.getMatrix());
  1259. // PATHSHAPE END
  1260. if ( !txfm.hasRotation() )
  1261. object->setPosition( txfm.getPosition() );
  1262. else
  1263. object->setTransform( txfm.getMatrix() );
  1264. }
  1265. //-----------------------------------------------------------------------------
  1266. DefineEngineMethod( SceneObject, getScale, Point3F, (),,
  1267. "Get the object's scale.\n"
  1268. "@return object scale as a Point3F" )
  1269. {
  1270. return object->getScale();
  1271. }
  1272. //-----------------------------------------------------------------------------
  1273. DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
  1274. "Set the object's scale.\n"
  1275. "@param scale object scale to set\n" )
  1276. {
  1277. object->setScale( scale );
  1278. }
  1279. //-----------------------------------------------------------------------------
  1280. DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
  1281. "Get the object's world bounding box.\n"
  1282. "@return six fields, two Point3Fs, containing the min and max points of the "
  1283. "worldbox." )
  1284. {
  1285. return object->getWorldBox();
  1286. }
  1287. //-----------------------------------------------------------------------------
  1288. DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
  1289. "Get the center of the object's world bounding box.\n"
  1290. "@return the center of the world bounding box for this object." )
  1291. {
  1292. Point3F center;
  1293. object->getWorldBox().getCenter( &center );
  1294. return center;
  1295. }
  1296. //-----------------------------------------------------------------------------
  1297. DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
  1298. "Get the object's bounding box (relative to the object's origin).\n"
  1299. "@return six fields, two Point3Fs, containing the min and max points of the "
  1300. "objectbox." )
  1301. {
  1302. return object->getObjBox();
  1303. }
  1304. //-----------------------------------------------------------------------------
  1305. DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
  1306. "Check if this object has a global bounds set.\n"
  1307. "If global bounds are set to be true, then the object is assumed to have an "
  1308. "infinitely large bounding box for collision and rendering purposes.\n"
  1309. "@return true if the object has a global bounds." )
  1310. {
  1311. return object->isGlobalBounds();
  1312. }
  1313. DefineEngineMethod(SceneObject, setForwardVector, void, (VectorF newForward, VectorF upVector), (VectorF(0, 0, 0), VectorF(0, 0, 1)),
  1314. "Sets the forward vector of a scene object, making it face Y+ along the new vector.\n"
  1315. "@param The new forward vector to set.\n"
  1316. "@param (Optional) The up vector to use to help orient the rotation.")
  1317. {
  1318. object->setForwardVector(newForward, upVector);
  1319. }
  1320. // PATHSHAPE
  1321. // Move RenderTransform by set amount
  1322. // no longer used
  1323. void SceneObject::moveRender(const Point3F &delta)
  1324. {
  1325. Point3F pos;
  1326. const MatrixF& tmat = getRenderTransform();
  1327. tmat.getColumn(3,&pos);
  1328. AngAxisF aa(tmat);
  1329. pos += delta;
  1330. MatrixF mat;
  1331. aa.setMatrix(&mat);
  1332. mat.setColumn(3,pos);
  1333. setRenderTransform(mat);
  1334. }
  1335. void SceneObject::PerformUpdatesForChildren(MatrixF mat){
  1336. for (U32 i=0; i < getNumChildren(); i++) {
  1337. SceneObject *o = getChild(i);
  1338. o->updateChildTransform(); //update the position of the child object
  1339. }
  1340. }
  1341. // This function will move the players based on how much it's
  1342. // parent have moved
  1343. void SceneObject::updateChildTransform(){
  1344. if (getParent() != NULL){
  1345. MatrixF one;
  1346. MatrixF two;
  1347. MatrixF three;
  1348. MatrixF four;
  1349. MatrixF mat;
  1350. one= getTransform();
  1351. two = getParent()->getTransform();
  1352. one.affineInverse();
  1353. four.mul(two,one);
  1354. mat.mul(getParent()->mLastXform,getTransform());
  1355. setTransform(mat);
  1356. }
  1357. }
  1358. // This function will move the rendered image based on how much it's
  1359. // parent have moved since the processtick.
  1360. // For some reason the player object must be updated via it's GetRenderTransform seen below,
  1361. // Other objects seem to require getTransform() only
  1362. void SceneObject::updateRenderChangesByParent(){
  1363. if (getParent() != NULL){
  1364. MatrixF renderXform = getParent()->getRenderTransform();
  1365. MatrixF xform = getParent()->getTransform();
  1366. xform.affineInverse();
  1367. MatrixF offset;
  1368. offset.mul(renderXform, xform);
  1369. MatrixF mat;
  1370. //add the "offset" caused by the parents change, and add it to it's own
  1371. // This is needed by objects that update their own render transform thru interpolate tick
  1372. // Mostly for stationary objects.
  1373. if (getClassName() == StringTable->insert("Player"))
  1374. mat.mul(offset,getRenderTransform());
  1375. else
  1376. mat.mul(offset,getTransform());
  1377. setRenderTransform(mat);
  1378. }
  1379. }
  1380. //Ramen - Move Transform by set amount
  1381. //written by Anthony Lovell
  1382. void SceneObject::move(F32 x, F32 y, F32 z)
  1383. {
  1384. Point3F delta;
  1385. delta.x = x;
  1386. delta.y = y;
  1387. delta.z = z;
  1388. move(delta);
  1389. }
  1390. // move by a specified delta in root coordinate space
  1391. void SceneObject::move(const Point3F &delta)
  1392. {
  1393. Point3F pos;
  1394. const MatrixF& tmat = getTransform();
  1395. tmat.getColumn(3,&pos);
  1396. AngAxisF aa(tmat);
  1397. pos += delta;
  1398. MatrixF mat;
  1399. aa.setMatrix(&mat);
  1400. mat.setColumn(3,pos);
  1401. setTransform(mat);
  1402. }
  1403. //written by Anthony Lovell ----------------------------------------------------------
  1404. U32
  1405. SceneObject::getNumChildren() const
  1406. {
  1407. U32 num = 0;
  1408. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling)
  1409. num++;
  1410. return num;
  1411. }
  1412. //written by Anthony Lovell ----------------------------------------------------------
  1413. SceneObject *
  1414. SceneObject::getChild(U32 index) const
  1415. {
  1416. SceneObject *cur = mGraph.firstChild;
  1417. for (U32 i = 0;
  1418. cur && i < index;
  1419. i++)
  1420. cur = cur->mGraph.nextSibling;
  1421. return cur;
  1422. }
  1423. void SceneObject::UpdateXformChange(const MatrixF &mat){
  1424. // This function gets the difference between the Transform and current Render transform
  1425. // Used for Interpolation matching with the child objects who rely on this data.
  1426. MatrixF oldxform = getTransform();
  1427. oldxform.affineInverse();
  1428. mLastXform.mul(mat,oldxform);
  1429. }
  1430. //----------------------------------------------------------
  1431. bool
  1432. SceneObject::attachChildAt(SceneObject *subObject, MatrixF atThisOffset, S32 node)
  1433. {
  1434. AssertFatal(subObject, "attaching a null subObject");
  1435. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1436. bool b = subObject->attachToParent(this, &atThisOffset, node);
  1437. if (!b)
  1438. return false;
  1439. return true;
  1440. }
  1441. //----------------------------------------------------------
  1442. bool
  1443. SceneObject::attachChildAt(SceneObject *subObject, Point3F atThisPosition)
  1444. {
  1445. AssertFatal(subObject, "attaching a null subObject");
  1446. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1447. bool b = subObject->attachToParent(this);
  1448. if (!b)
  1449. return false;
  1450. subObject->mGraph.objToParent.setColumn(3, atThisPosition);
  1451. // calcTransformFromLocalTransform();
  1452. return true;
  1453. }
  1454. //----------------------------------------------------------
  1455. bool
  1456. SceneObject::attachChild(SceneObject *child)
  1457. {
  1458. AssertFatal(child, "attaching a null subObject");
  1459. AssertFatal(!isChildOf(child), "cyclic attachChild()");
  1460. return child->attachToParent(this);
  1461. }
  1462. //----------------------------------------------------------
  1463. /// returns a count of children plus their children, recursively
  1464. U32
  1465. SceneObject::getNumProgeny() const
  1466. {
  1467. U32 num = 0;
  1468. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling) {
  1469. num += 1 + cur->getNumProgeny();
  1470. }
  1471. return num;
  1472. }
  1473. DefineEngineMethod(SceneObject, getNumChildren, S32, (),, "returns number of direct child objects")
  1474. {
  1475. return object->getNumChildren();
  1476. }
  1477. DefineEngineMethod(SceneObject, getNumProgeny, S32, (),, "returns number of recursively-nested child objects")
  1478. {
  1479. return object->getNumProgeny();
  1480. }
  1481. DefineEngineMethod(SceneObject, getChild, S32, (S32 _index), (0), "getChild(S32 index) -- returns child SceneObject at given index")
  1482. {
  1483. SceneObject *s = object->getChild(_index);
  1484. return s ? s->getId() : 0;
  1485. }
  1486. DefineEngineMethod(SceneObject, attachChildAt, bool, (SceneObject* _subObject, MatrixF _offset, S32 _node), (nullAsType<SceneObject*>(), MatrixF::Identity, 0), "(SceneObject subObject, MatrixF offset, S32 offset)"
  1487. "Mount object to this one with the specified offset expressed in our coordinate space.")
  1488. {
  1489. if (_subObject != nullptr)
  1490. {
  1491. return object->attachChildAt(_subObject, _offset, _node);
  1492. }
  1493. else
  1494. {
  1495. Con::errorf("Couldn't addObject()!");
  1496. return false;
  1497. }
  1498. }
  1499. DefineEngineMethod(SceneObject, attachToParent, bool, (const char*_sceneObject), ,"attachToParent(SceneObject)"
  1500. "specify a null or non-null parent")
  1501. {
  1502. SceneObject * t;
  1503. if(Sim::findObject(_sceneObject, t))
  1504. {
  1505. return object->attachToParent(t);
  1506. }
  1507. else
  1508. {
  1509. if ((!String::compare("0", _sceneObject))|| (!String::compare("", _sceneObject)))
  1510. return object->attachToParent(NULL);
  1511. else
  1512. {
  1513. Con::errorf("Couldn't setParent()!");
  1514. return false;
  1515. }
  1516. }
  1517. }
  1518. DefineEngineMethod(SceneObject, getParent, S32, (),, "returns ID of parent SceneObject")
  1519. {
  1520. SceneObject *p = object->getParent();
  1521. return p ? p->getId() : -1;
  1522. }
  1523. DefineEngineMethod(SceneObject, attachChild, bool, (const char*_subObject),, "(SceneObject subObject)"
  1524. "attach an object to this one, preserving its present transform.")
  1525. {
  1526. SceneObject * t;
  1527. MatrixF m;
  1528. if(Sim::findObject(_subObject, t))
  1529. return object->attachChild(t);
  1530. Con::errorf("Couldn't addObject()!");
  1531. return false;
  1532. }
  1533. bool SceneObject::isChildOf(SceneObject *so)
  1534. {
  1535. SceneObject *p = mGraph.parent;
  1536. if (p) {
  1537. if (p == so)
  1538. return true;
  1539. else
  1540. return p->isChildOf(so);
  1541. } else
  1542. return false;
  1543. }
  1544. bool SceneObject::attachToParent(SceneObject *newParent, MatrixF *atThisOffset/* = NULL */, S32 node )
  1545. {
  1546. SceneObject *oldParent = mGraph.parent;
  1547. if (oldParent == newParent)
  1548. return true;
  1549. // cycles in the scene hierarchy are forbidden!
  1550. // that is: a SceneObject cannot be a child of its progeny
  1551. if (newParent && newParent->isChildOf(this))
  1552. return false;
  1553. mGraph.parent = newParent;
  1554. if (oldParent) {
  1555. clearNotify(oldParent);
  1556. // remove this SceneObject from the list of children of oldParent
  1557. SceneObject *cur = oldParent->mGraph.firstChild;
  1558. if (cur == this) { // if we are the first child, this is easy
  1559. oldParent->mGraph.firstChild = mGraph.nextSibling;
  1560. } else {
  1561. while (cur->mGraph.nextSibling != this) {
  1562. cur = cur->mGraph.nextSibling;
  1563. // ASSERT cur != NULL;
  1564. }
  1565. cur->mGraph.nextSibling = mGraph.nextSibling;
  1566. }
  1567. oldParent->onLostChild(this);
  1568. }
  1569. if (newParent) {
  1570. deleteNotify(newParent); // if we are deleted, inform our parent
  1571. // add this SceneObject to the list of children of oldParent
  1572. mGraph.nextSibling = newParent->mGraph.firstChild;
  1573. newParent->mGraph.firstChild = this;
  1574. mGraph.parent = newParent;
  1575. newParent->onNewChild(this);
  1576. if (atThisOffset)
  1577. mGraph.objToParent = *atThisOffset;
  1578. } else {
  1579. mGraph.parent = NULL;
  1580. mGraph.nextSibling = NULL;
  1581. mGraph.objToParent = mObjToWorld;
  1582. }
  1583. onLostParent(oldParent);
  1584. onNewParent(newParent);
  1585. setMaskBits(MountedMask);
  1586. return true;
  1587. }
  1588. DefineEngineMethod(SceneObject, detachChild, bool, (const char*_subObject),, "SceneObject subObject")
  1589. {
  1590. SceneObject * t;
  1591. if(Sim::findObject(_subObject, t)) {
  1592. return t->attachToParent(NULL);
  1593. } else
  1594. return false;
  1595. }
  1596. IMPLEMENT_CALLBACK(SceneObject, onNewParent, void, (SceneObject *newParent), (newParent), "");
  1597. IMPLEMENT_CALLBACK(SceneObject, onLostParent, void, (SceneObject *oldParent), (oldParent), "");
  1598. IMPLEMENT_CALLBACK(SceneObject, onNewChild, void, (SceneObject *newKid), (newKid), "");
  1599. IMPLEMENT_CALLBACK(SceneObject, onLostChild, void, (SceneObject *lostKid), (lostKid), "");
  1600. void SceneObject::onNewParent(SceneObject *newParent) { if (isServerObject()) onNewParent_callback(newParent); }
  1601. void SceneObject::onLostParent(SceneObject *oldParent) { if (isServerObject()) onLostParent_callback(oldParent); }
  1602. void SceneObject::onNewChild(SceneObject *newKid) { if (isServerObject()) onNewChild_callback(newKid); }
  1603. void SceneObject::onLostChild(SceneObject *lostKid) { if (isServerObject()) onLostChild_callback(lostKid); }