CNA Samples

Rectangle Collision

Ported from Microsoft's XNA 4.0 2D Collision Tutorial 1: Rectangle.

Source for this sample on GitHub ↗

A falling block and the player have overlapping bounds, turning the background red even though their visible pixels do not touch. Play ▸

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

About this sample

Move the person beneath falling blocks. The background turns red whenever the two sprites' rectangular bounds overlap. Their visible shapes can miss each other and still count as a hit. This first tutorial uses Rectangle.Intersects; the per-pixel tutorial tests the opaque texture pixels instead.

Controls

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