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