User Tools

Site Tools


guides:compute-water-elevation

This is an old revision of the document!


Custom component: how to compute ground or water elevation

Since 1.5, Foundation allows to mod Custom component and links it to a fbx (see Exemple02)

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,
                                   ToPosition,
                                   raycastResult,
                                   2 ^ OBJECT_FLAG.WATER:toNumber())
    then
        MyMod:logWarning("MY_CUSTOM_COMPONENT: Water not found on the vertical of "
                         .. tostring(_globalPosition))
        return _globalPosition[2]
    else
        return raycastResult["Position"][2]
    end
end

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

If the water (or the ground) is not found (for instance when the _globalPosition out of the map) then a warning is edited 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 (the water HAVEN'T the same elevation anywhere on a vanilla map !).

guides/compute-water-elevation.1587655298.txt.gz · Last modified: 2020/04/23 11:21 by vjraymon

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki