Scene.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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. #ifndef _SCENE_H_
  23. #define _SCENE_H_
  24. #ifndef _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. #ifndef _VECTOR2_H_
  28. #include "2d/core/Vector2.h"
  29. #endif
  30. #ifndef _NETOBJECT_H_
  31. #include "network/netObject.h"
  32. #endif
  33. #ifndef _TICKABLE_H_
  34. #include "platform/Tickable.h"
  35. #endif
  36. #ifndef _PHYSICS_PROXY_H_
  37. #include "2d/scene/PhysicsProxy.h"
  38. #endif
  39. #ifndef _WORLD_QUERY_H_
  40. #include "2d/scene/WorldQuery.h"
  41. #endif
  42. #ifndef _DEBUG_DRAW_H_
  43. #include "2d/scene/DebugDraw.h"
  44. #endif
  45. #ifndef _HASHTABLE_H_
  46. #include "collection/hashTable.h"
  47. #endif
  48. #ifndef _BATCH_RENDER_H_
  49. #include "2d/core/BatchRender.h"
  50. #endif
  51. #ifndef _SCENE_RENDER_QUEUE_H_
  52. #include "2d/scene/SceneRenderQueue.h"
  53. #endif
  54. #ifndef _SCENE_RENDER_OBJECT_H_
  55. #include "2d/scene/SceneRenderObject.h"
  56. #endif
  57. #ifndef _BEHAVIOR_COMPONENT_H_
  58. #include "component/behaviors/behaviorComponent.h"
  59. #endif
  60. #ifndef _ASSET_PTR_H_
  61. #include "assets/assetPtr.h"
  62. #endif
  63. //-----------------------------------------------------------------------------
  64. extern EnumTable jointTypeTable;
  65. ///-----------------------------------------------------------------------------
  66. class SceneObject;
  67. class SceneWindow;
  68. ///-----------------------------------------------------------------------------
  69. struct tDeleteRequest
  70. {
  71. SimObjectId mObjectId;
  72. SceneObject* mpSceneObject;
  73. bool mSafeDeleteReady;
  74. };
  75. ///-----------------------------------------------------------------------------
  76. struct TickContact
  77. {
  78. TickContact()
  79. {
  80. initialize( NULL, NULL, NULL, NULL, NULL );
  81. }
  82. void initialize(
  83. b2Contact* pContact,
  84. SceneObject* pSceneObjectA,
  85. SceneObject* pSceneObjectB,
  86. b2Fixture* pFixtureA,
  87. b2Fixture* pFixtureB )
  88. {
  89. mpContact = pContact;
  90. mpSceneObjectA = pSceneObjectA;
  91. mpSceneObjectB = pSceneObjectB;
  92. mpFixtureA = pFixtureA;
  93. mpFixtureB = pFixtureB;
  94. // Get world manifold.
  95. if ( mpContact != NULL )
  96. {
  97. mPointCount = pContact->GetManifold()->pointCount;
  98. mpContact->GetWorldManifold( &mWorldManifold );
  99. }
  100. else
  101. {
  102. mPointCount = 0;
  103. }
  104. // Reset impulses.
  105. for (U32 i = 0; i < b2_maxManifoldPoints; i++)
  106. {
  107. mNormalImpulses[i] = 0;
  108. mTangentImpulses[i] = 0;
  109. }
  110. }
  111. inline SceneObject* getCollideWith( SceneObject* pMe ) const
  112. {
  113. return pMe == mpSceneObjectA ? mpSceneObjectB : mpSceneObjectA;
  114. }
  115. inline b2Fixture* getCollideWithFixture( b2Fixture* pMe ) const
  116. {
  117. return pMe == mpFixtureA ? mpFixtureB : mpFixtureA;
  118. }
  119. b2Contact* mpContact;
  120. SceneObject* mpSceneObjectA;
  121. SceneObject* mpSceneObjectB;
  122. b2Fixture* mpFixtureA;
  123. b2Fixture* mpFixtureB;
  124. U32 mPointCount;
  125. b2WorldManifold mWorldManifold;
  126. F32 mNormalImpulses[b2_maxManifoldPoints];
  127. F32 mTangentImpulses[b2_maxManifoldPoints];
  128. };
  129. ///-----------------------------------------------------------------------------
  130. class Scene :
  131. public BehaviorComponent,
  132. public TamlChildren,
  133. public PhysicsProxy,
  134. public b2ContactListener,
  135. public b2DestructionListener,
  136. public virtual Tickable
  137. {
  138. public:
  139. typedef HashMap<S32, b2Joint*> typeJointHash;
  140. typedef HashMap<b2Joint*, S32> typeReverseJointHash;
  141. typedef Vector<tDeleteRequest> typeDeleteVector;
  142. typedef Vector<TickContact> typeContactVector;
  143. typedef HashMap<b2Contact*, TickContact> typeContactHash;
  144. typedef Vector<AssetPtr<AssetBase>*> typeAssetPtrVector;
  145. /// Scene Debug Options.
  146. enum DebugOption
  147. {
  148. SCENE_DEBUG_INVALID,
  149. ///
  150. SCENE_DEBUG_METRICS = BIT(0),
  151. SCENE_DEBUG_FPS_METRICS = BIT(1),
  152. SCENE_DEBUG_CONTROLLERS = BIT(2),
  153. SCENE_DEBUG_JOINTS = BIT(3),
  154. SCENE_DEBUG_WIREFRAME_RENDER = BIT(4),
  155. SCENE_DEBUG_AUDIO_SOURCES = BIT(5),
  156. ///
  157. SCENE_DEBUG_AABB = BIT(16),
  158. SCENE_DEBUG_OOBB = BIT(17),
  159. SCENE_DEBUG_SLEEP = BIT(18),
  160. SCENE_DEBUG_COLLISION_SHAPES = BIT(19),
  161. SCENE_DEBUG_POSITION_AND_COM = BIT(20),
  162. SCENE_DEBUG_SORT_POINTS = BIT(21),
  163. };
  164. /// Pick mode.
  165. enum PickMode
  166. {
  167. PICK_INVALID,
  168. ///---
  169. PICK_ANY,
  170. PICK_AABB,
  171. PICK_OOBB,
  172. PICK_COLLISION,
  173. };
  174. /// Debug drawing.
  175. DebugDraw mDebugDraw;
  176. private:
  177. typedef BehaviorComponent Parent;
  178. typedef SceneObject Children;
  179. /// World.
  180. b2World* mpWorld;
  181. WorldQuery* mpWorldQuery;
  182. b2Vec2 mWorldGravity;
  183. S32 mVelocityIterations;
  184. S32 mPositionIterations;
  185. b2BlockAllocator mBlockAllocator;
  186. b2Body* mpGroundBody;
  187. /// Scene occupancy.
  188. typeSceneObjectVector mSceneObjects;
  189. typeSceneObjectVector mTickedSceneObjects;
  190. /// Joint access.
  191. typeJointHash mJoints;
  192. typeReverseJointHash mReverseJoints;
  193. S32 mJointMasterId;
  194. /// Scene controllers.
  195. SimObjectPtr<SimSet> mControllers;
  196. /// Asset pre-loads.
  197. typeAssetPtrVector mAssetPreloads;
  198. /// Scene time.
  199. F32 mSceneTime;
  200. bool mScenePause;
  201. /// Debug and metrics.
  202. DebugStats mDebugStats;
  203. U32 mDebugMask;
  204. SceneObject* mpDebugSceneObject;
  205. /// Layer sorting and draw order.
  206. SceneRenderQueue::RenderSort mLayerSortModes[MAX_LAYERS_SUPPORTED];
  207. /// Batch rendering.
  208. BatchRender mBatchRenderer;
  209. /// Window rendering.
  210. SceneWindow* mpCurrentRenderWindow;
  211. /// Window attachments.
  212. SimSet mAttachedSceneWindows;
  213. /// Delete requests.
  214. typeDeleteVector mDeleteRequests;
  215. typeDeleteVector mDeleteRequestsTemp;
  216. /// Miscellaneous.
  217. S32 mIsEditorScene;
  218. bool mUpdateCallback;
  219. bool mRenderCallback;
  220. typeContactHash mBeginContacts;
  221. typeContactVector mEndContacts;
  222. U32 mSceneIndex;
  223. private:
  224. /// Contacts.
  225. void forwardContacts( void );
  226. void dispatchBeginContactCallbacks( void );
  227. void dispatchEndContactCallbacks( void );
  228. /// Joint definition.
  229. struct CommonJointDefinition
  230. {
  231. CommonJointDefinition()
  232. {
  233. jointType = e_unknownJoint;
  234. collideConnected = false;
  235. pSceneObjectA = NULL;
  236. pSceneObjectB = NULL;
  237. localAnchorA = b2Vec2_zero;
  238. localAnchorB = b2Vec2_zero;
  239. }
  240. b2JointType jointType;
  241. bool collideConnected;
  242. SceneObject* pSceneObjectA;
  243. SceneObject* pSceneObjectB;
  244. b2Vec2 localAnchorA;
  245. b2Vec2 localAnchorB;
  246. };
  247. protected:
  248. /// Taml callbacks.
  249. virtual void onTamlPreRead( void );
  250. virtual void onTamlPostRead( const TamlCustomNodes& customNodes );
  251. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  252. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  253. public:
  254. Scene();
  255. virtual ~Scene();
  256. /// Engine.
  257. virtual bool onAdd();
  258. virtual void onRemove();
  259. virtual void onDeleteNotify( SimObject* object );
  260. static void initPersistFields();
  261. /// Contact processing.
  262. virtual void PreSolve( b2Contact* pContact, const b2Manifold* pOldManifold ) {}
  263. virtual void PostSolve( b2Contact* pContact, const b2ContactImpulse* pImpulse );
  264. virtual void BeginContact( b2Contact* pContact );
  265. virtual void EndContact( b2Contact* pContact );
  266. const typeContactHash& getBeginContacts( void ) const { return mBeginContacts; }
  267. const typeContactVector& getEndContacts( void ) const { return mEndContacts; }
  268. /// Integration.
  269. virtual void processTick();
  270. virtual void interpolateTick( F32 delta );
  271. virtual void advanceTime( F32 timeDelta ) {};
  272. /// Render output.
  273. void sceneRender( const SceneRenderState* pSceneRenderState );
  274. /// World.
  275. inline b2World* getWorld( void ) const { return mpWorld; }
  276. inline WorldQuery* getWorldQuery( const bool clearQuery = false ) { if ( clearQuery ) mpWorldQuery->clearQuery(); return mpWorldQuery; }
  277. b2BlockAllocator* getBlockAllocator( void ) { return &mBlockAllocator; }
  278. inline b2Body* getGroundBody( void ) const { return mpGroundBody; }
  279. virtual ePhysicsProxyType getPhysicsProxyType( void ) const { return PhysicsProxy::PHYSIC_PROXY_GROUNDBODY; }
  280. void setGravity( const b2Vec2& gravity ) { mWorldGravity = gravity; if (mpWorld) mpWorld->SetGravity( gravity ); }
  281. inline b2Vec2 getGravity( void ) { if (mpWorld) mWorldGravity = mpWorld->GetGravity(); return mWorldGravity; }
  282. inline void setVelocityIterations( const S32 iterations ) { mVelocityIterations = iterations; }
  283. inline S32 getVelocityIterations( void ) const { return mVelocityIterations; }
  284. inline void setPositionIterations( const S32 iterations ) { mPositionIterations = iterations; }
  285. inline S32 getPositionIterations( void ) const { return mPositionIterations; }
  286. /// Scene occupancy.
  287. void clearScene( bool deleteObjects = true );
  288. void addToScene( SceneObject* pSceneObject );
  289. void removeFromScene( SceneObject* pSceneObject );
  290. inline typeSceneObjectVectorConstRef getSceneObjects( void ) const { return mSceneObjects; }
  291. inline U32 getSceneObjectCount( void ) const { return mSceneObjects.size(); }
  292. SceneObject* getSceneObject( const U32 objectIndex ) const;
  293. U32 getSceneObjects( typeSceneObjectVector& objects ) const;
  294. U32 getSceneObjects( typeSceneObjectVector& objects, const U32 sceneLayer ) const;
  295. void mergeScene( const Scene* pScene );
  296. inline SimSet* getControllers( void ) { return mControllers; }
  297. inline S32 getAssetPreloadCount( void ) const { return mAssetPreloads.size(); }
  298. const AssetPtr<AssetBase>* getAssetPreload( const S32 index ) const;
  299. void addAssetPreload( const char* pAssetId );
  300. void removeAssetPreload( const char* pAssetId );
  301. void clearAssetPreloads( void );
  302. /// Scene time.
  303. inline F32 getSceneTime( void ) const { return mSceneTime; };
  304. inline void setScenePause( bool status ) { mScenePause = status; }
  305. inline bool getScenePause( void ) const { return mScenePause; };
  306. /// Joint access.
  307. inline U32 getJointCount( void ) const { return mJoints.size(); }
  308. b2JointType getJointType( const S32 jointId );
  309. b2Joint* findJoint( const S32 jointId );
  310. S32 findJointId( b2Joint* pJoint );
  311. S32 createJoint( b2JointDef* pJointDef );
  312. bool deleteJoint( const U32 jointId );
  313. bool hasJoints( SceneObject* pSceneObject );
  314. /// Distance joint.
  315. S32 createDistanceJoint(
  316. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  317. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  318. const F32 length = -1.0f,
  319. const F32 frequency = 0.0f,
  320. const F32 dampingRatio = 0.0f,
  321. const bool collideConnected = false );
  322. void setDistanceJointLength(
  323. const U32 jointId,
  324. const F32 length );
  325. F32 getDistanceJointLength( const U32 jointId );
  326. void setDistanceJointFrequency(
  327. const U32 jointId,
  328. const F32 frequency );
  329. F32 getDistanceJointFrequency( const U32 jointId );
  330. void setDistanceJointDampingRatio(
  331. const U32 jointId,
  332. const F32 dampingRatio );
  333. F32 getDistanceJointDampingRatio( const U32 jointId );
  334. /// Rope joint.
  335. S32 createRopeJoint(
  336. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  337. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  338. const F32 maxLength = -1.0f,
  339. const bool collideConnected = false );
  340. void setRopeJointMaxLength(
  341. const U32 jointId,
  342. const F32 maxLength );
  343. F32 getRopeJointMaxLength( const U32 jointId );
  344. /// Revolute joint.
  345. S32 createRevoluteJoint(
  346. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  347. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  348. const bool collideConnected = false );
  349. void setRevoluteJointLimit(
  350. const U32 jointId,
  351. const bool enableLimit,
  352. const F32 lowerAngle, const F32 upperAngle );
  353. bool getRevoluteJointLimit(
  354. const U32 jointId,
  355. bool& enableLimit,
  356. F32& lowerAngle, F32& upperAngle );
  357. void setRevoluteJointMotor(
  358. const U32 jointId,
  359. const bool enableMotor,
  360. const F32 motorSpeed = b2_pi,
  361. const F32 maxMotorTorque = 0.0f );
  362. bool getRevoluteJointMotor(
  363. const U32 jointId,
  364. bool& enableMotor,
  365. F32& motorSpeed,
  366. F32& maxMotorTorque );
  367. F32 getRevoluteJointAngle( const U32 jointId );
  368. F32 getRevoluteJointSpeed( const U32 jointId );
  369. /// Weld joint.
  370. S32 createWeldJoint(
  371. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  372. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  373. const F32 frequency = 0.0f,
  374. const F32 dampingRatio = 0.0f,
  375. const bool collideConnected = false );
  376. void setWeldJointFrequency(
  377. const U32 jointId,
  378. const F32 frequency );
  379. F32 getWeldJointFrequency( const U32 jointId );
  380. void setWeldJointDampingRatio(
  381. const U32 jointId,
  382. const F32 dampingRatio );
  383. F32 getWeldJointDampingRatio( const U32 jointId );
  384. /// Wheel joint.
  385. S32 createWheelJoint(
  386. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  387. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  388. const b2Vec2& worldAxis,
  389. const bool collideConnected = false );
  390. void setWheelJointMotor(
  391. const U32 jointId,
  392. const bool enableMotor,
  393. const F32 motorSpeed = b2_pi,
  394. const F32 maxMotorTorque = 0.0f );
  395. bool getWheelJointMotor(
  396. const U32 jointId,
  397. bool& enableMotor,
  398. F32& motorSpeed,
  399. F32& maxMotorTorque );
  400. void setWheelJointFrequency(
  401. const U32 jointId,
  402. const F32 frequency );
  403. F32 getWheelJointFrequency( const U32 jointId );
  404. void setWheelJointDampingRatio(
  405. const U32 jointId,
  406. const F32 dampingRatio );
  407. F32 getWheelJointDampingRatio( const U32 jointId );
  408. /// Friction joint.
  409. S32 createFrictionJoint(
  410. const SceneObject* pSceneObjectA,const SceneObject* pSceneObjectB,
  411. const b2Vec2& localAnchorA = b2Vec2_zero, const b2Vec2& localAnchorB = b2Vec2_zero,
  412. const F32 maxForce = 0.0f,
  413. const F32 maxTorque = 0.0f,
  414. const bool collideConnected = false );
  415. void setFrictionJointMaxForce(
  416. const U32 jointId,
  417. const F32 maxForce );
  418. F32 getFrictionJointMaxForce( const U32 jointId );
  419. void setFrictionJointMaxTorque(
  420. const U32 jointId,
  421. const F32 maxTorque );
  422. F32 getFrictionJointMaxTorque( const U32 jointId );
  423. /// Prismatic joint.
  424. S32 createPrismaticJoint(
  425. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  426. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  427. const b2Vec2& worldAxis,
  428. const bool collideConnected = false );
  429. void setPrismaticJointLimit(
  430. const U32 jointId,
  431. const bool enableLimit,
  432. const F32 lowerTranslation, const F32 upperTranslation );
  433. bool getPrismaticJointLimit(
  434. const U32 jointId,
  435. bool& enableLimit,
  436. F32& lowerTranslation, F32& upperTranslation );
  437. void setPrismaticJointMotor(
  438. const U32 jointId,
  439. const bool enableMotor,
  440. const F32 motorSpeed = b2_pi,
  441. const F32 maxMotorForce = 0.0f );
  442. bool getPrismaticJointMotor(
  443. const U32 jointId,
  444. bool& enableMotor,
  445. F32& motorSpeed,
  446. F32& maxMotorTorque );
  447. /// Pulley joint.
  448. S32 createPulleyJoint(
  449. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  450. const b2Vec2& localAnchorA, const b2Vec2& localAnchorB,
  451. const b2Vec2& worldGroundAnchorA, const b2Vec2& worldGroundAnchorB,
  452. const F32 ratio,
  453. const F32 lengthA = -1.0f, const F32 lengthB = -1.0f,
  454. const bool collideConnected = false );
  455. /// Target (a.k.a Mouse) joint.
  456. S32 createTargetJoint(
  457. const SceneObject* pSceneObject,
  458. const b2Vec2& worldTarget,
  459. const F32 maxForce,
  460. const bool useCenterOfMass = false,
  461. const F32 frequency = 5.0f,
  462. const F32 dampingRatio = 0.7f,
  463. const bool collideConnected = false );
  464. void setTargetJointTarget(
  465. const U32 jointId,
  466. const b2Vec2& worldTarget );
  467. b2Vec2 getTargetJointTarget( const U32 jointId );
  468. void setTargetJointMaxForce(
  469. const U32 jointId,
  470. const F32 maxForce );
  471. F32 getTargetJointMaxForce( const U32 jointId );
  472. void setTargetJointFrequency(
  473. const U32 jointId,
  474. const F32 frequency );
  475. F32 getTargetJointFrequency( const U32 jointId );
  476. void setTargetJointDampingRatio(
  477. const U32 jointId,
  478. const F32 dampingRatio );
  479. F32 getTargetJointDampingRatio( const U32 jointId );
  480. /// Motor Joint.
  481. S32 createMotorJoint(
  482. const SceneObject* pSceneObjectA, const SceneObject* pSceneObjectB,
  483. const b2Vec2 linearOffset = b2Vec2_zero,
  484. const F32 angularOffset = 0.0f,
  485. const F32 maxForce = 1.0f,
  486. const F32 maxTorque = 1.0f,
  487. const F32 correctionFactor = 0.3f,
  488. const bool collideConnected = false );
  489. void setMotorJointLinearOffset(
  490. const U32 jointId,
  491. const b2Vec2& linearOffset );
  492. b2Vec2 getMotorJointLinearOffset( const U32 jointId );
  493. void setMotorJointAngularOffset(
  494. const U32 jointId,
  495. const F32 angularOffset );
  496. F32 getMotorJointAngularOffset( const U32 jointId );
  497. void setMotorJointMaxForce(
  498. const U32 jointId,
  499. const F32 maxForce );
  500. F32 getMotorJointMaxForce( const U32 jointId );
  501. void setMotorJointMaxTorque(
  502. const U32 jointId,
  503. const F32 maxTorque );
  504. F32 getMotorJointMaxTorque( const U32 jointId );
  505. /// Debug and metrics.
  506. inline void setDebugOn( const U32 debugMask ) { mDebugMask |= debugMask; }
  507. inline void setDebugOff( const U32 debugMask ) { mDebugMask &= ~debugMask; }
  508. inline U32 getDebugMask( void ) const { return mDebugMask; }
  509. DebugStats& getDebugStats( void ) { return mDebugStats; }
  510. inline void resetDebugStats( void ) { mDebugStats.reset(); }
  511. void setDebugSceneObject( SceneObject* pSceneObject );
  512. inline SceneObject* getDebugSceneObject( void ) const { return mpDebugSceneObject; }
  513. /// Layer sorting.
  514. void setLayerSortMode( const U32 layer, const SceneRenderQueue::RenderSort sortMode );
  515. SceneRenderQueue::RenderSort getLayerSortMode( const U32 layer );
  516. /// Window attachments.
  517. void attachSceneWindow( SceneWindow* pSceneWindow2D );
  518. void detachSceneWindow( SceneWindow* pSceneWindow2D );
  519. void detachAllSceneWindows( void );
  520. bool isSceneWindowAttached( SceneWindow* pSceneWindow2D );
  521. inline SimSet& getAttachedSceneWindows( void ) { return mAttachedSceneWindows; }
  522. /// Delete requests.
  523. void addDeleteRequest( SceneObject* pSceneObject );
  524. void processDeleteRequests( const bool forceImmediate );
  525. /// Destruction listeners.
  526. virtual void SayGoodbye( b2Joint* pJoint );
  527. virtual void SayGoodbye( b2Fixture* pFixture ) {}
  528. virtual SceneObject* create( const char* pType );
  529. /// Miscellaneous.
  530. inline void setBatchingEnabled( const bool enabled ) { mBatchRenderer.setBatchEnabled( enabled ); }
  531. inline bool getBatchingEnabled( void ) const { return mBatchRenderer.getBatchEnabled(); }
  532. inline bool getIsEditorScene( void ) const { return ((mIsEditorScene > 0) ? true : false); }
  533. inline void setIsEditorScene( bool status ) { mIsEditorScene += (status ? 1 : -1); }
  534. static U32 getGlobalSceneCount( void );
  535. inline U32 getSceneIndex( void ) const { return mSceneIndex; }
  536. inline void setUpdateCallback( const bool callback ) { mUpdateCallback = callback; }
  537. inline bool getUpdateCallback( void ) const { return mUpdateCallback; }
  538. inline void setRenderCallback( const bool callback ) { mRenderCallback = callback; }
  539. inline bool getRenderCallback( void ) const { return mRenderCallback; }
  540. static SceneRenderRequest* createDefaultRenderRequest( SceneRenderQueue* pSceneRenderQueue, SceneObject* pSceneObject );
  541. /// Taml children.
  542. virtual U32 getTamlChildCount( void ) const { return (U32)mSceneObjects.size(); }
  543. virtual SimObject* getTamlChild( const U32 childIndex ) const;
  544. virtual void addTamlChild( SimObject* pSimObject );
  545. static b2JointType getJointTypeEnum(const char* label);
  546. static const char* getJointTypeDescription( b2JointType jointType );
  547. static PickMode getPickModeEnum(const char* label);
  548. static const char* getPickModeDescription( PickMode pickMode );
  549. static DebugOption getDebugOptionEnum(const char* label);
  550. static const char* getDebugOptionDescription( DebugOption debugOption );
  551. b2ParticleSystem* mParticleSystem;
  552. /// Declare Console Object.
  553. DECLARE_CONOBJECT(Scene);
  554. protected:
  555. /// Physics.
  556. static bool setGravity( void* obj, const char* data ) { static_cast<Scene*>(obj)->setGravity( Vector2( data ) ); return false; }
  557. static const char* getGravity(void* obj, const char* data) { return Vector2(static_cast<Scene*>(obj)->getGravity()).scriptThis(); }
  558. static bool writeGravity( void* obj, StringTableEntry pFieldName ) { return Vector2(static_cast<Scene*>(obj)->getGravity()).notEqual( Vector2::getZero() ); }
  559. static bool writeVelocityIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getVelocityIterations() != 8; }
  560. static bool writePositionIterations( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getPositionIterations() != 3; }
  561. static bool writeLayerSortMode( void* obj, StringTableEntry pFieldName )
  562. {
  563. // Find the layer index portion of the layer sort mode field.
  564. const char* pLayerNumber = pFieldName;
  565. while( true )
  566. {
  567. // Fetch character.
  568. char value = *pLayerNumber;
  569. // Finish if end of the field or is numeric.
  570. if ( value == 0 || ( value >= '0' && value <= '9' ) )
  571. break;
  572. // Move to next value.
  573. pLayerNumber++;
  574. };
  575. // Sanity!
  576. AssertFatal( *pLayerNumber != 0, "Scene::writeLayerSortMode() - Could not find the layer index portion of the layer sort mode field." );
  577. // Fetch layer number.
  578. const U32 layer = dAtoi(pLayerNumber);
  579. // Just allow the write if an bad parse.
  580. if ( layer > MAX_LAYERS_SUPPORTED )
  581. return true;
  582. return static_cast<Scene*>(obj)->getLayerSortMode( layer ) != SceneRenderQueue::RENDER_SORT_NEWEST;
  583. }
  584. // Callbacks.
  585. static bool writeUpdateCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getUpdateCallback(); }
  586. static bool writeRenderCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<Scene*>(obj)->getRenderCallback(); }
  587. public:
  588. static SimObjectPtr<Scene> LoadingScene;
  589. };
  590. //-----------------------------------------------------------------------------
  591. extern void findObjectsCallback(SceneObject* pSceneObject, void* storage);
  592. extern void findLayeredObjectsCallback(SceneObject* pSceneObject, void* storage);
  593. #endif // _SCENE_H_