User Tools

Site Tools


tutorial:pixel_raycast

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:pixel_raycast [2020/08/15 00:46] emmanuelmesstutorial:pixel_raycast [2023/02/22 08:23] – Minor formatting fix redgrapefruit
Line 5: Line 5:
 All of this is client side. All of this is client side.
  
-There are two types, center pixel (crosshair) and arbitrary pixel.+There are two cases, center pixel (crosshair) and arbitrary pixel.
  
 ===== Special case: Center pixel  ===== ===== Special case: Center pixel  =====
Line 34: Line 34:
 ==== For arbitrary reach ==== ==== For arbitrary reach ====
  
-The code above allows for the normal reach, 3 blocks for survival and 4.5 in creative. If you want the raycast to reach farther you need to use the general case below. Example [[https://github.com/EmmanuelMess/BoundingBoxMinecraftMod|here]].+The code above allows for the normal reach, 3 blocks for survival and 4.5 in creative. If you want the raycast to reach farther you need to use the general case below.
  
 ===== General case: Arbitrary pixel  ===== ===== General case: Arbitrary pixel  =====
 +
 +Example [[https://github.com/EmmanuelMess/BoundingBoxMinecraftMod|here]].
  
 For this one we need to precalculate a few things: For this one we need to precalculate a few things:
Line 77: Line 79:
 Then you have this reimplementation of the code at GameRenderer#updateTargetedEntity: Then you have this reimplementation of the code at GameRenderer#updateTargetedEntity:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
-private static HitResult rayTraceInDirection(MinecraftClient client, float tickDelta, Vec3d direction) {+private static HitResult raycastInDirection(MinecraftClient client, float tickDelta, Vec3d direction) {
     Entity entity = client.getCameraEntity();     Entity entity = client.getCameraEntity();
     if (entity == null || client.world == null) {     if (entity == null || client.world == null) {
Line 84: Line 86:
  
     double reachDistance = client.interactionManager.getReachDistance();//Change this to extend the reach     double reachDistance = client.interactionManager.getReachDistance();//Change this to extend the reach
-    HitResult target = rayTrace(entity, reachDistance, tickDelta, false, direction);+    HitResult target = raycast(entity, reachDistance, tickDelta, false, direction);
     boolean tooFar = false;     boolean tooFar = false;
     double extendedReach = reachDistance;     double extendedReach = reachDistance;
     if (client.interactionManager.hasExtendedReach()) {     if (client.interactionManager.hasExtendedReach()) {
-        extendedReach = 6.0D;+        extendedReach = 6.0D;//Change this to extend the reach
         reachDistance = extendedReach;         reachDistance = extendedReach;
     } else {     } else {
Line 108: Line 110:
             .stretch(entity.getRotationVec(1.0F).multiply(reachDistance))             .stretch(entity.getRotationVec(1.0F).multiply(reachDistance))
             .expand(1.0D, 1.0D, 1.0D);             .expand(1.0D, 1.0D, 1.0D);
-    EntityHitResult entityHitResult = ProjectileUtil.rayTrace(+    EntityHitResult entityHitResult = ProjectileUtil.raycast(
             entity,             entity,
             cameraPos,             cameraPos,
Line 136: Line 138:
 } }
  
-private static HitResult rayTrace(+private static HitResult raycast(
         Entity entity,         Entity entity,
         double maxDistance,         double maxDistance,
Line 144: Line 146:
 ) { ) {
     Vec3d end = entity.getCameraPosVec(tickDelta).add(direction.multiply(maxDistance));     Vec3d end = entity.getCameraPosVec(tickDelta).add(direction.multiply(maxDistance));
-    return entity.world.rayTrace(new RayTraceContext(+    return entity.world.raycast(new RaycastContext(
             entity.getCameraPosVec(tickDelta),             entity.getCameraPosVec(tickDelta),
             end,             end,
-            RayTraceContext.ShapeType.OUTLINE, +            RaycastContext.ShapeType.OUTLINE, 
-            includeFluids ? RayTraceContext.FluidHandling.ANY : RayTraceContext.FluidHandling.NONE,+            includeFluids ? RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE,
             entity             entity
     ));     ));
Line 154: Line 156:
 </code> </code>
  
-Once you have the direction and the raytracer, you can put them together:+Once you have the direction and the raycaster, you can put them together:
 <code java [enable_line_numbers="true"]> <code java [enable_line_numbers="true"]>
 Vec3d direction = map( Vec3d direction = map(
Line 166: Line 168:
         height         height
 ); );
-HitResult hit = rayTraceInDirection(client, tickDelta, direction);+HitResult hit = raycastInDirection(client, tickDelta, direction);
  
 switch(hit.getType()) { switch(hit.getType()) {
Line 185: Line 187:
 </code> </code>
  
-Here x and y are you pixel coordinates.+Here x and y are your pixel coordinates.
  
 ==== Performance considerations ==== ==== Performance considerations ====
  
-This is EXPENSIVE, if you do it too many times, it WILL get slow. Especially for long reaches.+This is EXPENSIVE, if you do it too many times, it WILL get slow. Especially for long reaches. If you **need** to do many raycasts in a single frame, [[https://stackoverflow.com/q/777997/3124150|this]] link might be helpful.
tutorial/pixel_raycast.txt · Last modified: 2023/11/30 22:33 by famrofexl