CCPhysics3DWorld.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __PHYSICS_3D_WORLD_H__
  21. #define __PHYSICS_3D_WORLD_H__
  22. #include "math/CCMath.h"
  23. #include "base/CCRef.h"
  24. #include "base/ccConfig.h"
  25. #if CC_USE_3D_PHYSICS
  26. #if (CC_ENABLE_BULLET_INTEGRATION)
  27. class btDynamicsWorld;
  28. class btDefaultCollisionConfiguration;
  29. class btCollisionDispatcher;
  30. class btDbvtBroadphase;
  31. class btSequentialImpulseConstraintSolver;
  32. class btGhostPairCallback;
  33. class btRigidBody;
  34. class btCollisionObject;
  35. NS_CC_BEGIN
  36. /**
  37. * @addtogroup _3d
  38. * @{
  39. */
  40. class Physics3DObject;
  41. class Physics3DConstraint;
  42. class Physics3DDebugDrawer;
  43. class Physics3DComponent;
  44. class Physics3DShape;
  45. class Renderer;
  46. /**
  47. * @brief The description of Physics3DWorld.
  48. */
  49. struct CC_DLL Physics3DWorldDes
  50. {
  51. bool isDebugDrawEnabled; //using physics debug draw?, false by default
  52. cocos2d::Vec3 gravity;//gravity, (0, -9.8, 0)
  53. Physics3DWorldDes()
  54. {
  55. isDebugDrawEnabled = false;
  56. gravity = cocos2d::Vec3(0.f, -9.8f, 0.f);
  57. }
  58. };
  59. /**
  60. * @brief The physics information container, include Physics3DObjects, Physics3DConstraints, collision information and so on.
  61. */
  62. class CC_DLL Physics3DWorld : public Ref
  63. {
  64. friend class Physics3DComponent;
  65. public:
  66. struct HitResult
  67. {
  68. cocos2d::Vec3 hitPosition;
  69. cocos2d::Vec3 hitNormal;
  70. Physics3DObject* hitObj;
  71. };
  72. /**
  73. * Creates a Physics3DWorld with Physics3DWorldDes.
  74. *
  75. * @return An autoreleased Physics3DWorld object.
  76. */
  77. static Physics3DWorld* create(Physics3DWorldDes* info);
  78. /** set gravity for the physics world */
  79. void setGravity(const Vec3& gravity);
  80. /** get current gravity */
  81. Vec3 getGravity() const;
  82. /** Add a Physics3DObject. */
  83. void addPhysics3DObject(Physics3DObject* physicsObj);
  84. /** Remove a Physics3DObject. */
  85. void removePhysics3DObject(Physics3DObject* physicsObj);
  86. /** Remove all Physics3DObjects. */
  87. void removeAllPhysics3DObjects();
  88. /** Add a Physics3DConstraint. */
  89. void addPhysics3DConstraint(Physics3DConstraint* constraint, bool disableCollisionsBetweenLinkedObjs = true);
  90. /** Remove a Physics3DConstraint. */
  91. void removePhysics3DConstraint(Physics3DConstraint* constraint);
  92. /** Remove all Physics3DConstraint. */
  93. void removeAllPhysics3DConstraints();
  94. /** Simulate one frame. */
  95. void stepSimulate(float dt);
  96. /** Enable or disable debug drawing. */
  97. void setDebugDrawEnable(bool enableDebugDraw);
  98. /** Check debug drawing is enabled. */
  99. bool isDebugDrawEnabled() const;
  100. /** Internal method, the updater of debug drawing, need called each frame. */
  101. void debugDraw(cocos2d::Renderer* renderer);
  102. /** Get the list of Physics3DObjects. */
  103. const std::vector<Physics3DObject*>& getPhysicsObjects() const { return _objects; }
  104. /**
  105. * Ray cast method
  106. * @param startPos The start position of ray.
  107. * @param endPos The end position of ray.
  108. * @param result the result of ray cast.
  109. */
  110. bool rayCast(const cocos2d::Vec3& startPos, const cocos2d::Vec3& endPos, HitResult* result);
  111. /** Performs a swept shape cast on all objects in the Physics3DWorld. */
  112. bool sweepShape(Physics3DShape* shape, const cocos2d::Mat4& startTransform, const cocos2d::Mat4& endTransform, HitResult* result);
  113. CC_CONSTRUCTOR_ACCESS:
  114. Physics3DWorld();
  115. virtual ~Physics3DWorld();
  116. bool init(Physics3DWorldDes* info);
  117. Physics3DObject* getPhysicsObject(const btCollisionObject* btObj);
  118. void collisionChecking();
  119. bool needCollisionChecking();
  120. void setGhostPairCallback();
  121. protected:
  122. std::vector<Physics3DObject*> _objects;
  123. std::vector<Physics3DComponent*> _physicsComponents; //physics3d components
  124. bool _needCollisionChecking;
  125. bool _collisionCheckingFlag;
  126. bool _needGhostPairCallbackChecking;
  127. #if (CC_ENABLE_BULLET_INTEGRATION)
  128. btDynamicsWorld* _btPhyiscsWorld;
  129. btDefaultCollisionConfiguration* _collisionConfiguration;
  130. btCollisionDispatcher* _dispatcher;
  131. btDbvtBroadphase* _broadphase;
  132. btSequentialImpulseConstraintSolver* _solver;
  133. btGhostPairCallback *_ghostCallback;
  134. Physics3DDebugDrawer* _debugDrawer;
  135. #endif // CC_ENABLE_BULLET_INTEGRATION
  136. };
  137. // end of 3d group
  138. /// @}
  139. NS_CC_END
  140. #endif
  141. #endif //CC_USE_3D_PHYSICS
  142. #endif // __PHYSICS_3D_WORLD_H__