function determineNewDirections(E)local R=0 local S=0 local l=parts.TurretRing2 local g=l.CFrame.lookVector local j=g:Dot(E)R=math.pi/2-math.acos(j)local r=(E-j*g).unit local X=((l.CFrame-l.CFrame.p)*CFrame.new(0,-1,0)).p S=math.acos(X:Dot(r))local n=X:Cross(r)if n:Dot(g)<0 then S=-S end local c={}c[1]=R c[2]=S return c end function sign(E)if E<0 then return-1 else return 1 end end
-- Determine the gun elevation and tilt
function determineNewDirections(targetVector)
	local targElev = 0
	local targRot = 0

	local turretMain = parts.TurretRing2

	-- Vector normal to the plane which is the turret ring
	local turretNormal = turretMain.CFrame.lookVector

	local angleFromNormal = turretNormal:Dot(targetVector)

	-- Determine gun elevation
	targElev = math.pi / 2 - math.acos(angleFromNormal)

	-- Projection of target vector onto the turret plane
	local targetProjection = (targetVector - (angleFromNormal * turretNormal)).unit

	local forwardVector = (
		(turretMain.CFrame - turretMain.CFrame.p) -- Orientation only of brick
		* CFrame.new(0, -1, 0)
	).p -- Translated down 1

	-- Determine angle between forward vector and the projected target vector
	targRot = math.acos(forwardVector:Dot(targetProjection))

	-- Determine the sign of the angle
	local vectorCross = forwardVector:Cross(targetProjection)

	-- If the cross product is opposite to the normal vector, make the angle negative
	if vectorCross:Dot(turretNormal) < 0 then
		targRot = -targRot
	end

	-- Put rotation and elevation into an array
	local rotations = {}
	rotations[1] = targElev
	rotations[2] = targRot

	return rotations
end

function sign(input)
	if input < 0 then
		return -1
	else
		return 1
	end
end
