Interpolation Trouble

im just trying to get the player viewmodel to interpolate its position to an specific vector , it interpolates to the vector successfully but when i try to return it back to its original position rather than moving smooth-ish it just snaps back

[LUA]function SWEP:GetViewModelPosition( pos, ang )

if ( !self.IronPos ) then return pos, ang end

local bInterp = self.Weapon:GetNetworkedBool( “Interpolate” ) // On SWEP:SecondaryAttack i set both interpolate and Ironsight to true
local bIron = self.Weapon:GetNetworkedBool(“Ironsight”)
local NewPos = Vector(0,0,0)
local NewAng = Vector(0,0,0)

if(bIron == true) then
NewPos = self.IronPos;
NewAng = self.IronAng;
end

if ( bInterp != self.bLastInterp ) then

  self.bLastInterp = bInterp 
  self.finterpTime = CurTime()
  
  if ( bInterp ) then 
     self.SwayScale    = 0.3
     self.BobScale  = 0.1
  else 
     self.SwayScale    = 1.0
     self.BobScale  = 1.0
  end

end

local fIronTime = self.fIronTime or 0

if ( !bInterp && finterpTime < CurTime() - INTERPOLATION_TIME ) then
return pos, ang
end

local Mul = 1.0

if ( finterpTime > CurTime() - INTERPOLATION_TIME ) then

  Mul = math.Clamp( (CurTime() - finterpTime) / INTERPOLATION_TIME, 0, 1 )
  
  if (!bInterp) then Mul = 1 - Mul end

end

  ang = ang * 1
  ang:RotateAroundAxis( ang:Right(),     NewAng.x * Mul )
  ang:RotateAroundAxis( ang:Up(),     NewAng.y * Mul )
  ang:RotateAroundAxis( ang:Forward(),   NewAng.z * Mul )

local Right = ang:Right()
local Up = ang:Up()
local Forward = ang:Forward()

pos = pos + NewPos.x * Right * Mul
pos = pos + NewPos.y * Forward * Mul
pos = pos + NewPos.z * Up * Mul

return pos, ang

end[/LUA]

So yeah…

Upon first look, I think your “snapping” problem comes from this part of the code:

[LUA]
if ( !bInterp && finterpTime < CurTime() - INTERPOLATION_TIME ) then
return pos, ang
end
[/LUA]

If bInterp is only set to true when you aim, it will be set to false when you aren’t aiming, thus returning the original position and angle. I might be wrong, but I suggest looking into this.

Alright so tried setting it to true as soon i aim ( and leave it like that even when i un-aim ) and the result is nearly the same, first time i press right click = interpolates fine, press it again snaps back, now if i try to aim AGAIN it snaps back to the position rather than moving smooth ish

You are using a really hard method.

[LUA]local NewPos = Vector(0,0,0)
local NewAng = Vector(0,0,0)
function SWEP:GetViewModelPosition( pos, ang )

if ( !self.IronPos ) then return pos, ang end

local bInterp = self.Weapon:GetNetworkedBool( “Interpolate” ) // On SWEP:SecondaryAttack i set both interpolate and Ironsight to true
local bIron = self.Weapon:GetNetworkedBool(“Ironsight”)

if bIron then
NewPos = Lerp(FrameTime(), NewPos, self.IronPos)
NewAng = Lerp(FrameTime(), NewAng, self.IronAng)
else
NewPos = Lerp(FrameTime(), NewPos, Vector(0,0,0))
NewAng = Lerp(FrameTime(), NewAng, Vector(0,0,0))
end

if ( bInterp ) then
self.SwayScale = 0.3
self.BobScale = 0.1
else
self.SwayScale = 1.0
self.BobScale = 1.0
end

ang = ang * 1
ang:RotateAroundAxis( ang:Right(), NewAng.x )
ang:RotateAroundAxis( ang:Up(), NewAng.y )
ang:RotateAroundAxis( ang:Forward(), NewAng.z )

local Right = ang:Right()
local Up = ang:Up()
local Forward = ang:Forward()

pos = pos + NewPos.x * Right
pos = pos + NewPos.y * Forward
pos = pos + NewPos.z * Up

return pos, ang

end[/LUA]

There’s a global for linear interpolation.

[LUA]Lerp(delta, from, to)[/LUA]

Also, using FrameTime() keeps it smooth even on low framerates.

feel free to correct me if im wrong but Lerp only interpolates numbers, LerpVector is the function that actually interpolates an vector and yes i have alredy tried it as well ( using frametime to try to keep it smooth ) but it dident worked well…probably missed someting

‘Lerp’ is defined via substraction, multiplication and addition. All operators of which vectors support, so no, it works just fine with vectors or anything else that support those operators.

A little uglier for garbage collection, though.

Although to be fair I’m not sure how nicely LerpVector and LerpAngle behave.

alright, Thanks for the help ! its working now