When should you trade to prevent castling? The main question I have is about a game I played. What do I need to know about how to take a piece so that my opponent's king can no longer castle?

In the following game, I played Bxd2+ to do so. The Stockfish lines seem nonhuman. 

[FEN "r1b2rk1/1p3ppp/2pp4/p1q1p1bN/P1P1P3/1PP3Q1/3N1PPP/R3K2R b - - 0 1"]

1... Bxd2+ {I played this to keep the king in the center.} (1... Bh6 2. Nf6+ Kh8 3. Qh4 d5 4. g4 Qe7 5. g5 gxf6 6. gxh6 Rg8 7. cxd5 cxd5 8. exd5 {Why would black want this?}) (1... f6 2. Nxg7 Kh8 3. Nh5 Rg8 4. Qd3 Bxd2+ 5. Qxd2 {Why would black give up the g7 pawn and then take the knight allowing the queen to capture back instead of what I played forcing the king to move?})
The problem with that move is a number of things.


You gave up the bishop pair.
His attack on your king is stronger than yours on his king, and you traded off a defender.
You have no time to go after his king since yours is threatened with mate on the move.
Already with three minors traded, his threats aside, there might not be enough firepower to attack his king with impunity.


Keeping a king in the center is only part of the idea behind keeping a king in the center. You need to be able to open more lines to attack it, and that also requires having other pieces that are already somewhat trained on the opposing king already. Once the king is stuck, you either need to be able to open lines immediately with pawn play, or an additional sacrifice. If you cannot do that, you should question the trade, and ask if you are simply trading your good piece, in this case a B with some good scope, for a worse piece, in this case a barely-developed Nd2. 

In your line with 1...Bh6, your line is best play for white per the computer, and in that line, white is barely holding it together. At the end, there is 8...Rg4 9.Qh5 Bd7, and that is advantageous for black, so that is why he would want that. Black's K is quite safe on h8 there, but where does white go with his? The Qh5 is also not comfortable, and in some lines Rf4 and Bg4 are threats. White is walking a very fine line there. From there, if black can find a way to open the position just a little, the white king will come under a strong attack.

 [FEN "r1b2rk1/1p3ppp/2pp4/p1q1p1bN/P1P1P3/1PP3Q1/3N1PPP/R3K2R b - - 0 1"]

1... Bh6 2. Nf6+ Kh8 3. Qh4 d5 4. g4 Qe7 5. g5 gxf6 6. gxh6 Rg8 7. cxd5 cxd5 8. exd5 Rg4 9. Qh5 Bd7 {White is walking a very fine line here. From here, if black can find a way to open the position just a little, the white king will come under a strong attack.}