Page 1 of 1

Lua: raycast3D()

PostPosted: Tue Jul 17, 2012 23:11
by Temperest
This is a small function that allows you to cast a ray in a given direction. Currently, a more advanced version is being used in my unreleased FPS mod for MineTest for hit detection for the rifle.

It uses a ray supercover algorithm adapted for 3D. To read more about the algorithm, see here.

Example:

Using raycast3D() to detect the node a player is pointing at regardless of how far away it is, and placing a MESE node on it:

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
raycast3D(pos, dir, function(pos, nearest, function(pos, nearest, distance)
    if minetest.env:get_node(pos).name ~= "air" then
        env:add_node(nearest, {name="default:mese"})
        return
    end
end)


Parameters:

raycast3D(pos, dir, callback)

pos - MineTest position reference: {x=number, y=number, z=number}, where number is either an integer or a float value.
dir - Unit vector in MineTest direction format (obtainable through functions like player:get_look_dir()): {x=number, y=number, z=number}, where x * x + y * y + z * z == 1.
callback - function accepting the following parameters: pos (current node being tested), nearest (previous node being tested), and distance (distance from starting point of ray).

Download:

Link

License: AGPLv3