sceneObject.cpp 60 KB

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