tutorial:pixel_raycast
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:pixel_raycast [2020/08/15 13:17] – [Performance considerations] emmanuelmess | tutorial:pixel_raycast [2023/11/30 22:33] (current) – [For arbitrary reach] Naming fix famrofexl | ||
|---|---|---|---|
| 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. | + | 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 replace <code java> HitResult hit = client.crosshairTarget;</ |
| + | |||
| + | <code java [enable_line_numbers=" | ||
| + | double maxReach = 1000; //The farthest target the cameraEntity can detect | ||
| + | float tickDelta = 1.0F; //Used for tracking animation progress; no tracking is 1.0F | ||
| + | boolean includeFluids = true; //Whether to detect fluids as blocks | ||
| + | |||
| + | HitResult hit = client.cameraEntity.raycast(maxReach, | ||
| + | </ | ||
| ===== General case: Arbitrary pixel ===== | ===== General case: Arbitrary pixel ===== | ||
| Line 79: | Line 87: | ||
| Then you have this reimplementation of the code at GameRenderer# | Then you have this reimplementation of the code at GameRenderer# | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | private static HitResult | + | private static HitResult |
| Entity entity = client.getCameraEntity(); | Entity entity = client.getCameraEntity(); | ||
| if (entity == null || client.world == null) { | if (entity == null || client.world == null) { | ||
| Line 86: | Line 94: | ||
| double reachDistance = client.interactionManager.getReachDistance();// | double reachDistance = client.interactionManager.getReachDistance();// | ||
| - | HitResult target = rayTrace(entity, reachDistance, | + | HitResult target = raycast(entity, reachDistance, |
| boolean tooFar = false; | boolean tooFar = false; | ||
| double extendedReach = reachDistance; | double extendedReach = reachDistance; | ||
| Line 110: | Line 118: | ||
| .stretch(entity.getRotationVec(1.0F).multiply(reachDistance)) | .stretch(entity.getRotationVec(1.0F).multiply(reachDistance)) | ||
| .expand(1.0D, | .expand(1.0D, | ||
| - | EntityHitResult entityHitResult = ProjectileUtil.rayTrace( | + | EntityHitResult entityHitResult = ProjectileUtil.raycast( |
| entity, | entity, | ||
| cameraPos, | cameraPos, | ||
| Line 138: | Line 146: | ||
| } | } | ||
| - | private static HitResult | + | private static HitResult |
| Entity entity, | Entity entity, | ||
| double maxDistance, | double maxDistance, | ||
| Line 146: | Line 154: | ||
| ) { | ) { | ||
| 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, | ||
| - | | + | |
| - | includeFluids ? RayTraceContext.FluidHandling.ANY : RayTraceContext.FluidHandling.NONE, | + | includeFluids ? RaycastContext.FluidHandling.ANY : RaycastContext.FluidHandling.NONE, |
| entity | entity | ||
| )); | )); | ||
| Line 156: | Line 164: | ||
| </ | </ | ||
| - | 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=" | <code java [enable_line_numbers=" | ||
| Vec3d direction = map( | Vec3d direction = map( | ||
| Line 168: | Line 176: | ||
| height | height | ||
| ); | ); | ||
| - | HitResult hit = rayTraceInDirection(client, tickDelta, direction); | + | HitResult hit = raycastInDirection(client, tickDelta, direction); |
| switch(hit.getType()) { | switch(hit.getType()) { | ||
| Line 187: | Line 195: | ||
| </ | </ | ||
| - | 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. If you *need* to do many raycasts in a single frame, [[https:// | + | 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:// |
tutorial/pixel_raycast.1597497466.txt.gz · Last modified: 2020/08/15 13:17 by emmanuelmess