sceneObject.cpp 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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. mZoneListHandle = 0;
  108. mNumCurrZones = 0;
  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( mZoneListHandle == 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()
  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 )
  826. {
  827. SceneZoneSpaceManager* manager = getSceneManager()->getZoneManager();
  828. _updateZoningState();
  829. // Not the most efficient way to do this, walking the list,
  830. // but it's an uncommon call...
  831. U32 numZones = 0;
  832. U32* zones = NULL;
  833. zones = manager->getZoneIDS(this, numZones);
  834. return index < numZones ? zones[index] : 0;
  835. }
  836. //-----------------------------------------------------------------------------
  837. Point3F SceneObject::getPosition() const
  838. {
  839. Point3F pos;
  840. mObjToWorld.getColumn(3, &pos);
  841. return pos;
  842. }
  843. //-----------------------------------------------------------------------------
  844. Point3F SceneObject::getRenderPosition() const
  845. {
  846. Point3F pos;
  847. mRenderObjToWorld.getColumn(3, &pos);
  848. return pos;
  849. }
  850. //-----------------------------------------------------------------------------
  851. void SceneObject::setPosition(const Point3F &pos)
  852. {
  853. AssertFatal( !mIsNaN( pos ), "SceneObject::setPosition() - The position is NaN!" );
  854. MatrixF xform = mObjToWorld;
  855. xform.setColumn(3, pos);
  856. setTransform(xform);
  857. }
  858. //-----------------------------------------------------------------------------
  859. F32 SceneObject::distanceTo(const Point3F &pnt) const
  860. {
  861. return mWorldBox.getDistanceToPoint( pnt );
  862. }
  863. //-----------------------------------------------------------------------------
  864. void SceneObject::processAfter( ProcessObject *obj )
  865. {
  866. AssertFatal( dynamic_cast<SceneObject*>( obj ), "SceneObject::processAfter - Got non-SceneObject!" );
  867. mAfterObject = (SceneObject*)obj;
  868. if ( mAfterObject->mAfterObject == this )
  869. mAfterObject->mAfterObject = NULL;
  870. getProcessList()->markDirty();
  871. }
  872. //-----------------------------------------------------------------------------
  873. void SceneObject::clearProcessAfter()
  874. {
  875. mAfterObject = NULL;
  876. }
  877. //-----------------------------------------------------------------------------
  878. void SceneObject::setProcessTick( bool t )
  879. {
  880. if ( t == mProcessTick )
  881. return;
  882. if ( mProcessTick )
  883. {
  884. if ( !getMountedObjectCount() )
  885. plUnlink(); // Only unlink if there is nothing mounted to us
  886. mProcessTick = false;
  887. }
  888. else
  889. {
  890. // Just to be sure...
  891. plUnlink();
  892. getProcessList()->addObject( this );
  893. mProcessTick = true;
  894. }
  895. }
  896. //-----------------------------------------------------------------------------
  897. ProcessList* SceneObject::getProcessList() const
  898. {
  899. if ( isClientObject() )
  900. return ClientProcessList::get();
  901. else
  902. return ServerProcessList::get();
  903. }
  904. //-------------------------------------------------------------------------
  905. bool SceneObject::isMounted()
  906. {
  907. resolveMountPID();
  908. return mMount.object != NULL;
  909. }
  910. //-----------------------------------------------------------------------------
  911. S32 SceneObject::getMountedObjectCount()
  912. {
  913. S32 count = 0;
  914. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  915. count++;
  916. return count;
  917. }
  918. //-----------------------------------------------------------------------------
  919. SceneObject* SceneObject::getMountedObject(S32 idx)
  920. {
  921. if (idx >= 0) {
  922. S32 count = 0;
  923. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  924. if (count++ == idx)
  925. return itr;
  926. }
  927. return NULL;
  928. }
  929. //-----------------------------------------------------------------------------
  930. S32 SceneObject::getMountedObjectNode(S32 idx)
  931. {
  932. if (idx >= 0) {
  933. S32 count = 0;
  934. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  935. if (count++ == idx)
  936. return itr->mMount.node;
  937. }
  938. return -1;
  939. }
  940. //-----------------------------------------------------------------------------
  941. SceneObject* SceneObject::getMountNodeObject(S32 node)
  942. {
  943. for (SceneObject* itr = mMount.list; itr; itr = itr->mMount.link)
  944. if (itr->mMount.node == node)
  945. return itr;
  946. return NULL;
  947. }
  948. //-----------------------------------------------------------------------------
  949. bool SceneObject::_setMountPID( void* object, const char* index, const char* data )
  950. {
  951. SceneObject* so = static_cast<SceneObject*>( object );
  952. if ( so )
  953. {
  954. // Unmount old object (PID reference is released even if it had been resolved yet)
  955. if ( so->mMountPID )
  956. {
  957. so->mMountPID->decRefCount();
  958. so->mMountPID = NULL;
  959. }
  960. so->unmount();
  961. // Get the new PID (new object will be mounted on demand)
  962. Con::setData( TypePID, &so->mMountPID, 0, 1, &data );
  963. if ( so->mMountPID )
  964. so->mMountPID->incRefCount(); // Prevent PID from being deleted out from under us!
  965. }
  966. return false;
  967. }
  968. void SceneObject::resolveMountPID()
  969. {
  970. if ( mMountPID && !mMount.object )
  971. {
  972. SceneObject *obj = dynamic_cast< SceneObject* >( mMountPID->getObject() );
  973. if ( obj )
  974. obj->mountObject( this, mMount.node, mMount.xfm );
  975. }
  976. }
  977. //-----------------------------------------------------------------------------
  978. void SceneObject::mountObject( SceneObject *obj, S32 node, const MatrixF &xfm )
  979. {
  980. if ( obj->mMount.object == this )
  981. {
  982. // Already mounted to this
  983. // So update our node and xfm which may have changed.
  984. obj->mMount.node = node;
  985. obj->mMount.xfm = xfm;
  986. }
  987. else
  988. {
  989. if ( obj->mMount.object )
  990. obj->unmount();
  991. obj->mMount.object = this;
  992. obj->mMount.node = node;
  993. obj->mMount.link = mMount.list;
  994. obj->mMount.xfm = xfm;
  995. mMount.list = obj;
  996. // Assign PIDs to both objects
  997. if ( isServerObject() )
  998. {
  999. obj->getOrCreatePersistentId();
  1000. if ( !obj->mMountPID )
  1001. {
  1002. obj->mMountPID = getOrCreatePersistentId();
  1003. obj->mMountPID->incRefCount();
  1004. }
  1005. }
  1006. obj->onMount( this, node );
  1007. }
  1008. }
  1009. //-----------------------------------------------------------------------------
  1010. void SceneObject::unmountObject( SceneObject *obj )
  1011. {
  1012. if ( obj->mMount.object == this )
  1013. {
  1014. // Find and unlink the object
  1015. for ( SceneObject **ptr = &mMount.list; *ptr; ptr = &(*ptr)->mMount.link )
  1016. {
  1017. if ( *ptr == obj )
  1018. {
  1019. *ptr = obj->mMount.link;
  1020. break;
  1021. }
  1022. }
  1023. obj->mMount.object = NULL;
  1024. obj->mMount.link = NULL;
  1025. if( obj->mMountPID != NULL ) // Only on server.
  1026. {
  1027. obj->mMountPID->decRefCount();
  1028. obj->mMountPID = NULL;
  1029. }
  1030. obj->onUnmount( this, obj->mMount.node );
  1031. }
  1032. }
  1033. //-----------------------------------------------------------------------------
  1034. void SceneObject::unmount()
  1035. {
  1036. if (mMount.object)
  1037. mMount.object->unmountObject(this);
  1038. }
  1039. //-----------------------------------------------------------------------------
  1040. void SceneObject::onMount( SceneObject *obj, S32 node )
  1041. {
  1042. deleteNotify( obj );
  1043. if ( !isGhost() )
  1044. {
  1045. setMaskBits( MountedMask );
  1046. //onMount_callback( node );
  1047. }
  1048. }
  1049. //-----------------------------------------------------------------------------
  1050. void SceneObject::onUnmount( SceneObject *obj, S32 node )
  1051. {
  1052. clearNotify(obj);
  1053. if ( !isGhost() )
  1054. {
  1055. setMaskBits( MountedMask );
  1056. //onUnmount_callback( node );
  1057. }
  1058. }
  1059. //-----------------------------------------------------------------------------
  1060. void SceneObject::getMountTransform( S32 index, const MatrixF &xfm, MatrixF *outMat )
  1061. {
  1062. MatrixF mountTransform( xfm );
  1063. const Point3F &scale = getScale();
  1064. Point3F position = mountTransform.getPosition();
  1065. position.convolve( scale );
  1066. mountTransform.setPosition( position );
  1067. outMat->mul( mObjToWorld, mountTransform );
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. void SceneObject::getRenderMountTransform( F32 delta, S32 index, const MatrixF &xfm, MatrixF *outMat )
  1071. {
  1072. MatrixF mountTransform( xfm );
  1073. const Point3F &scale = getScale();
  1074. Point3F position = mountTransform.getPosition();
  1075. position.convolve( scale );
  1076. mountTransform.setPosition( position );
  1077. outMat->mul( mRenderObjToWorld, mountTransform );
  1078. }
  1079. //=============================================================================
  1080. // Console API.
  1081. //=============================================================================
  1082. // MARK: ---- Console API ----
  1083. //-----------------------------------------------------------------------------
  1084. DefineEngineMethod( SceneObject, getType, S32, (),,
  1085. "Return the type mask for this object.\n"
  1086. "@return The numeric type mask for the object." )
  1087. {
  1088. return object->getTypeMask();
  1089. }
  1090. //-----------------------------------------------------------------------------
  1091. DefineEngineMethod( SceneObject, mountObject, bool,
  1092. ( SceneObject* objB, S32 slot, TransformF txfm ), ( TransformF::Identity ),
  1093. "@brief Mount objB to this object at the desired slot with optional transform.\n\n"
  1094. "@param objB Object to mount onto us\n"
  1095. "@param slot Mount slot ID\n"
  1096. "@param txfm (optional) mount offset transform\n"
  1097. "@return true if successful, false if failed (objB is not valid)" )
  1098. {
  1099. if ( objB )
  1100. {
  1101. object->mountObject( objB, slot, txfm.getMatrix() );
  1102. return true;
  1103. }
  1104. return false;
  1105. }
  1106. //-----------------------------------------------------------------------------
  1107. DefineEngineMethod( SceneObject, unmountObject, bool, ( SceneObject* target ),,
  1108. "@brief Unmount an object from ourselves.\n\n"
  1109. "@param target object to unmount\n"
  1110. "@return true if successful, false if failed\n" )
  1111. {
  1112. if ( target )
  1113. {
  1114. object->unmountObject(target);
  1115. return true;
  1116. }
  1117. return false;
  1118. }
  1119. //-----------------------------------------------------------------------------
  1120. DefineEngineMethod( SceneObject, unmount, void, (),,
  1121. "Unmount us from the currently mounted object if any.\n" )
  1122. {
  1123. object->unmount();
  1124. }
  1125. //-----------------------------------------------------------------------------
  1126. DefineEngineMethod( SceneObject, isMounted, bool, (),,
  1127. "@brief Check if we are mounted to another object.\n\n"
  1128. "@return true if mounted to another object, false if not mounted." )
  1129. {
  1130. return object->isMounted();
  1131. }
  1132. //-----------------------------------------------------------------------------
  1133. DefineEngineMethod( SceneObject, getObjectMount, S32, (),,
  1134. "@brief Get the object we are mounted to.\n\n"
  1135. "@return the SimObjectID of the object we're mounted to, or 0 if not mounted." )
  1136. {
  1137. return object->isMounted()? object->getObjectMount()->getId(): 0;
  1138. }
  1139. //-----------------------------------------------------------------------------
  1140. DefineEngineMethod( SceneObject, getMountedObjectCount, S32, (),,
  1141. "Get the number of objects mounted to us.\n"
  1142. "@return the number of mounted objects." )
  1143. {
  1144. return object->getMountedObjectCount();
  1145. }
  1146. //-----------------------------------------------------------------------------
  1147. DefineEngineMethod( SceneObject, getMountedObject, S32, ( S32 slot ),,
  1148. "Get the object mounted at a particular slot.\n"
  1149. "@param slot mount slot index to query\n"
  1150. "@return ID of the object mounted in the slot, or 0 if no object." )
  1151. {
  1152. SceneObject* mobj = object->getMountedObject( slot );
  1153. return mobj? mobj->getId(): 0;
  1154. }
  1155. //-----------------------------------------------------------------------------
  1156. DefineEngineMethod( SceneObject, getMountedObjectNode, S32, ( S32 slot ),,
  1157. "@brief Get the mount node index of the object mounted at our given slot.\n\n"
  1158. "@param slot mount slot index to query\n"
  1159. "@return index of the mount node used by the object mounted in this slot." )
  1160. {
  1161. return object->getMountedObjectNode( slot );
  1162. }
  1163. //-----------------------------------------------------------------------------
  1164. DefineEngineMethod( SceneObject, getMountNodeObject, S32, ( S32 node ),,
  1165. "@brief Get the object mounted at our given node index.\n\n"
  1166. "@param node mount node index to query\n"
  1167. "@return ID of the first object mounted at the node, or 0 if none found." )
  1168. {
  1169. SceneObject* mobj = object->getMountNodeObject( node );
  1170. return mobj? mobj->getId(): 0;
  1171. }
  1172. //-----------------------------------------------------------------------------
  1173. DefineEngineMethod( SceneObject, getTransform, TransformF, (),,
  1174. "Get the object's transform.\n"
  1175. "@return the current transform of the object\n" )
  1176. {
  1177. return object->getTransform();
  1178. }
  1179. //-----------------------------------------------------------------------------
  1180. DefineEngineMethod( SceneObject, getInverseTransform, TransformF, (),,
  1181. "Get the object's inverse transform.\n"
  1182. "@return the inverse transform of the object\n" )
  1183. {
  1184. return object->getWorldTransform();
  1185. }
  1186. //-----------------------------------------------------------------------------
  1187. DefineEngineMethod( SceneObject, getPosition, Point3F, (),,
  1188. "Get the object's world position.\n"
  1189. "@return the current world position of the object\n" )
  1190. {
  1191. return object->getTransform().getPosition();
  1192. }
  1193. DefineEngineMethod( SceneObject, setPosition, void, (Point3F pos),,
  1194. "Set the object's world position.\n"
  1195. "@param pos the new world position of the object\n" )
  1196. {
  1197. return object->setPosition(pos);
  1198. }
  1199. //-----------------------------------------------------------------------------
  1200. DefineEngineMethod( SceneObject, getEulerRotation, Point3F, (),,
  1201. "Get Euler rotation of this object.\n"
  1202. "@return the orientation of the object in the form of rotations around the "
  1203. "X, Y and Z axes in degrees.\n" )
  1204. {
  1205. Point3F euler = object->getTransform().toEuler();
  1206. // Convert to degrees.
  1207. euler.x = mRadToDeg( euler.x );
  1208. euler.y = mRadToDeg( euler.y );
  1209. euler.z = mRadToDeg( euler.z );
  1210. return euler;
  1211. }
  1212. DefineEngineMethod(SceneObject, setEulerRotation, void, (Point3F inRot), ,
  1213. "set Euler rotation of this object.\n"
  1214. "@set the orientation of the object in the form of rotations around the "
  1215. "X, Y and Z axes in degrees.\n")
  1216. {
  1217. MatrixF curMat = object->getTransform();
  1218. Point3F curPos = curMat.getPosition();
  1219. Point3F curScale = curMat.getScale();
  1220. EulerF inRotRad = inRot * M_PI_F / 180.0;
  1221. curMat.set(inRotRad, curPos);
  1222. curMat.scale(curScale);
  1223. object->setTransform(curMat);
  1224. }
  1225. //-----------------------------------------------------------------------------
  1226. DefineEngineMethod( SceneObject, getForwardVector, VectorF, (),,
  1227. "Get the direction this object is facing.\n"
  1228. "@return a vector indicating the direction this object is facing.\n"
  1229. "@note This is the object's y axis." )
  1230. {
  1231. return object->getTransform().getForwardVector();
  1232. }
  1233. //-----------------------------------------------------------------------------
  1234. DefineEngineMethod( SceneObject, getRightVector, VectorF, (),,
  1235. "Get the right vector of the object.\n"
  1236. "@return a vector indicating the right direction of this object."
  1237. "@note This is the object's x axis." )
  1238. {
  1239. return object->getTransform().getRightVector();
  1240. }
  1241. //-----------------------------------------------------------------------------
  1242. DefineEngineMethod( SceneObject, getUpVector, VectorF, (),,
  1243. "Get the up vector of the object.\n"
  1244. "@return a vector indicating the up direction of this object."
  1245. "@note This is the object's z axis." )
  1246. {
  1247. return object->getTransform().getUpVector();
  1248. }
  1249. //-----------------------------------------------------------------------------
  1250. DefineEngineMethod( SceneObject, setTransform, void, ( TransformF txfm ),,
  1251. "Set the object's transform (orientation and position)."
  1252. "@param txfm object transform to set" )
  1253. {
  1254. // PATHSHAPE
  1255. object->PerformUpdatesForChildren(txfm.getMatrix());
  1256. // PATHSHAPE END
  1257. if ( !txfm.hasRotation() )
  1258. object->setPosition( txfm.getPosition() );
  1259. else
  1260. object->setTransform( txfm.getMatrix() );
  1261. }
  1262. //-----------------------------------------------------------------------------
  1263. DefineEngineMethod( SceneObject, getScale, Point3F, (),,
  1264. "Get the object's scale.\n"
  1265. "@return object scale as a Point3F" )
  1266. {
  1267. return object->getScale();
  1268. }
  1269. //-----------------------------------------------------------------------------
  1270. DefineEngineMethod( SceneObject, setScale, void, ( Point3F scale ),,
  1271. "Set the object's scale.\n"
  1272. "@param scale object scale to set\n" )
  1273. {
  1274. object->setScale( scale );
  1275. }
  1276. //-----------------------------------------------------------------------------
  1277. DefineEngineMethod( SceneObject, getWorldBox, Box3F, (),,
  1278. "Get the object's world bounding box.\n"
  1279. "@return six fields, two Point3Fs, containing the min and max points of the "
  1280. "worldbox." )
  1281. {
  1282. return object->getWorldBox();
  1283. }
  1284. //-----------------------------------------------------------------------------
  1285. DefineEngineMethod( SceneObject, getWorldBoxCenter, Point3F, (),,
  1286. "Get the center of the object's world bounding box.\n"
  1287. "@return the center of the world bounding box for this object." )
  1288. {
  1289. Point3F center;
  1290. object->getWorldBox().getCenter( &center );
  1291. return center;
  1292. }
  1293. //-----------------------------------------------------------------------------
  1294. DefineEngineMethod( SceneObject, getObjectBox, Box3F, (),,
  1295. "Get the object's bounding box (relative to the object's origin).\n"
  1296. "@return six fields, two Point3Fs, containing the min and max points of the "
  1297. "objectbox." )
  1298. {
  1299. return object->getObjBox();
  1300. }
  1301. //-----------------------------------------------------------------------------
  1302. DefineEngineMethod( SceneObject, isGlobalBounds, bool, (),,
  1303. "Check if this object has a global bounds set.\n"
  1304. "If global bounds are set to be true, then the object is assumed to have an "
  1305. "infinitely large bounding box for collision and rendering purposes.\n"
  1306. "@return true if the object has a global bounds." )
  1307. {
  1308. return object->isGlobalBounds();
  1309. }
  1310. DefineEngineMethod(SceneObject, setForwardVector, void, (VectorF newForward, VectorF upVector), (VectorF(0, 0, 0), VectorF(0, 0, 1)),
  1311. "Sets the forward vector of a scene object, making it face Y+ along the new vector.\n"
  1312. "@param The new forward vector to set.\n"
  1313. "@param (Optional) The up vector to use to help orient the rotation.")
  1314. {
  1315. object->setForwardVector(newForward, upVector);
  1316. }
  1317. // PATHSHAPE
  1318. // Move RenderTransform by set amount
  1319. // no longer used
  1320. void SceneObject::moveRender(const Point3F &delta)
  1321. {
  1322. Point3F pos;
  1323. const MatrixF& tmat = getRenderTransform();
  1324. tmat.getColumn(3,&pos);
  1325. AngAxisF aa(tmat);
  1326. pos += delta;
  1327. MatrixF mat;
  1328. aa.setMatrix(&mat);
  1329. mat.setColumn(3,pos);
  1330. setRenderTransform(mat);
  1331. }
  1332. void SceneObject::PerformUpdatesForChildren(MatrixF mat){
  1333. for (U32 i=0; i < getNumChildren(); i++) {
  1334. SceneObject *o = getChild(i);
  1335. o->updateChildTransform(); //update the position of the child object
  1336. }
  1337. }
  1338. // This function will move the players based on how much it's
  1339. // parent have moved
  1340. void SceneObject::updateChildTransform(){
  1341. if (getParent() != NULL){
  1342. MatrixF one;
  1343. MatrixF two;
  1344. MatrixF three;
  1345. MatrixF four;
  1346. MatrixF mat;
  1347. one= getTransform();
  1348. two = getParent()->getTransform();
  1349. one.affineInverse();
  1350. four.mul(two,one);
  1351. mat.mul(getParent()->mLastXform,getTransform());
  1352. setTransform(mat);
  1353. }
  1354. }
  1355. // This function will move the rendered image based on how much it's
  1356. // parent have moved since the processtick.
  1357. // For some reason the player object must be updated via it's GetRenderTransform seen below,
  1358. // Other objects seem to require getTransform() only
  1359. void SceneObject::updateRenderChangesByParent(){
  1360. if (getParent() != NULL){
  1361. MatrixF renderXform = getParent()->getRenderTransform();
  1362. MatrixF xform = getParent()->getTransform();
  1363. xform.affineInverse();
  1364. MatrixF offset;
  1365. offset.mul(renderXform, xform);
  1366. MatrixF mat;
  1367. //add the "offset" caused by the parents change, and add it to it's own
  1368. // This is needed by objects that update their own render transform thru interpolate tick
  1369. // Mostly for stationary objects.
  1370. if (getClassName() == StringTable->insert("Player"))
  1371. mat.mul(offset,getRenderTransform());
  1372. else
  1373. mat.mul(offset,getTransform());
  1374. setRenderTransform(mat);
  1375. }
  1376. }
  1377. //Ramen - Move Transform by set amount
  1378. //written by Anthony Lovell
  1379. void SceneObject::move(F32 x, F32 y, F32 z)
  1380. {
  1381. Point3F delta;
  1382. delta.x = x;
  1383. delta.y = y;
  1384. delta.z = z;
  1385. move(delta);
  1386. }
  1387. // move by a specified delta in root coordinate space
  1388. void SceneObject::move(const Point3F &delta)
  1389. {
  1390. Point3F pos;
  1391. const MatrixF& tmat = getTransform();
  1392. tmat.getColumn(3,&pos);
  1393. AngAxisF aa(tmat);
  1394. pos += delta;
  1395. MatrixF mat;
  1396. aa.setMatrix(&mat);
  1397. mat.setColumn(3,pos);
  1398. setTransform(mat);
  1399. }
  1400. //written by Anthony Lovell ----------------------------------------------------------
  1401. U32
  1402. SceneObject::getNumChildren() const
  1403. {
  1404. U32 num = 0;
  1405. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling)
  1406. num++;
  1407. return num;
  1408. }
  1409. //written by Anthony Lovell ----------------------------------------------------------
  1410. SceneObject *
  1411. SceneObject::getChild(U32 index) const
  1412. {
  1413. SceneObject *cur = mGraph.firstChild;
  1414. for (U32 i = 0;
  1415. cur && i < index;
  1416. i++)
  1417. cur = cur->mGraph.nextSibling;
  1418. return cur;
  1419. }
  1420. void SceneObject::UpdateXformChange(const MatrixF &mat){
  1421. // This function gets the difference between the Transform and current Render transform
  1422. // Used for Interpolation matching with the child objects who rely on this data.
  1423. MatrixF oldxform = getTransform();
  1424. oldxform.affineInverse();
  1425. mLastXform.mul(mat,oldxform);
  1426. }
  1427. //----------------------------------------------------------
  1428. bool
  1429. SceneObject::attachChildAt(SceneObject *subObject, MatrixF atThisOffset, S32 node)
  1430. {
  1431. AssertFatal(subObject, "attaching a null subObject");
  1432. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1433. bool b = subObject->attachToParent(this, &atThisOffset, node);
  1434. if (!b)
  1435. return false;
  1436. return true;
  1437. }
  1438. //----------------------------------------------------------
  1439. bool
  1440. SceneObject::attachChildAt(SceneObject *subObject, Point3F atThisPosition)
  1441. {
  1442. AssertFatal(subObject, "attaching a null subObject");
  1443. AssertFatal(!isChildOf(subObject), "cyclic attachChild()");
  1444. bool b = subObject->attachToParent(this);
  1445. if (!b)
  1446. return false;
  1447. subObject->mGraph.objToParent.setColumn(3, atThisPosition);
  1448. // calcTransformFromLocalTransform();
  1449. return true;
  1450. }
  1451. //----------------------------------------------------------
  1452. bool
  1453. SceneObject::attachChild(SceneObject *child)
  1454. {
  1455. AssertFatal(child, "attaching a null subObject");
  1456. AssertFatal(!isChildOf(child), "cyclic attachChild()");
  1457. return child->attachToParent(this);
  1458. }
  1459. //----------------------------------------------------------
  1460. /// returns a count of children plus their children, recursively
  1461. U32
  1462. SceneObject::getNumProgeny() const
  1463. {
  1464. U32 num = 0;
  1465. for (SceneObject *cur = mGraph.firstChild; cur; cur = cur->mGraph.nextSibling) {
  1466. num += 1 + cur->getNumProgeny();
  1467. }
  1468. return num;
  1469. }
  1470. DefineEngineMethod(SceneObject, getNumChildren, S32, (),, "returns number of direct child objects")
  1471. {
  1472. return object->getNumChildren();
  1473. }
  1474. DefineEngineMethod(SceneObject, getNumProgeny, S32, (),, "returns number of recursively-nested child objects")
  1475. {
  1476. return object->getNumProgeny();
  1477. }
  1478. DefineEngineMethod(SceneObject, getChild, S32, (S32 _index), (0), "getChild(S32 index) -- returns child SceneObject at given index")
  1479. {
  1480. SceneObject *s = object->getChild(_index);
  1481. return s ? s->getId() : 0;
  1482. }
  1483. DefineEngineMethod(SceneObject, attachChildAt, bool, (SceneObject* _subObject, MatrixF _offset, S32 _node), (nullAsType<SceneObject*>(), MatrixF::Identity, 0), "(SceneObject subObject, MatrixF offset, S32 offset)"
  1484. "Mount object to this one with the specified offset expressed in our coordinate space.")
  1485. {
  1486. if (_subObject != nullptr)
  1487. {
  1488. return object->attachChildAt(_subObject, _offset, _node);
  1489. }
  1490. else
  1491. {
  1492. Con::errorf("Couldn't addObject()!");
  1493. return false;
  1494. }
  1495. }
  1496. DefineEngineMethod(SceneObject, attachToParent, bool, (const char*_sceneObject), ,"attachToParent(SceneObject)"
  1497. "specify a null or non-null parent")
  1498. {
  1499. SceneObject * t;
  1500. if(Sim::findObject(_sceneObject, t))
  1501. {
  1502. return object->attachToParent(t);
  1503. }
  1504. else
  1505. {
  1506. if ((!String::compare("0", _sceneObject))|| (!String::compare("", _sceneObject)))
  1507. return object->attachToParent(NULL);
  1508. else
  1509. {
  1510. Con::errorf("Couldn't setParent()!");
  1511. return false;
  1512. }
  1513. }
  1514. }
  1515. DefineEngineMethod(SceneObject, getParent, S32, (),, "returns ID of parent SceneObject")
  1516. {
  1517. SceneObject *p = object->getParent();
  1518. return p ? p->getId() : -1;
  1519. }
  1520. DefineEngineMethod(SceneObject, attachChild, bool, (const char*_subObject),, "(SceneObject subObject)"
  1521. "attach an object to this one, preserving its present transform.")
  1522. {
  1523. SceneObject * t;
  1524. MatrixF m;
  1525. if(Sim::findObject(_subObject, t))
  1526. return object->attachChild(t);
  1527. Con::errorf("Couldn't addObject()!");
  1528. return false;
  1529. }
  1530. bool SceneObject::isChildOf(SceneObject *so)
  1531. {
  1532. SceneObject *p = mGraph.parent;
  1533. if (p) {
  1534. if (p == so)
  1535. return true;
  1536. else
  1537. return p->isChildOf(so);
  1538. } else
  1539. return false;
  1540. }
  1541. bool SceneObject::attachToParent(SceneObject *newParent, MatrixF *atThisOffset/* = NULL */, S32 node )
  1542. {
  1543. SceneObject *oldParent = mGraph.parent;
  1544. if (oldParent == newParent)
  1545. return true;
  1546. // cycles in the scene hierarchy are forbidden!
  1547. // that is: a SceneObject cannot be a child of its progeny
  1548. if (newParent && newParent->isChildOf(this))
  1549. return false;
  1550. mGraph.parent = newParent;
  1551. if (oldParent) {
  1552. clearNotify(oldParent);
  1553. // remove this SceneObject from the list of children of oldParent
  1554. SceneObject *cur = oldParent->mGraph.firstChild;
  1555. if (cur == this) { // if we are the first child, this is easy
  1556. oldParent->mGraph.firstChild = mGraph.nextSibling;
  1557. } else {
  1558. while (cur->mGraph.nextSibling != this) {
  1559. cur = cur->mGraph.nextSibling;
  1560. // ASSERT cur != NULL;
  1561. }
  1562. cur->mGraph.nextSibling = mGraph.nextSibling;
  1563. }
  1564. oldParent->onLostChild(this);
  1565. }
  1566. if (newParent) {
  1567. deleteNotify(newParent); // if we are deleted, inform our parent
  1568. // add this SceneObject to the list of children of oldParent
  1569. mGraph.nextSibling = newParent->mGraph.firstChild;
  1570. newParent->mGraph.firstChild = this;
  1571. mGraph.parent = newParent;
  1572. newParent->onNewChild(this);
  1573. if (atThisOffset)
  1574. mGraph.objToParent = *atThisOffset;
  1575. } else {
  1576. mGraph.parent = NULL;
  1577. mGraph.nextSibling = NULL;
  1578. mGraph.objToParent = mObjToWorld;
  1579. }
  1580. onLostParent(oldParent);
  1581. onNewParent(newParent);
  1582. setMaskBits(MountedMask);
  1583. return true;
  1584. }
  1585. DefineEngineMethod(SceneObject, detachChild, bool, (const char*_subObject),, "SceneObject subObject")
  1586. {
  1587. SceneObject * t;
  1588. if(Sim::findObject(_subObject, t)) {
  1589. return t->attachToParent(NULL);
  1590. } else
  1591. return false;
  1592. }
  1593. IMPLEMENT_CALLBACK(SceneObject, onNewParent, void, (SceneObject *newParent), (newParent), "");
  1594. IMPLEMENT_CALLBACK(SceneObject, onLostParent, void, (SceneObject *oldParent), (oldParent), "");
  1595. IMPLEMENT_CALLBACK(SceneObject, onNewChild, void, (SceneObject *newKid), (newKid), "");
  1596. IMPLEMENT_CALLBACK(SceneObject, onLostChild, void, (SceneObject *lostKid), (lostKid), "");
  1597. void SceneObject::onNewParent(SceneObject *newParent) { if (isServerObject()) onNewParent_callback(newParent); }
  1598. void SceneObject::onLostParent(SceneObject *oldParent) { if (isServerObject()) onLostParent_callback(oldParent); }
  1599. void SceneObject::onNewChild(SceneObject *newKid) { if (isServerObject()) onNewChild_callback(newKid); }
  1600. void SceneObject::onLostChild(SceneObject *lostKid) { if (isServerObject()) onLostChild_callback(lostKid); }