Terraincast

Query terrain height at a given world position.

Terraincast

terraincast(world_position, terrain_entity, height_values=None, return_normals=False)

Uses the X and Z of world_position to look up the corresponding Y on a terrain mesh.

Parameters

NameTypeDefaultDescription
world_positionVec3Position in world space.
terrain_entityEntityEntity using a Terrain model.
height_valueslistNoneRaw height map data; if None it’s taken from model.
return_normalsboolFalseIf True, also return surface normal at that point.

Example

from ursina import *
from ursina.terraincast import terraincast

app = Ursina()

terrain = Entity(
    model=Terrain('heightmap_1', skip=8),
    scale=(40,5,20),
    texture='heightmap_1'
)
player = Entity(model='sphere', scale=.2, origin_y=-.5, color=color.azure)

def update():
    move = Vec3(held_keys['d'] - held_keys['a'], 0, held_keys['w'] - held_keys['s']).normalized()
    player.position += move * time.dt * 4
    y = terraincast(player.world_position, terrain, terrain.model.height_values)
    if y is not None:
        player.y = y

EditorCamera()
app.run()