|
|
CoVector2 = CoVector2 or BaseClass()
|
|
local CoVector2 = CoVector2
|
|
function CoVector2:__init(x, y)
|
|
self.x = x
|
|
self.y = y
|
|
|
|
-- local now_time = Status.NowTime
|
|
-- if CoVector2.cache_time == nil or CoVector2.cache_time < now_time then
|
|
-- -- if CoVector2.call_num == 1 then
|
|
-- print("创建Vector2个数", CoVector2.call_num, now_time)
|
|
-- -- print(xxxxx+3)
|
|
-- -- end
|
|
-- CoVector2.call_num = 0
|
|
-- CoVector2.cache_time = now_time
|
|
-- end
|
|
-- CoVector2.call_num = CoVector2.call_num + 1
|
|
-- if CoVector2.call_num >1 then
|
|
-- -- print("创建Vector2个数", CoVector2.call_num, now_time)
|
|
-- print(xxxxx+3)
|
|
-- end
|
|
|
|
local meta_table = getmetatable(self)
|
|
meta_table.__add = function (v1, v2)
|
|
return CoVector2.New(v1.x + v2.x, v1.y + v2.y)
|
|
end
|
|
|
|
meta_table.__sub = function (v1, v2)
|
|
return CoVector2.New(v1.x - v2.x, v1.y - v2.y)
|
|
end
|
|
|
|
meta_table.__mul = function (v1, v2)
|
|
local f = tonumber(v2)
|
|
if f ~= nil then
|
|
return CoVector2.New(v1.x * f, v1.y * f)
|
|
else
|
|
-- print("~+~+~+ co.Vector2 相乘2", v1.x, v1.y, v2.x, v2.y)
|
|
return CoVector2.New(v1.x * v2.x, v1.y * v2.y)
|
|
end
|
|
end
|
|
|
|
meta_table.__div = function (v1, v2)
|
|
local f = tonumber(v2)
|
|
if f ~= nil then
|
|
return CoVector2.New(v1.x / f, v1.y / f)
|
|
else
|
|
return CoVector2.New(v1.x / v2.x, v1.y / v2.y)
|
|
end
|
|
end
|
|
|
|
meta_table.__tostring = function (v1)
|
|
return string.format("co.Vector2(%.4f, %.4f)", v1.x, v1.y)
|
|
end
|
|
end
|
|
function CoVector2:__defineVar()
|
|
return {
|
|
_class_type = self,
|
|
--_cid = self._id,
|
|
_iid = _in_obj_ins_id,
|
|
x = 0,
|
|
y = 0
|
|
}
|
|
end
|
|
function CoVector2:__delete()
|
|
-- body
|
|
end
|
|
|
|
function CoVector2:copyVector2(v)
|
|
self.x, self.y = v.x, v.y
|
|
end
|
|
function CoVector2:setXY(x, y)
|
|
self.x, self.y = x, y
|
|
end
|
|
function CoVector2:Mul(x, y)
|
|
y = y or x
|
|
if x and y then
|
|
self.x, self.y = self.x * x, self.y * y
|
|
end
|
|
end
|
|
|
|
function CoVector2:length()
|
|
return math.sqrt(self.x * self.x + self.y * self.y)
|
|
end
|
|
|
|
function CoVector2:squaredLength()
|
|
return self.x * self.x + self.y * self.y
|
|
end
|
|
|
|
--把对象存进缓存队列
|
|
function CoVector2:DeleteV()
|
|
local free_num = co.free_vector2_num
|
|
|
|
if co.free_vector2_num < 100 then
|
|
co.free_vector2_num = free_num + 1
|
|
co.free_vector2_list[free_num+1] = self
|
|
end
|
|
end
|
|
|
|
|
|
function CoVector2:normalise()
|
|
local l = math.sqrt(self.x * self.x + self.y * self.y)
|
|
if l >= 1e-6 then
|
|
local f = 1 / l
|
|
self.x = self.x * f
|
|
self.y = self.y * f
|
|
end
|
|
return l
|
|
end
|
|
|
|
function CoVector2:distance(v2)
|
|
local dx = self.x - v2.x
|
|
local dy = self.y - v2.y
|
|
return math.sqrt(dx * dx + dy * dy)
|
|
end
|
|
|
|
function CoVector2:squaredDistance(v2)
|
|
local dx = self.x - v2.x
|
|
local dy = self.y - v2.y
|
|
return dx * dx + dy * dy
|
|
end
|
|
|
|
function CoVector2:dotProduct(v2)
|
|
return self.x * v2.x + self.y * v2.y
|
|
end
|
|
|
|
function CoVector2:crossProduct(v2)
|
|
return self.x * v2.y - self.y * v2.x
|
|
end
|
|
|
|
function CoVector2:angleBetween(v2)
|
|
local lenProduct = self:length() * v2:length()
|
|
if lenProduct < 1e-6 then
|
|
lenProduct = 1e-6
|
|
end
|
|
|
|
local f = self:dotProduct(v2) / lenProduct
|
|
if f < -1 then
|
|
f = -1
|
|
elseif f > 1 then
|
|
f = 1
|
|
end
|
|
|
|
return math.acos(f)
|
|
end
|
|
|
|
--返回值顺时针方向为正角度
|
|
function CoVector2:angleTo(v2)
|
|
local angle = self:angleBetween(v2)
|
|
if self:crossProduct(v2) < 0 then
|
|
angle = math.pi * 2 - angle
|
|
end
|
|
return angle
|
|
end
|
|
|
|
function CoVector2:toGameVector2()
|
|
if self.game_vector2 == nil then
|
|
self.game_vector2 = Game.Vector2(self.x, self.y)
|
|
else
|
|
self.game_vector2.x = self.x
|
|
self.game_vector2.y = self.y
|
|
end
|
|
return self.game_vector2
|
|
end
|
|
|
|
|
|
--沿着dir方向移动dist距离
|
|
function CoVector2:Move(dir, dist)
|
|
self.x = self.x + dir.x * dist
|
|
self.y = self.y + dir.y * dist
|
|
end
|
|
|
|
--把V1向量按照“X轴正方向转到V2方向”的方式旋转
|
|
function CoVector2:rotateFromXAxisTo(v2, proj_modify)
|
|
local r1 = co.XAix:angleTo(self)
|
|
local r2 = co.XAix:angleTo(v2)
|
|
local r = r1 + r2
|
|
local l = self:length()
|
|
|
|
local cos_r = math.cos(r)
|
|
local sin_r = math.sin(r)
|
|
|
|
--在转到90度的时候要缩短一些,以产生透视效果
|
|
local p = proj_modify and (cos_r * 0.2929 + 0.7071) or 1
|
|
self.x, self.y = cos_r * l, sin_r * l * p
|
|
return self
|
|
end
|
|
|
|
--radian为正数时顺时针旋转
|
|
function CoVector2:rotate(radian )
|
|
local r = co.XAix:angleTo(self)
|
|
r = r + radian
|
|
local l = self:length()
|
|
local cos_r = math.cos(r)
|
|
local sin_r = math.sin(r)
|
|
self.x, self.y = cos_r * l, sin_r * l
|
|
return self
|
|
end
|
|
|
|
|