在2d游戏中常用的向量方式

function cc.exports.VectorRotateByAngle(vector,angle)--计算向量旋转后的向量,angle:正数逆时针,负输顺时针

    angle = angle*math.pi/180

    local sinAngle = math.sin(angle)

    local cosAngle = math.cos(angle)

    return cc.p(vector.x * cosAngle - vector.y * sinAngle,vector.x * sinAngle + vector.y * cosAngle)

end

function cc.exports.AngleBetweenVector(v1,v2)--计算两个向量的夹角,值为正数

    local n = v1.x*v2.x + v1.y*v2.y

    local m = Vector2ToLen(v1)*Vector2ToLen(v2)

    return math.acos(n/m)*(180/math.pi)

end

function cc.exports.NomalizeVector(vector)--单位化向量

    local vecLen = math.sqrt(vector.x*vector.x + vector.y*vector.y)--单位化向量

    return cc.p(vector.x/vecLen,vector.y/vecLen)

end

function cc.exports.VectorToLen(vec2)--计算向量的长度

    return math.sqrt(vec2.x*vec2.x+vec2.y*vec2.y)

end

原文地址:https://www.cnblogs.com/HemJohn/p/4818816.html