Those guns that are compatible with PS2/XBOX etc...

How do they work? How does it know where the gun is pointing?

This king of thing...