skip to content

Physics Queries & Character Control

Raycasts, shape casts and overlap queries with layer filters, triggers, and kinematic character controllers. Interviewers probe it for practical gotchas like slopes, steps and moving platforms.

on this pageshow

explore

questions

6

In game physics, how do a raycast, a shape cast and an overlap query differ, and when would you choose each one?

level: juniorimportance: must knowfreq 62%

answer

  1. a line, a swept volume, a volume held still
  2. what each one returns
  3. a thin line slips past a ledge
  4. overlap: who touches, but no distance

basics

~20 s

A raycast traces a thin line and reports hits along it; a shape cast sweeps a sphere, capsule or box along a path and reports where it first touches; an overlap query lists what intersects a stationary shape.

solid answer

~50 s

A **raycast** is the cheapest query: a line from an origin, in a direction, up to a maximum distance, returning the hit point, surface normal, distance and collider. It suits line-of-fire checks and probing a single point. A **shape cast** sweeps a sphere, capsule or box along the same kind of path and returns the first contact, with distance and normal; I use it whenever the *size* of the thing matters, such as a character's ground check or "will my capsule fit if I move here", because a thin ray slips past ledge edges and gaps the body would hit. An **overlap query** tests a shape at one pose and returns every collider touching it, without distance or normal: "is there room to stand up from a crouch", "what is inside this explosion radius". All three run against the physics world's broad-phase structure and take the same filters: a layer mask, whether to include triggers, and colliders to ignore.

code

pseudocode · 9 lines
pseudocode
function isGrounded(character):
    radius = character.capsuleRadius * 0.95
    start  = character.footPosition + up * character.capsuleRadius   // bottom hemisphere centre
    filter = QueryFilter(mask = WORLD | PLATFORMS, includeTriggers = false,
                         ignore = [character.collider])
    hit = sphereCast(start, radius, -up, maxDistance = 0.05 * character.capsuleRadius + GROUND_MARGIN, filter)
    if hit is none:
        return false
    return dot(hit.normal, up) >= cos(character.maxSlopeAngle)

go deeper

for a junior

Recall the three query kinds and what each returns: a raycast's hit, a shape cast's first contact with distance, an overlap's list of touching colliders. Give one everyday use for each.

for a middle

Explain why a thin ray misses ledges a capsule would rest on, why a shape cast's edge normal is tilted, and which filters every query accepts.

for a senior

Show the judgment in a robust ground check: sphere cast with the body's width, starting clear of geometry, normal tested against the slope limit, own collider ignored, no per-frame allocation.

for a principal

Weigh standardising a small set of query helpers for gameplay teams, with agreed filters and buffers, against letting each feature call raw queries and rediscover the same bugs.

## Three questions you can ask the physics world A physics system does not only simulate bodies; it also answers **spatial queries** from gameplay code. Three kinds cover almost every need, and choosing the wrong one is a classic source of bugs. | Query | What moves | Typical result | Typical use | |---|---|---|---| | **Raycast** | an infinitely thin line | first hit (or all hits) with point, normal, distance, collider | line of fire, probing one point, interaction prompts | | **Shape cast** (sweep) | a sphere, capsule or box along a path | first contact with point, normal, distance | ground checks, "can my body move here", melee sweeps | | **Overlap query** | nothing: a shape at one pose | every collider intersecting the shape | headroom checks, area effects, spawn clearance | All three are answered the same way internally: the physics world's **broad-phase structure** (typically a bounding-volume tree) shortlists candidate colliders, and an exact test runs on each candidate. That is why they are fast enough to call many times per frame, and why they respect the same filters. ## Raycasts A raycast takes an **origin**, a **direction** and a **maximum distance**. The common variants are: - **closest hit**: the nearest surface along the ray, the default for most gameplay; - **any hit**: stop at the first surface found, cheaper when you only need a yes or no, such as "is this path blocked"; - **all hits**: every surface along the ray, usually written into a caller-provided buffer so the query does not allocate. The hit carries the **point**, the **surface normal**, the **distance** along the ray and the **collider** that was hit. The weakness is that a ray has no thickness: it measures one line, not the space a body occupies. ## Shape casts A shape cast (also called a sweep) moves a whole shape from a start pose along a direction and reports the **first time of contact**: how far the shape travelled before touching something, plus the contact point and normal. It answers questions about a body with a size. A character controller lives on shape casts: before moving its capsule it sweeps the capsule along the desired motion to find what it would hit. Two practical gotchas: 1. **Starting in contact.** If the shape already overlaps a collider at its start pose, engines differ: some ignore that collider, some report a hit at zero distance. Starting the cast slightly back, or with a slightly smaller shape, avoids the ambiguity. 2. **Edge normals.** A sphere or capsule touching an edge reports the normal of that edge contact, which is tilted, not the normal of either face. When you need the face normal (to decide whether a surface is walkable) a short raycast at the hit point gives it. ## Overlap queries An overlap query places a shape at one pose and returns every collider it intersects. It does not report a distance or a normal, because nothing moved. That makes it right for questions of the form "is anything here": - can a crouching character stand up: overlap the standing capsule at the current position; - what does an explosion affect: overlap a sphere at the blast centre, then apply effects to each result; - is a spawn point clear: overlap the character's capsule there before spawning. Note that a bounding-box query against the broad phase alone is not an overlap query: it returns candidates whose boxes overlap, including false positives the exact test would reject. ## Filters every query takes Queries share a filter block, and most query bugs are filter bugs: - a **layer mask** that limits which categories of collider can be hit; - a flag for whether **triggers** count as hits; - an **ignore list**, most often the querying character's own collider; - sometimes a per-hit callback that accepts or rejects a candidate. ## Worked example: the ground check before a jump A third-person character may only jump when grounded. The naive check is a raycast straight down from the capsule's centre, a little longer than half its height. It works on flat floors and fails at a ledge: when the character stands with its centre past the edge, its rounded bottom still rests on the ledge but the ray passes beyond the edge and hits nothing, so the jump is refused. The robust check is a **downward sphere cast** with a radius slightly smaller than the capsule's, starting at the centre of the bottom hemisphere and travelling just past the capsule's lowest point by a small margin. It catches the ledge because it has the body's width. Its hit normal then goes through the slope-limit test, so the side of a steep rock does not count as ground.

  • Why prefer the 'any hit' variant of a raycast for some checks?
    When the question is only whether something blocks the line, such as whether a path between two points is obstructed, the query can stop at the first surface it finds instead of searching for the nearest. That saves traversal and exact tests, especially in dense geometry. Use closest hit when you need the point or the normal of the nearest surface.
  • How do you keep queries from allocating memory every frame?
    Use the variants that write results into a buffer the caller owns and that return a count. Size the buffer for the realistic worst case, and treat a full buffer as a signal that results may have been dropped. Allocating result lists per query generates garbage or allocator traffic in hot gameplay code.
  • A shape cast for a ground check sometimes returns a tilted normal on flat stairs. Why?
    The capsule is touching a stair edge, and a sweep reports the normal of the edge contact, which points from the edge towards the shape's centre and is tilted. Cast a short ray at the hit point, or slightly inward, to read the tread's face normal before applying the slope test.

  • A raycast is enough for any ground check
  • An overlap query tells you the distance to the object
  • A broad-phase box query gives exact overlaps
  • Shape casts behave the same in every engine when starting inside geometry
  • Queries ignore layers and triggers unless you write the filtering yourself
open as a page

How does a kinematic character controller's collide-and-slide loop work, and how should it treat walkable slopes differently from steep walls?

level: middleimportance: must knowfreq 60%

basics

~20 s

Each step the controller sweeps its capsule along the desired move, stops just short of the first hit, removes the part of the remaining motion that pushes into the surface and repeats; the slope angle decides whether a surface is ground or wall.

open as a page

In game physics, how does a trigger volume differ from a solid collider, and how do layer masks decide what collides or gets queried?

level: juniorimportance: should knowfreq 55%

basics

~20 s

A solid collider takes part in contact resolution, so bodies push against it; a trigger only detects overlap and reports enter and exit events. Layer masks filter which collider pairs are tested and which colliders a query may hit.

open as a page

A kinematic character controller standing on a moving lift lags behind it and slides off a rotating platform; how should the controller ride moving platforms?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Track the platform under the character; each step, after the platform moves, carry the character by the platform's displacement at its foot point, rotation included, then apply its own movement. On leaving, inherit the platform's velocity at that point.

open as a page

A third-person character controller catches on stair edges going up and goes briefly airborne on each step going down; how do a step offset and ground snapping fix it?

level: seniorimportance: should knowfreq 46%

basics

~20 s

Going up, a step-up pass lifts the capsule by the step height, moves it forward and casts back down onto the tread if it is walkable. Going down, ground snapping casts down after each move and pins a grounded character to the surface.

open as a page

Should a game's player character be a kinematic character controller or a dynamic rigid body, and how would you decide for a project?

level: principalimportance: should knowfreq 38%

basics

~20 s

Neither is always right: a kinematic controller gives precise, designer-tuned movement but every physical interaction must be coded; a dynamic body gets pushing, knockback and riding objects from the solver but fights it on slopes, steps and walls.

open as a page