CCPhysicsSprite.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* Copyright (c) 2012 Scott Lembcke and Howling Moon Software
  2. * Copyright (c) 2012 cocos2d-x.org
  3. * Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #ifndef __PHYSICSNODES_CCPHYSICSSPRITE_H__
  24. #define __PHYSICSNODES_CCPHYSICSSPRITE_H__
  25. #include "2d/CCSprite.h"
  26. #include "extensions/ExtensionMacros.h"
  27. #include "extensions/ExtensionExport.h"
  28. #include "base/CCEventListenerCustom.h"
  29. #if (CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION)
  30. struct cpBody;
  31. class b2Body;
  32. NS_CC_EXT_BEGIN
  33. /** A Sprite subclass that is bound to a physics body.
  34. It works with:
  35. - Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined
  36. - Objective-Chipmunk: Preprocessor macro CC_ENABLE_CHIPMUNK_INTEGRATION should be defined
  37. - Box2d: Preprocessor macro CC_ENABLE_BOX2D_INTEGRATION should be defined
  38. Features and Limitations:
  39. - Scale and Skew properties are ignored.
  40. - Position and rotation are going to updated from the physics body
  41. - If you update the rotation or position manually, the physics body will be updated
  42. - You can't enble both Chipmunk support and Box2d support at the same time. Only one can be enabled at compile time
  43. * @lua NA
  44. */
  45. class CC_EX_DLL PhysicsSprite : public Sprite
  46. {
  47. public:
  48. static PhysicsSprite* create();
  49. /** Creates an sprite with a texture.
  50. The rect used will be the size of the texture.
  51. The offset will be (0,0).
  52. */
  53. static PhysicsSprite* createWithTexture(Texture2D *pTexture);
  54. /** Creates an sprite with a texture and a rect.
  55. The offset will be (0,0).
  56. */
  57. static PhysicsSprite* createWithTexture(Texture2D *pTexture, const Rect& rect);
  58. /** Creates an sprite with an sprite frame. */
  59. static PhysicsSprite* createWithSpriteFrame(SpriteFrame *pSpriteFrame);
  60. /** Creates an sprite with an sprite frame name.
  61. An SpriteFrame will be fetched from the SpriteFrameCache by name.
  62. If the SpriteFrame doesn't exist it will raise an exception.
  63. @since v0.9
  64. */
  65. static PhysicsSprite* createWithSpriteFrameName(const char *pszSpriteFrameName);
  66. /** Creates an sprite with an image filename.
  67. The rect used will be the size of the image.
  68. The offset will be (0,0).
  69. */
  70. static PhysicsSprite* create(const char *pszFileName);
  71. /** Creates an sprite with an image filename and a rect.
  72. The offset will be (0,0).
  73. */
  74. static PhysicsSprite* create(const char *pszFileName, const Rect& rect);
  75. PhysicsSprite();
  76. virtual bool isDirty() const override;
  77. /** Keep the sprite's rotation separate from the body. */
  78. bool isIgnoreBodyRotation() const;
  79. void setIgnoreBodyRotation(bool bIgnoreBodyRotation);
  80. //
  81. // Chipmunk specific
  82. //
  83. /** Body accessor when using regular Chipmunk */
  84. cpBody* getCPBody() const;
  85. void setCPBody(cpBody *pBody);
  86. //
  87. // Box2d specific
  88. //
  89. /** Body accessor when using box2d */
  90. b2Body* getB2Body() const;
  91. void setB2Body(b2Body *pBody);
  92. float getPTMRatio() const;
  93. void setPTMRatio(float fPTMRatio);
  94. virtual void syncPhysicsTransform() const;
  95. // overrides
  96. virtual const Vec2& getPosition() const override;
  97. virtual void getPosition(float* x, float* y) const override;
  98. virtual float getPositionX() const override;
  99. virtual float getPositionY() const override;
  100. virtual Vec3 getPosition3D() const override;
  101. virtual void setPosition(const Vec2 &position) override;
  102. virtual void setPosition(float x, float y) override;
  103. virtual void setPositionX(float x) override;
  104. virtual void setPositionY(float y) override;
  105. virtual void setPosition3D(const Vec3& position) override;
  106. virtual float getRotation() const override;
  107. virtual void setRotation(float fRotation) override;
  108. virtual void onEnter() override;
  109. virtual void onExit() override;
  110. protected:
  111. const Vec2& getPosFromPhysics() const;
  112. void afterUpdate(EventCustom *event);
  113. protected:
  114. bool _ignoreBodyRotation;
  115. // chipmunk specific
  116. cpBody *_CPBody;
  117. // box2d specific
  118. b2Body *_pB2Body;
  119. float _PTMRatio;
  120. // Event for update synchronise physic transform
  121. cocos2d::EventListenerCustom* _syncTransform;
  122. };
  123. NS_CC_EXT_END
  124. #endif // CC_ENABLE_CHIPMUNK_INTEGRATION || CC_ENABLE_BOX2D_INTEGRATION
  125. #endif // __PHYSICSNODES_CCPHYSICSSPRITE_H__