CCPUPlane.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_PU_PARTICLE_3D_PLANE_H__
  23. #define __CC_PU_PARTICLE_3D_PLANE_H__
  24. #include "base/CCRef.h"
  25. #include "math/CCMath.h"
  26. #include "3d/CCAABB.h"
  27. #include <vector>
  28. NS_CC_BEGIN
  29. /** Defines a plane in 3D space.
  30. @remarks
  31. A plane is defined in 3D space by the equation
  32. Ax + By + Cz + D = 0
  33. @par
  34. This equates to a vector (the normal of the plane, whose x, y
  35. and z components equate to the coefficients A, B and C
  36. respectively), and a constant (D) which is the distance along
  37. the normal you have to go to move the plane back to the origin.
  38. */
  39. class PUPlane
  40. {
  41. public:
  42. /** Default constructor - sets everything to 0.
  43. */
  44. PUPlane ();
  45. PUPlane (const PUPlane& rhs);
  46. /** Construct a plane through a normal, and a distance to move the plane along the normal.*/
  47. PUPlane (const Vec3& rkNormal, float fConstant);
  48. /** Construct a plane using the 4 constants directly **/
  49. PUPlane (float a, float b, float c, float d);
  50. PUPlane (const Vec3& rkNormal, const Vec3& rkPoint);
  51. PUPlane (const Vec3& rkPoint0, const Vec3& rkPoint1,
  52. const Vec3& rkPoint2);
  53. /** The "positive side" of the plane is the half space to which the
  54. plane normal points. The "negative side" is the other half
  55. space. The flag "no side" indicates the plane itself.
  56. */
  57. enum Side
  58. {
  59. NO_SIDE,
  60. POSITIVE_SIDE,
  61. NEGATIVE_SIDE,
  62. BOTH_SIDE
  63. };
  64. //Side getSide (const Vec3& rkPoint) const;
  65. ///**
  66. //Returns the side where the alignedBox is. The flag BOTH_SIDE indicates an intersecting box.
  67. //One corner ON the plane is sufficient to consider the box and the plane intersecting.
  68. //*/
  69. //Side getSide (const AABB& rkBox) const;
  70. ///** Returns which side of the plane that the given box lies on.
  71. // The box is defined as centre/half-size pairs for effectively.
  72. //@param centre The centre of the box.
  73. //@param halfSize The half-size of the box.
  74. //@return
  75. // POSITIVE_SIDE if the box complete lies on the "positive side" of the plane,
  76. // NEGATIVE_SIDE if the box complete lies on the "negative side" of the plane,
  77. // and BOTH_SIDE if the box intersects the plane.
  78. //*/
  79. //Side getSide (const Vec3& centre, const Vec3& halfSize) const;
  80. /** This is a pseudodistance. The sign of the return value is
  81. positive if the point is on the positive side of the plane,
  82. negative if the point is on the negative side, and zero if the
  83. point is on the plane.
  84. @par
  85. The absolute value of the return value is the true distance only
  86. when the plane normal is a unit length vector.
  87. */
  88. float getDistance (const Vec3& rkPoint) const;
  89. /** Redefine this plane based on 3 points. */
  90. void redefine(const Vec3& rkPoint0, const Vec3& rkPoint1,
  91. const Vec3& rkPoint2);
  92. /** Redefine this plane based on a normal and a point. */
  93. void redefine(const Vec3& rkNormal, const Vec3& rkPoint);
  94. /** Project a vector onto the plane.
  95. @remarks This gives you the element of the input vector that is perpendicular
  96. to the normal of the plane. You can get the element which is parallel
  97. to the normal of the plane by subtracting the result of this method
  98. from the original vector, since parallel + perpendicular = original.
  99. @param v The input vector
  100. */
  101. Vec3 projectVector(const Vec3& v) const;
  102. /** Normalises the plane.
  103. @remarks
  104. This method normalises the plane's normal and the length scale of d
  105. is as well.
  106. @note
  107. This function will not crash for zero-sized vectors, but there
  108. will be no changes made to their components.
  109. @return The previous length of the plane's normal.
  110. */
  111. float normalize(void);
  112. Vec3 normal;
  113. float d;
  114. /// Comparison operator
  115. bool operator==(const PUPlane& rhs) const
  116. {
  117. return (rhs.d == d && rhs.normal == normal);
  118. }
  119. bool operator!=(const PUPlane& rhs) const
  120. {
  121. return (rhs.d != d || rhs.normal != normal);
  122. }
  123. };
  124. typedef std::vector<PUPlane> PlaneList;
  125. NS_CC_END
  126. #endif