Vector2_Tips.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. --------------------------------------------------------------------------------
  2. -- Copyright (c) 2015 , 蒙占志(topameng) topameng@gmail.com
  3. -- All rights reserved.
  4. -- Use, modification and distribution are subject to the "MIT License"
  5. --------------------------------------------------------------------------------
  6. -- added by wsh @ 2017-12-28
  7. -- 注意:
  8. -- 1、已经被修改,别从tolua轻易替换来做升级
  9. local sqrt = math.sqrt
  10. local setmetatable = setmetatable
  11. local rawget = rawget
  12. local math = math
  13. local acos = math.acos
  14. local max = math.max
  15. ---@class Vector2
  16. ---@field public x number
  17. ---@field public y number
  18. ---@field zero Vector2
  19. Vector2 = {}
  20. ---@class _getter
  21. local _getter = {}
  22. local unity_vector2 = CS.UnityEngine.Vector2
  23. Vector2.isValueType = true
  24. Vector2.__index = function(t,k)
  25. local var = rawget(Vector2, k)
  26. if var ~= nil then
  27. return var
  28. end
  29. var = rawget(_getter, k)
  30. if var ~= nil then
  31. return var(t)
  32. end
  33. return rawget(unity_vector2, k)
  34. end
  35. Vector2.__call = function(t, x, y)
  36. return setmetatable({x = x or 0, y = y or 0}, Vector2)
  37. end
  38. function Vector2.New(x, y)
  39. return setmetatable({x = x or 0, y = y or 0}, Vector2)
  40. end
  41. function Vector2:Set(x,y)
  42. self.x = x or 0
  43. self.y = y or 0
  44. end
  45. function Vector2:Copy(b)
  46. self:Set(b.x,b.y)
  47. end
  48. function Vector2:Get()
  49. return self.x, self.y
  50. end
  51. function Vector2:SqrMagnitude()
  52. return self.x * self.x + self.y * self.y
  53. end
  54. function Vector2:Clone()
  55. return setmetatable({x = self.x, y = self.y}, Vector2)
  56. end
  57. function Vector2.Normalize(v)
  58. local x = v.x
  59. local y = v.y
  60. local magnitude = sqrt(x * x + y * y)
  61. if magnitude > 1e-05 then
  62. x = x / magnitude
  63. y = y / magnitude
  64. else
  65. x = 0
  66. y = 0
  67. end
  68. return setmetatable({x = x, y = y}, Vector2)
  69. end
  70. function Vector2:SetNormalize()
  71. local magnitude = sqrt(self.x * self.x + self.y * self.y)
  72. if magnitude > 1e-05 then
  73. self.x = self.x / magnitude
  74. self.y = self.y / magnitude
  75. else
  76. self.x = 0
  77. self.y = 0
  78. end
  79. return self
  80. end
  81. function Vector2.Dot(lhs, rhs)
  82. return lhs.x * rhs.x + lhs.y * rhs.y
  83. end
  84. function Vector2.Angle(from, to)
  85. local x1,y1 = from.x, from.y
  86. local d = sqrt(x1 * x1 + y1 * y1)
  87. if d > 1e-5 then
  88. x1 = x1/d
  89. y1 = y1/d
  90. else
  91. x1,y1 = 0,0
  92. end
  93. local x2,y2 = to.x, to.y
  94. d = sqrt(x2 * x2 + y2 * y2)
  95. if d > 1e-5 then
  96. x2 = x2/d
  97. y2 = y2/d
  98. else
  99. x2,y2 = 0,0
  100. end
  101. d = x1 * x2 + y1 * y2
  102. if d < -1 then
  103. d = -1
  104. elseif d > 1 then
  105. d = 1
  106. end
  107. return acos(d) * 57.29578
  108. end
  109. function Vector2.Magnitude(v)
  110. return sqrt(v.x * v.x + v.y * v.y)
  111. end
  112. function Vector2.Reflect(dir, normal)
  113. local dx = dir.x
  114. local dy = dir.y
  115. local nx = normal.x
  116. local ny = normal.y
  117. local s = -2 * (dx * nx + dy * ny)
  118. return setmetatable({x = s * nx + dx, y = s * ny + dy}, Vector2)
  119. end
  120. function Vector2.Distance(a, b)
  121. return sqrt((a.x - b.x) ^ 2 + (a.y - b.y) ^ 2)
  122. end
  123. function Vector2.Lerp(a, b, t)
  124. if t < 0 then
  125. t = 0
  126. elseif t > 1 then
  127. t = 1
  128. end
  129. return setmetatable({x = a.x + (b.x - a.x) * t, y = a.y + (b.y - a.y) * t}, Vector2)
  130. end
  131. function Vector2.LerpUnclamped(a, b, t)
  132. return setmetatable({x = a.x + (b.x - a.x) * t, y = a.y + (b.y - a.y) * t}, Vector2)
  133. end
  134. function Vector2.MoveTowards(current, target, maxDistanceDelta)
  135. local cx = current.x
  136. local cy = current.y
  137. local x = target.x - cx
  138. local y = target.y - cy
  139. local s = x * x + y * y
  140. if s > maxDistanceDelta * maxDistanceDelta and s ~= 0 then
  141. s = maxDistanceDelta / sqrt(s)
  142. return setmetatable({x = cx + x * s, y = cy + y * s}, Vector2)
  143. end
  144. return setmetatable({x = target.x, y = target.y}, Vector2)
  145. end
  146. function Vector2.ClampMagnitude(v, maxLength)
  147. local x = v.x
  148. local y = v.y
  149. local sqrMag = x * x + y * y
  150. if sqrMag > maxLength * maxLength then
  151. local mag = maxLength / sqrt(sqrMag)
  152. x = x * mag
  153. y = y * mag
  154. return setmetatable({x = x, y = y}, Vector2)
  155. end
  156. return setmetatable({x = x, y = y}, Vector2)
  157. end
  158. function Vector2.SmoothDamp(current, target, Velocity, smoothTime, maxSpeed, deltaTime)
  159. deltaTime = deltaTime or Time.deltaTime
  160. maxSpeed = maxSpeed or math.huge
  161. smoothTime = math.max(0.0001, smoothTime)
  162. local num = 2 / smoothTime
  163. local num2 = num * deltaTime
  164. num2 = 1 / (1 + num2 + 0.48 * num2 * num2 + 0.235 * num2 * num2 * num2)
  165. local tx = target.x
  166. local ty = target.y
  167. local cx = current.x
  168. local cy = current.y
  169. local vecx = cx - tx
  170. local vecy = cy - ty
  171. local m = vecx * vecx + vecy * vecy
  172. local n = maxSpeed * smoothTime
  173. if m > n * n then
  174. m = n / sqrt(m)
  175. vecx = vecx * m
  176. vecy = vecy * m
  177. end
  178. m = Velocity.x
  179. n = Velocity.y
  180. local vec3x = (m + num * vecx) * deltaTime
  181. local vec3y = (n + num * vecy) * deltaTime
  182. Velocity.x = (m - num * vec3x) * num2
  183. Velocity.y = (n - num * vec3y) * num2
  184. m = cx - vecx + (vecx + vec3x) * num2
  185. n = cy - vecy + (vecy + vec3y) * num2
  186. if (tx - cx) * (m - tx) + (ty - cy) * (n - ty) > 0 then
  187. m = tx
  188. n = ty
  189. Velocity.x = 0
  190. Velocity.y = 0
  191. end
  192. return setmetatable({x = m, y = n}, Vector2), Velocity
  193. end
  194. function Vector2.Max(a, b)
  195. return setmetatable({x = math.max(a.x, b.x), y = math.max(a.y, b.y)}, Vector2)
  196. end
  197. function Vector2.Min(a, b)
  198. return setmetatable({x = math.min(a.x, b.x), y = math.min(a.y, b.y)}, Vector2)
  199. end
  200. function Vector2.Scale(a, b)
  201. return setmetatable({x = a.x * b.x, y = a.y * b.y}, Vector2)
  202. end
  203. function Vector2:Div(d)
  204. self.x = self.x / d
  205. self.y = self.y / d
  206. return self
  207. end
  208. function Vector2:Mul(d)
  209. self.x = self.x * d
  210. self.y = self.y * d
  211. return self
  212. end
  213. function Vector2:Add(b)
  214. self.x = self.x + b.x
  215. self.y = self.y + b.y
  216. return self
  217. end
  218. function Vector2:AddXY(x,y)
  219. self.x = self.x + x
  220. self.y = self.y + y
  221. return self
  222. end
  223. function Vector2:Sub(b)
  224. self.x = self.x - b.x
  225. self.y = self.y - b.y
  226. return
  227. end
  228. ---@param vec Vector4
  229. function Vector2:IsSelfMetatable(vec)
  230. return self.x == nil
  231. end
  232. Vector2.__tostring = function(self)
  233. return string.format("(%f,%f)", self.x, self.y)
  234. end
  235. Vector2.__div = function(va, d)
  236. return setmetatable({x = va.x / d, y = va.y / d}, Vector2)
  237. end
  238. Vector2.__mul = function(a, d)
  239. if type(d) == "number" then
  240. return setmetatable({x = a.x * d, y = a.y * d}, Vector2)
  241. else
  242. return setmetatable({x = a * d.x, y = a * d.y}, Vector2)
  243. end
  244. end
  245. Vector2.__add = function(a, b)
  246. return setmetatable({x = a.x + b.x, y = a.y + b.y}, Vector2)
  247. end
  248. Vector2.__sub = function(a, b)
  249. return setmetatable({x = a.x - b.x, y = a.y - b.y}, Vector2)
  250. end
  251. Vector2.__unm = function(v)
  252. return setmetatable({x = -v.x, y = -v.y}, Vector2)
  253. end
  254. Vector2.__eq = function(a,b)
  255. if not a.x or not b.x then
  256. return false
  257. end
  258. return ((a.x - b.x) ^ 2 + (a.y - b.y) ^ 2) < 9.999999e-11
  259. end
  260. _getter.up = function() return setmetatable({x = 0, y = 1}, Vector2) end
  261. _getter.right = function() return setmetatable({x = 1, y = 0}, Vector2) end
  262. _getter.zero = function() return setmetatable({x = 0, y = 0}, Vector2) end
  263. _getter.one = function() return setmetatable({x = 1, y = 1}, Vector2) end
  264. _getter.magnitude = Vector2.Magnitude
  265. _getter.normalized = Vector2.Normalize
  266. _getter.sqrMagnitude = Vector2.SqrMagnitude
  267. Vector2.unity_vector2 = CS.UnityEngine.Vector2
  268. CS.UnityEngine.Vector2 = Vector2
  269. setmetatable(Vector2, Vector2)
  270. Vector2.zeroNonAlloc = Vector2(0,0)