-- 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
