Although it is a bone, the kneecap (patella) is actually contained right within the tendon of the quads muscle. This is very unusual in the body - to find a bone inside a tendon (a 'sesamoid' bone) - and what it means in reality is that any alteration of the direction of forces in the whole quads muscle/tendon/bone complex has to affect the patella.
Now, the undersurface of the patella is v-shaped and sits within a groove in the underlying femur and, although it is free to slide up and down the groove as the leg bends and straightens, it is not free to move from side to side as it is contained by the walls of this groove.
You may like to take a look at this video below, which has been taken during arthroscopic surgery, and which shows the patella from above as in the sketch we have just seen.
You will see that as the knee is bent by the surgeon the patella engages in the groove of the femur and slips out of view.
[You may have also seen a curtain-like suprapatellar plica stretching horizontally across the field of view on the left. This is a different topic, but mentioned just so that you know. The shadowy stuff on the right is just some unimportant loose tissue floating into view.] (Video footage courtesy of The Knee Foundation)