CCPhysicsHelper.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCPHYSICS_HELPER_H__
  22. #define __CCPHYSICS_HELPER_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_PHYSICS
  25. #include "chipmunk/chipmunk.h"
  26. #include "platform/CCPlatformMacros.h"
  27. #include "math/CCGeometry.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup physics
  31. * @{
  32. * @addtogroup physics_2d
  33. * @{
  34. */
  35. /**
  36. * A physics helper class.
  37. *
  38. * Support for conversion between the chipmunk types and cocos types, eg: cpVect to Vec2, cpVect to Size, cpFloat to float.
  39. */
  40. class PhysicsHelper
  41. {
  42. public:
  43. /** Make cpVect type convert to Vec2 type. */
  44. static Vec2 cpv2point(const cpVect& vec) { return Vec2(vec.x, vec.y); }
  45. /** Make Vec2 type convert to cpVect type. */
  46. static cpVect point2cpv(const Vec2& point) { return cpv(point.x, point.y); }
  47. /** Make cpVect type convert to Size type. */
  48. static Size cpv2size(const cpVect& vec) { return Size(vec.x, vec.y); }
  49. /** Make Size type convert to cpVect type. */
  50. static cpVect size2cpv(const Size& size) { return cpv(size.width, size.height); }
  51. /** Make cpFloat type convert to float type. */
  52. static float cpfloat2float(cpFloat f) { return f; }
  53. /** Make Rect type convert to cpBB type. */
  54. static cpBB rect2cpbb(const Rect& rect) { return cpBBNew(rect.origin.x, rect.origin.y, rect.origin.x + rect.size.width, rect.origin.y + rect.size.height); }
  55. /** Make cpBB type convert to Rect type. */
  56. static Rect cpbb2rect(const cpBB& bb) { return Rect(bb.l, bb.b, bb.r - bb.l, bb.t - bb.b); }
  57. /**
  58. Make cpVect array convert to Vec2 array.
  59. @param cpvs The be converted object, it's a cpVect array.
  60. @param out The converted object, it's a Vec2 array.
  61. @param count It's cpvs array length.
  62. @return The out object's pointer.
  63. */
  64. static Vec2* cpvs2points(const cpVect* cpvs, Vec2* out, int count)
  65. {
  66. for (int i = 0; i < count; ++i)
  67. {
  68. out[i] = cpv2point(cpvs[i]);
  69. }
  70. return out;
  71. }
  72. /**
  73. Make Vec2 array convert to cpVect array.
  74. @param points The be converted object, it's a Vec2 array.
  75. @param out The converted object, it's a cpVect array.
  76. @param count It's points array length.
  77. @return The out object's pointer.
  78. */
  79. static cpVect* points2cpvs(const Vec2* points, cpVect* out, int count)
  80. {
  81. for (int i = 0; i < count; ++i)
  82. {
  83. out[i] = point2cpv(points[i]);
  84. }
  85. return out;
  86. }
  87. };
  88. /** @} */
  89. /** @} */
  90. NS_CC_END
  91. #endif // CC_USE_PHYSICS
  92. #endif // __CCPHYSICS_HELPER_H__