Skip to content

Grid Casting

KrystilizeNevaDies edited this page Sep 19, 2021 · 4 revisions

Grid casting is a technique used to iterate over units in space.

It is commonly used in voxel games that use a 1 wide grid to represent blocks, e.g. Minecraft, Terraria, and Stonehearth.

To facilitate this, we provide the GridCast class.

Lets go through some examples.

Example 1: Basic 1 wide grid casting.

double posX = Math.random(), posY = Math.random(), posZ = Math.random();
double dirX = Math.random(), dirY = Math.random(), dirZ = Math.random();

Iterator<Vector3d> iterator = GridCast.createGridIterator(
    posX, posY, posZ,
    dirX, dirY, dirZ
);

while (iterator.hasNext()) {
    Vector3d gridUnit = iterator.next();
    System.out.println(gridUnit.x() + ":" + gridUnit.y() + ":" + gridUnit.z());
}

This example above will create an Iterator over the blocks that lay on the line formed by the position and direction specified.

Then, while the iterator has a next block (which will always happen without a maxLength), the next block is printed to the console.

GridCast#createExactGridIterator may also be used if you wish to get the exact position that a grid unit was intersected.

Rayfast is a geometry library designed for use in java game servers. It features efficient ray and block casting.

Clone this wiki locally