Lua: raycast3D()

Post Reply
Temperest
Member
Posts: 651
Joined: Tue Nov 15, 2011 23:13
GitHub: Uberi

Lua: raycast3D()

by Temperest » Post

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:

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
WorldEdit 1.0 released

The Mesecons Laboratory - the art of Mesecons circuitry
Latest article: Mesecons Basics.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 26 guests