# Ray

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/core/shape/ray.js#L20

An infinite ray. Rays are commonly used for picking, raycasting and intersection tests.

A ray is an [origin](https://developer.mozilla.org/docs/Web/API/Window/origin) and a [direction](https://api.playcanvas.com/engine/classes/Ray.md#direction). It performs no intersection itself: pass it
to the `intersectsRay` method of a [BoundingBox](https://api.playcanvas.com/engine/classes/BoundingBox.md), [BoundingSphere](https://api.playcanvas.com/engine/classes/BoundingSphere.md),
[OrientedBox](https://api.playcanvas.com/engine/classes/OrientedBox.md), [Plane](https://api.playcanvas.com/engine/classes/Plane.md) or [Tri](https://api.playcanvas.com/engine/classes/Tri.md). Keep the direction normalized, as those tests
require it. The constructor copies the vectors it is given, and [set](https://api.playcanvas.com/engine/classes/Ray.md#set) updates both in
place.

**Example**

```ts
// A ray from the camera through a screen position
const ray = new Ray();
entity.camera.screenToWorld(x, y, entity.camera.nearClip, ray.origin);
entity.camera.screenToWorld(x, y, entity.camera.farClip, ray.direction);
ray.direction.sub(ray.origin).normalize();
```

## Constructors

### constructor

```ts
new Ray(origin?: Vec3, direction?: Vec3)
```

Creates a new Ray instance. The ray is infinite, starting at a given origin and pointing in
a given direction.

**Parameters**

- `origin` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The starting point of the ray. The constructor copies
  this parameter. Defaults to the origin (0, 0, 0).
- `direction` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): The direction of the ray. The constructor copies
  this parameter. Defaults to a direction down the world negative Z axis (0, 0, -1).

**Example**

```ts
// Create a new ray starting at the position of this entity and pointing down
// the entity's negative Z axis
const ray = new Ray(this.entity.getPosition(), this.entity.forward);
```

## Properties

### direction

```ts
readonly direction: Vec3
```

The direction of the ray.

### origin

```ts
readonly origin: Vec3
```

The starting point of the ray.

## Methods

### clone

```ts
clone(): Ray
```

Returns a clone of the Ray.

**Returns** [`Ray`](https://api.playcanvas.com/engine/classes/Ray.md): A duplicate Ray.

### copy

```ts
copy(src: Ray): Ray
```

Copies the contents of a source Ray.

**Parameters**

- `src` ([`Ray`](https://api.playcanvas.com/engine/classes/Ray.md)): The Ray to copy from.

**Returns** [`Ray`](https://api.playcanvas.com/engine/classes/Ray.md): Self for chaining.

### set

```ts
set(origin: Vec3, direction: Vec3): Ray
```

Sets origin and direction to the supplied vector values.

**Parameters**

- `origin` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The starting point of the ray.
- `direction` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The direction of the ray.

**Returns** [`Ray`](https://api.playcanvas.com/engine/classes/Ray.md): Self for chaining.
