Skip to content

Commit

Permalink
WireLib.clampPos/Force optimization
Browse files Browse the repository at this point in the history
Speeds up WireLib.clampPos by about 2x and WireLib.clampForce by about 5x
  • Loading branch information
Astralcircle authored Dec 15, 2024
1 parent eb9451c commit b3bdd27
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions lua/wire/wireshared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -942,11 +942,7 @@ local minx, miny, minz = -16384, -16384, -16384
local maxx, maxy, maxz = 16384, 16384, 16384
local clamp = math.Clamp
function WireLib.clampPos(pos)
pos = Vector(pos)
pos.x = clamp(pos.x, minx, maxx)
pos.y = clamp(pos.y, miny, maxy)
pos.z = clamp(pos.z, minz, maxz)
return pos
return Vector(clamp(pos.x, minx, maxx), clamp(pos.y, miny, maxy), clamp(pos.z, minz, maxz))
end

function WireLib.setPos(ent, pos)
Expand Down Expand Up @@ -1014,10 +1010,12 @@ end)

-- Nan never equals itself, so if the value doesn't equal itself replace it with 0.
function WireLib.clampForce( v )
local x, y, z = v:Unpack()

return Vector(
v[1] == v[1] and math.Clamp( v[1], min_force, max_force ) or 0,
v[2] == v[2] and math.Clamp( v[2], min_force, max_force ) or 0,
v[3] == v[3] and math.Clamp( v[3], min_force, max_force ) or 0
x == x and clamp( x, min_force, max_force ) or 0,
y == y and clamp( y, min_force, max_force ) or 0,
z == z and clamp( z, min_force, max_force ) or 0
)
end

Expand Down

0 comments on commit b3bdd27

Please sign in to comment.