Cocos2d.lua 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. cc = cc or {}
  2. function cc.clampf(value, min_inclusive, max_inclusive)
  3. -- body
  4. local temp = 0
  5. if min_inclusive > max_inclusive then
  6. temp = min_inclusive
  7. min_inclusive = max_inclusive
  8. max_inclusive = temp
  9. end
  10. if value < min_inclusive then
  11. return min_inclusive
  12. elseif value < max_inclusive then
  13. return value
  14. else
  15. return max_inclusive
  16. end
  17. end
  18. --Point
  19. function cc.p(_x,_y)
  20. if nil == _y then
  21. return { x = _x.x, y = _x.y }
  22. else
  23. return { x = _x, y = _y }
  24. end
  25. end
  26. function cc.pAdd(pt1,pt2)
  27. return {x = pt1.x + pt2.x , y = pt1.y + pt2.y }
  28. end
  29. function cc.pSub(pt1,pt2)
  30. return {x = pt1.x - pt2.x , y = pt1.y - pt2.y }
  31. end
  32. function cc.pMul(pt1,factor)
  33. return { x = pt1.x * factor , y = pt1.y * factor }
  34. end
  35. function cc.pMidpoint(pt1,pt2)
  36. return { x = (pt1.x + pt2.x) / 2.0 , y = ( pt1.y + pt2.y) / 2.0 }
  37. end
  38. function cc.pForAngle(a)
  39. return { x = math.cos(a), y = math.sin(a) }
  40. end
  41. function cc.pGetLength(pt)
  42. return math.sqrt( pt.x * pt.x + pt.y * pt.y )
  43. end
  44. function cc.pNormalize(pt)
  45. local length = cc.pGetLength(pt)
  46. if 0 == length then
  47. return { x = 1.0,y = 0.0 }
  48. end
  49. return { x = pt.x / length, y = pt.y / length }
  50. end
  51. function cc.pCross(self,other)
  52. return self.x * other.y - self.y * other.x
  53. end
  54. function cc.pDot(self,other)
  55. return self.x * other.x + self.y * other.y
  56. end
  57. function cc.pToAngleSelf(self)
  58. return math.atan2(self.y, self.x)
  59. end
  60. function cc.pGetAngle(self,other)
  61. local a2 = cc.pNormalize(self)
  62. local b2 = cc.pNormalize(other)
  63. local angle = math.atan2(cc.pCross(a2, b2), cc.pDot(a2, b2) )
  64. if math.abs(angle) < 1.192092896e-7 then
  65. return 0.0
  66. end
  67. return angle
  68. end
  69. function cc.pGetDistance(startP,endP)
  70. return cc.pGetLength(cc.pSub(startP,endP))
  71. end
  72. function cc.pIsLineIntersect(A, B, C, D, s, t)
  73. if ((A.x == B.x) and (A.y == B.y)) or ((C.x == D.x) and (C.y == D.y))then
  74. return false, s, t
  75. end
  76. local BAx = B.x - A.x
  77. local BAy = B.y - A.y
  78. local DCx = D.x - C.x
  79. local DCy = D.y - C.y
  80. local ACx = A.x - C.x
  81. local ACy = A.y - C.y
  82. local denom = DCy * BAx - DCx * BAy
  83. s = DCx * ACy - DCy * ACx
  84. t = BAx * ACy - BAy * ACx
  85. if (denom == 0) then
  86. if (s == 0 or t == 0) then
  87. return true, s , t
  88. end
  89. return false, s, t
  90. end
  91. s = s / denom
  92. t = t / denom
  93. return true,s,t
  94. end
  95. function cc.pPerp(pt)
  96. return { x = -pt.y, y = pt.x }
  97. end
  98. function cc.RPerp(pt)
  99. return { x = pt.y, y = -pt.x }
  100. end
  101. function cc.pProject(pt1, pt2)
  102. return { x = pt2.x * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) , y = pt2.y * (cc.pDot(pt1,pt2) / cc.pDot(pt2,pt2)) }
  103. end
  104. function cc.pRotate(pt1, pt2)
  105. return { x = pt1.x * pt2.x - pt1.y * pt2.y, y = pt1.x * pt2.y + pt1.y * pt2.x }
  106. end
  107. function cc.pUnrotate(pt1, pt2)
  108. return { x = pt1.x * pt2.x + pt1.y * pt2.y, pt1.y * pt2.x - pt1.x * pt2.y }
  109. end
  110. --Calculates the square length of pt
  111. function cc.pLengthSQ(pt)
  112. return cc.pDot(pt,pt)
  113. end
  114. --Calculates the square distance between pt1 and pt2
  115. function cc.pDistanceSQ(pt1,pt2)
  116. return cc.pLengthSQ(cc.pSub(pt1,pt2))
  117. end
  118. function cc.pGetClampPoint(pt1,pt2,pt3)
  119. return { x = cc.clampf(pt1.x, pt2.x, pt3.x), y = cc.clampf(pt1.y, pt2.y, pt3.y) }
  120. end
  121. function cc.pFromSize(sz)
  122. return { x = sz.width, y = sz.height }
  123. end
  124. function cc.pLerp(pt1,pt2,alpha)
  125. return cc.pAdd(cc.pMul(pt1, 1.0 - alpha), cc.pMul(pt2,alpha) )
  126. end
  127. function cc.pFuzzyEqual(pt1,pt2,variance)
  128. if (pt1.x - variance <= pt2.x) and (pt2.x <= pt1.x + variance) and (pt1.y - variance <= pt2.y) and (pt2.y <= pt1.y + variance) then
  129. return true
  130. else
  131. return false
  132. end
  133. end
  134. function cc.pRotateByAngle(pt1, pt2, angle)
  135. return cc.pAdd(pt2, cc.pRotate( cc.pSub(pt1, pt2),cc.pForAngle(angle)))
  136. end
  137. function cc.pIsSegmentIntersect(pt1,pt2,pt3,pt4)
  138. local s,t,ret = 0,0,false
  139. ret,s,t =cc.pIsLineIntersect(pt1, pt2, pt3, pt4,s,t)
  140. if ret and s >= 0.0 and s <= 1.0 and t >= 0.0 and t <= 1.0 then
  141. return true
  142. end
  143. return false
  144. end
  145. function cc.pGetIntersectPoint(pt1,pt2,pt3,pt4)
  146. local s,t, ret = 0,0,false
  147. ret,s,t = cc.pIsLineIntersect(pt1,pt2,pt3,pt4,s,t)
  148. if ret then
  149. return cc.p(pt1.x + s * (pt2.x - pt1.x), pt1.y + s * (pt2.y - pt1.y))
  150. else
  151. return cc.p(0,0)
  152. end
  153. end
  154. --Size
  155. function cc.size( _width,_height )
  156. return { width = _width, height = _height }
  157. end
  158. --Rect
  159. function cc.rect(_x,_y,_width,_height)
  160. return { x = _x, y = _y, width = _width, height = _height }
  161. end
  162. function cc.rectEqualToRect(rect1,rect2)
  163. if ((rect1.x ~= rect2.x) or (rect1.y ~= rect2.y) or
  164. ( rect1.width ~= rect2.width) or ( rect1.height ~= rect2.height)) then
  165. return false
  166. end
  167. return true
  168. end
  169. function cc.rectGetMaxX(rect)
  170. return rect.x + rect.width
  171. end
  172. function cc.rectGetMidX(rect)
  173. return rect.x + rect.width / 2.0
  174. end
  175. function cc.rectGetMinX(rect)
  176. return rect.x
  177. end
  178. function cc.rectGetMaxY(rect)
  179. return rect.y + rect.height
  180. end
  181. function cc.rectGetMidY(rect)
  182. return rect.y + rect.height / 2.0
  183. end
  184. function cc.rectGetMinY(rect)
  185. return rect.y
  186. end
  187. function cc.rectContainsPoint( rect, point )
  188. local ret = false
  189. if (point.x >= rect.x) and (point.x <= rect.x + rect.width) and
  190. (point.y >= rect.y) and (point.y <= rect.y + rect.height) then
  191. ret = true
  192. end
  193. return ret
  194. end
  195. function cc.rectIntersectsRect( rect1, rect2 )
  196. local intersect = not ( rect1.x > rect2.x + rect2.width or
  197. rect1.x + rect1.width < rect2.x or
  198. rect1.y > rect2.y + rect2.height or
  199. rect1.y + rect1.height < rect2.y )
  200. return intersect
  201. end
  202. function cc.rectUnion( rect1, rect2 )
  203. local rect = cc.rect(0, 0, 0, 0)
  204. rect.x = math.min(rect1.x, rect2.x)
  205. rect.y = math.min(rect1.y, rect2.y)
  206. rect.width = math.max(rect1.x + rect1.width, rect2.x + rect2.width) - rect.x
  207. rect.height = math.max(rect1.y + rect1.height, rect2.y + rect2.height) - rect.y
  208. return rect
  209. end
  210. function cc.rectIntersection( rect1, rect2 )
  211. local intersection = cc.rect(
  212. math.max(rect1.x, rect2.x),
  213. math.max(rect1.y, rect2.y),
  214. 0, 0)
  215. intersection.width = math.min(rect1.x + rect1.width, rect2.x + rect2.width) - intersection.x
  216. intersection.height = math.min(rect1.y + rect1.height, rect2.y + rect2.height) - intersection.y
  217. return intersection
  218. end
  219. --Color3B
  220. function cc.c3b( _r,_g,_b )
  221. return { r = _r, g = _g, b = _b }
  222. end
  223. --Color4B
  224. function cc.c4b( _r,_g,_b,_a )
  225. return { r = _r, g = _g, b = _b, a = _a }
  226. end
  227. --Color4F
  228. function cc.c4f( _r,_g,_b,_a )
  229. return { r = _r, g = _g, b = _b, a = _a }
  230. end
  231. local function isFloatColor(c)
  232. return (c.r <= 1 and c.g <= 1 and c.b <= 1) and (math.ceil(c.r) ~= c.r or math.ceil(c.g) ~= c.g or math.ceil(c.b) ~= c.b)
  233. end
  234. function cc.convertColor(input, typ)
  235. assert(type(input) == "table" and input.r and input.g and input.b, "cc.convertColor() - invalid input color")
  236. local ret
  237. if typ == "3b" then
  238. if isFloatColor(input) then
  239. ret = {r = math.ceil(input.r * 255), g = math.ceil(input.g * 255), b = math.ceil(input.b * 255)}
  240. else
  241. ret = {r = input.r, g = input.g, b = input.b}
  242. end
  243. elseif typ == "4b" then
  244. if isFloatColor(input) then
  245. ret = {r = math.ceil(input.r * 255), g = math.ceil(input.g * 255), b = math.ceil(input.b * 255)}
  246. else
  247. ret = {r = input.r, g = input.g, b = input.b}
  248. end
  249. if input.a then
  250. if math.ceil(input.a) ~= input.a or input.a >= 1 then
  251. ret.a = input.a * 255
  252. else
  253. ret.a = input.a
  254. end
  255. else
  256. ret.a = 255
  257. end
  258. elseif typ == "4f" then
  259. if isFloatColor(input) then
  260. ret = {r = input.r, g = input.g, b = input.b}
  261. else
  262. ret = {r = input.r / 255, g = input.g / 255, b = input.b / 255}
  263. end
  264. if input.a then
  265. if math.ceil(input.a) ~= input.a or input.a >= 1 then
  266. ret.a = input.a
  267. else
  268. ret.a = input.a / 255
  269. end
  270. else
  271. ret.a = 255
  272. end
  273. else
  274. error(string.format("cc.convertColor() - invalid type %s", typ), 0)
  275. end
  276. return ret
  277. end
  278. --Vertex2F
  279. function cc.vertex2F(_x,_y)
  280. return { x = _x, y = _y }
  281. end
  282. --Vertex3F
  283. function cc.Vertex3F(_x,_y,_z)
  284. return { x = _x, y = _y, z = _z }
  285. end
  286. --Tex2F
  287. function cc.tex2F(_u,_v)
  288. return { u = _u, v = _v }
  289. end
  290. --PointSprite
  291. function cc.PointSprite(_pos,_color,_size)
  292. return { pos = _pos, color = _color, size = _size }
  293. end
  294. --Quad2
  295. function cc.Quad2(_tl,_tr,_bl,_br)
  296. return { tl = _tl, tr = _tr, bl = _bl, br = _br }
  297. end
  298. --Quad3
  299. function cc.Quad3(_tl, _tr, _bl, _br)
  300. return { tl = _tl, tr = _tr, bl = _bl, br = _br }
  301. end
  302. --V2F_C4B_T2F
  303. function cc.V2F_C4B_T2F(_vertices, _colors, _texCoords)
  304. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  305. end
  306. --V2F_C4F_T2F
  307. function cc.V2F_C4F_T2F(_vertices, _colors, _texCoords)
  308. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  309. end
  310. --V3F_C4B_T2F
  311. function cc.V3F_C4B_T2F(_vertices, _colors, _texCoords)
  312. return { vertices = _vertices, colors = _colors, texCoords = _texCoords }
  313. end
  314. --V2F_C4B_T2F_Quad
  315. function cc.V2F_C4B_T2F_Quad(_bl, _br, _tl, _tr)
  316. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  317. end
  318. --V3F_C4B_T2F_Quad
  319. function cc.V3F_C4B_T2F_Quad(_tl, _bl, _tr, _br)
  320. return { tl = _tl, bl = _bl, tr = _tr, br = _br }
  321. end
  322. --V2F_C4F_T2F_Quad
  323. function cc.V2F_C4F_T2F_Quad(_bl, _br, _tl, _tr)
  324. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  325. end
  326. --T2F_Quad
  327. function cc.T2F_Quad(_bl, _br, _tl, _tr)
  328. return { bl = _bl, br = _br, tl = _tl, tr = _tr }
  329. end
  330. --AnimationFrameData
  331. function cc.AnimationFrameData( _texCoords, _delay, _size)
  332. return { texCoords = _texCoords, delay = _delay, size = _size }
  333. end
  334. --PhysicsMaterial
  335. function cc.PhysicsMaterial(_density, _restitution, _friction)
  336. return { density = _density, restitution = _restitution, friction = _friction }
  337. end
  338. function cc.vec3(_x, _y, _z)
  339. return { x = _x, y = _y, z = _z }
  340. end
  341. function cc.vec4(_x, _y, _z, _w)
  342. return { x = _x, y = _y, z = _z, w = _w }
  343. end
  344. function cc.vec3add(vec3a, vec3b)
  345. return {x = vec3a.x + vec3b.x, y = vec3a.y + vec3b.y, z = vec3a.z + vec3b.z}
  346. end
  347. function cc.vec3sub(vec3a, vec3b)
  348. return {x = vec3a.x - vec3b.x, y = vec3a.y - vec3b.y, z = vec3a.z - vec3b.z}
  349. end
  350. function cc.vec3mul(vec3, factor)
  351. return {x = vec3.x * factor, y = vec3.y * factor, z = vec3.z * factor}
  352. end
  353. function cc.vec3dot(vec3a, vec3b)
  354. return vec3a.x * vec3b.x + vec3a.y * vec3b.y + vec3a.z * vec3b.z
  355. end
  356. function cc.vec3normalize(vec3)
  357. local n = vec3.x * vec3.x + vec3.y * vec3.y + vec3.z * vec3.z
  358. if n == 1.0 then
  359. return vec3
  360. end
  361. n = math.sqrt(n)
  362. if n < 2e-37 then
  363. return vec3
  364. end
  365. n = 1.0 / n
  366. return {x = vec3.x * n, y = vec3.y * n, z = vec3.z * n}
  367. end
  368. function cc.quaternion(_x, _y ,_z,_w)
  369. return { x = _x, y = _y, z = _z, w = _w }
  370. end
  371. function cc.quaternion_createFromAxisAngle(axis, angle)
  372. local halfAngle = angle * 0.5
  373. local sinHalfAngle = math.sin(halfAngle)
  374. local normal = cc.vec3(axis.x, axis.y, axis.z)
  375. normal = cc.vec3normalize(normal)
  376. local dst = cc.vec3(0.0, 0.0, 0.0)
  377. dst.x = normal.x * sinHalfAngle
  378. dst.y = normal.y * sinHalfAngle
  379. dst.z = normal.z * sinHalfAngle
  380. dst.w = math.cos(halfAngle)
  381. return dst
  382. end
  383. function cc.blendFunc(_src, _dst)
  384. return {src = _src, dst = _dst}
  385. end
  386. cc.mat4 = cc.mat4 or {}
  387. function cc.mat4.new(...)
  388. local params = {...}
  389. local size = #params
  390. local obj = {}
  391. if 1 == size then
  392. assert(type(params[1]) == "table" , "type of input params are wrong to new a mat4 when num of params is 1")
  393. for i= 1, 16 do
  394. if params[1][i] ~= nil then
  395. obj[i] = params[1][i]
  396. else
  397. obj[i] = 0
  398. end
  399. end
  400. elseif 16 == size then
  401. for i= 1, 16 do
  402. obj[i] = params[i]
  403. end
  404. end
  405. setmetatable(obj, {__index = cc.mat4})
  406. return obj
  407. end
  408. function cc.mat4.getInversed(self)
  409. return mat4_getInversed(self)
  410. end
  411. function cc.mat4.transformVector(self, vector, dst)
  412. return mat4_transformVector(self, vector, dst)
  413. end
  414. function cc.mat4.multiply(self, mat)
  415. return mat4_multiply(self, mat)
  416. end
  417. function cc.mat4.decompose(self, scale, rotation, translation)
  418. return mat4_decompose(self, scale ,rotation, translation)
  419. end
  420. function cc.mat4.createIdentity()
  421. return cc.mat4.new(1.0 ,0.0, 0.0, 0.0,
  422. 0.0, 1.0, 0.0, 0.0,
  423. 0.0, 0.0, 1.0, 0.0,
  424. 0.0, 0.0, 0.0, 1.0)
  425. end
  426. function cc.mat4.createTranslation(translation, dst)
  427. assert(type(translation) == "table" and type(dst) == "table", "The type of input parameters should be table")
  428. dst = cc.mat4.createIdentity()
  429. dst[13] = translation.x
  430. dst[14] = translation.y
  431. dst[15] = translation.z
  432. return dst
  433. end
  434. function cc.mat4.createRotation(q, dst)
  435. assert(type(q) == "table" and type(dst) == "table", "The type of input parameters should be table")
  436. local x2 = q.x + q.x
  437. local y2 = q.y + q.y
  438. local z2 = q.z + q.z
  439. local xx2 = q.x * x2
  440. local yy2 = q.y * y2
  441. local zz2 = q.z * z2
  442. local xy2 = q.x * y2
  443. local xz2 = q.x * z2
  444. local yz2 = q.y * z2
  445. local wx2 = q.w * x2
  446. local wy2 = q.w * y2
  447. local wz2 = q.w * z2
  448. dst[1] = 1.0 - yy2 - zz2
  449. dst[2] = xy2 + wz2
  450. dst[3] = xz2 - wy2
  451. dst[4] = 0.0
  452. dst[5] = xy2 - wz2
  453. dst[6] = 1.0 - xx2 - zz2
  454. dst[7] = yz2 + wx2
  455. dst[8] = 0.0
  456. dst[9] = xz2 + wy2
  457. dst[10] = yz2 - wx2
  458. dst[11] = 1.0 - xx2 - yy2
  459. dst[12] = 0.0
  460. dst[13] = 0.0
  461. dst[14] = 0.0
  462. dst[15] = 0.0
  463. dst[16] = 1.0
  464. return dst
  465. end
  466. function cc.mat4.translate(self,vec3)
  467. return mat4_translate(self,vec3)
  468. end
  469. function cc.mat4.createRotationZ(self,angle)
  470. return mat4_createRotationZ(self,angle)
  471. end
  472. function cc.mat4.setIdentity(self)
  473. return mat4_setIdentity(self)
  474. end
  475. function cc.mat4.createTranslation(...)
  476. return mat4_createTranslation(...)
  477. end
  478. function cc.mat4.createRotation(...)
  479. return mat4_createRotation(...)
  480. end