CNA Samples

Per-Pixel Collision

Ported from Microsoft's XNA 4.0 2D Collision Tutorial 2: Per-Pixel.

Source for this sample on GitHub ↗

A falling black block touches the stick-figure player, turning the background red. Play ▸

Opens in a new tab. Click the canvas so it receives keyboard input.

About this sample

Move the person under falling blocks. The background turns red only when opaque pixels of the two sprites overlap. Their rectangular bounds can overlap while the visible shapes miss. The sample reads the original texture pixels through Texture2D.GetData and tests the shared area, pixel by pixel.

Controls

ActionKeyboardGamepad
Move personLeft / Right arrowD-pad left / right
ExitEsc or close the tabBack