Community Forum

How to detect terrain type

Forum Overview >> Scripting

CategoryScripting
Created11.06.2020 11:45


Kenny Je (kenny456) 11.06.2020 11:45
Hi, is there any way how to detect type of terrain (road, gravel, sand, grass,..) on what i am riding with some lua function?
I tried:
getTerrainAttributesAtWorldPos
getDensityAtWorldPos

but with no succes.

Bilbo Beutlin (BBeutlin) 11.06.2020 14:47
local _, _, _, _ , materialId = getTerrainAttributesAtWorldPos(g_currentMission.terrainRootNode, x, y, z, true, true, true, true, false)

This is also used eg. in
https://gdn.giants-software.com/documentation_scripting_fs19.php?version=script&category=70&class=10578#getHoofSurfaceSound167202

There's a function in "Terrain Detail" which returns name from ID.

Kenny Je (kenny456) 12.06.2020 05:56
Thank for your reply Bilbo, but when i used it with x,y,z = getWorldTranslation(getRootNode()) or any other transform group which i created, materialId is still the same number even if i am moving. It is 1 on my savegame on Ravenport and 5 when i start new game on Felsburn. What node i have to use for x,y,z parameters?

Bilbo Beutlin (BBeutlin) 12.06.2020 08:07
*g* With 'getRootNode()' you won't get the location of an object. See
https://gdn.giants-software.com/documentation_scripting_fs19.php?version=engine&category=4&function=510
If you had print'ed out the x,y,z you had noticed always the same values.

Every object has in its data structure the variable .rootNode, often used as 'self.rootNode'. This is the objectId you need.

Kenny Je (kenny456) 13.06.2020 10:22
I used this in my update loop:

local x,y,z = getWorldTranslation(self.rootNode)
print(x..','..y..','..z)
local _, _, _, _, materialId = getTerrainAttributesAtWorldPos(g_currentMission.terrainRootNode, x, y, z, true, true, true, true, false)
if materialId ~= nil then
print("materialId - "..materialId)
end

Still doesnt work. x,y,z are changing offcourse but materialId is allways 1. What i am doing wrong?

Bilbo Beutlin (BBeutlin) 13.06.2020 10:40
There are a few exceptions to consider.
See the cited function 'getHoofSurfaceSound()' above (in my 1st post).

Kenny Je (kenny456) 16.06.2020 10:54
I created transform group and tried to move it up or down with function, but materialId is still 1 everywhere and in every height.

x,y,z = getTranslation(spec.myTransform)
if Input.isKeyPressed(Input.KEY_i) then
y = y + 0.01
elseif Input.isKeyPressed(Input.KEY_j) then
y = y - 0.01
end
setTranslation(spec.myTransform, x,y,z)
local x,y,z = getWorldTranslation(spec.myTransform)
local _, _, _, _, materialId = getTerrainAttributesAtWorldPos(g_currentMission.terrainRootNode, x, y, z, true, true, true, true, false)

Where can i find horse i3d file. I want to see how hoof nodes are placed. Or i need "groundRaycastCallback" function as in Rideable:updateFootsteps for proper work "getTerrainAttributesAtWorldPos" function?

Bilbo Beutlin (BBeutlin) 16.06.2020 13:36
All animals' i3d's are in $data/*.bar files. Not easily to access.
There's an XML extractor around which does what the name says (also an i3d has XML structure). But you won't get other files like textures etc.

The "groundRaycastCallback" should also work. Finally it is used in the game engine itself. ;)

Shawn E Carter (ShawnDaGeek) 17.06.2020 11:42
I am interested in this, so that i may clone trees onto grass only texture. No luck here so far but will post back if i find a solution.

Gtx | Andy (GtX_Andy) 06.08.2020 12:29
Well, I am late to the party but saw the post and decided to make a quick example of how this could be done.

Some of the suggestions are on the right track such as 'getTerrainAttributesAtWorldPos' but you are best to use the 'raycastClosest' function also to make sure you are reading the terrain not a shape object :-)

In this example script I have used the 'surfaceSounds' table to define the type of ground or shape that is under foot. This is an easy example as they should work on all maps even those using custom ground types if set up correctly.

Example Mod with script to use as reference or direct code use.
https://workupload.com/file/UC5y9wXKv6V

Maybe this will help in some way, any questions feel free to ask I will try and answer when I am online. :-)


Note: Log in to post. Create a new account here.