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.kmVec3 = function (x, y, z) { 28 this.x = x || 0; 29 this.y = y || 0; 30 this.z = z || 0; 31 }; 32 33 cc.kmVec3Fill = function(pOut, x, y , z){ 34 if(!pOut) 35 return new cc.kmVec3(x, y , z); 36 pOut.x = x; 37 pOut.y = y; 38 pOut.z = z; 39 return pOut; 40 }; 41 42 cc.kmVec3Length = function(pIn){ 43 return Math.sqrt(cc.kmSQR(pIn.x) + cc.kmSQR(pIn.y) + cc.kmSQR(pIn.z)); 44 }; 45 46 cc.kmVec3LengthSq = function(pIn){ 47 return cc.kmSQR(pIn.x) + cc.kmSQR(pIn.y) + cc.kmSQR(pIn.z) 48 } ; 49 50 cc.kmVec3Normalize = function(pOut,pIn){ 51 var l = 1.0 / cc.kmVec3Length(pIn); 52 53 pOut.x = pIn.x * l; 54 pOut.y = pIn.y * l; 55 pOut.z = pIn.z * l; 56 return pOut; 57 }; 58 59 cc.kmVec3Cross = function(pOut, pV1,pV2){ 60 pOut.x = (pV1.y * pV2.z) - (pV1.z * pV2.y); 61 pOut.y = (pV1.z * pV2.x) - (pV1.x * pV2.z); 62 pOut.z = (pV1.x * pV2.y) - (pV1.y * pV2.x); 63 return pOut; 64 }; 65 66 cc.kmVec3Dot = function(pV1, pV2){ 67 return ( pV1.x * pV2.x 68 + pV1.y * pV2.y 69 + pV1.z * pV2.z ); 70 } ; 71 72 cc.kmVec3Add = function(pOut, pV1, pV2){ 73 pOut.x = pV1.x + pV2.x; 74 pOut.y = pV1.y + pV2.y; 75 pOut.z = pV1.z + pV2.z; 76 return pOut; 77 }; 78 79 cc.kmVec3Subtract = function(pOut, pV1, pV2){ 80 pOut.x = pV1.x - pV2.x; 81 pOut.y = pV1.y - pV2.y; 82 pOut.z = pV1.z - pV2.z; 83 return pOut; 84 }; 85 86 cc.kmVec3Transform = function(pOut, pV, pM){ 87 /* 88 a = (Vx, Vy, Vz, 1) 89 b = (a×M)T 90 Out = (bx, by, bz) 91 */ 92 pOut.x = pV.x * pM.mat[0] + pV.y * pM.mat[4] + pV.z * pM.mat[8] + pM.mat[12]; 93 pOut.y = pV.x * pM.mat[1] + pV.y * pM.mat[5] + pV.z * pM.mat[9] + pM.mat[13]; 94 pOut.z = pV.x * pM.mat[2] + pV.y * pM.mat[6] + pV.z * pM.mat[10] + pM.mat[14]; 95 return pOut; 96 }; 97 98 cc.kmVec3TransformNormal = function(pOut, pV, pM){ 99 /* 100 a = (Vx, Vy, Vz, 0) 101 b = (a×M)T 102 Out = (bx, by, bz) 103 */ 104 //Omits the translation, only scaling + rotating 105 pOut.x = pV.x * pM.mat[0] + pV.y * pM.mat[4] + pV.z * pM.mat[8]; 106 pOut.y = pV.x * pM.mat[1] + pV.y * pM.mat[5] + pV.z * pM.mat[9]; 107 pOut.z = pV.x * pM.mat[2] + pV.y * pM.mat[6] + pV.z * pM.mat[10]; 108 return pOut; 109 }; 110 111 cc.kmVec3TransformCoord = function(pOut,pV,pM){ 112 /* 113 a = (Vx, Vy, Vz, 1) 114 b = (a×M)T 115 Out = 1⁄bw(bx, by, bz) 116 */ 117 var v = new cc.kmVec4(); 118 var inV = new cc.kmVec4(); 119 cc.kmVec4Fill(inV, pV.x, pV.y, pV.z, 1.0); 120 121 cc.kmVec4Transform(v, inV,pM); 122 123 pOut.x = v.x / v.w; 124 pOut.y = v.y / v.w; 125 pOut.z = v.z / v.w; 126 127 return pOut; 128 }; 129 130 cc.kmVec3Scale = function(pOut, pIn, s){ 131 pOut.x = pIn.x * s; 132 pOut.y = pIn.y * s; 133 pOut.z = pIn.z * s; 134 135 return pOut; 136 }; 137 138 cc.kmVec3AreEqual = function(p1, p2){ 139 if ((p1.x < (p2.x + cc.kmEpsilon) && p1.x > (p2.x - cc.kmEpsilon)) && 140 (p1.y < (p2.y + cc.kmEpsilon) && p1.y > (p2.y - cc.kmEpsilon)) && 141 (p1.z < (p2.z + cc.kmEpsilon) && p1.z > (p2.z - cc.kmEpsilon))) { 142 return 1; 143 } 144 145 return 0; 146 }; 147 148 cc.kmVec3InverseTransform = function(pOut, pVect,pM){ 149 var v1 = new cc.kmVec3(pVect.x - pM.mat[12], pVect.y - pM.mat[13],pVect.z - pM.mat[14]); 150 151 pOut.x = v1.x * pM.mat[0] + v1.y * pM.mat[1] + v1.z * pM.mat[2]; 152 pOut.y = v1.x * pM.mat[4] + v1.y * pM.mat[5] + v1.z * pM.mat[6]; 153 pOut.z = v1.x * pM.mat[8] + v1.y * pM.mat[9] + v1.z * pM.mat[10]; 154 155 return pOut; 156 }; 157 158 cc.kmVec3InverseTransformNormal = function(pOut, pVect, pM){ 159 pOut.x = pVect.x * pM.mat[0] + pVect.y * pM.mat[1] + pVect.z * pM.mat[2]; 160 pOut.y = pVect.x * pM.mat[4] + pVect.y * pM.mat[5] + pVect.z * pM.mat[6]; 161 pOut.z = pVect.x * pM.mat[8] + pVect.y * pM.mat[9] + pVect.z * pM.mat[10]; 162 163 return pOut; 164 }; 165 166 cc.kmVec3Assign = function(pOut,pIn){ 167 if (pOut == pIn) 168 return pOut; 169 170 pOut.x = pIn.x; 171 pOut.y = pIn.y; 172 pOut.z = pIn.z; 173 return pOut; 174 }; 175 176 cc.kmVec3Zero = function(pOut){ 177 pOut.x = 0.0; 178 pOut.y = 0.0; 179 pOut.z = 0.0; 180 181 return pOut; 182 }; 183 184 cc.kmVec3ToTypeArray = function(vecValue){ 185 if(!vecValue) 186 return null; 187 188 var tyArr = new Float32Array(3); 189 tyArr[0] = vecValue.x; 190 tyArr[1] = vecValue.y; 191 tyArr[2] = vecValue.z; 192 return tyArr; 193 }; 194 195 196 197 198 199 200 201 202 203 204