melonJS
    Preparing search index...

    Class Ray3d

    A 3D ray — an origin and a unit direction — in the engine's 3D space (Y-down, +Z forward, the Camera3d convention).

    What a pointer is under a perspective camera: one screen pixel covers a whole line of world points, from the camera out through that pixel. Camera3d#screenToRay builds one, and Mesh#intersectRay, Camera3d#pick and the pointer events of a mesh under a Camera3d all test against one.

    Every intersect* method returns the distance along the ray to the first point where it enters the shape, or null when it misses; the shape behind the ray's origin does not count. Ray3d#at turns a distance into the point.

    import { Ray3d, Vector3d } from "melonjs";

    // where a click meets the floor at y = 0 (Y-down: up is (0, -1, 0))
    const ray = camera.screenToRay(pointer.gameScreenX, pointer.gameScreenY);
    const t = ray.intersectPlane(new Vector3d(0, 0, 0), new Vector3d(0, -1, 0));
    if (t !== null) {
    const onFloor = ray.at(t);
    }
    Index
    • Parameters

      • Optionalox: number = 0

        x of the origin

      • Optionaloy: number = 0

        y of the origin

      • Optionaloz: number = 0

        z of the origin

      • Optionaldx: number = 0

        x of the direction

      • Optionaldy: number = 0

        y of the direction

      • Optionaldz: number = 1

        z of the direction (the default points down +Z, into the screen)

      Returns Ray3d

    direction: Vector3d

    which way it points, always of unit length

    origin: Vector3d

    where the ray starts

    • Distance along the ray to where it enters an axis-aligned box; 0 when the origin is already inside it.

      Parameters

      Returns number | null

      the distance, or null on a miss

    • Distance along the ray to a plane, given a point on it and its normal (either side: the normal need not face the ray).

      Parameters

      • point: Vector3d

        any point on the plane

      • normal: Vector3d

        the plane's normal (need not be unit length)

      Returns number | null

      the distance, or null when the ray runs parallel to the plane or points away from it

    • Distance along the ray to where it enters a sphere; 0 when the origin is already inside it.

      Parameters

      Returns number | null

      the distance, or null on a miss

    • Set the origin and direction. The direction is normalized; a zero direction leaves the ray pointing down +Z.

      Parameters

      • ox: number

        x of the origin

      • oy: number

        y of the origin

      • oz: number

        z of the origin

      • dx: number

        x of the direction

      • dy: number

        y of the direction

      • dz: number

        z of the direction

      Returns this

      this ray, for chaining