plane.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008, Luke Benstead.
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  16. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  19. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. cc.KM_PLANE_LEFT = 0;
  24. cc.KM_PLANE_RIGHT = 1;
  25. cc.KM_PLANE_BOTTOM = 2;
  26. cc.KM_PLANE_TOP = 3;
  27. cc.KM_PLANE_NEAR = 4;
  28. cc.KM_PLANE_FAR = 5;
  29. cc.kmPlane = function (a, b, c, d) {
  30. this.a = a || 0;
  31. this.b = b || 0;
  32. this.c = c || 0;
  33. this.d = d || 0;
  34. };
  35. cc.POINT_INFRONT_OF_PLANE = 0;
  36. cc.POINT_BEHIND_PLANE = 1;
  37. cc.POINT_ON_PLANE = 2;
  38. cc.kmPlaneDot = function(pP, pV){
  39. //a*x + b*y + c*z + d*w
  40. return (pP.a * pV.x +
  41. pP.b * pV.y +
  42. pP.c * pV.z +
  43. pP.d * pV.w);
  44. };
  45. cc.kmPlaneDotCoord = function(pP, pV){
  46. return (pP.a * pV.x +
  47. pP.b * pV.y +
  48. pP.c * pV.z + pP.d);
  49. };
  50. cc.kmPlaneDotNormal = function(pP, pV){
  51. return (pP.a * pV.x +
  52. pP.b * pV.y +
  53. pP.c * pV.z);
  54. };
  55. cc.kmPlaneFromPointNormal = function(pOut, pPoint, pNormal){
  56. /*
  57. Planea = Nx
  58. Planeb = Ny
  59. Planec = Nz
  60. Planed = -N·P
  61. */
  62. pOut.a = pNormal.x;
  63. pOut.b = pNormal.y;
  64. pOut.c = pNormal.z;
  65. pOut.d = -cc.kmVec3Dot(pNormal, pPoint);
  66. return pOut;
  67. };
  68. /**
  69. * Creates a plane from 3 points. The result is stored in pOut.
  70. * pOut is returned.
  71. */
  72. cc.kmPlaneFromPoints = function(pOut, p1, p2, p3){
  73. /*
  74. v = (B - A) × (C - A)
  75. n = 1/|v| v
  76. Outa = nx
  77. Outb = ny
  78. Outc = nz
  79. Outd = -n·A
  80. */
  81. var n = new cc.kmVec3(), v1 = new cc.kmVec3(), v2 = new cc.kmVec3();
  82. cc.kmVec3Subtract(v1, p2, p1); //Create the vectors for the 2 sides of the triangle
  83. cc.kmVec3Subtract(v2, p3, p1);
  84. cc.kmVec3Cross(n, v1, v2); //Use the cross product to get the normal
  85. cc.kmVec3Normalize(n, n); //Normalize it and assign to pOut.m_N
  86. pOut.a = n.x;
  87. pOut.b = n.y;
  88. pOut.c = n.z;
  89. pOut.d = cc.kmVec3Dot(cc.kmVec3Scale(n, n, -1.0), p1);
  90. return pOut;
  91. };
  92. cc.kmPlaneIntersectLine = function(pOut, pP, pV1, pV2){
  93. throw "cc.kmPlaneIntersectLine() hasn't been implemented.";
  94. /*
  95. n = (Planea, Planeb, Planec)
  96. d = V - U
  97. Out = U - d·(Pd + n·U)/(d·n) [iff d·n ≠ 0]
  98. */
  99. //var d = new cc.kmVec3();
  100. //cc.kmVec3Subtract(d, pV2, pV1); //Get the direction vector
  101. //TODO: Continue here!
  102. /*if (fabs(kmVec3Dot(&pP.m_N, &d)) > kmEpsilon)
  103. {
  104. //If we get here then the plane and line are parallel (i.e. no intersection)
  105. pOut = nullptr; //Set to nullptr
  106. return pOut;
  107. } */
  108. //return null;
  109. };
  110. cc.kmPlaneNormalize = function(pOut, pP){
  111. var n = new cc.kmVec3();
  112. n.x = pP.a;
  113. n.y = pP.b;
  114. n.z = pP.c;
  115. var l = 1.0 / cc.kmVec3Length(n); //Get 1/length
  116. cc.kmVec3Normalize(n, n); //Normalize the vector and assign to pOut
  117. pOut.a = n.x;
  118. pOut.b = n.y;
  119. pOut.c = n.z;
  120. pOut.d = pP.d * l; //Scale the D value and assign to pOut
  121. return pOut;
  122. };
  123. cc.kmPlaneScale = function(pOut, pP, s){
  124. cc.log("cc.kmPlaneScale() has not been implemented.");
  125. };
  126. /**
  127. * Returns POINT_INFRONT_OF_PLANE if pP is infront of pIn. Returns
  128. * POINT_BEHIND_PLANE if it is behind. Returns POINT_ON_PLANE otherwise
  129. */
  130. cc.kmPlaneClassifyPoint = function(pIn, pP){
  131. // This function will determine if a point is on, in front of, or behind
  132. // the plane. First we store the dot product of the plane and the point.
  133. var distance = pIn.a * pP.x + pIn.b * pP.y + pIn.c * pP.z + pIn.d;
  134. // Simply put if the dot product is greater than 0 then it is infront of it.
  135. // If it is less than 0 then it is behind it. And if it is 0 then it is on it.
  136. if(distance > 0.001) return cc.POINT_INFRONT_OF_PLANE;
  137. if(distance < -0.001) return cc.POINT_BEHIND_PLANE;
  138. return cc.POINT_ON_PLANE;
  139. };