CCPhysicsSprite.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  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 deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * 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 FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #ifndef __PHYSICSNODES_CCPHYSICSSPRITE_H__
  23. #define __PHYSICSNODES_CCPHYSICSSPRITE_H__
  24. #include "2d/CCSprite.h"
  25. #include "extensions/ExtensionMacros.h"
  26. #include "extensions/ExtensionExport.h"
  27. #include "base/CCEventListenerCustom.h"
  28. #if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
  29. struct cpBody;
  30. class b2Body;
  31. NS_CC_EXT_BEGIN
  32. /** A Sprite subclass that is bound to a physics body.
  33. It works with:
  34. - Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined
  35. - Objective-Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined
  36. - Box2d: Preprocessor macro CC_ENABLE_BOX2D_INTEGRATION should be defined
  37. Features and Limitations:
  38. - Scale and Skew properties are ignored.
  39. - Position and rotation are going to updated from the physics body
  40. - If you update the rotation or position manually, the physics body will be updated
  41. - You can't enble both Chipmunk support and Box2d support at the same time. Only one can be enabled at compile time
  42. * @lua NA
  43. */
  44. class CC_EX_DLL PhysicsSprite : public Sprite
  45. {
  46. public:
  47. static PhysicsSprite* create();
  48. /** Creates an sprite with a texture.
  49. The rect used will be the size of the texture.
  50. The offset will be (0,0).
  51. */
  52. static PhysicsSprite* createWithTexture(Texture2D *pTexture);
  53. /** Creates an sprite with a texture and a rect.
  54. The offset will be (0,0).
  55. */
  56. static PhysicsSprite* createWithTexture(Texture2D *pTexture, const Rect& rect);
  57. /** Creates an sprite with an sprite frame. */
  58. static PhysicsSprite* createWithSpriteFrame(SpriteFrame *pSpriteFrame);
  59. /** Creates an sprite with an sprite frame name.
  60. An SpriteFrame will be fetched from the SpriteFrameCache by name.
  61. If the SpriteFrame doesn't exist it will raise an exception.
  62. @since v0.9
  63. */
  64. static PhysicsSprite* createWithSpriteFrameName(const char *pszSpriteFrameName);
  65. /** Creates an sprite with an image filename.
  66. The rect used will be the size of the image.
  67. The offset will be (0,0).
  68. */
  69. static PhysicsSprite* create(const char *pszFileName);
  70. /** Creates an sprite with an image filename and a rect.
  71. The offset will be (0,0).
  72. */
  73. static PhysicsSprite* create(const char *pszFileName, const Rect& rect);
  74. PhysicsSprite();
  75. virtual bool isDirty() const override;
  76. /** Keep the sprite's rotation separate from the body. */
  77. bool isIgnoreBodyRotation() const;
  78. void setIgnoreBodyRotation(bool bIgnoreBodyRotation);
  79. //
  80. // Chipmunk specific
  81. //
  82. /** Body accessor when using regular Chipmunk */
  83. cpBody* getCPBody() const;
  84. void setCPBody(cpBody *pBody);
  85. //
  86. // Box2d specific
  87. //
  88. /** Body accessor when using box2d */
  89. b2Body* getB2Body() const;
  90. void setB2Body(b2Body *pBody);
  91. float getPTMRatio() const;
  92. void setPTMRatio(float fPTMRatio);
  93. virtual void syncPhysicsTransform() const;
  94. // overrides
  95. virtual const Vec2& getPosition() const override;
  96. virtual void getPosition(float* x, float* y) const override;
  97. virtual float getPositionX() const override;
  98. virtual float getPositionY() const override;
  99. virtual Vec3 getPosition3D() const override;
  100. virtual void setPosition(const Vec2 &position) override;
  101. virtual void setPosition(float x, float y) override;
  102. virtual void setPositionX(float x) override;
  103. virtual void setPositionY(float y) override;
  104. virtual void setPosition3D(const Vec3& position) override;
  105. virtual float getRotation() const override;
  106. virtual void setRotation(float fRotation) override;
  107. virtual void onEnter() override;
  108. virtual void onExit() override;
  109. protected:
  110. const Vec2& getPosFromPhysics() const;
  111. void afterUpdate(EventCustom *event);
  112. protected:
  113. bool _ignoreBodyRotation;
  114. // chipmunk specific
  115. cpBody *_CPBody;
  116. // box2d specific
  117. b2Body *_pB2Body;
  118. float _PTMRatio;
  119. // Event for update synchronise physic transform
  120. cocos2d::EventListenerCustom* _syncTransform;
  121. };
  122. NS_CC_EXT_END
  123. #endif // CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION
  124. #endif // __PHYSICSNODES_CCPHYSICSSPRITE_H__