[deleted]
For WWI and early WWII, the stadimeter. It was developed in the late 1800's and by the first decade of the 20th century it was standard issue in US warships and then submarines; in the latter case, it would be fully integrated into the periscope. How stadimeters work is that they get a few knowns -- the height of the enemy ship at its maximum extent, and that's really all that needs to be known! From there it's basic trigonometry, which the stadimeter calculates itself. Essentially by knowing the height of the enemy ship and plugging it into the stadimeter, and then finding the angle of the periscope to the top of the enemy ship, you calculate the distance from you to them. For instance, 1 degree at 100ft mast height = 1 mile distance.
Next we need to know its course. This is what really changed between WWI and WWII the most. This is also done through trigonometry as well, and through analog computers in the WWII case; the Torpedo Data Computer (TDC). The periscope would have another analog computer in it where the if you point it at any object, it gives its bearing -- that is, its relative direction to the submarines front end. This was punched into the TDC, where all this information was gathered, and later would be able to automatically 'track' the ships too! In WWI, well, you had to do it by hand.
Next we needed speed. Generally there are two methods for this -- the first makes use of the stadimeter. If you know the course of the ship, and you get two distances over a set piece of time, it's relatively simple algebra to deduce its speed. However, as the stadimeter is more of an approximation, this can be misleading. The other is to use sonar to listen to the propeller revolutions, because if details of the ships propellers are known (from the identification handbook) so can the amount of distance traveled per revolution and vwala.
These calculations were relatively easy to calculate with these tools and led to the US submarine fleet being the most devastating force in US history. 260 US submarines would be deployed to the Pacific and they would take out 1560 vessels worth 5 million tonnes of resources, over 1/2 of all tonnage sunk during the war including every single power combined from the Allies. These 260 submarines, less than 3% of the total size of the US fleet, would be responsible for taking out 1/3 of the Japanese warships and 2/3rds of their merchant fleet. But it came at a cost, it would require exposing their position above water. This would lead to the US submarine crews suffering the highest rate of casualties of any other branch of the military during the war, a 22% death rate (3506 out of 16,000 who served) with 52 submarines lost.