TerrainPatch

Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Game.Graphics.Worlds.Terrain, Libraries.Interface.Item3D, Libraries.Interface.Item, Libraries.Language.Object, Libraries.Game.Graphics.Model

A TerrainPatch is a rectangle of a terrain's samples that the physics engine can collide with. It is the shared part of TerrainRegionModel, which draws a whole region, and TerrainCollisionPiece, which only collides with a small square near the player. It is a Terrain, so the physics engine's HeightmapTerrainShape works with it unchanged. The shape asks the terrain for the corners of each triangle, and this class answers by reading the heights straight from the TerrainData, so the answers always match the current heights. A patch covers the samples from its first sample up to its size along x and z, and reaches one sample further, into whatever comes next, where that sample exists. That way neighboring patches share their edges and there are no gaps between them. The patch's own grid has one unit between samples. The vertex spacing is applied as its x and z scale, the way HeightmapTerrainShape expects. The patch sits at the lowest height it covers, and its heights are measured up from there, so the shape's box, which starts at the patch's position, holds all of the ground.

Example Code

use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainPatch

TerrainData data
data:AddRegion(0, 0)
TerrainPatch patch
patch:Measure(data, 0, 0, 16)
patch:ApplyCollisionShape()
patch:PlaceInWorld()

Actions

ApplyCollisionShape()

This action gives the patch its sizes and its collision shape, from the last call to Measure. Once its width, height and depth are all set, Item3D gives it a plain box shape, so the terrain shape is set after them.

FillAtPixel(integer x, integer y, Libraries.Containers.Array<Libraries.Compute.Vector3> vertices, Libraries.Game.BoundingBox box, boolean leftTriangle)

This action fills in the three corners of one triangle of the ground, in the patch's own grid, and the box around them. The collision shape calls it for each triangle it tests. The heights are read from the TerrainData, so they are always the current ones. Each grid cell is split into two triangles: the left one is the top left, bottom left and top right corners, and the right one is the top right, bottom left and bottom right corners. This matches Terrain and the meshes TerrainRegionModel draws.

Parameters

  • x integer: The cell's column. Cells past the last one use the last one.
  • y integer: The cell's row, which is along z.
  • vertices Libraries.Containers.Array<Libraries.Compute.Vector3>: Exactly three vectors, which are filled with the triangle's corners.
  • box Libraries.Game.BoundingBox: The box to fill in around the triangle, pushed out by a small margin.
  • leftTriangle boolean: True for the left triangle of the cell, false for the right one.

GetBaseHeight()

This action returns the lowest height the patch covers, which is the height it sits at.

Returns

number: The lowest height, in world units.

GetFirstSampleX()

This action returns the patch's first sample along x.

Returns

integer: The first sample along x.

GetFirstSampleZ()

This action returns the patch's first sample along z.

Returns

integer: The first sample along z.

GetPatchHeight(integer column, integer row)

This action returns the height of a sample, counted from the patch's first sample. Samples outside the patch can be asked for, which is how normals at the edges are worked out. Where no region holds a sample, the nearest sample inside the patch is used instead, so the ground is treated as continuing flat past the edge of the world.

Parameters

  • column integer: The sample's column, counted from the patch's first sample.
  • row integer: The sample's row, counted from the patch's first sample.

Returns

number: The height, in world units.

GetPixelHeight()

This action returns how many samples the patch covers along z. The collision shape reads this, as it does for Terrain.

Returns

integer: The number of samples along z.

GetPixelWidth()

This action returns how many samples the patch covers along x. The collision shape reads this, as it does for Terrain.

Returns

integer: The number of samples along x.

GetTerrainData()

This action returns the TerrainData the patch reads its heights from.

Returns

Libraries.Game.Graphics.Worlds.TerrainData: The terrain's data, or undefined if the patch has not been measured.

GetTopHeight()

This action returns the highest height the patch covers.

Returns

number: The highest height, in world units.

IsAffectedBy(integer minimumX, integer minimumZ, integer maximumX, integer maximumZ)

This action returns true if a change to a rectangle of samples would change this patch. Its heights reach one sample past its size, and the normals of anything drawn from it read one sample further on each side, so the rectangle is compared against the patch grown by one sample, and by two on the far sides.

Parameters

  • minimumX integer: The rectangle's smallest sample along x.
  • minimumZ integer: The rectangle's smallest sample along z.
  • maximumX integer: The rectangle's largest sample along x.
  • maximumZ integer: The rectangle's largest sample along z.

Returns

boolean: True if the patch should be rebuilt.

Measure(Libraries.Game.Graphics.Worlds.TerrainData data, integer firstX, integer firstZ, integer size)

This action sets which samples the patch covers and finds the lowest and highest heights among them. It reaches one sample past its size along x, and along z, where that sample has a height.

Parameters

  • data Libraries.Game.Graphics.Worlds.TerrainData: The heights of the whole terrain.
  • firstX integer: The patch's first sample along x.
  • firstZ integer: The patch's first sample along z.
  • size integer: How many cells the patch covers along each side.

PlaceInWorld()

This action places the patch in the world and scales it to the vertex spacing. It is called after physics has been set up, the same order a game uses for Terrain.

Inherited Actions

110 actions from Libraries.Game.Graphics.Model
14 actions from Libraries.Game.Graphics.Worlds.Terrain
62 actions from Libraries.Interface.Item
211 actions from Libraries.Interface.Item3D
3 actions from Libraries.Language.Object