how to mask out a object

so I have a curtain that I want to animate but as regular curtains curl around at the top of the box I can’t do that ( more like that’d take much more time) so what I want to do is that lets say for ex a plane goes inside a cube but it should not show the part that’s coming out of it