Matter.js Collision Filter Categories Visualizer — Free Interactive Snippet

Matter.js Collision Filter Categories Visualizer · Visualizers · Plain HTML, CSS & JS · Live preview

CategoryVisualizers

What's included

Features

Live collision matrix
Symmetric toggles for every pair.
Category and mask bits
Hex and binary per colour.
Group override demo
Negative group beats the matrix.
Pass-through highlighting
Dashed outlines on filtered overlaps.
Instant updates
No rebuild; filters are plain objects.
Collision feed
Latest hit pair in an aria-live log.
Three shape categories
Colour and shape both identify a body.
Drag interaction
Mouse constraint with page scrolling kept.

About this UI Snippet

Matter.js collisionFilter Explained — Category, Mask and Group

Screenshot of the Matter.js Collision Filter Categories Visualizer snippet rendered live

Every Matter.js body has a collisionFilter with three fields: category, mask and group. They decide which pairs of bodies are even considered for collision — the tool behind one-way platforms, ghosts, bullets that ignore their shooter and ragdolls that don't tangle. This visualizer makes the bits visible.

Category: what am I?

A category is a single bit — 0x0002, 0x0004, 0x0008... up to 32 categories. Matter's default is 0x0001, which the walls use here. Each body belongs to exactly one category.

Mask: what will I touch?

A mask is the bitwise OR of every category a body is willing to collide with. The panel shows each colour's mask in hex and binary; toggling a matrix cell adds or removes that bit. Every mask here includes the wall bit, so nothing falls through the floor.

The rule

Two bodies collide only if each one's mask contains the other's category: (a.mask & b.category) !== 0 && (b.mask & a.category) !== 0. Because both sides must agree, the matrix is kept symmetric — a one-way yes still means "no".

Group: the override

If two bodies share the same non-zero group, category and mask are ignored: a positive group means they always collide, a negative group means they never do. Turn on Ghost group and every blue body gets group -1; blue still hits red and green, but blue bodies now fall straight through each other even though the matrix says blue-blue collides. That's exactly how Matter's own Body.nextGroup(true) keeps a ragdoll's limbs or a car's wheels from colliding with each other.

Seeing pass-throughs

Each frame, pairs that are overlapping but filtered out (checked with Detector.canCollide and Collision.collides) get a dashed amber outline, so you can see the ghosting as it happens. Filters are plain objects on each body, so changes apply on the very next step — no rebuild needed.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet into an AI assistant like Claude and ask it to explain the category/mask rule using the binary shown in the panel. Ask it to add a fourth category, one-way platforms, bullets that ignore their shooter, or an export of the matrix as ready-to-paste JavaScript constants. It can also help you plan collision layers for your own game.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an interactive Matter.js 0.20 (from a CDN) collision filter visualizer in plain HTML, CSS and JavaScript.

Requirements:
- A canvas with walls and two tilted shelves in the default category 0x0001, and red circles, green squares and blue triangles in categories 0x0002, 0x0004 and 0x0008.
- A side panel with a symmetric 3x3 toggle matrix of which colours collide (start: same colours collide, red passes through green, blue hits all). Each body's mask = wall bit OR allowed category bits, updated live on its collisionFilter.
- Show each colour's category and mask in hex and binary, and the rule (a.mask & b.category) && (b.mask & a.category) plus the group override.
- A "Ghost group" toggle that gives all blue bodies group -1 so they pass through each other, marked in the matrix.
- Draw dashed amber outlines on overlapping pairs that are filtered out (Detector.canCollide + Collision.collides).
- An aria-live log of the latest hit pair, a Spawn button, drag with a mouse constraint (wheel listeners removed), and a cap on body count.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

Requires
<div class="cf">
  <div class="cf-stage" id="cfStage"></div>
  <aside class="cf-side">
    <h2>Collision filters</h2>
    <p class="cf-lead">Click a cell to toggle whether two categories collide. Bodies pass through each other when they don't.</p>
    <table class="cf-matrix" id="cfMatrix" aria-label="Collision matrix"></table>
    <div class="cf-bits" id="cfBits"></div>
    <div class="cf-rule">
      <b>Rule</b>
      <code>(a.mask &amp; b.category) !== 0<br>&amp;&amp; (b.mask &amp; a.category) !== 0</code>
      <span>Both sides must accept each other. Same non-zero <em>group</em> overrides: positive = always collide, negative = never.</span>
    </div>
    <div class="cf-actions">
      <button type="button" id="cfSpawn">Spawn more</button>
      <button type="button" id="cfGhost" class="ghost">Ghost group: off</button>
    </div>
    <div class="cf-log" id="cfLog" aria-live="polite"></div>
  </aside>
</div>

Step by step

How to Use

  1. 1
    Watch the default rulesRed and green pass through each other; blue hits everything.
  2. 2
    Toggle a cellSwitch any pair between collide and pass through.
  3. 3
    Read the bitsCategory and mask update in hex and binary.
  4. 4
    Try the ghost groupBlue bodies stop colliding with each other.
  5. 5
    Drag and spawnPush bodies into each other or add more.

Real-world uses

Common Use Cases

Learning Matter.js
The filter rules made concrete.
Game design
Plan layers for players, enemies and bullets.
Debugging physics
Check why two bodies won't collide.
Teaching bitmasks
Bitwise AND and OR with instant feedback.
Ragdoll and vehicle rigs
Why parts share a negative group.
Related: Matter.js Hill Climb Car
Negative groups on a vehicle: Matter.js Hill Climb Car Game.
Related: Matter.js Physics Playground

Got questions?

Frequently Asked Questions

Each body has collisionFilter.category (one bit), mask (bits of categories it may hit) and group. Two bodies collide when each mask contains the other's category, unless they share a non-zero group, which overrides: positive always collides, negative never does.

The ground uses the default category 0x0001. If a body's mask doesn't include 0x0001, it ignores the ground. Include the ground's category bit in every mask that should land on it.

It returns a new unique negative group number. Give it to all parts of a composite (a car, a ragdoll, a soft body) so those parts never collide with each other while still colliding with everything else.

Yes. Assign new values to body.collisionFilter.category, mask or group; the next collision check uses them. Waking sleeping bodies makes the change visible immediately.

32, because categories are bits in a 32-bit integer.