I know a trivial difference of "that" and "this" uses. Such as the difference between "this chair" and "that chair". But I don't understand one case. When one person is coming into a room and another has been doing something. And the first one asks him "Why are you doing that?". I heard about the same questions with "that" in many analogous situations in different movies. And the main question is why "that", not "this". I think that "this" more fits here, because a situation is in front of an asking person.
Although the action may be taking place in front of the person asking the question, it is nevertheless taking place at a distance, both physically and situationally. It's the OTHER person who is performing the action, and so it's taking place 'over there', in a different personal space. Why are you doing this, on the other hand, would often refer to an action that affects both parties.
You could use either word in that context, although "that" sounds much more natural to me. And the meaning is slightly different.
Why are you doing that?
Would be taken to mean "Why are you doing that specific thing that you are doing?"
Why are you doing this?
Would be taken in a more general sense, as if the thing the person is doing is affecting an entire situation.
Let me illustrate with an example.
Suppose a wife walks in to find her husband with another woman. If she says Why are you doing that? it would be taken to mean "Why are doing those things with that woman?" Whereas if she says Why are you doing this? she probably means "Why are you acting this way, disregarding my feelings, and putting our entire marriage at risk?"