Ray

Ray

Overview

Description

A Ray represents a ray in 3D space. It is defined by two properties: the origin and the direction. It is commonly used in calculations such as collision detection, raycasting, and geometry operations.

Properties

Origin

Vector3 The starting point of the ray in 3D space.

Code Samples

local origin = Vector3.new(0, 0, 0)
local direction = Vector3.new(1, 0, 0)
local ray = Ray.new(origin, direction)
print(ray.Origin) -- Output: (0, 0, 0)

Direction

Vector3 The normalized vector indicating the direction of the ray.

Code Samples

local origin = Vector3.new(0, 0, 0)
local direction = Vector3.new(1, 0, 0)
local ray = Ray.new(origin, direction)
print(ray.Direction) -- Output: (1, 0, 0)

Constructors

new

Creates a new Ray instance with the given Origin and Direction.

Parameters

Vector3 InOrigin

The origin point of the ray in 3D space.

Vector3 InDirection

The direction vector of the ray. It is usually normalized.

Return

Ray

A new `Ray` instance.

Code Samples

local origin = Vector3.new(0, 0, 0)
local direction = Vector3.new(1, 0, 0)
local ray = Ray.new(origin, direction)
print(ray) -- Output: Ray

Methods

Unit

Returns a Ray where the direction vector is normalized.

Parameters

None

Return

Ray

A copy of the ray, but with a normalized direction vector.

Code Samples

local origin = Vector3.new(0, 0, 0)
local direction = Vector3.new(2, 0, 0)
local ray = Ray.new(origin, direction)
local unitRay = ray:Unit()
print(unitRay.Direction) -- Output: (1, 0, 0)

ClosestPoint

Finds the closest point on the ray to a given point in 3D space.

Parameters

Vector3 InPoint

The point in 3D space to find the closest point to.

Return

Vector3

The closest point on the ray to the provided point.

Code Samples

local rayOrigin = Vector3.new(0, 0, 0)
local rayDirection = Vector3.new(1, 0, 0)
local ray = Ray.new(rayOrigin, rayDirection)
local point = Vector3.new(0, 1, 0)
local closestPoint = ray:ClosestPoint(point)
print(closestPoint) -- Output: (0, 0, 0)

Distance

Calculates the perpendicular distance between the ray and a given point.

Parameters

Vector3 InPoint

The point in 3D space to calculate the perpendicular distance to.

Return

number

The distance between the ray and the point.

Code Samples

local origin = Vector3.new(0, 0, 0)
local direction = Vector3.new(1, 0, 0)
local ray = Ray.new(origin, direction)
local point = Vector3.new(0, 1, 0)
local distance = ray:Distance(point)
print(distance) -- Output: 1

Last updated