User Tools

Site Tools


Custom Component: Computing Ground or Water Elevation

The following LUA code computes and returns the global Y elevation of water from a global position:

function MY_CUSTOM_COMPONENT:computeWaterElevation(_globalPosition)
    local raycastResult = {}
    -- Raycast from the _globalPosition + 1000
    -- to _globalPosition - 1000
    -- only on objects with a WATER flag
    local FromPosition = { _globalPosition[1], _globalPosition[2]+1000, _globalPosition[3] }
    local ToPosition = { _globalPosition[1], _globalPosition[2]-1000, _globalPosition[3] }
    if not self:getLevel():rayCast(FromPosition,
                                   2 ^ OBJECT_FLAG.WATER:toNumber())
        MyMod:logWarning("MY_CUSTOM_COMPONENT: Water not found on the vertical of "
                         .. tostring(_globalPosition))
        return _globalPosition[2]
        return raycastResult["Position"][2]

You can do the same with the ground by replacing OBJECT_FLAG.WATER by OBJECT_FLAG.GROUND

If water (or ground) is not found (for instance when the _globalPosition is outside the map) then a warning is entered in the logs, and the code returns the Y of the _globalPosition.

This can be used, for instance, when moving an object on the ground or water whatever the slope.

Note: Water doesn't have the same elevation at all points on a vanilla map!

guides/compute-water-elevation.txt · Last modified: 2021/02/23 11:52 by minotorious

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki