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 /** 28 * @ignore 29 */ 30 cc.KM_PLANE_LEFT = 0; 31 32 cc.KM_PLANE_RIGHT = 1; 33 34 cc.KM_PLANE_BOTTOM = 2; 35 36 cc.KM_PLANE_TOP = 3; 37 38 cc.KM_PLANE_NEAR = 4; 39 40 cc.KM_PLANE_FAR = 5; 41 42 cc.kmPlane = function (a, b, c, d) { 43 this.a = a || 0; 44 this.b = b || 0; 45 this.c = c || 0; 46 this.d = d || 0; 47 }; 48 49 cc.POINT_INFRONT_OF_PLANE = 0; 50 51 cc.POINT_BEHIND_PLANE = 1; 52 53 cc.POINT_ON_PLANE = 2; 54 55 cc.kmPlaneDot = function(pP, pV){ 56 //a*x + b*y + c*z + d*w 57 return (pP.a * pV.x + 58 pP.b * pV.y + 59 pP.c * pV.z + 60 pP.d * pV.w); 61 }; 62 63 cc.kmPlaneDotCoord = function(pP, pV){ 64 return (pP.a * pV.x + 65 pP.b * pV.y + 66 pP.c * pV.z + pP.d); 67 }; 68 69 cc.kmPlaneDotNormal = function(pP, pV){ 70 return (pP.a * pV.x + 71 pP.b * pV.y + 72 pP.c * pV.z); 73 }; 74 75 cc.kmPlaneFromPointNormal = function(pOut, pPoint, pNormal){ 76 /* 77 Planea = Nx 78 Planeb = Ny 79 Planec = Nz 80 Planed = −N⋅P 81 */ 82 pOut.a = pNormal.x; 83 pOut.b = pNormal.y; 84 pOut.c = pNormal.z; 85 pOut.d = -cc.kmVec3Dot(pNormal, pPoint); 86 87 return pOut; 88 }; 89 90 /** 91 * Creates a plane from 3 points. The result is stored in pOut. 92 * pOut is returned. 93 */ 94 cc.kmPlaneFromPoints = function(pOut, p1, p2, p3){ 95 /* 96 v = (B − A) × (C − A) 97 n = 1⁄|v| v 98 Outa = nx 99 Outb = ny 100 Outc = nz 101 Outd = −n⋅A 102 */ 103 104 var n = new cc.kmVec3(), v1 = new cc.kmVec3(), v2 = new cc.kmVec3(); 105 cc.kmVec3Subtract(v1, p2, p1); //Create the vectors for the 2 sides of the triangle 106 cc.kmVec3Subtract(v2, p3, p1); 107 cc.kmVec3Cross(n, v1, v2); //Use the cross product to get the normal 108 109 cc.kmVec3Normalize(n, n); //Normalize it and assign to pOut.m_N 110 111 pOut.a = n.x; 112 pOut.b = n.y; 113 pOut.c = n.z; 114 pOut.d = cc.kmVec3Dot(cc.kmVec3Scale(n, n, -1.0), p1); 115 116 return pOut; 117 }; 118 119 cc.kmPlaneIntersectLine = function(pOut, pP, pV1, pV2){ 120 throw "cc.kmPlaneIntersectLine() hasn't been implemented."; 121 /* 122 n = (Planea, Planeb, Planec) 123 d = V − U 124 Out = U − d⋅(Pd + n⋅U)⁄(d⋅n) [iff d⋅n ≠ 0] 125 */ 126 //var d = new cc.kmVec3(); 127 128 //cc.kmVec3Subtract(d, pV2, pV1); //Get the direction vector 129 130 //TODO: Continue here! 131 /*if (fabs(kmVec3Dot(&pP.m_N, &d)) > kmEpsilon) 132 { 133 //If we get here then the plane and line are parallel (i.e. no intersection) 134 pOut = nullptr; //Set to nullptr 135 136 return pOut; 137 } */ 138 139 //return null; 140 }; 141 142 cc.kmPlaneNormalize = function(pOut, pP){ 143 var n = new cc.kmVec3(); 144 145 n.x = pP.a; 146 n.y = pP.b; 147 n.z = pP.c; 148 149 var l = 1.0 / cc.kmVec3Length(n); //Get 1/length 150 cc.kmVec3Normalize(n, n); //Normalize the vector and assign to pOut 151 152 pOut.a = n.x; 153 pOut.b = n.y; 154 pOut.c = n.z; 155 156 pOut.d = pP.d * l; //Scale the D value and assign to pOut 157 158 return pOut; 159 }; 160 161 cc.kmPlaneScale = function(pOut, pP, s){ 162 cc.log("cc.kmPlaneScale() has not been implemented."); 163 }; 164 165 /** 166 * Returns POINT_INFRONT_OF_PLANE if pP is infront of pIn. Returns 167 * POINT_BEHIND_PLANE if it is behind. Returns POINT_ON_PLANE otherwise 168 */ 169 cc.kmPlaneClassifyPoint = function(pIn, pP){ 170 // This function will determine if a point is on, in front of, or behind 171 // the plane. First we store the dot product of the plane and the point. 172 var distance = pIn.a * pP.x + pIn.b * pP.y + pIn.c * pP.z + pIn.d; 173 174 // Simply put if the dot product is greater than 0 then it is infront of it. 175 // If it is less than 0 then it is behind it. And if it is 0 then it is on it. 176 if(distance > 0.001) return cc.POINT_INFRONT_OF_PLANE; 177 if(distance < -0.001) return cc.POINT_BEHIND_PLANE; 178 179 return cc.POINT_ON_PLANE; 180 }; 181 182 183