IT lexicon › Programming › Mouse picking

Mouse picking

Programming På svenska → Updated: 2026-10-27

Answering "what did the player just click on?" when the mouse only knows a pixel and the scene has three dimensions.

The usual approach runs the transform chain backwards. The cursor's pixel becomes normalised device coordinates, gets multiplied by the inverse of the projection and view matrices, and turns into a ray in the world, which is then tested against colliders or a bounding volume hierarchy. Whatever it hits first wins.

The alternative is ID picking: every object is drawn into an invisible buffer in a unique colour, and the pixel under the cursor is read back. That is pixel-exact even for awkward shapes, but reading back from the GPU is expensive unless it is deferred a frame or two. OpenGL once had a built-in selection mode, GL_SELECT, deprecated in version 3.0 in 2008 and mourned by few.

← Back to the lexicon