Vector cleanups (#2895)

* Vector cleanup

* Fix indentation
This commit is contained in:
Denneisk 2023-11-27 04:20:10 -05:00 committed by GitHub
parent 3764d10f6b
commit 0643eabefa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 37 deletions

View File

@ -13,26 +13,7 @@ local asin = math.asin
local rad2deg = 180 / pi
local deg2rad = pi / 180
-- Remove this when a later, mandatory update is made.
-- These were added in the August (08) 9 (09) 2023 (23) update
if VERSION < 230809 then
function math.CubicBezier(frac, p0, p1, p2, p3)
local frac2 = frac * frac
local inv = 1 - frac
local inv2 = inv * inv
return inv2 * inv * p0 + 3 * inv2 * frac * p1 + 3 * inv * frac2 * p2 + frac2 * frac * p3
end
function math.QuadraticBezier(frac, p0, p1, p2)
local frac2 = frac * frac
local inv = 1 - frac
local inv2 = inv * inv
return inv2 * p0 + 2 * inv * frac * p1 + frac2 * p2
end
end
local LerpVector = LerpVector
local quadraticBezier = math.QuadraticBezier
local cubicBezier = math.CubicBezier
@ -215,9 +196,10 @@ end
__e2setcost(5)
--- Returns a random vector with its components between <min> and <max>
e2function vector randvec( normal min, normal max)
local range = max-min
return Vector(min+random()*range, min+random()*range, min+random()*range)
e2function vector randvec(number min, number max)
local v = Vector()
v:Random(min, max)
return v
end
--- Returns a random vector between <min> and <max>
@ -432,16 +414,12 @@ end
__e2setcost(10)
--- min/max based on vector length - returns shortest/longest vector
e2function vector min(vector rv1, vector rv2)
local length1 = ( rv1[1] * rv1[1] + rv1[2] * rv1[2] + rv1[3] * rv1[3] ) ^ 0.5
local length2 = ( rv2[1] * rv2[1] + rv2[2] * rv2[2] + rv2[3] * rv2[3] ) ^ 0.5
if length1 < length2 then return rv1 else return rv2 end
e2function vector min(vector vec1, vector vec2)
return vec1:LengthSqr() < vec2:LengthSqr() and vec1 or vec2
end
e2function vector max(vector rv1, vector rv2)
local length1 = ( rv1[1] * rv1[1] + rv1[2] * rv1[2] + rv1[3] * rv1[3] ) ^ 0.5
local length2 = ( rv2[1] * rv2[1] + rv2[2] * rv2[2] + rv2[3] * rv2[3] ) ^ 0.5
if length1 > length2 then return rv1 else return rv2 end
e2function vector max(vector vec1, vector vec2)
return vec1:LengthSqr() > vec2:LengthSqr() and vec1 or vec2
end
--- component-wise min/max
@ -498,9 +476,13 @@ e2function vector clamp(vector value, vector min, vector max)
return Vector(x, y, z)
end
--- Mix two vectors by a given proportion (between 0 and 1)
e2function vector mix(vector vec1, vector vec2, ratio)
return vec1 * ratio + vec2 * (1 - ratio)
e2function vector lerp(vector from, vector to, fraction)
return LerpVector(fraction, from, to)
end
[deprecated = "Use lerp instead"]
e2function vector mix(vector to, vector from, fraction)
return LerpVector(fraction, from, to)
end
e2function vector bezier(vector startVec, vector tangent, vector endVec, ratio)
@ -543,11 +525,11 @@ end
__e2setcost(3)
e2function angle vector:toAngle()
return Vector(this[1], this[2], this[3]):Angle()
return this:Angle()
end
e2function angle vector:toAngle(vector up)
return Vector(this[1], this[2], this[3]):AngleEx(up)
return this:AngleEx(up)
end
--------------------------------------------------------------------------------
@ -654,7 +636,7 @@ end
--------------------------------------------------------------------------------
-- Credits to Wizard of Ass for bearing(v,a,v) and elevation(v,a,v)
local ANG_ZERO = Angle(0, 0, 0)
local ANG_ZERO = angle_zero
e2function number bearing(vector originpos, angle originangle, vector pos)
pos = WorldToLocal(pos, ANG_ZERO, originpos, originangle)
return rad2deg * -atan2(pos.y, pos.x)

View File

@ -512,6 +512,7 @@ E2Helper.Descriptions["length(xv4:)"] = "Gets the length of the vector"
E2Helper.Descriptions["length2(xv2:)"] = "Gets the squared length of the vector"
E2Helper.Descriptions["length2(v:)"] = "Gets the squared length of the vector"
E2Helper.Descriptions["length2(xv4:)"] = "Gets the squared length of the vector"
E2Helper.Descriptions["lerp(vvn)"] = "Performs linear interpolation. Returns a new value between 'from' and 'to', based on a 0-1 percentage ('fraction')"
E2Helper.Descriptions["distance(xv2:xv2)"] = "Gets the distance between 2D vectors"
E2Helper.Descriptions["distance(v:v)"] = "Gets the distance between vectors"
E2Helper.Descriptions["distance(xv4:xv4)"] = "Gets the distance between 4D vectors"