Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector cleanups #2895

Merged
merged 2 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 19 additions & 37 deletions lua/entities/gmod_wire_expression2/core/vector.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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
Comment on lines +417 to +422
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are really stupid. Why don't they max/min the components? Is there a different function that does that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, maxVec declared right below this

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you even need that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could see uses for both.

end

--- component-wise min/max
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lua/wire/client/e2descriptions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading