CCPhysicsHelper.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  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 __CCPHYSICS_HELPER_H__
  21. #define __CCPHYSICS_HELPER_H__
  22. #include "base/ccConfig.h"
  23. #if CC_USE_PHYSICS
  24. #include "chipmunk/chipmunk.h"
  25. #include "platform/CCPlatformMacros.h"
  26. #include "math/CCGeometry.h"
  27. NS_CC_BEGIN
  28. /**
  29. * @addtogroup physics
  30. * @{
  31. * @addtogroup physics_2d
  32. * @{
  33. */
  34. /**
  35. * A physics helper class.
  36. *
  37. * Support for conversion between the chipmunk types and cocos types, eg: cpVect to Vec2, cpVect to Size, cpFloat to float.
  38. */
  39. class PhysicsHelper
  40. {
  41. public:
  42. /** Make cpVect type convert to Vec2 type. */
  43. static Vec2 cpv2point(const cpVect& vec) { return Vec2(vec.x, vec.y); }
  44. /** Make Vec2 type convert to cpVect type. */
  45. static cpVect point2cpv(const Vec2& point) { return cpv(point.x, point.y); }
  46. /** Make cpVect type convert to Size type. */
  47. static Size cpv2size(const cpVect& vec) { return Size(vec.x, vec.y); }
  48. /** Make Size type convert to cpVect type. */
  49. static cpVect size2cpv(const Size& size) { return cpv(size.width, size.height); }
  50. /** Make cpFloat type convert to float type. */
  51. static float cpfloat2float(cpFloat f) { return f; }
  52. /** Make Rect type convert to cpBB type. */
  53. 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); }
  54. /** Make cpBB type convert to Rect type. */
  55. static Rect cpbb2rect(const cpBB& bb) { return Rect(bb.l, bb.b, bb.r - bb.l, bb.t - bb.b); }
  56. /**
  57. Make cpVect array convert to Vec2 array.
  58. @param cpvs The be converted object, it's a cpVect array.
  59. @param out The converted object, it's a Vec2 array.
  60. @param count It's cpvs array length.
  61. @return The out object's pointer.
  62. */
  63. static Vec2* cpvs2points(const cpVect* cpvs, Vec2* out, int count)
  64. {
  65. for (int i = 0; i < count; ++i)
  66. {
  67. out[i] = cpv2point(cpvs[i]);
  68. }
  69. return out;
  70. }
  71. /**
  72. Make Vec2 array convert to cpVect array.
  73. @param points The be converted object, it's a Vec2 array.
  74. @param out The converted object, it's a cpVect array.
  75. @param count It's points array length.
  76. @return The out object's pointer.
  77. */
  78. static cpVect* points2cpvs(const Vec2* points, cpVect* out, int count)
  79. {
  80. for (int i = 0; i < count; ++i)
  81. {
  82. out[i] = point2cpv(points[i]);
  83. }
  84. return out;
  85. }
  86. };
  87. /** @} */
  88. /** @} */
  89. NS_CC_END
  90. #endif // CC_USE_PHYSICS
  91. #endif // __CCPHYSICS_HELPER_H__