physics.reflectRay()

Type Function
Library physics.*
Return value The x and y of the reflected ray
Revision Release 2024.3703
Keywords ray, raycast, casting, physics, collision, reflect, reflection
See also physics.rayCast()

Overview

This function is used to reflect a vector as returned by physics.rayCast().

The reflected vector returned represents the direction of the reflection, and has a magnitude (length) of 1.

Syntax

physics.reflectRay( fromX, fromY, hit )
fromX (required)

Number. The starting x position of the ray.

fromY (required)

Number. The starting y position of the ray.

hit (required)

Table. An entry from the hits array returned by physics.rayCast().

Example

local hits = physics.rayCast( 0, 0, 200, 300, "closest" )

if ( hits ) then

    -- There's at least one hit
    print( "Hit count: " .. tostring( #hits ) )
    print( "The first object hit is: ", hits[1].object, " at position: ", hits[1].position.x, hits[1].position.y, " where the surface normal is: ", hits[1].normal.x, hits[1].normal.y )

    local reflected_ray_direction_x, reflected_ray_direction_y = physics.reflectRay( 0, 0, hits[1] )
    print( "Reflected direction: ", reflected_ray_direction_x, reflected_ray_direction_y )
    
else
    -- No hits
end