Once again i am having a brainfart with some simple vector math.
I’m trying to modify some old code and make it more modular.
Here’s some code from an old SWEP. It basically allows you to specify the sprint position for a weapon, using the GetViewModelPosition function.
[lua]
function SWEP:AngApproach( newang, ang, mul )
ang:RotateAroundAxis( ang:Right(), newang.x * mul )
ang:RotateAroundAxis( ang:Up(), newang.y * mul )
ang:RotateAroundAxis( ang:Forward(), newang.z * mul )
return ang
end
function SWEP:PosApproach( newpos, pos, ang, 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
end
function SWEP:MoveViewModelTo( newpos, newang, pos, ang, mul )
ang = self:AngApproach( newang, ang, mul )
pos = self:PosApproach( newpos, pos, ang, mul )
return pos, ang
end
function SWEP:GetViewModelPosition( pos, ang )
// shortened for sake of brevity - imagine this is what gets returned if you sprint
return self:MoveViewModelTo( SomeSprintPosition, SomeSprintAngle, pos, ang, timescale )
end[/lua]
So basically it calculates the position/angle of the viewmodel based on a time scale for any amount of time (between 0 and 1) and an arbitrary sprint position/angle and adds it to the original position/angle.
What i want to do is store a single position/angle which can change at any time, and calculate the position of the viewmodel based on that using math.Approach. This would mean smooth transitions between an infinite amount of viewmodel positions.
I hope i explained that well enough. Here’s the code i have currently.
[lua]
function SWEP:ApproachAngPos( pos, ang, basepos, baseang, speed ) // note that self.CurAng and self.CurPos are initialized to Vector(0,0,0) when the SWEP is created, which means the viewmodel is in the normal position.
self.CurAng.x = math.Approach( self.CurAng.x, ang.x, FrameTime() * speed )
self.CurAng.y = math.Approach( self.CurAng.y, ang.y, FrameTime() * speed )
self.CurAng.z = math.Approach( self.CurAng.z, ang.z, FrameTime() * speed ) // this works
self.CurPos.x = math.Approach( self.CurPos.x, pos.x, FrameTime() * speed )
self.CurPos.y = math.Approach( self.CurPos.y, pos.y, FrameTime() * speed )
self.CurPos.z = math.Approach( self.CurPos.z, pos.z, FrameTime() * speed ) // this shit is wrong
return basepos + self.CurPos, baseang + self.CurAng
end
function SWEP:GetViewModelPosition( pos, ang )
return self.Weapon:ApproachAngPos( SomeDifferentTargetPos, SomeDifferentTargetAng, pos, ang, 25 )
end[/lua]
If you compare the old code and the new code, you’ll notice that i replicated the angle calculating properly. I tested it and it worked. But the position calculations are a little trickier since the original code requires some multiplication with the angles.
Any insight would be appreciated, my brain isn’t in the mood for this right now.