SceneObject.h 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  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_OBJECT_H_
  23. #define _SCENE_OBJECT_H_
  24. #ifndef _SCENE_H_
  25. #include "2d/scene/Scene.h"
  26. #endif
  27. #ifndef _DEBUG_STATS_H_
  28. #include "2d/scene/DebugStats.h"
  29. #endif
  30. #ifndef _SIMBASE_H_
  31. #include "sim/simBase.h"
  32. #endif
  33. #ifndef _MMATH_H_
  34. #include "math/mMath.h"
  35. #endif
  36. #ifndef _SCENE_WINDOW_H_
  37. #include "2d/gui/SceneWindow.h"
  38. #endif
  39. #ifndef _BATCH_RENDER_H_
  40. #include "2d/core/BatchRender.h"
  41. #endif
  42. #ifndef _UTILITY_H_
  43. #include "2d/core/Utility.h"
  44. #endif
  45. #ifndef _PHYSICS_PROXY_H_
  46. #include "2d/scene/PhysicsProxy.h"
  47. #endif
  48. #ifndef _SCENE_RENDER_OBJECT_H_
  49. #include "2d/scene/SceneRenderObject.h"
  50. #endif
  51. #ifndef _BEHAVIOR_COMPONENT_H_
  52. #include "component/behaviors/behaviorComponent.h"
  53. #endif
  54. #ifndef _BEHAVIORINSTANCE_H_
  55. #include "component/behaviors/behaviorInstance.h"
  56. #endif
  57. //-----------------------------------------------------------------------------
  58. struct tDestroyNotification
  59. {
  60. SceneObject* mpSceneObject;
  61. U32 mRefCount;
  62. };
  63. //-----------------------------------------------------------------------------
  64. typedef VectorPtr<b2FixtureDef*> typeCollisionFixtureDefVector;
  65. typedef VectorPtr<b2Fixture*> typeCollisionFixtureVector;
  66. typedef Vector<tDestroyNotification> typeDestroyNotificationVector;
  67. //-----------------------------------------------------------------------------
  68. const S32 GL_INVALID_BLEND_FACTOR = -1;
  69. const S32 INVALID_COLLISION_SHAPE_INDEX = -1;
  70. //-----------------------------------------------------------------------------
  71. extern EnumTable bodyTypeTable;
  72. extern EnumTable srcBlendFactorTable;
  73. extern EnumTable dstBlendFactorTable;
  74. //-----------------------------------------------------------------------------
  75. class SceneObject :
  76. public BehaviorComponent,
  77. public SceneRenderObject,
  78. public PhysicsProxy
  79. {
  80. private:
  81. typedef BehaviorComponent Parent;
  82. public:
  83. friend class Scene;
  84. friend class SceneWindow;
  85. friend class ContactFilter;
  86. friend class WorldQuery;
  87. friend class DebugDraw;
  88. friend class SceneObjectMoveToEvent;
  89. friend class SceneObjectRotateToEvent;
  90. protected:
  91. /// Scene.
  92. SimObjectPtr<Scene> mpScene;
  93. /// Target Scene.
  94. /// NOTE: Unfortunately this is required as the scene can be set via a field which
  95. /// occurs before the object is registered with the simulation therefore
  96. /// any simulation related functionality cannot be used such as TorqueScript
  97. /// callbacks.
  98. SimObjectPtr<Scene> mpTargetScene;
  99. /// Lifetime.
  100. F32 mLifetime;
  101. bool mLifetimeActive;
  102. /// Scene layers.
  103. U32 mSceneLayer;
  104. U32 mSceneLayerMask;
  105. F32 mSceneLayerDepth;
  106. /// Scene groups.
  107. U32 mSceneGroup;
  108. U32 mSceneGroupMask;
  109. /// Area.
  110. Vector2 mSize;
  111. bool mAutoSizing;
  112. b2AABB mPreTickAABB;
  113. b2AABB mCurrentAABB;
  114. Vector2 mLocalSizeOOBB[4];
  115. Vector2 mRenderOOBB[4];
  116. S32 mWorldProxyId;
  117. /// Position / Angle.
  118. Vector2 mPreTickPosition;
  119. F32 mPreTickAngle;
  120. Vector2 mRenderPosition;
  121. F32 mRenderAngle;
  122. bool mSpatialDirty;
  123. /// Body.
  124. b2Body* mpBody;
  125. b2BodyDef mBodyDefinition;
  126. U32 mWorldQueryKey;
  127. /// Collision control.
  128. U32 mCollisionLayerMask;
  129. U32 mCollisionGroupMask;
  130. bool mCollisionSuppress;
  131. b2FixtureDef mDefaultFixture;
  132. bool mGatherContacts;
  133. Scene::typeContactVector* mpCurrentContacts;
  134. /// General collision shape access.
  135. typeCollisionFixtureDefVector mCollisionFixtureDefs;
  136. typeCollisionFixtureVector mCollisionFixtures;
  137. /// Render visibility.
  138. bool mVisible;
  139. /// Render blending.
  140. bool mBlendMode;
  141. S32 mSrcBlendFactor;
  142. S32 mDstBlendFactor;
  143. ColorF mBlendColor;
  144. F32 mAlphaTest;
  145. /// Render sorting.
  146. Vector2 mSortPoint;
  147. /// Input events.
  148. bool mUseInputEvents;
  149. /// Script callbacks.
  150. bool mUpdateCallback;
  151. bool mCollisionCallback;
  152. bool mSleepingCallback;
  153. bool mLastAwakeState;
  154. /// Debug mode.
  155. U32 mDebugMask;
  156. /// Camera mounting.
  157. SceneWindow* mpAttachedCamera;
  158. /// GUI attachment.
  159. bool mAttachedGuiSizeControl;
  160. GuiControl* mpAttachedGui;
  161. SceneWindow* mpAttachedGuiSceneWindow;
  162. /// Safe deletion.
  163. bool mBeingSafeDeleted;
  164. bool mSafeDeleteReady;
  165. /// Destroy notifications.
  166. typeDestroyNotificationVector mDestroyNotifyList;
  167. /// Miscellaneous.
  168. bool mBatchIsolated;
  169. U32 mSerialiseKey;
  170. bool mEditorTickAllowed;
  171. bool mPickingAllowed;
  172. bool mAlwaysInScope;
  173. U32 mMoveToEventId;
  174. U32 mRotateToEventId;
  175. U32 mSerialId;
  176. StringTableEntry mRenderGroup;
  177. protected:
  178. static S32 QSORT_CALLBACK sceneObjectLayerDepthSort(const void* a, const void* b);
  179. /// Scene (un)registering.
  180. virtual void OnRegisterScene( Scene* pScene );
  181. virtual void OnUnregisterScene( Scene* pScene );
  182. /// Ticking.
  183. void resetTickSpatials( const bool resize = false );
  184. inline bool getSpatialDirty( void ) const { return mSpatialDirty; }
  185. /// Contact processing.
  186. void initializeContactGathering( void );
  187. /// Taml callbacks.
  188. virtual void onTamlCustomWrite( TamlCustomNodes& customNodes );
  189. virtual void onTamlCustomRead( const TamlCustomNodes& customNodes );
  190. public:
  191. SceneObject();
  192. virtual ~SceneObject();
  193. /// Engine.
  194. virtual bool onAdd();
  195. virtual void onRemove();
  196. virtual void onDestroyNotify( SceneObject* pSceneObject );
  197. static void initPersistFields();
  198. /// Integration.
  199. virtual void preIntegrate( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  200. virtual void integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  201. virtual void postIntegrate(const F32 totalTime, const F32 elapsedTime, DebugStats *pDebugStats);
  202. virtual void interpolateObject( const F32 timeDelta );
  203. inline bool getIsEditorTickAllowed( void ) const { return mEditorTickAllowed; }
  204. /// Render batching.
  205. inline void setBatchIsolated( const bool batchIsolated ) { mBatchIsolated = batchIsolated; }
  206. virtual bool getBatchIsolated( void ) { return mBatchIsolated; }
  207. virtual bool isBatchRendered( void ) { return true; }
  208. virtual bool validRender( void ) const { return true; }
  209. virtual bool shouldRender( void ) const { return false; }
  210. /// Render Output.
  211. virtual bool canPrepareRender( void ) const { return false; }
  212. virtual void scenePrepareRender( const SceneRenderState* pSceneRenderState, SceneRenderQueue* pSceneRenderQueue ) {}
  213. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer ) {}
  214. virtual void sceneRenderFallback( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  215. virtual void sceneRenderOverlay( const SceneRenderState* pSceneRenderState );
  216. /// Networking.
  217. virtual U32 packUpdate(NetConnection * conn, U32 mask, BitStream *stream);
  218. virtual void unpackUpdate(NetConnection * conn, BitStream *stream);
  219. /// Scene.
  220. inline Scene* const getScene( void ) const { return mpScene; }
  221. inline F32 getSceneTime( void ) const { if ( mpScene ) return mpScene->getSceneTime(); else return 0.0f; }
  222. /// Enabled.
  223. virtual void setEnabled( const bool enabled );
  224. /// Lifetime.
  225. void setLifetime( const F32 lifetime );
  226. inline F32 getLifetime( void ) const { return mLifetime; }
  227. void updateLifetime( const F32 elapsedTime );
  228. /// Scene layers.
  229. void setSceneLayer( const U32 sceneLayer );
  230. inline U32 getSceneLayer(void) const { return mSceneLayer; }
  231. inline U32 getSceneLayerMask( void ) const { return mSceneLayerMask; }
  232. /// Scene Layer depth.
  233. inline void setSceneLayerDepth( const F32 order ) { mSceneLayerDepth = order; };
  234. inline F32 getSceneLayerDepth( void ) const { return mSceneLayerDepth; }
  235. bool setSceneLayerDepthFront( void );
  236. bool setSceneLayerDepthBack( void );
  237. bool setSceneLayerDepthForward( void );
  238. bool setSceneLayerDepthBackward( void );
  239. /// Scene groups.
  240. void setSceneGroup( const U32 sceneGroup );
  241. inline U32 getSceneGroup(void ) const { return mSceneGroup; }
  242. inline U32 getSceneGroupMask( void ) const { return mSceneGroupMask; }
  243. /// Area.
  244. virtual void setArea( const Vector2& corner1, const Vector2& corner2 );
  245. inline bool getAutoSizing( void ) const { return mAutoSizing; }
  246. virtual void setSize( const Vector2& size );
  247. inline void setSize( const F32 width, const F32 height ){ setSize( Vector2(width, height) ); }
  248. inline Vector2 getSize( void ) const { return mSize; }
  249. inline Vector2 getHalfSize( void ) const { return mSize * 0.5f; }
  250. inline b2AABB getAABB( void ) const { return mCurrentAABB; }
  251. inline RectF getAABBRectangle( void ) const { const b2Vec2 size = mCurrentAABB.upperBound-mCurrentAABB.lowerBound; return RectF( mCurrentAABB.lowerBound.x, mCurrentAABB.lowerBound.y, size.x, size.y ); }
  252. inline S32 getWorldProxy( void ) const { return mWorldProxyId; }
  253. /// Position / Angle.
  254. virtual void setPosition( const Vector2& position );
  255. inline Vector2 getPosition(void) const { if ( mpScene ) return mpBody->GetPosition(); else return mBodyDefinition.position; }
  256. inline Vector2 getRenderPosition(void) const { return mRenderPosition; }
  257. inline F32 getRenderAngle(void) const { return mRenderAngle; }
  258. inline const b2Vec2* getRenderOOBB(void) const { return mRenderOOBB; }
  259. inline const b2Vec2* getLocalSizedOOBB( void ) const { return mLocalSizeOOBB; }
  260. virtual void setAngle( const F32 radians );
  261. inline F32 getAngle(void) const { if ( mpScene ) return mpBody->GetAngle(); else return mBodyDefinition.angle; }
  262. virtual void setFixedAngle( const bool fixed ) { if ( mpScene ) mpBody->SetFixedRotation( fixed ); else mBodyDefinition.fixedRotation = fixed; }
  263. inline bool getFixedAngle(void) const { if ( mpScene ) return mpBody->IsFixedRotation(); else return mBodyDefinition.fixedRotation; }
  264. b2Transform getTransform( void ) const { if ( mpScene ) return mpBody->GetTransform(); else return b2Transform( mBodyDefinition.position, b2Rot(mBodyDefinition.angle) ); }
  265. b2Transform getRenderTransform( void ) const { return b2Transform( getRenderPosition(), b2Rot( getRenderAngle()) ); }
  266. inline Vector2 getLocalCenter(void) const { if ( mpScene ) return mpBody->GetLocalCenter(); else return b2Vec2_zero; }
  267. inline Vector2 getWorldCenter(void) const { if ( mpScene ) return mpBody->GetWorldCenter(); else return mBodyDefinition.position; }
  268. Vector2 getLocalPoint( const Vector2& worldPoint );
  269. Vector2 getWorldPoint( const Vector2& localPoint );
  270. Vector2 getLocalVector( const Vector2& worldVector );
  271. Vector2 getWorldVector( const Vector2& localVector );
  272. bool getIsPointInOOBB( const Vector2& worldPoint );
  273. bool getIsPointInCollisionShape( const U32 shapeIndex, const Vector2& worldPoint );
  274. /// Body.
  275. virtual ePhysicsProxyType getPhysicsProxyType( void ) const { return PhysicsProxy::PHYSIC_PROXY_SCENEOBJECT; }
  276. inline b2Body* getBody( void ) const { return mpBody; }
  277. void setBodyType( const b2BodyType type );
  278. inline b2BodyType getBodyType(void) const { if ( mpScene ) return mpBody->GetType(); else return mBodyDefinition.type; }
  279. inline void setActive( const bool active ) { if ( mpScene ) mpBody->SetActive( active ); else mBodyDefinition.active = active; }
  280. inline bool getActive(void) const { if ( mpScene ) return mpBody->IsActive(); else return mBodyDefinition.active; }
  281. inline void setAwake( const bool awake ) { if ( mpScene ) mpBody->SetAwake( awake ); else mBodyDefinition.awake = awake; }
  282. inline bool getAwake(void) const { if ( mpScene ) return mpBody->IsAwake(); else return mBodyDefinition.awake; }
  283. inline void setBullet( const bool bullet ) { if ( mpScene ) mpBody->SetBullet( bullet ); else mBodyDefinition.bullet = bullet; }
  284. inline bool getBullet(void) const { if ( mpScene ) return mpBody->IsBullet(); else return mBodyDefinition.bullet; }
  285. inline void setSleepingAllowed( const bool allowed ) { if ( mpScene ) mpBody->SetSleepingAllowed( allowed ); else mBodyDefinition.allowSleep = allowed; }
  286. inline bool getSleepingAllowed(void) const { if ( mpScene ) return mpBody->IsSleepingAllowed(); else return mBodyDefinition.allowSleep; }
  287. inline F32 getMass( void ) const { if ( mpScene ) return mpBody->GetMass(); else return 0.0f; }
  288. inline F32 getInertia( void ) const { if ( mpScene ) return mpBody->GetInertia(); else return 0.0f; }
  289. /// Collision control.
  290. void setCollisionAgainst( const SceneObject* pSceneObject, const bool clearMasks );
  291. inline void setCollisionGroupMask( const U32 groupMask ) { mCollisionGroupMask = groupMask; }
  292. inline U32 getCollisionGroupMask(void) const { return mCollisionGroupMask; }
  293. inline void setCollisionLayerMask( const U32 layerMask ) { mCollisionLayerMask = layerMask; }
  294. inline U32 getCollisionLayerMask(void) const { return mCollisionLayerMask; }
  295. void setDefaultDensity( const F32 density, const bool updateShapes = true );
  296. inline F32 getDefaultDensity( void ) const { return mDefaultFixture.density; }
  297. void setDefaultFriction( const F32 friction, const bool updateShapes = true );
  298. inline F32 getDefaultFriction( void ) const { return mDefaultFixture.friction; }
  299. void setDefaultRestitution( const F32 restitution, const bool updateShapes = true );
  300. inline F32 getDefaultRestitution( void ) const { return mDefaultFixture.restitution; }
  301. inline void setCollisionSuppress( const bool status ) { mCollisionSuppress = status; }
  302. inline bool getCollisionSuppress(void) const { return mCollisionSuppress; }
  303. inline const Scene::typeContactVector* getCurrentContacts( void ) const { return mpCurrentContacts; }
  304. inline U32 getCurrentContactCount( void ) const { if ( mpCurrentContacts != NULL ) return mpCurrentContacts->size(); else return 0; }
  305. virtual void setGatherContacts( const bool gatherContacts ) { mGatherContacts = gatherContacts; initializeContactGathering(); }
  306. inline bool getGatherContacts( void ) const { return mGatherContacts; }
  307. virtual void onBeginCollision( const TickContact& tickContact );
  308. virtual void onEndCollision( const TickContact& tickContact );
  309. /// Velocities.
  310. inline void setLinearVelocity( const Vector2& velocity ) { if ( mpScene ) mpBody->SetLinearVelocity( velocity ); else mBodyDefinition.linearVelocity = velocity; }
  311. inline Vector2 getLinearVelocity(void) const { if ( mpScene ) return mpBody->GetLinearVelocity(); else return mBodyDefinition.linearVelocity; }
  312. inline Vector2 getLinearVelocityFromWorldPoint( const Vector2& worldPoint ) { if ( mpScene ) return mpBody->GetLinearVelocityFromWorldPoint( worldPoint ); else return mBodyDefinition.linearVelocity; }
  313. inline Vector2 getLinearVelocityFromLocalPoint( const Vector2& localPoint ) { if ( mpScene ) return mpBody->GetLinearVelocityFromLocalPoint( localPoint ); else return mBodyDefinition.linearVelocity; }
  314. inline void setAngularVelocity( const F32 velocity ) { if ( mpScene ) mpBody->SetAngularVelocity( velocity ); else mBodyDefinition.angularVelocity = velocity; }
  315. inline F32 getAngularVelocity(void) const { if ( mpScene ) return mpBody->GetAngularVelocity(); else return mBodyDefinition.angularVelocity; }
  316. inline void setLinearDamping( const F32 damping ) { if ( mpScene ) mpBody->SetLinearDamping( damping ); else mBodyDefinition.linearDamping = damping; }
  317. inline F32 getLinearDamping(void) const { if ( mpScene ) return mpBody->GetLinearDamping(); else return mBodyDefinition.linearDamping; }
  318. inline void setAngularDamping( const F32 damping ) { if ( mpScene ) mpBody->SetAngularDamping( damping ); else mBodyDefinition.angularDamping = damping; }
  319. inline F32 getAngularDamping(void) const { if ( mpScene ) return mpBody->GetAngularDamping(); else return mBodyDefinition.angularDamping; }
  320. /// Move/Rotate to.
  321. bool moveTo( const Vector2& targetWorldPoint, const F32 speed, const bool autoStop = true, const bool warpToTarget = true );
  322. bool rotateTo( const F32 targetAngle, const F32 speed, const bool autoStop = true, const bool warpToTarget = true );
  323. void cancelMoveTo( const bool autoStop = true );
  324. void cancelRotateTo( const bool autoStop = true );
  325. inline bool isMoveToComplete( void ) const { return mMoveToEventId == 0; }
  326. inline bool isRotateToComplete( void ) const { return mRotateToEventId == 0; }
  327. /// Force and impulse.
  328. void applyForce( const Vector2& worldForce, const bool wake = true );
  329. void applyForce( const Vector2& worldForce, const Vector2& worldPoint, const bool wake = true);
  330. void applyTorque( const F32 torque, const bool wake = true );
  331. void applyLinearImpulse( const Vector2& worldImpulse, const bool wake = true );
  332. void applyLinearImpulse( const Vector2& worldImpulse, const Vector2& worldPoint, const bool wake = true );
  333. void applyAngularImpulse( const F32 impulse, const bool wake = true );
  334. /// Gravity scaling.
  335. inline void setGravityScale( const F32 scale ) { if ( mpScene ) mpBody->SetGravityScale( scale ); else mBodyDefinition.gravityScale = scale; }
  336. inline F32 getGravityScale(void) const { if ( mpScene ) return mpBody->GetGravityScale(); else return mBodyDefinition.gravityScale; }
  337. /// General collision shape access.
  338. void deleteCollisionShape( const U32 shapeIndex );
  339. void clearCollisionShapes( void );
  340. inline U32 getCollisionShapeCount( void ) const { if ( mpScene ) return mCollisionFixtures.size(); else return mCollisionFixtureDefs.size(); }
  341. b2Shape::Type getCollisionShapeType( const U32 shapeIndex ) const;
  342. S32 getCollisionShapeIndex( const b2Fixture* pFixture ) const;
  343. void setCollisionShapeDefinition( const U32 shapeIndex, const b2FixtureDef& fixtureDef );
  344. b2FixtureDef getCollisionShapeDefinition( const U32 shapeIndex ) const;
  345. const b2CircleShape* getCollisionCircleShape( const U32 shapeIndex ) const;
  346. const b2PolygonShape* getCollisionPolygonShape( const U32 shapeIndex ) const;
  347. const b2ChainShape* getCollisionChainShape( const U32 shapeIndex ) const;
  348. const b2EdgeShape* getCollisionEdgeShape( const U32 shapeIndex ) const;
  349. void setCollisionShapeDensity( const U32 shapeIndex, const F32 density );
  350. F32 getCollisionShapeDensity( const U32 shapeIndex ) const;
  351. void setCollisionShapeFriction( const U32 shapeIndex, const F32 friction );
  352. F32 getCollisionShapeFriction( const U32 shapeIndex ) const;
  353. void setCollisionShapeRestitution( const U32 shapeIndex, const F32 restitution );
  354. F32 getCollisionShapeRestitution( const U32 shapeIndex ) const;
  355. void setCollisionShapeIsSensor( const U32 shapeIndex, const bool isSensor );
  356. bool getCollisionShapeIsSensor( const U32 shapeIndex ) const;
  357. /// Circle collision shape creation.
  358. S32 createCircleCollisionShape( const F32 radius );
  359. S32 createCircleCollisionShape( const F32 radius, const b2Vec2& localPosition );
  360. /// Circle collision shape access.
  361. F32 getCircleCollisionShapeRadius( const U32 shapeIndex ) const;
  362. Vector2 getCircleCollisionShapeLocalPosition( const U32 shapeIndex ) const;
  363. /// Polygon collision shape creation.
  364. S32 createPolygonCollisionShape( const U32 pointCount, const b2Vec2* localPoints );
  365. S32 createPolygonBoxCollisionShape( const F32 width, const F32 height );
  366. S32 createPolygonBoxCollisionShape( const F32 width, const F32 height, const b2Vec2& localCentroid );
  367. S32 createPolygonBoxCollisionShape( const F32 width, const F32 height, const b2Vec2& localCentroid, const F32 rotation );
  368. /// Polygon collision shape access.
  369. U32 getPolygonCollisionShapePointCount( const U32 shapeIndex ) const;
  370. Vector2 getPolygonCollisionShapeLocalPoint( const U32 shapeIndex, const U32 pointIndex ) const;
  371. /// Chain collision shape creation.
  372. S32 createChainCollisionShape( const U32 pointCount, const b2Vec2* localPoints );
  373. S32 createChainCollisionShape( const U32 pointCount, const b2Vec2* localPoints,
  374. const bool hasAdjacentLocalPositionStart, const bool hasAdjacentLocalPositionEnd,
  375. const b2Vec2& adjacentLocalPositionStart, const b2Vec2& adjacentLocalPositionEnd );
  376. /// Chain collision shape access.
  377. U32 getChainCollisionShapePointCount( const U32 shapeIndex ) const;
  378. Vector2 getChainCollisionShapeLocalPoint( const U32 shapeIndex, const U32 pointIndex ) const;
  379. bool getChainCollisionShapeHasAdjacentStart( const U32 shapeIndex ) const;
  380. bool getChainCollisionShapeHasAdjacentEnd( const U32 shapeIndex ) const;
  381. Vector2 getChainCollisionShapeAdjacentStart( const U32 shapeIndex ) const;
  382. Vector2 getChainCollisionShapeAdjacentEnd( const U32 shapeIndex ) const;
  383. /// Edge collision shape creation.
  384. S32 createEdgeCollisionShape( const b2Vec2& localPositionStart, const b2Vec2& localPositionEnd );
  385. S32 createEdgeCollisionShape( const b2Vec2& localPositionStart, const b2Vec2& localPositionEnd,
  386. const bool hasAdjacentLocalPositionStart, const bool hasAdjacentLocalPositionEnd,
  387. const b2Vec2& adjacentLocalPositionStart, const b2Vec2& adjacentLocalPositionEnd );
  388. /// Edge collision shape access.
  389. Vector2 getEdgeCollisionShapeLocalPositionStart( const U32 shapeIndex ) const;
  390. Vector2 getEdgeCollisionShapeLocalPositionEnd( const U32 shapeIndex ) const;
  391. bool getEdgeCollisionShapeHasAdjacentStart( const U32 shapeIndex ) const;
  392. bool getEdgeCollisionShapeHasAdjacentEnd( const U32 shapeIndex ) const;
  393. Vector2 getEdgeCollisionShapeAdjacentStart( const U32 shapeIndex ) const;
  394. Vector2 getEdgeCollisionShapeAdjacentEnd( const U32 shapeIndex ) const;
  395. /// Render visibility.
  396. inline void setVisible( const bool status ) { mVisible = status; }
  397. inline bool getVisible(void) const { return mVisible; }
  398. /// Render blending.
  399. inline void setBlendMode( const bool blendMode ) { mBlendMode = blendMode; }
  400. inline bool getBlendMode( void ) const { return mBlendMode; }
  401. inline void setSrcBlendFactor( const S32 blendFactor ) { mSrcBlendFactor = blendFactor; }
  402. inline S32 getSrcBlendFactor( void ) const { return mSrcBlendFactor; }
  403. inline void setDstBlendFactor( const S32 blendFactor ) { mDstBlendFactor = blendFactor; }
  404. inline S32 getDstBlendFactor( void ) const { return mDstBlendFactor; }
  405. inline void setBlendColor( const ColorF& blendColor ) { mBlendColor = blendColor; }
  406. inline const ColorF& getBlendColor( void ) const { return mBlendColor; }
  407. inline void setBlendAlpha( const F32 alpha ) { mBlendColor.alpha = alpha; }
  408. inline F32 getBlendAlpha( void ) const { return mBlendColor.alpha; }
  409. inline void setAlphaTest( const F32 alpha ) { mAlphaTest = alpha; }
  410. inline F32 getAlphaTest( void ) const { return mAlphaTest; }
  411. void setBlendOptions( void );
  412. static void resetBlendOptions( void );
  413. /// Render sorting.
  414. inline void setSortPoint( const Vector2& pt ) { mSortPoint = pt; }
  415. inline const Vector2& getSortPoint(void) const { return mSortPoint; }
  416. inline void setRenderGroup( const char* pRenderGroup ) { mRenderGroup = StringTable->insert(pRenderGroup); }
  417. inline StringTableEntry getRenderGroup( void ) const { return mRenderGroup; }
  418. /// Input events.
  419. inline void setUseInputEvents( bool mouseStatus ) { mUseInputEvents = mouseStatus; }
  420. inline bool getUseInputEvents( void ) const { return mUseInputEvents; }
  421. virtual void onInputEvent( StringTableEntry name, const GuiEvent& event, const Vector2& worldMousePoint );
  422. // Script callbacks.
  423. inline void setUpdateCallback( bool status ) { mUpdateCallback = status; }
  424. inline bool getUpdateCallback( void ) const { return mUpdateCallback; }
  425. inline void setCollisionCallback( const bool status ) { mCollisionCallback = status; }
  426. inline bool getCollisionCallback(void) const { return mCollisionCallback; }
  427. inline void setSleepingCallback( bool status ) { mSleepingCallback = status; }
  428. inline bool getSleepingCallback( void ) const { return mSleepingCallback; }
  429. /// Debug mode.
  430. inline void setDebugOn( const U32 debugMask ) { mDebugMask |= debugMask; }
  431. inline void setDebugOff( const U32 debugMask ) { mDebugMask &= ~debugMask; }
  432. inline U32 getDebugMask( void ) const { return mDebugMask; }
  433. /// Camera mounting.
  434. inline void addCameraMountReference( SceneWindow* pAttachedCamera ) { mpAttachedCamera = pAttachedCamera; }
  435. inline void removeCameraMountReference( void ) { mpAttachedCamera = NULL; }
  436. inline void dismountCamera( void ) { if ( mpAttachedCamera ) mpAttachedCamera->dismountMe( this ); }
  437. // GUI attachment.
  438. void attachGui( GuiControl* pGuiControl, SceneWindow* pSceneWindow, const bool sizeControl );
  439. void detachGui( void );
  440. inline void updateAttachedGui( void );
  441. // Picking.
  442. inline void setPickingAllowed( const bool pickingAllowed ) { mPickingAllowed = pickingAllowed; }
  443. inline bool getPickingAllowed(void) const { return mPickingAllowed; }
  444. /// Cloning.
  445. virtual void copyFrom( SceneObject* pSceneObject, const bool copyDynamicFields );
  446. virtual void copyTo( SimObject* object );
  447. S32 copyCollisionShapes( SceneObject* pSceneObject, const bool clearTargetShapes = true, const S32 shapeIndex = -1 );
  448. /// Safe deletion.
  449. inline void setSafeDelete( const bool status ) { mSafeDeleteReady = status; }
  450. inline bool getSafeDelete( void ) const { return mSafeDeleteReady; }
  451. inline bool isBeingDeleted( void ) const { return mBeingSafeDeleted; }
  452. virtual void safeDelete( void );
  453. /// Destroy notifications.
  454. void addDestroyNotification( SceneObject* pSceneObject );
  455. void removeDestroyNotification( SceneObject* pSceneObject );
  456. void processDestroyNotifications( void );
  457. /// Component notifications.
  458. void notifyComponentsAddToScene( void );
  459. void notifyComponentsRemoveFromScene( void );
  460. void notifyComponentsUpdate( void );
  461. /// Miscellaneous.
  462. inline const char* scriptThis(void) const { return Con::getIntArg(getId()); }
  463. inline bool getIsAlwaysInScope(void) const { return mAlwaysInScope; }
  464. inline void setWorldQueryKey( const U32 key ) { mWorldQueryKey = key; }
  465. inline U32 getWorldQueryKey( void ) const { return mWorldQueryKey; }
  466. static U32 getGlobalSceneObjectCount( void );
  467. inline U32 getSerialId( void ) const { return mSerialId; }
  468. // Read / Write fields.
  469. virtual bool writeField(StringTableEntry fieldname, const char* value);
  470. static b2BodyType getBodyTypeEnum(const char* label);
  471. static const char* getBodyTypeDescription(const b2BodyType bodyType);
  472. static b2Shape::Type getCollisionShapeTypeEnum(const char* label);
  473. static const char* getCollisionShapeTypeDescription(const b2Shape::Type collisionShapeType);
  474. static S32 getSrcBlendFactorEnum(const char* label);
  475. static S32 getDstBlendFactorEnum(const char* label);
  476. static const char* getSrcBlendFactorDescription(const GLenum factor);
  477. static const char* getDstBlendFactorDescription(const GLenum factor);
  478. /// Declare Console Object.
  479. DECLARE_CONOBJECT( SceneObject );
  480. protected:
  481. S32 copyCircleCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const;
  482. S32 copyPolygonCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const;
  483. S32 copyChainCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const;
  484. S32 copyEdgeCollisionShapeTo( SceneObject* pSceneObject, const b2FixtureDef& fixtureDef ) const;
  485. protected:
  486. /// Lifetime.
  487. static bool setLifetime(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setLifetime(dAtof(data)); return false; }
  488. static bool writeLifetime( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getLifetime() > 0.0f ; }
  489. /// Scene layers.
  490. static bool setSceneLayer(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setSceneLayer(dAtoi(data)); return false; }
  491. static bool writeSceneLayer( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSceneLayer() > 0 ; }
  492. static bool setSceneLayerDepth(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setSceneLayerDepth(dAtof(data)); return false; }
  493. static bool writeSceneLayerDepth( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<SceneObject*>(obj)->getSceneLayerDepth()); }
  494. /// Scene groups.
  495. static bool setSceneGroup(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setSceneGroup(dAtoi(data)); return false; }
  496. static bool writeSceneGroup( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSceneGroup() > 0 ; }
  497. /// Area.
  498. static bool setSize(void* obj, const char* data)
  499. {
  500. SceneObject* pSceneObject = static_cast<SceneObject*>(obj);
  501. if ( pSceneObject->getAutoSizing() )
  502. {
  503. Con::warnf( "Cannot set the size of a type '%s' as it automatically sizes itself.", pSceneObject->getClassName() );
  504. return false;
  505. }
  506. pSceneObject->setSize(Vector2(data));
  507. return false;
  508. }
  509. static bool writeSize( void* obj, StringTableEntry pFieldName ) { SceneObject* pSceneObject = static_cast<SceneObject*>(obj); return !pSceneObject->getAutoSizing() && pSceneObject->getSize().notEqual(Vector2::getOne()); }
  510. /// Position / Angle.
  511. static bool setPosition(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setPosition(Vector2(data)); return false; }
  512. static const char* getPosition(void* obj, const char* data) { return static_cast<SceneObject*>(obj)->getPosition().scriptThis(); }
  513. static bool writePosition( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getPosition().notZero(); }
  514. static bool setAngle(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setAngle(mDegToRad(dAtof(data))); return false; }
  515. static const char* getAngle(void* obj, const char* data) { return Con::getFloatArg( mRadToDeg(static_cast<SceneObject*>(obj)->getAngle() ) ); }
  516. static bool writeAngle( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<SceneObject*>(obj)->getAngle()); }
  517. static bool setFixedAngle(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setFixedAngle(dAtob(data)); return false; }
  518. static const char* getFixedAngle(void* obj, const char* data) { return Con::getBoolArg( static_cast<SceneObject*>(obj)->getFixedAngle() ); }
  519. static bool writeFixedAngle( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getFixedAngle() == true; }
  520. /// Body.
  521. static bool setBodyType(void* obj, const char* data)
  522. {
  523. // Fetch body type.
  524. const b2BodyType type = getBodyTypeEnum( data );
  525. // Check for error.
  526. if ( type != b2_staticBody && type != b2_kinematicBody && type != b2_dynamicBody )
  527. return false;
  528. static_cast<SceneObject*>(obj)->setBodyType(type);
  529. return false;
  530. }
  531. static const char* getBodyType(void* obj, const char* data) { return getBodyTypeDescription( static_cast<SceneObject*>(obj)->getBodyType() ); }
  532. static bool writeBodyType( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getBodyType() != b2_dynamicBody; }
  533. static bool setActive(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setActive(dAtob(data)); return false; }
  534. static const char* getActive(void* obj, const char* data) { return Con::getBoolArg( static_cast<SceneObject*>(obj)->getActive() ); }
  535. static bool writeActive( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getActive() == false; }
  536. static bool setAwake(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setAwake(dAtob(data)); return false; }
  537. static const char* getAwake(void* obj, const char* data) { return Con::getBoolArg( static_cast<SceneObject*>(obj)->getAwake() ); }
  538. static bool writeAwake( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getAwake() == false; }
  539. static bool setBullet(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setBullet(dAtob(data)); return false; }
  540. static const char* getBullet(void* obj, const char* data) { return Con::getBoolArg( static_cast<SceneObject*>(obj)->getBullet() ); }
  541. static bool writeBullet( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getBullet() == true; }
  542. static bool setSleepingAllowed(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setSleepingAllowed(dAtob(data)); return false; }
  543. static const char* getSleepingAllowed(void* obj, const char* data) { return Con::getBoolArg( static_cast<SceneObject*>(obj)->getSleepingAllowed() ); }
  544. static bool writeSleepingAllowed( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSleepingAllowed() == false; }
  545. /// Collision control.
  546. static bool setDefaultDensity(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setDefaultDensity(dAtof(data)); return false; }
  547. static bool writeDefaultDensity( void* obj, StringTableEntry pFieldName ) { return mNotEqual(static_cast<SceneObject*>(obj)->getDefaultDensity(), 1.0f); }
  548. static bool setDefaultFriction(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setDefaultFriction(dAtof(data)); return false; }
  549. static bool writeDefaultFriction( void* obj, StringTableEntry pFieldName ) {return mNotEqual(static_cast<SceneObject*>(obj)->getDefaultFriction(), 0.2f); }
  550. static bool setDefaultRestitution(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setDefaultRestitution(dAtof(data)); return false; }
  551. static bool writeDefaultRestitution( void* obj, StringTableEntry pFieldName ) { return mNotEqual(static_cast<SceneObject*>(obj)->getDefaultRestitution(), 0.0f); }
  552. static bool setCollisionGroups(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setCollisionGroupMask(dAtoi(data)); return false; }
  553. static bool writeCollisionGroups( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getCollisionGroupMask() != MASK_ALL; }
  554. static bool setCollisionLayers(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setCollisionLayerMask(dAtoi(data)); return false; }
  555. static bool writeCollisionLayers( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getCollisionLayerMask() != MASK_ALL; }
  556. static bool writeCollisionSuppress( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getCollisionSuppress() == true; }
  557. static bool setGatherContacts(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setGatherContacts(dAtoi(data)); return false; }
  558. static bool writeGatherContacts( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getGatherContacts() == true; }
  559. /// Velocities.
  560. static bool setLinearVelocity(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setLinearVelocity(Vector2(data)); return false; }
  561. static const char* getLinearVelocity(void* obj, const char* data) { return static_cast<SceneObject*>(obj)->getLinearVelocity().scriptThis(); }
  562. static bool writeLinearVelocity( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getLinearVelocity().notZero(); }
  563. static bool setAngularVelocity(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setAngularVelocity(mDegToRad(dAtof(data))); return false; }
  564. static const char* getAngularVelocity(void* obj, const char* data) { return Con::getFloatArg( mRadToDeg(static_cast<SceneObject*>(obj)->getAngularVelocity() ) ); }
  565. static bool writeAngularVelocity( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<SceneObject*>(obj)->getAngularVelocity()); }
  566. static bool setLinearDamping(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setLinearDamping(dAtof(data)); return false; }
  567. static const char* getLinearDamping(void* obj, const char* data) { return Con::getFloatArg( static_cast<SceneObject*>(obj)->getLinearDamping() ); }
  568. static bool writeLinearDamping( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<SceneObject*>(obj)->getLinearDamping()); }
  569. static bool setAngularDamping(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setAngularDamping(dAtof(data)); return false; }
  570. static const char* getAngularDamping(void* obj, const char* data) { return Con::getFloatArg( static_cast<SceneObject*>(obj)->getAngularDamping() ); }
  571. static bool writeAngularDamping( void* obj, StringTableEntry pFieldName ) { return mNotZero(static_cast<SceneObject*>(obj)->getAngularDamping()); }
  572. /// Gravity scaling.
  573. static bool setGravityScale(void* obj, const char* data) { static_cast<SceneObject*>(obj)->setGravityScale(dAtof(data)); return false; }
  574. static const char* getGravityScale(void* obj, const char* data) { return Con::getFloatArg( static_cast<SceneObject*>(obj)->getGravityScale() ); }
  575. static bool writeGravityScale( void* obj, StringTableEntry pFieldName ) { return mNotEqual(static_cast<SceneObject*>(obj)->getGravityScale(), 1.0f); }
  576. /// Render visibility.
  577. static bool writeVisible( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getVisible() == false; }
  578. /// Render blending.
  579. static bool writeBlendMode( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getBlendMode() == false; }
  580. static bool writeSrcBlendFactor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSrcBlendFactor() != GL_SRC_ALPHA; }
  581. static bool writeDstBlendFactor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getDstBlendFactor() != GL_ONE_MINUS_SRC_ALPHA; }
  582. static bool writeBlendColor( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getBlendColor() != ColorF(1.0f, 1.0f, 1.0f, 1.0f); }
  583. static bool writeAlphaTest( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getAlphaTest() >= 0.0f; }
  584. /// Render sorting.
  585. static bool writeSortPoint( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSortPoint().notZero(); }
  586. static bool writeRenderGroup( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getRenderGroup() != StringTable->EmptyString; }
  587. /// Input events.
  588. static bool writeUseInputEvents( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getUseInputEvents() == true; }
  589. /// Picking.
  590. static bool writePickingAllowed( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getPickingAllowed() == false; }
  591. /// Script callbacks.
  592. static bool writeUpdateCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getUpdateCallback() == true; }
  593. static bool writeCollisionCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getCollisionCallback() == true; }
  594. static bool writeSleepingCallback( void* obj, StringTableEntry pFieldName ) { return static_cast<SceneObject*>(obj)->getSleepingCallback() == true; }
  595. /// Scene.
  596. static bool setScene(void* obj, const char* data)
  597. {
  598. Scene* pScene = dynamic_cast<Scene*>(Sim::findObject(data));
  599. SceneObject* object = static_cast<SceneObject*>(obj);
  600. if (pScene)
  601. {
  602. if (object->getScene()) object->getScene()->removeFromScene(object);
  603. // is the scene object registered?
  604. if ( object->isProperlyAdded() )
  605. {
  606. // Yes, so we can add to the scene now.
  607. pScene->addToScene(object);
  608. }
  609. else
  610. {
  611. // No, so just set the target scene directly and it will be added to that scene when registered.
  612. object->mpTargetScene = pScene;
  613. }
  614. }
  615. return false;
  616. }
  617. static bool writeScene( void* obj, StringTableEntry pFieldName ) { return false; }
  618. };
  619. #endif // _SCENE_OBJECT_H_