Class RaycastResult

java.lang.Object
net.risingworld.api.utils.RaycastResult

public final class RaycastResult extends Object
Represents the result of a raycast.
  • Method Details

    • hasCollision

      public boolean hasCollision()
      Gets whether or not a collision occurred. Usually it is not necessary to call this method, since in case that no collision occurred, there wouldn't be a RaycastResult object (i.e the callback result would be null).
      Returns:
      true if this object holds a result, false if not.
    • getDistance

      public float getDistance()
      Gets the distance between the player (or more precisely, the origin of the ray) and the collision point. Returns Float.MAX_VALUE if no collision occurred (see hasCollision().
      Returns:
      the distance to the collision point.
    • getLayer

      public int getLayer()
      Gets the hit layer. If no collision occurred, this methods returns 0.
      Returns:
      the layer of the object that was hit.
      See Also:

      Example: Check layer
      1//Get RayCastResult object (usually from a callback)
      2RayCastResult result = ...;
      3
      4//Now check the collision layer (see all layers in "Layer" class)
      5int layer = result.getLayer();
      6if(layer == Layer.TERRAIN){
      7 System.out.println("Collided with terrain!");
      8}
      9else if(layer == Layer.CONSTRUCTIONS || layer == Layer.TRANSPARENT_CONSTRUCTIONS){
      10 System.out.println("Collided with construction element!");
      11}
      12else if(layer == Layer.WATER){
      13 System.out.println("Collided with water!");
      14}
    • getCollisionPoint

      public Vector3f getCollisionPoint()
      Gets the collision point, i.e the position where the ray hit a collider.
      Note: If you want to get the world position of the hit object itself instead, see getObjectPosition()
      Returns:
      the collision position as a Vector3f.
    • getCollisionNormal

      public Vector3f getCollisionNormal()
      Gets the collision normal, i.e the normal of the surface the ray hit.
      This normal is already normalized.
      Returns:
      the collision normal as a normalized Vector3f, or null if no collision occurred.
    • getInstanceID

      public int getInstanceID()
      Gets the internal instance ID of the underlying Unity GameObject that was hit. The instance ID identifies the actual internal object. It's neither persistent nor identical between different players!
      Returns:
      the internal Unity object instance ID (may be positive or negative).
      See Also:
    • isTrigger

      public boolean isTrigger()
      Gets whether or not the hit collider was a "trigger" (i.e a ghost collider).
      Returns:
      true if the hit collider was a trigger, false if it's a solid collider.
    • getTriangleIndex

      public int getTriangleIndex()
      Gets the triangle index of the hit element mesh.
      Returns:
      the hit triangle index.
    • getObjectGlobalID

      public long getObjectGlobalID()
      Gets the global ID (of the element the ray collided with). This only works for elements which have an id (e.g objects, construction elements, plants, npc etc).
      Returns:
      the element type id, or -1 if no collision occurred or if the object does not have a type id (e.g terrain, water, grass etc).
      See Also:

      Example #1: Check if we look at a door, then open it
      1//Get RayCastResult object (usually from a callback)
      2RayCastResult result = ...;
      3
      4//Check if we have a collision
      5if(result != null){
      6 //Check if we hit an object element
      7 if(result.getLayer() == Layer.OBJECTS && result.getObjectGlobalID() > 0){
      8 //Get chunk position
      9 Vector3i chunk = result.getObjectChunkPosition();
      10
      11 //Get the object element based on the globalID and chunk coordinates
      12 ObjectElement element = World.getObjectElement(result.getObjectGlobalID(), chunk.x, chunk.y, chunk.z);
      13
      14 //Check if object is not null
      15 if(element != null){
      16 //Get the object definition
      17 Objects.ObjectDefinition def = element.getDefinition();
      18
      19 //Check if object is a door (determined by type)
      20 if(def.type == Objects.Type.Door){
      21 //If status is 0, the door is closed
      22 if(element.getStatus() == 0){
      23 //Set status to 1 (open)
      24 element.setStatus((byte) 1);
      25 }
      26 }
      27 }
      28 }
      29}
      Example #2: Paint the construction element we're looking at red
      1//Get RayCastResult object (usually from a callback)
      2RayCastResult result = ...;
      3
      4//Check if we have a collision
      5if(result != null){
      6 //Check if we hit a construction element (include transparent ones)
      7 if((result.getLayer() == Layer.CONSTRUCTIONS || result.getLayer() == Layer.TRANSPARENT_CONSTRUCTIONS) && result.getObjectGlobalID() > 0){
      8 //Get chunk position
      9 Vector3i chunk = result.getObjectChunkPosition();
      10
      11 //Get the construction element based on the globalID and chunk coordinates
      12 ConstructionElement element = World.getConstructionElement(result.getObjectGlobalID(), chunk.x, chunk.y, chunk.z);
      13
      14 //Check if element is not null
      15 if(element != null){
      16 //Change the color to red (we use the hex rgba code)
      17 element.setColor(0xFF0000FF);
      18 }
      19 }
      20}
    • getObjectPosition

      public Vector3f getObjectPosition()
      Gets the world position of the element that was hit by this ray. Not to be confused with the collision point, which is the actual point where the collision occurred.
      Returns:
      the world position of the hit element.
      See Also:
    • getObjectChunkPosition

      public Vector3i getObjectChunkPosition()
      Gets the chunk coordinates where the element is located that was hit by this ray. If we hit an object (like furniture), a construction element (like a block) or a plant, we can use the chunk position in combination with getObjectGlobalID() to get the object instance (e.g from World.getObject(long, int, int, int) or World.getConstructionElement(long, int, int, int).
      Returns:
      the chunk coordinates of the hit element.
      See Also:
    • toString

      public String toString()
      Gets a String representation of this RayCastResult.
      Overrides:
      toString in class Object
      Returns:
      a string representing the values of this RayCastResult.