githubEdit

Perform Raycasting

Cast rays to detect objects and hit points in your scene

Learn how to cast rays through your scene to detect objects, find hit points, and implement custom interaction logic.

:::tip When to Use Raycasting

  • Custom interaction detection beyond pointer events

  • Line-of-sight checks (can A see B?)

  • Projectile trajectories

  • Ground detection for character placement

  • Custom selection tools :::


Basic Raycasting

Cast a ray from a point in a direction:

import { Behaviour } from "@needle-tools/engine";
import { Vector3 } from "three";

export class BasicRaycast extends Behaviour {
    start() {
        // Set ray origin and direction
        const origin = new Vector3(0, 1, 0);
        const direction = new Vector3(0, 0, -1); // Forward

        // Cast ray using Needle's physics raycast
        const hits = this.context.physics.raycast(origin, direction);

        if (hits.length > 0) {
            const hit = hits[0];
            console.log("Hit object:", hit.object.name);
            console.log("Hit point:", hit.point);
            console.log("Distance:", hit.distance);
        }
    }
}

:::tip Use Needle's Raycast Method Always use this.context.physics.raycast() instead of three.js Raycaster directly. Needle's method:

  • Uses Mesh BVH acceleration for much faster raycasts against mesh geometry

  • Provides consistent results across the engine

  • For physics colliders specifically, use this.context.physics.engine.raycast() :::


Raycast from Camera

Cast a ray from the camera based on screen position:


Raycast Options

Configure raycasts with additional options:

RaycastOptions

Option
Type
Description

maxDistance

number

Maximum ray distance (default: Infinity)

precise

boolean

Use physics colliders for more accurate results

testTriggers

boolean

Include trigger colliders in results

layerMask

number

Filter objects by layer mask


Line of Sight Check

Check if one object can "see" another:


Ground Detection

Find the ground below an object:


Raycast Against Specific Objects

Only test ray against specific objects by filtering the results:


Filter by Layer

Use the layerMask option to control which layers are tested:


Visualize Rays (Debug)

Draw debug lines to see your rays:


Intersection Data

Raycasts return intersection data with useful information:


Performance Tips

Use Needle's Built-in Raycast

Always prefer this.context.physics.raycast() over three.js Raycaster:

Needle's built-in raycast is significantly faster because it:

  • Uses Mesh BVH (Bounding Volume Hierarchy) for spatial acceleration

  • Automatically optimizes for static vs dynamic objects

  • Tests against mesh geometry efficiently

:::tip Physics Colliders To raycast against physics colliders specifically, use this.context.physics.engine.raycast() instead. :::

Use Layers

Filter by layers to reduce tested objects:

Limit Ray Distance

Specify maxDistance to stop testing early:


Common Use Cases

Custom Grabbing/Picking

Projectile Path


Next Steps

Learn more:

Reference:

Last updated