natiny_physics_raycast

C
bool natiny_physics_raycast(float ox, float oy, float oz,
                            float dx, float dy, float dz,
                            float max_distance,
                            NatinyRaycastHit* hit);

Finds the first body along a ray.

Parameters

Name Type Description
ox, oy, oz float Where the ray starts, world space
dx, dy, dz float Which way it points; need not be normalized
max_distance float How far to look, in metres
hit NatinyRaycastHit* Receives the hit; may be null when only the yes-or-no is wanted

Returns

Type Description
bool true when something was hit within max_distance
C
typedef struct {
    NatinyBody body;
    float      x,  y,  z;
    float      nx, ny, nz;
    float      distance;
} NatinyRaycastHit;

Example

C
NatinyRaycastHit hit;
if (natiny_physics_raycast(px, py, pz, 0.0f, -1.0f, 0.0f, 2.0f, &hit)) {
    /* hit.body is what we are standing on, hit.y where its surface is */
}

Notes

  • The nearest hit, not the first one found.
  • A zero-length direction or a max_distance of zero or less returns false.
  • hit is zeroed before anything else happens, so a miss leaves it blank rather than stale.
  • The normal points out of the surface that was hit.
  • Static bodies answer immediately; nothing has to have been stepped first.