There is no accepted code of behaviour for encounters with royalty, but adhering to the traditional forms of address will prevent anxiety.
When you meet The Queen or other female member of the Royal Family for the first time you should address them as 'Your Majesty' or 'Your Royal Highness' respectively. From then on use 'Ma'am', which should rhyme with 'Pam'. For male members of the Royal Family, use 'Your Royal Highness' and subsequently 'Sir'.
When being introduced to a member of the Royal Family men should bow and women curtsey. A handshake is also acceptable. The bow should be made by bending from the neck or shoulders (not the waist) while briefly lowering your eyes. Bow again when the member of the Royal Family leaves. To make a curtsey briefly bend your knees with one foot forward (a bob rather a full balletic sweep). This should again be performed when the member of the Royal Family leaves.
Should you happen upon a royal during their time off, allow them the freedom to go about their business as an ordinary person. Assume that to royalty, being left alone is far from a slur; it is a luxury.