Phaser AE

Vector Hit Tests

Free packv3.0.0-32DVector

Did it touch, where, and what happens next. The collision and picking maths of a line-art game, as pure functions, so a game's whole collision model can be tested without a frame ever being drawn.

This is a Free pack. Every phaser.io account can use it in their games.

Released October 14, 2026 · updated October 20, 2026

Vector Hit Tests: a screenshot of its demo
A note about these graphics

Expansion packs don't come with graphics. This screenshot is one way of drawing what the pack does, made for its demo: none of the sprites, tiles or backgrounds in it are in the pack.

That's by design. A pack is a game's logic, mechanics and effects, and it works with whatever art you give it. Some packs draw effects, backdrops or 3D scenes of their own, but none carries a game's sprites.

So the look is yours. Make the art on phaser.io, bring your own, or let your agent draw it in code: the same pack can look like anything.

What it does

The first question is containment. pointInPath and pointInPolygon say whether a point is inside a closed outline, by even-odd ray casting, which stays correct for the lumpy concave outline of a rock and for outlines that cross themselves. The second is distance. An open polyline has no inside, so a shot can never be "in" a laser fence or a cave wall: distToPath gives the distance to the nearest point on the stroke, to compare against the line's half-width, and arcHit tests a point against one arc of a segmented ring, so a shot destroys only the shield section it crossed.

The third is time. Test only a bullet's new position and one moving faster than a wall is thick passes straight through it: in front last frame, behind this one, never overlapping. sweepVsSegments takes where the point was and where it is now, and returns the earliest wall crossed in between, with the contact point, the segment's index and how far along the path it happened. polylineSegments turns a terrain or any path into the wall list it takes.

Then the contact becomes motion. resolveSurface splits a velocity against the surface normal, bounces the normal part by a restitution and scrubs the tangential part by a friction, and below a bounceThreshold the body settles instead of jittering. surfaceFriction damps a grounded body along a slope each frame, fully on the flat and not at all on a wall. Between them a ship can skid along a wall, bounce off it or come to rest against it.

Features

  • Point in outline — pointInPath and pointInPolygon, correct for concave and self-intersecting outlines.
  • Distance to a stroke — distToPath for open or closed polylines, the right test for a line with no inside.
  • Segmented rings — arcHit tests one arc's radius and angular span, within a tolerance.
  • Swept tests — sweepVsSegments returns the earliest wall crossed between two positions, so fast shots cannot tunnel.
  • Surface response — resolveSurface with restitution, friction and a bounce threshold, and surfaceFriction for grounded bodies on slopes, with gravity in any direction.
  • The basics — circleHit for two circles and segmentIntersection for where two segments cross.
  • Pure — no backend and no state, so collision rules can be tested headless.

Good for

  • Vector shooters — shots landing on rocks and ships, in the style of Asteroids.
  • Cave and gravity flyers — a ship scraping, bouncing off or landing against cave walls, in the style of Thrust or Gravitar.
  • Terrain games — landers and buggies meeting a line landscape, swept against its segments.
  • Shield rings — destructible sections of a rotating ring, hit one arc at a time.
  • Editors and menus — clicking to select a line-art shape.

Make it yours

The response is a few numbers. A restitution of 0 and a high friction make a ship that sticks where it touches; a restitution near 1 and no friction make one that ricochets round a cave; a bounceThreshold lets it bounce off walls at speed but settle on a landing pad. surfaceFriction takes a coefficient and the direction of gravity, so the same call works when gravity points at a planet rather than down the screen.

Two different uses: a rock shooter that tests each shot against each rock with circleHit on bounding radii and then pointInPath for the close ones; and a cave flyer that sweeps its ship and every bullet against the cave's wall segments each frame and turns each contact into a bounce with resolveSurface.

Works with

  • vector-2d — draws the shapes these tests are run against.
  • core-vector — includes this pack with the rest of the vector kit.
  • vector-terrain — line landscapes, turned into wall segments with polylineSegments.
  • vector-motion — integration, gravity and wrap: the movement half beside this collision half.
  • vector-hull and physics-box2d — convex colliders and a physics engine, when shapes must collide with shapes.

Good to know

  • Every test is a point against geometry: there is no shape-against-shape test. Use circles on bounding radii, or convex hulls and a physics engine.
  • The swept test is a point, not a capsule, so a wide body moving fast can still clip a corner.
  • arcHit works in the ring's own space: subtract the centre and undo the ring's rotation first.

More Vector packs

Show all →