mirror of
https://github.com/Winded/RagdollMover.git
synced 2025-03-04 03:13:36 -05:00
Merge pull request #3 from vlazed/angle-snapping-fix
Angle snapping fix
This commit is contained in:
commit
20d08d7920
@ -654,36 +654,51 @@ do
|
||||
return result
|
||||
end
|
||||
|
||||
local mfmod = math.fmod
|
||||
local snapAngle do
|
||||
local accumulated = 0
|
||||
local lastStartAngle = 0
|
||||
local oldLocalAngle = 0
|
||||
|
||||
local function snapAngle(localized, startangle, snapamount)
|
||||
local localAng = mfmod(localized.y, 360)
|
||||
if localAng > 181 then localAng = localAng - 360 end
|
||||
if localAng < -181 then localAng = localAng + 360 end
|
||||
local floor = math.floor
|
||||
local ceil = math.ceil
|
||||
|
||||
local localStart = mfmod(startangle.y, 360)
|
||||
if localStart > 181 then localStart = localStart - 360 end
|
||||
if localStart < -181 then localStart = localStart + 360 end
|
||||
-- Accumulate delta angles per frame until startangle is different (stopped rotating)
|
||||
-- Allows for correct snapped angles set by the rotation delta
|
||||
function snapAngle(localized, startangle, snapamount, nonphys)
|
||||
local localAng = localized.y
|
||||
|
||||
if lastStartAngle ~= startangle.y then
|
||||
accumulated = 0
|
||||
oldLocalAngle = localAng
|
||||
lastStartAngle = startangle.y
|
||||
end
|
||||
|
||||
local diff = mfmod(localStart - localAng, 360)
|
||||
if diff > 181 then diff = diff - 360 end
|
||||
if diff < -181 then diff = diff + 360 end
|
||||
-- https://discussions.unity.com/t/can-i-read-from-a-rotation-that-doesnt-wrap-from-360-to-zero/621621/7
|
||||
while (localAng < oldLocalAngle - 180) do
|
||||
localAng = localAng + 360
|
||||
end
|
||||
while (localAng > oldLocalAngle + 180) do
|
||||
localAng = localAng - 360
|
||||
end
|
||||
|
||||
-- TODO:
|
||||
-- Workaround to rotating by 180 degrees by removing normalization step
|
||||
-- Still buggy, but allows rotating beyond 180 degrees
|
||||
if snapamount == 180 then
|
||||
diff = mfmod(localStart - localAng, 360)
|
||||
local delta = oldLocalAngle - localAng
|
||||
accumulated = accumulated + delta
|
||||
oldLocalAngle = localAng
|
||||
|
||||
local mathfunc = nil
|
||||
if accumulated >= 0 then
|
||||
mathfunc = floor
|
||||
else
|
||||
mathfunc = ceil
|
||||
end
|
||||
|
||||
|
||||
if nonphys then
|
||||
return -mathfunc(accumulated / snapamount) * snapamount
|
||||
else
|
||||
return startangle.y - (mathfunc(accumulated / snapamount) * snapamount)
|
||||
end
|
||||
end
|
||||
|
||||
local mathfunc = nil
|
||||
if diff >= 0 then
|
||||
mathfunc = math.floor
|
||||
else
|
||||
mathfunc = math.ceil
|
||||
end
|
||||
|
||||
return startangle.y - (mathfunc(diff / snapamount) * snapamount)
|
||||
end
|
||||
|
||||
function disc:ProcessMovement(_, offang, eyepos, eyeang, ent, bone, ppos, pnorm, movetype, snapamount, startangle, _, nphysangle) -- initially i had a table instead of separate things for initial bone pos and angle, but sync command can't handle tables and i thought implementing a way to handle those would be too much hassle
|
||||
@ -778,14 +793,7 @@ do
|
||||
|
||||
local rotationangle = localized.y
|
||||
if snapamount ~= 0 then
|
||||
local localAng = mfmod(localized.y, 360)
|
||||
if localAng > 181 then localAng = localAng - 360 end
|
||||
if localAng < -181 then localAng = localAng + 360 end
|
||||
|
||||
local mathfunc = math.floor
|
||||
if localAng < 0 then mathfunc = math.ceil end
|
||||
|
||||
rotationangle = mathfunc(localAng / snapamount) * snapamount
|
||||
rotationangle = snapAngle(localized, startangle, snapamount, true)
|
||||
end
|
||||
|
||||
if self.axistype == 4 then
|
||||
|
@ -2720,7 +2720,8 @@ local RGM_NOTIFY = { -- table with info for messages, true for errors
|
||||
[BONE_UNFROZEN] = false,
|
||||
}
|
||||
|
||||
local pl
|
||||
-- If we hotload this file, pl gets set to nil, which causes issues when tables attempt to index with this variable; initialize it here
|
||||
local pl = LocalPlayer()
|
||||
|
||||
hook.Add("InitPostEntity", "rgmSetPlayer", function()
|
||||
pl = LocalPlayer()
|
||||
|
Loading…
Reference in New Issue
Block a user