Skip to content

Raycaster

SuperSpaceEye edited this page Jan 23, 2025 · 19 revisions

Raycaster is a block that can cast rays from itself.

Peripheral name - "raycaster"

Raycaster can detect both blocks and entities.

For blocks it detects its hitbox, and for entities it detects its bounding box.

If Valkyrien Skies is installed, it can also detect blocks on VS ships, and cast rays from VS ships.

Function internally yields as many times as needed by queuing "raycaster_raycast_event" event if raycasting takes more than max_raycast_time_ms, but if it takes less time, then it directly returns result without yielding once.

raycast(max_distance, {var1, var2, var3}, euler_mode, immediate_execution, check_for_blocks_in_world)

  • max_distance - maximum distance a ray can travel.
  • {var1, var2, var3} (D {0, 0, 1}) - a table of var1, var2, var3. Should either be nil (then the default values will be used), have two values (then the last default value will be used instead), or have all three values.
  • euler_mode (D false) - whenever or not to use euler mode. If false, it will use vector rotation instead.
  • immediate_execution (D true) - If true, will start executing immediately and return the result. If false, read below.
  • check_for_block_in_world (D true) - If false, it will not check for blocks in world, and will only check for entities and VS ships. Option can be disabled in config.

If euler_mode is true, then

  • var1 (D 0) - is for pitch.
  • var2 (D 0) - is for yaw.
  • var3 - is unused, and can be set to nil

Pitch and yaw are bounded between -pi/2 and pi/2 and are in radians.

If euler_mode is false, then

  • var1 (D 0) - is for Y.
  • var2 (D 0) - is for X.
  • var3 (D 1) - is for planar distance. Controls FOV of a raycaster.

X and Y are unbounded and can be set to any value, but planar distance should be bigger than 0. The bigger the planar distance the bigger the FOV is.

The ray is centered around 0,0, with positive var1 being up, negative being down, positive var2 being right, negative being left.

If immediately_execute is false, then The function will return a table of functions.

  • begin() - will begin raycasting and will return the result.
  • getCurI() - will return current iter number starting from 0.
  • terminate() - will terminate raycasting, if it's not finished.

addStickers(state)

  • state - if true, will change texture of the raycaster to the one with stickers on it's sides. If false, to the one without stickers.

getConfigInfo()

Returns a table of Pair<String, value> from raycaster server config.

getFacingDirection()

Returns facing direction of a raycaster.

Raycaster Responses

Raycaster can return several responses depending on what it hit, all of which are tables containing some data.

If block was hit

  • "is_block" - the boolean "true"
  • "hit_pos" - position in world where ray intersects with block hitbox.
  • "abs_pos" - absolute position of a block that it hit
  • "distance" - euclidean distance to a collision point with the block
  • "block_type" - a string containing type of a block it hit
  • "rel_hit_pos" - hit_pos-ray_origin

If entity was hit

  • "is_entity" - the boolean "true"
  • "hit_pos" - position in world where ray intersects with entity hitbox.
  • "abs_pos" - absolute position of hit entity
  • "distance" - euclidean distance to the collision point with the entity
  • "id" - entity id
  • "rel_hit_pos" - hit_pos-ray_origin
  • "descriptionId" - a string containing type of hit entity

If block on VS ship was hit - The same as when normal block is hit, but with

  • "ship_id" - id of a ship
  • "hit_pos" - position in world where ray intersects with projection of shipyard block
  • "hit_box_ship" - position in world where ray actually intersects with block.

If nothing was hit

  • "is_block" - the boolean "true"
  • "distance" - max_distance given to function, or max_distance set by config, whichever is less.
  • "block_type" - a string containing "block.minecraft.air"

If an error happened:

  • "error" - a string with an error.