TerrainCollision

Package: Libraries.Game.Graphics.Worlds · Inherits from: Libraries.Language.Object

TerrainCollision gives a terrain collision only near one place, usually the player. The ground is split into a grid of small squares, the shape size across, and only the squares within a radius of the target have a collision piece. Far away ground costs nothing in physics, however large the world is. As the target moves into a new square of the grid, pieces that are now too far away are taken out of the game and moved to the new squares that came into reach. Pieces are kept and reused rather than made and thrown away. While the target stays in the same square, Update does nothing. Both the shape size and the radius are in samples, as in Terrain3D, whose dynamic collision mode this follows. Its defaults are a shape size of 16 and a radius of 64. The shape size must be a power of 2 that is no larger than the terrain's region size, so every square sits inside one region. Only squares in a region get a piece. Anything outside the radius, such as a ball rolling away from the player, has no ground under it.

Example Code

use Libraries.Game.Game
use Libraries.Game.Graphics.Worlds.TerrainData
use Libraries.Game.Graphics.Worlds.TerrainCollision

class Main is Game
TerrainData data
TerrainCollision collision

action Main
    StartGame()
end

action CreateGame
    EnablePhysics3D(true)
    data:AddRegion(0, 0)
    collision:SetTerrainData(data)
end

action Update(number seconds)
    // Keep collision around the camera.
    collision:Update(GetCurrentLayer3D(), GetCamera3D():GetX(), GetCamera3D():GetZ())
end
end

Actions

ChooseCells(number worldX, number worldZ)

This action works out which grid squares should have collision pieces for a target at a world position: those in a region whose centers are within the radius of the center of the target's own square. Use GetChosenCount, GetChosenX and GetChosenZ to read the result.

Parameters

  • worldX number: The target's position along x, in world units.
  • worldZ number: The target's position along z, in world units.

Clear()

This action takes every piece out of the game and keeps them for reuse. The next Update places pieces again.

GetActivePiece(integer index)

This action returns one of the collision pieces that are in the game.

Parameters

  • index integer: Which piece, from 0 to one less than GetActivePieceCount.

Returns

Libraries.Game.Graphics.Worlds.TerrainCollisionPiece: The piece.

GetActivePieceCount()

This action returns how many collision pieces are in the game.

Returns

integer: The number of active pieces.

GetCellStart(integer sample)

This action returns the first sample of the grid square that holds a sample, along one axis. Squares start at multiples of the shape size, so with a size of 16, sample 20 is in the square starting at 16, and sample -1 is in the square starting at -16.

Parameters

  • sample integer: A sample's number along one axis.

Returns

integer: The first sample of its square along that axis.

GetChosenCount()

This action returns how many squares the last ChooseCells chose.

Returns

integer: The number of squares.

GetChosenX(integer index)

This action returns the first sample along x of a square the last ChooseCells chose.

Parameters

  • index integer: Which square, from 0 to one less than GetChosenCount.

Returns

integer: The square's first sample along x.

GetChosenZ(integer index)

This action returns the first sample along z of a square the last ChooseCells chose.

Parameters

  • index integer: Which square, from 0 to one less than GetChosenCount.

Returns

integer: The square's first sample along z.

GetFriction()

This action returns how much the ground grips what rests on it.

Returns

number: The friction.

GetRadius()

This action returns how far from the target, in samples, the ground has collision.

Returns

integer: The radius, in samples.

GetShapeSize()

This action returns how many samples across each collision piece is.

Returns

integer: The number of cells along each side of a piece.

GetTerrainData()

This action returns the terrain whose ground the pieces follow.

Returns

Libraries.Game.Graphics.Worlds.TerrainData: The terrain's data.

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

This action places again any active pieces that cover a rectangle of samples, after heights there have changed. The pieces read heights straight from the terrain, but their position and size depend on the lowest and highest heights they cover.

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

integer: How many pieces were placed again.

Refresh()

This action asks for every piece to be placed again at the next Update, such as after the terrain has been built again.

SetFriction(number friction)

This action sets how much the ground grips what rests on it. It applies to pieces made after this, so set it before the first update.

Parameters

  • friction number: The friction.

SetRadius(integer samples)

This action sets how far from the target, in samples, the ground has collision. The default is 64, as in Terrain3D.

Parameters

  • samples integer: The radius, in samples. It must be more than 0.

SetShapeSize(integer size)

This action sets how many samples across each collision piece is. It must be a power of 2, at least 4. The default is 16, as in Terrain3D.

Parameters

  • size integer: The number of cells along each side of a piece.

SetTerrainData(Libraries.Game.Graphics.Worlds.TerrainData data)

This action sets the terrain whose ground the pieces follow.

Parameters

Update(Libraries.Game.Layer3D gameLayer, number worldX, number worldZ)

This action keeps collision pieces around a target. Call it every frame with the target's position; it only does work when the target has moved into a different square of the grid, or after the heights or settings have changed.

Parameters

  • gameLayer Libraries.Game.Layer3D: The layer to add the pieces to, usually the game's current 3D layer.
  • worldX number: The target's position along x, in world units.
  • worldZ number: The target's position along z, in world units.

Returns

boolean: True if any pieces were moved.

Inherited Actions

3 actions from Libraries.Language.Object