Ray: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>GoldenUrg
initial from forum posts/experiments
 
>JulienDethurens
No edit summary
 
(30 intermediate revisions by 10 users not shown)
Line 1: Line 1:
{{CatUp|Data Types}} {{CatUp|Scripting}}
{{Map|Scripting|Data Types}}
__NOTOC__
__TOC__


==Description==
A Ray is a half-line, that is finite in one direction, but infinite in the other. It can be defined by a 3-D point, where the line originates from, and a direction vector, which is the direction it goes in. The direction is similar to the lookVector of [[CFrame#Properties|CFrame]].


A Ray is a 3-D point and a direction.  
Often, it is useful to construct a Ray at one point directed toward another. There is no [[constructor]] defined for this intent, but it can be be done as follows:


==Usage==
local start = Vector3.new(...)
local lookAt = Vector3.new(...)
local ray = Ray.new(start, (lookAt - start).unit)


The Ray objects main purpose seems to be to act as a basis for Ray tracing. Its methods allow comparison of Ray against other 3-D points.
The Ray object's main purpose is to act as a basis for Ray casting. Its methods allow comparison of Ray against other 3-D points. Rays are also used in the [[FindPartOnRay (Method)|FindPartOnRay]] method of the Workspace.


For example you can detect if a Ray intersects a sphere:
For example you can detect if a Ray intersects a sphere:
<pre>
function intersect_sphere( ray, sphere_position, sphere_radius )
  return ray:Distance( sphere_position ) <= sphere_radius
end
</pre>
To check if your target is pointing at a sphere (even if other things are in the way):
<pre>
if intersect_sphere( mouse.UnitRay, sphere.Position, sphere.Size.X ) then
  -- do on hit
end
</pre>


==[[Constructors]]==
function intersectsSphere(ray, position, radius)
    return ray:Distance(position) <= radius
end


{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
To check if the mouse is pointing at a sphere (even if other things are in the way):
 
if intersectsSphere(mouse.UnitRay, sphere.Position, sphere.Size.X/2) then
    -- handle collision
end
 
{| class="wikitable"
|+Constructors
! Constructor !! Description
! Constructor !! Description
|-
|-
| Ray.new('''Origin''', '''Direction''') || Creates a new Ray with given '''Origin''' and '''Direction'''.
| Ray.new(Vector3 '''Origin''', Vector3 '''Direction''') || Creates a new Ray with given '''Origin''' and '''Direction'''.
|}
|}


== [[Methods]] ==
{| class="wikitable"
{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
|+Methods
! Member Function !! Description
! Method !! Description
|-
|-
| ''Ray'':ClosestPoint(Vector3 '''point''') || Returns the closest point on the Ray to '''point'''. Note Rays are unidirectional.
| ''Ray'':ClosestPoint(Vector3 '''point''') || Returns the closest point on the Ray to '''point'''. Note Rays are unidirectional.
Line 40: Line 40:
|}
|}


== [[Properties]] ==
== Properties ==
All of these properties are Read Only (you can't just set them Vector3.x = 5, it doesn't work) but you can create new
 
All of these properties are read-only:


{|border="1" cellspacing="5"  style=" -webkit-border-radius: 4px; -moz-border-radius: 4px; height: 100%; background-color: #FFFFFF; border-top: dashed 2px #ff0000; border-left: dashed 2px #ff0000; border-bottom: dashed 2px #aa0000; border-right: dashed 2px #aa0000; margin: 6px; margin-right: 10px; margin-left: 10px; clear: none; padding: 2px;"
{| class="wikitable"
|+Properties
! Property !! Type !! Description
! Property !! Type !! Description
|-
|-
| Vector3.'''Origin''' || [[Vector3]] || The Origin position
| ''Ray''.'''Origin''' || [[Vector3]] || The Origin position
|-
| ''Ray''.'''Direction''' || [[Vector3]] || The Direction vector
|-
|-
| Vector3.'''Direction''' || [[Vector3]] || The Direction vector
| ''Ray''.'''Unit''' || [[Ray]] || The Ray with a normalized Direction
|}
|}


== Limitations ==
== Limitations ==
Advanced Note: Technically the direction need not be a unit vector, but most Ray methods assume it is.
Technically, the length of the direction vector does not matter. However most Ray methods assume it is 1, so direction is a unit vector. As a result, you will get incorrect results if you do not make the direction a unit vector, or use Ray.Unit.


== See Also ==
== See Also ==
Line 58: Line 62:
*[[Vector3]]
*[[Vector3]]
*[[UnitRay (Property)|UnitRay]]
*[[UnitRay (Property)|UnitRay]]
[[Category:Data types]]

Latest revision as of 17:55, 7 April 2012

A Ray is a half-line, that is finite in one direction, but infinite in the other. It can be defined by a 3-D point, where the line originates from, and a direction vector, which is the direction it goes in. The direction is similar to the lookVector of CFrame.

Often, it is useful to construct a Ray at one point directed toward another. There is no constructor defined for this intent, but it can be be done as follows:

local start = Vector3.new(...)
local lookAt = Vector3.new(...)
local ray = Ray.new(start, (lookAt - start).unit)

The Ray object's main purpose is to act as a basis for Ray casting. Its methods allow comparison of Ray against other 3-D points. Rays are also used in the FindPartOnRay method of the Workspace.

For example you can detect if a Ray intersects a sphere:

function intersectsSphere(ray, position, radius)
    return ray:Distance(position) <= radius
end

To check if the mouse is pointing at a sphere (even if other things are in the way):

if intersectsSphere(mouse.UnitRay, sphere.Position, sphere.Size.X/2) then
    -- handle collision
end
Constructors
Constructor Description
Ray.new(Vector3 Origin, Vector3 Direction) Creates a new Ray with given Origin and Direction.
Methods
Method Description
Ray:ClosestPoint(Vector3 point) Returns the closest point on the Ray to point. Note Rays are unidirectional.
Ray:Distance(Vector3 point) Returns the distance from point to ClosestPoint(point)

Properties

All of these properties are read-only:

Properties
Property Type Description
Ray.Origin Vector3 The Origin position
Ray.Direction Vector3 The Direction vector
Ray.Unit Ray The Ray with a normalized Direction

Limitations

Technically, the length of the direction vector does not matter. However most Ray methods assume it is 1, so direction is a unit vector. As a result, you will get incorrect results if you do not make the direction a unit vector, or use Ray.Unit.

See Also