stop gun from rotating

I’m making a 2d platformer where the player has a gun and he can shoot.
the gun is rotated according to the mouse and it works fine.
what I want to do is that the gun won’t pass the ground… for example:
this is the current state (the state i want to change):
18369-gunbad.jpg

and this is the state I want to achieve:
18370-gundgood.jpg

Now, I could do some kind of collisoncheck with the ground and then stop rotating the gun.
but then there is still another problem:
if the player jumps and then shoot down on the ground (which he can do), and then he falls back to the ground, when he lands the gun will stay at the same rotation and will touch the ground, so I need a way to check for a fixed rotation.

Also, the platform can be rotated.

Thanks a lot!

I think dynamically clamping the rotation is you best bet. First calculate the distance from the gun at a horizontal potion to the ground directly below the gun. Then use this distance and the gun rotation point and some trigonometry to calculate the max rotation before the gun will penetrate the ground and use this number to clamp the gun rotation.

Hope that helps

EDIT:

here is a pic18372-gun.png