struct Eagle::Ray

Overview

A 3D ray: an #origin and a unit #direction. Use it for picking, line of sight and simple hit-scan weapons.

Camera3D#mouse_ray gives you the ray under the mouse cursor. Each intersect_* method returns the distance along the ray to the hit, or nil on a miss. Pass that distance to #at to get the hit point.

ray = Ray.new(v3(0, 2, 0), v3(0, -1, 0))
if t = ray.intersect_plane(Vec3::ZERO, Vec3::UP)
  ground = ray.at(t) # => (0, 0, 0)
end
ray.intersect_sphere(v3(0, -5, 0), 1) # => 6.0

Defined in:

eagle/math/rect.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(origin : Vec3, direction : Vec3) #

Creates a ray. direction is normalized for you.


Instance Method Detail

def at(t : Number) : Vec3 #

The point at distance t along the ray.


def direction : Vec3 #

Unit direction of the ray.


def direction=(direction : Vec3) #

Unit direction of the ray.


def intersect_aabb(box : AABB) : Float32 | Nil #

Distance to the first hit on box, or nil. If the ray starts inside the box, this is the distance to where it exits.


def intersect_plane(point : Vec3, normal : Vec3) : Float32 | Nil #

Distance to where the ray crosses the plane through point with normal, or nil when the ray is parallel to it or pointing away.


def intersect_sphere(center : Vec3, radius : Number) : Float32 | Nil #

Distance to the first hit on a sphere, or nil.


def origin : Vec3 #

Where the ray starts.


def origin=(origin : Vec3) #

Where the ray starts.