Talos docs

Hit

Since 0.6Changed in 0.7

Result of a raycast or shape cast.

Plain text

A cast that hits nothing answers null, never undefined.

Signature

export interface Hit {
  /** The entity whose collider was hit. */
  entity: EntityHandle
  /** Where the cast met it, world space, metres. */
  point: Vec3
  /** The surface normal at `point`. */
  normal: Vec3
  /** From the ray's `from` (or the shape's start) to the hit, in metres: `dir` is normalised first, so this is a length, not a multiple of `dir`. */
  distance: number
  /** Present only when the hit was on an instanced mesh: the stable id of the instance hit (see {@link Instances}). Absent for every other hit. */
  instance?: number
}

Import: import type { Hit } from "@argus/api"

Members

MemberSignatureDescription
entityentity: EntityHandleThe entity whose collider was hit.
pointpoint: Vec3Where the cast met it, world space, metres.
normalnormal: Vec3The surface normal at point.
distancedistance: numberFrom the ray's from (or the shape's start) to the hit, in metres: dir is normalised first, so this is a length, not a multiple of dir.
instanceinstance?: numberPresent only when the hit was on an instanced mesh: the stable id of the instance hit (see {@link Instances}). Absent for every other hit.

Example

The tested fixture fixtures/recipes/drone-search/scenes/behaviors/carrier.ts.

import type { Behavior } from "@argus/api"

/** Moves a kinematic body round `props.path` (world [x, z] points) at `props.speed` m/s, facing where it goes: the
 * stand-in for a boat or a truck the drone launches from, and for the contact it looks for. */
interface Props {
  path: [number, number][]
  speed: number
}

interface State {
  next: number
}

export default {
  init(ctx) {
    ctx.state.next ??= 0
  },
  update(ctx, dt) {
    const path = ctx.props.path
    const goal = path[ctx.state.next % path.length]
    if (goal === undefined) return
    const p = ctx.entity.position
    const to: [number, number, number] = [goal[0], p[1], goal[1]]
    ctx.entity.lookAt(to)
    if (ctx.entity.moveToward(to, ctx.props.speed * dt)) ctx.state.next = (ctx.state.next + 1) % path.length
  },
} satisfies Behavior<State, Props>