VectorHelper = {} function VectorHelper.Dot(v1, v2) return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z end function VectorHelper.Mul(v1, v2) return Vector.New(v1.X * v2.X, v1.Y * v2.Y, v1.Z * v2.Z) end function VectorHelper.Mul2D(v1, v2) return {X = v1.X * v2.X, Y = v1.Y * v2.Y} end function VectorHelper.MulScalar(v1, S) return Vector.New(v1.X * S, v1.Y * S, v1.Z * S) end function VectorHelper.MulScalar2D(v1, S) return Vector2D.New(v1.X * S, v1.Y * S) end function VectorHelper.MulNumber(v1, number) return Vector.New(v1.X * number, v1.Y * number, v1.Z * number) end function VectorHelper.MulNumber2D(v1, number) -- return Vector.New(v1.X * number, v1.Y * number) return {X = v1.X * number, Y = v1.Y * number} end function VectorHelper.Sub(v1, v2) return Vector.New(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z) end function VectorHelper.Sub2D(v1, v2) return Vector2D.New(v1.X - v2.X, v1.Y - v2.Y) end function VectorHelper.Add(v1, v2) return Vector.New(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z) end function VectorHelper.Add2D(v1, v2) return Vector2D.New(v1.X + v2.X, v1.Y + v2.Y) end function VectorHelper.Div2DScalar(v1, s) return Vector2D.New(v1.X / s , v1.Y / s); end --- 归一化方法 ---@param v FVector function VectorHelper.Normalize(v) local Len = VectorHelper.Length(v) return VectorHelper.ToLuaTable(VectorHelper.MulNumber(v, 1./Len)); end --- 归一化方法 ---@param v FVector2D function VectorHelper.Normalize2D(v) local Len = VectorHelper.Length2D(v) return VectorHelper.ToLuaTable2D(VectorHelper.MulNumber2D(v, 1./Len)); end function VectorHelper.ToString(v) return string.format("(%.2f,%.2f,%.2f)", v.X, v.Y, v.Z) end ---@param v FVector ---@return FRotator function VectorHelper.VecToRot(v) return { Roll = v.X, Pitch = v.Y, Yaw = v.Z }; end function VectorHelper.ToString2D(v) return string.format("(%.2f,%.2f)", v.X, v.Y) end function VectorHelper.RotToString(v) return string.format("(Roll = %.2f, Pitch = %.2f, Yaw = %.2f)", v.Roll, v.Pitch, v.Yaw) end function VectorHelper.ToStringInt2D(v) if v == nil then return "[X=nil,Y=nil]"; end return string.format("[%s, %s]", tostring(v.X), tostring(v.Y)); end function VectorHelper.ToIntString(v) return "(" .. tostring(math.floor(v.X)) .. "," .. tostring( math.floor(v.Y)) .. "," .. tostring(math.floor(v.Z)) .. ")"; end ---@param v FVector ---@return FVector function VectorHelper.HandleLessVector(v) if v == nil then return { X = 0, Y = 0, Z = 0 }; end if v.X == nil then v.X = 0; end if v.Y == nil then v.Y = 0; end if v.Z == nil then v.Z = 0; end return { X = v.X, Y = v.Y, Z = v.Z}; end ---@param v FRotator ---@return FRotator function VectorHelper.HandleLessRotator(v) if v == nil then return { Pitch = 0, Yaw = 0, Roll = 0}; end if v.Pitch == nil then v.Pitch = 0; end if v.Yaw == nil then v.Yaw = 0; end if v.Roll == nil then v.Roll = 0; end return { Pitch = v.Pitch, Yaw = v.Yaw, Roll = v.Roll }; end ---@param v FVector2D ---@return FVector2D function VectorHelper.HandleLessVector2D(v) if v == nil then return { X = 0, Y = 0}; end if v.X == nil then v.X = 0; end if v.Y == nil then v.Y = 0; end --if v.Z == nil then v.Z = 0; end return { X = v.X, Y = v.Y}; end function VectorHelper.Cross(v1, v2) return Vector.New(v1.Y * v2.Z - v2.Y * v1.Z, v1.Z * v2.X - v2.Z * v1.X, v1.X * v2.Y - v2.X * v1.Y) end function VectorHelper.GetDistance(vector1, vector2) if vector1 == nil or vector2 == nil then return 0 end if vector1.X == nil or vector2.X == nil then return 0 end if vector1.Y == nil or vector2.Y == nil then return 0 end if vector1.Z == nil or vector2.Z == nil then return 0 end local disX = vector1.X - vector2.X local disY = vector1.Y - vector2.Y local disZ = vector1.Z - vector2.Z return math.sqrt(disX ^ 2 + disY ^ 2 + disZ ^ 2) end function VectorHelper.GetDistance2D(vector1, vector2) if vector1 == nil or vector2 == nil then return 0 end if vector1.X == nil or vector2.X == nil then return 0 end if vector1.Y == nil or vector2.Y == nil then return 0 end local disX = vector1.X - vector2.X local disY = vector1.Y - vector2.Y return math.sqrt(disX ^ 2 + disY ^ 2) end function VectorHelper.LengthSquared(v) return v.X ^ 2 + v.Y ^ 2 + v.Z ^ 2 end function VectorHelper.LengthSquared2D(v) return v.X ^ 2 + v.Y ^ 2 end function VectorHelper.Length(v) return math.sqrt(VectorHelper.LengthSquared(v)) end function VectorHelper.Length2D(v) return math.sqrt(VectorHelper.LengthSquared2D(v)) end function VectorHelper.ToLuaTable(v) return {X = v.X, Y = v.Y, Z = v.Z} end function VectorHelper.ToLuaTable2D(v) return {X = v.X, Y = v.Y} end function VectorHelper.RotToLuaTable(v) return {Roll=v.Roll, Pitch=v.Pitch, Yaw=v.Yaw} end function VectorHelper.ColorToVector(LinearColor) return {X = LinearColor.R, Y = LinearColor.G, Z = LinearColor.B } end function VectorHelper.RotToString(v) return string.format("Roll=%.2f, Pitch=%.2f, Yaw=%.2f", v.Roll, v.Pitch, v.Yaw); end ---@param v FRotator ---@param v1 FRotator ---@return FRotator function VectorHelper.RotAdd(v, v1) return { Pitch = v.Pitch + v1.Pitch, Yaw = v.Yaw + v1.Yaw, Roll = v.Roll + v1.Roll, }; end ---@param v FRotator ---@param c number ---@return FRotator function VectorHelper.RotMulNumber(v, c) return { Pitch = v.Pitch * c, Yaw = v.Yaw * c, Roll = v.Roll * c}; end ---@param v FRotator ---@param v1 FRotator ---@return FRotator function VectorHelper.RotMul(v, v1) return { Pitch = v.Pitch * v.Pitch, Yaw = v.Yaw * v.Yaw, Roll = v.Roll * v.Roll}; end function VectorHelper.GetMiddlePoint(vector1, vector2) return {X = (vector1.X + vector2.X) / 2, Y = (vector1.Y + vector2.Y) / 2,Z = (vector1.Z + vector2.Z) / 2} end function VectorHelper.FromCpp(CppVector) return {X = CppVector.X, Y = CppVector.Y, Z = CppVector.Z}; end function VectorHelper.Equal(vector1, vector2) return (vector1.X == vector2.X and vector1.Y == vector2.Y and vector1.Z == vector2.Z); end ---@param v FVector|table function VectorHelper.MakeVector(v) if table.isEmpty(v) then return {}; end if v.X == nil and v.Y == nil and v.Z == nil then -- 此时是 table return { X = v[1], Y = v[2], Z = v[3] }; else return VectorHelper.ToLuaTable(v); end end function VectorHelper.MakeVector(v1, v2, ...) return { X = v1, Y = v2, Z = ... }; end ---@param v FVector|table|FRotator function VectorHelper.MakeRotator(v) if table.isEmpty(v) then return {}; end if v.X == nil and v.Y == nil and v.Z == nil and v.Pitch == nil and v.Roll == nil and v.Yaw == nil then -- 此时是 table return { Pitch = v[1], Roll = v[2], Yaw = v[3] }; elseif v.X ~= nil or v.Y ~= nil or v.Z ~= nil then return { Pitch = v.X, Roll = v.Y, Yaw = v.Z }; else return { Pitch = v.Pitch, Roll = v.Roll, Yaw = v.Yaw }; end end --计算两点之间连线与Y轴的夹角 function VectorHelper.GetAngleByPos(PosA,PosB) local P = {}; P.X = PosB.X - PosA.X; P.Y = PosB.Y - PosA.Y; local Pi = 3.1415926; local Dis = math.sqrt( (P.X * P.X) + (P.Y * P.Y)); local FAngle = math.acos (P.Y/ Dis); if( P.X < 0) then FAngle = 2 * Pi - FAngle; end local Angle = FAngle * 180 / Pi; Angle = Angle > 0 and (90 - Angle) or (90 + math.abs(Angle)); return Angle; end function VectorHelper.RotZero() return {Roll = 0, Pitch = 0, Yaw = 0} end function VectorHelper.VectorZero() return {X = 0, Y = 0, Z = 0} end function VectorHelper.ScaleOne() return {X = 1, Y = 1, Z = 1} end function VectorHelper.CosineValue(v1, v2) return VectorHelper.Dot(v1, v2) / (VectorHelper.Length(v1) * VectorHelper.Length(v2)) end function VectorHelper.Vector2DInRange(v, vMin, vMax) return (v.X <= vMax.X and v.X >= vMin.X and v.Y <= vMax.Y and v.Y >= vMin.Y) end function VectorHelper.VectorInRange(v, vMin, vMax) return (v.X <= vMax.X and v.X >= vMin.X and v.Y <= vMax.Y and v.Y >= vMin.Y and v.Z <= vMax.Z and v.Z >= vMin.Z) end return VectorHelper;