Influence CalcView based on weapon muzzle angle

Hi,

I was just wondering if it would be possible to influence the player’s view angles based on the muzzle of the weapon. I’m not sure how to make it so that sprinting/other viewmodel position modifiers don’t affect the attachment angle I’m reading, so I’ve just removed this feature from my code until now. Here’s what I have:



function SWEP:CalcView(ply, pos, ang, fov)

	if ply != LocalPlayer() then return end
	
	if !CLIENT then return end
	local viewbobintensity = 0.3 * GetConVarNumber("sv_tfa_viewbob_intensity",1)
	
	pos, ang = self:CalculateBob( pos, ang, viewbobintensity )
	
	return pos, ang , fov
	
end

function SWEP:GetViewModelPosition(pos, ang)

	local isp=self.CLIronSightsProgress--self:GetIronSightsRatio()
	local rsp=self.CLRunSightsProgress--self:GetRunSightsRatio()
	local nwp=self.CLNearWallProgress--self:GetNearWallRatio()
	local tmp_ispos = self.SightsPos or self.IronSightsPos
	local tmp_isa = self.SightsAng or self.IronSightsAng
	local tmp_rspos = self.RunSightsPos or tmp_ispos
	local tmp_rsa  = self.RunSightsAng or tmp_isa
	if tmp_isa==nil then 
		return
	end
	local ang2=Angle(ang.p,ang.y,ang.r)
	local ang3=Angle(ang.p,ang.y,ang.r)	
	local ang4=Angle(ang.p,ang.y,ang.r)	
	
	self.SwayScale 	= Lerp(isp,1,self.IronBobMult)
	self.SwayScale  = Lerp(rsp,self.SwayScale,self.SprintBobMult)
	
	--self.BobScale 	= Lerp(isp,1,self.IronBobMult)
	--self.BobScale  = Lerp(rsp,self.BobScale,self.SprintBobMult)
	self.BobScale = 0 
	self.BobScaleCustom 	= Lerp(isp,1,self.IronBobMult)
	self.BobScaleCustom  = Lerp(rsp,self.BobScaleCustom,self.SprintBobMult)
	
	ang2:RotateAroundAxis(ang2:Right(), 		tmp_isa.x)
	ang2:RotateAroundAxis(ang2:Up(), 		tmp_isa.y)
	ang2:RotateAroundAxis(ang2:Forward(), 	tmp_isa.z)
	
	ang=QerpAngle(isp, ang, ang2)
	
	ang3:RotateAroundAxis(ang3:Right(), 		tmp_rsa.x)
	ang3:RotateAroundAxis(ang3:Up(), 		tmp_rsa.y)
	ang3:RotateAroundAxis(ang3:Forward(), 	tmp_rsa.z)
	
	ang=QerpAngle(rsp, ang, ang3)
	
	local tmp_nwsightsang = tmp_rsa
	if self.NearWallSightsAng then
		tmp_nwsightsang = self.NearWallSightsAng
	end
	
	ang4:RotateAroundAxis(ang4:Right(), 		tmp_nwsightsang.x)
	ang4:RotateAroundAxis(ang4:Up(), 		tmp_nwsightsang.y)
	ang4:RotateAroundAxis(ang4:Forward(), 	tmp_nwsightsang.z)
	
	ang=QerpAngle(nwp, ang, ang4)
	
	
	pos = pos + self.VMOffset
	
	target = pos * 1 -- Copy pos to target
	target:Add( ang:Right() * (tmp_ispos.x) )
	target:Add( ang:Forward() * (tmp_ispos.y) )
	target:Add( ang:Up() * (tmp_ispos.z) )
		
	pos=QerpVector( isp, pos, target)
	
	target = pos * 1 -- Copy pos to target
	target:Add( ang:Right() * (tmp_rspos.x) )
	target:Add( ang:Forward() * (tmp_rspos.y) )
	target:Add( ang:Up() * (tmp_rspos.z) )
		
	pos=QerpVector( rsp, pos, target)
	
	local tmp_nwsightspos = tmp_rspos
	if self.NearWallSightsPos then
		tmp_nwsightspos = self.NearWallSightsPos
	end
	
	target = pos * 1 -- Copy pos to target
	target:Add( ang:Right() * (tmp_nwsightspos.x) )
	target:Add( ang:Forward() * (tmp_nwsightspos.y) )
	target:Add( ang:Up() * (tmp_nwsightspos.z) )
		
	pos=QerpVector( nwp, pos, target)
	
	--Start viewbob code
	
	local gunbobintensity = GetConVarNumber("sv_tfa_gunbob_intensity",1) * 0.65
	
	pos, ang = self:CalculateBob( pos, ang, gunbobintensity)
	
	--End viewbob code
	
	return pos, ang 
end

function SWEP:CalculateBob(pos, ang, ci)
	local customboboffsetx,customboboffsety,customboboffsetz,customboboffset,mypi,curtimecompensated, owvel, runspeed, sprintspeed, timehasbeensprinting, tironsightscale
	
	if ci == nil then
		ci = 1
	end
	
	tironsightscale = 1 - 0.6 * self:GetIronSightsRatio()
	owvel = self.Owner:GetVelocity():Length()
	runspeed = self.Owner:GetWalkSpeed()
	curtimecompensated = self.bobtimevar or 0
	timehasbeensprinting = self.bobtimehasbeensprinting or 0
	if !self.BobScaleCustom then
		self.BobScaleCustom = 1
	end
	mypi=0.5*3.14159
	customboboffsetx=math.cos(mypi*curtimecompensated*0.5)
	customboboffsetz=math.sin(mypi*curtimecompensated)
	customboboffsety=math.sin(mypi*curtimecompensated*3/8)*0.5
	customboboffsetx = customboboffsetx - ( math.sin(mypi*(timehasbeensprinting/2))*0.5 + math.sin(mypi*(timehasbeensprinting/6))*2 )*math.max(0, (owvel-runspeed*0.8)/(runspeed))
	customboboffset = Vector(customboboffsetx,customboboffsety,customboboffsetz)
	customboboffset = customboboffset*self.BobScaleCustom*0.3
	
	customboboffset = customboboffset * (ci or 1)
	
	
	ang:RotateAroundAxis(ang:Right(), 		(customboboffset.x))
	ang:Normalize()
	ang:RotateAroundAxis(ang:Up(), 		(customboboffset.y))
	ang:Normalize()
	ang:RotateAroundAxis(ang:Forward(), 	(customboboffset.z))
	ang:Normalize()
	
	local localisedmove,localisedangle = WorldToLocal(self.Owner:GetVelocity(),self.Owner:GetVelocity():Angle(),Vector(0,0,0),self.Owner:EyeAngles()) 
	
	ang:RotateAroundAxis(ang:Forward(), 		(math.Approach(localisedmove.y,0,1)/(runspeed/8)*tironsightscale ) * (ci or 1) * (-1 + 2 *(self.ViewModelFlip and 1 or 0) ))	
	ang:Normalize()
	ang:RotateAroundAxis(ang:Right(), 		(math.Approach(localisedmove.x,0,1)/(runspeed) )*tironsightscale * (ci or 1) * (-1 + 2 *(self.ViewModelFlip and 1 or 0) ) )
	ang:Normalize()
	
	pos:Add( ang:Right() * (customboboffset.x) )
	pos:Add( ang:Forward() * (customboboffset.y) )
	pos:Add( ang:Up() * (customboboffset.z) )
	
	--And now you're done with the messiest part of ALL of my code!  :D
	
	return pos, ang 
end


function SWEP:GetMuzzlePos( ignorepos )
	if !IsValid(self) then return nil end
	if !IsValid(self.Owner) then return nil end
	local ply=self.Owner
	local vm = ply:GetViewModel()
	local obj = vm:LookupAttachment( self.MuzzleAttachment and self.MuzzleAttachment or "1")
	local pos = vm:GetPos()
	local ang = vm:GetAngles()
	local rpos = vm:GetRenderOrigin()
	local rang = vm:GetRenderAngles()
	if ignorepos then
		vm:SetPos(ply:GetShootPos())
		vm:SetAngles(ply:EyeAngles())
		vm:SetRenderOrigin(ply:GetShootPos())
		vm:SetRenderAngles(ply:EyeAngles())
	end
	local muzzlepos = vm:GetAttachment( obj )
	vm:SetPos(pos)
	vm:SetAngles(ang)
	vm:SetRenderOrigin(rpos)
	vm:SetRenderAngles(rang)
	return muzzlepos 
end