sceneObject.cpp 43 KB

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