Ray tracing function

raycast

The raycast function traces a ray through the world, detecting entities and blocks along the path. Useful for line-of-sight checks, targeting systems, and custom projectiles.


Function Signature

result = await raycast(
    world,
    start,
    direction,
    max_distance=64.0,
    ray_size=0.2,
    include_entities=True,
    include_blocks=True,
    ignore_passable=True
)
from bridge import *

# Raycast from a player's eye position in their look direction
result = await raycast(
    player.world,
    (player.location.x, player.location.y + 1.62, player.location.z),
    (player.location.yaw, player.location.pitch),
    max_distance=50.0
)

if result:
    if result.entity:
        await player.send_message(f"§aLooking at: {result.entity.type.name}")
    elif result.block:
        await player.send_message(f"§aLooking at block: {result.block.type.name}")

RaycastResult

Returned by raycast() when a hit occurs.

Attributes

x

X coordinate of the hit point.

y

Y coordinate of the hit point.

z

Z coordinate of the hit point.

entity

The entity that was hit, or None if a block was hit.

block

The block that was hit, or None if an entity was hit.

start_x

X coordinate of the ray origin.

start_y

Y coordinate of the ray origin.

start_z

Z coordinate of the ray origin.

yaw

Yaw direction of the ray.

pitch

Pitch direction of the ray.

distance

Distance from the ray origin to the hit point.

hit_face

The face of the block that was hit (e.g. "NORTH", "UP"), or None for entity hits.


Example: Target selector

from bridge import *

@command("Smite what you're looking at")
async def smite(player: Player, args: list[str]):
    loc = player.location
    result = await raycast(
        player.world,
        (loc.x, loc.y + 1.62, loc.z),
        (loc.yaw, loc.pitch),
        max_distance=100.0,
        include_entities=True,
        include_blocks=False
    )

    if result and result.entity:
        hit_loc = Location(result.x, result.y, result.z, player.world.name)
        await player.world.strike_lightning(hit_loc)
        await player.send_message(f"§e⚡ Smited {result.entity.type.name}!")
    else:
        await player.send_message("§cNo entity in sight!")

Example: Build tool

from bridge import *

@command("Place a block where you look")
async def place(player: Player, args: list[str]):
    material = args[0] if args else "STONE"
    loc = player.location
    result = await raycast(
        player.world,
        (loc.x, loc.y + 1.62, loc.z),
        (loc.yaw, loc.pitch),
        max_distance=50.0,
        include_entities=False,
        include_blocks=True
    )

    if result and result.block:
        # Place block on the hit face
        bx, by, bz = int(result.x), int(result.y), int(result.z)
        if result.hit_face == "UP": by += 1
        elif result.hit_face == "DOWN": by -= 1
        elif result.hit_face == "NORTH": bz -= 1
        elif result.hit_face == "SOUTH": bz += 1
        elif result.hit_face == "EAST": bx += 1
        elif result.hit_face == "WEST": bx -= 1

        await player.world.set_block(bx, by, bz, material)
        await player.send_message(f"§aPlaced {material}!")
    else:
        await player.send_message("§cNo block in range!")

Example: Line of sight check

from bridge import *

async def can_see(player, target_location):
    """Check if a player has line of sight to a location."""
    loc = player.location
    # Calculate direction (simplified — yaw/pitch calculation)
    result = await raycast(
        player.world,
        (loc.x, loc.y + 1.62, loc.z),
        (loc.yaw, loc.pitch),
        max_distance=64.0,
        include_entities=False,
        include_blocks=True,
        ignore_passable=True
    )

    if result is None:
        return True  # Nothing blocking
    return result.distance > loc.distance(target_location)