Skip to content

Commit

Permalink
raycast from simulated player position
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 30, 2025
1 parent bddaea1 commit d8b2752
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/zenith/command/impl/RaycastCommand.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zenith.command.impl;

import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.zenith.Proxy;
import com.zenith.command.Command;
import com.zenith.command.CommandUsage;
import com.zenith.command.brigadier.CommandCategory;
Expand All @@ -25,6 +26,7 @@ public CommandUsage commandUsage() {
public LiteralArgumentBuilder<CommandContext> register() {
return command("raycast").executes(c -> {
var sim = MODULE.get(PlayerSimulation.class);
if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true);
var result = RaycastHelper.playerBlockOrEntityRaycast(sim.getBlockReachDistance(), sim.getEntityInteractDistance());
var embed = c.getSource().getEmbed();
embed.title("Raycast Result")
Expand All @@ -43,6 +45,7 @@ public LiteralArgumentBuilder<CommandContext> register() {
}})
.then(literal("e").executes(c -> {
var sim = MODULE.get(PlayerSimulation.class);
if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true);
var result = RaycastHelper.playerEntityRaycast(sim.getEntityInteractDistance());
c.getSource().getEmbed()
.title("Raycast Result")
Expand All @@ -53,6 +56,7 @@ public LiteralArgumentBuilder<CommandContext> register() {
}))
.then(literal("b").executes(c -> {
var sim = MODULE.get(PlayerSimulation.class);
if (Proxy.getInstance().hasActivePlayer()) sim.syncFromCache(true);
var result = RaycastHelper.playerBlockRaycast(sim.getBlockReachDistance(), false);
c.getSource().getEmbed()
.title("Raycast Result")
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/com/zenith/feature/world/raycast/RaycastHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.zenith.feature.world.World;
import com.zenith.mc.block.*;
import com.zenith.mc.entity.EntityData;
import com.zenith.module.impl.PlayerSimulation;
import com.zenith.util.math.MathHelper;
import org.cloudburstmc.math.vector.Vector3d;

Expand All @@ -15,7 +16,8 @@
public class RaycastHelper {

public static BlockRaycastResult playerBlockRaycast(double maxDistance, boolean includeFluids) {
return blockRaycastFromPos(CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), maxDistance, includeFluids);
var sim = MODULE.get(PlayerSimulation.class);
return blockRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), maxDistance, includeFluids);
}

public static BlockRaycastResult blockRaycastFromPos(double x, double y, double z, double yaw, double pitch, double maxDistance, boolean includeFluids) {
Expand Down Expand Up @@ -83,7 +85,8 @@ public static BlockRaycastResult blockRaycast(double x1, double y1, double z1, /
}

public static EntityRaycastResult playerEntityRaycast(double maxDistance) {
return entityRaycastFromPos(CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(), CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(), maxDistance);
var sim = MODULE.get(PlayerSimulation.class);
return entityRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), maxDistance);
}

public static EntityRaycastResult entityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double maxDistance) {
Expand Down Expand Up @@ -122,6 +125,35 @@ private static EntityRaycastResult entityRaycast(final double x1, final double y
return resultRaycast;
}

// ignoring all intersections with other blocks and entities
public static EntityRaycastResult playerEyeRaycastThroughToTarget(Entity target, double entityReachDistance) {
var sim = MODULE.get(PlayerSimulation.class);
return playerEyeRaycastThroughToTarget(target, sim.getYaw(), sim.getPitch(), entityReachDistance);
}

public static EntityRaycastResult playerEyeRaycastThroughToTarget(Entity target, float yaw, float pitch, double entityReachDistance) {
var sim = MODULE.get(PlayerSimulation.class);
final double x1 = sim.getX();
final double y1 = sim.getEyeY();
final double z1 = sim.getZ();
var rayEndPos = MathHelper.calculateRayEndPos(x1, y1, z1, yaw, pitch, entityReachDistance);
final double startX = MathHelper.lerp(-1.0E-7, x1, rayEndPos.getX());
final double startY = MathHelper.lerp(-1.0E-7, y1, rayEndPos.getY());
final double startZ = MathHelper.lerp(-1.0E-7, z1, rayEndPos.getZ());
final double endX = MathHelper.lerp(-1.0E-7, rayEndPos.getX(), x1);
final double endY = MathHelper.lerp(-1.0E-7, rayEndPos.getY(), y1);
final double endZ = MathHelper.lerp(-1.0E-7, rayEndPos.getZ(), z1);
EntityRaycastResult resultRaycast = EntityRaycastResult.miss();
EntityData data = ENTITY_DATA.getEntityData(target.getEntityType());
if (data == null) return resultRaycast;
LocalizedCollisionBox cb = entityCollisionBox(target, data);
RayIntersection intersection = cb.rayIntersection(startX, startY, startZ, endX, endY, endZ);
if (intersection != null) {
resultRaycast = new EntityRaycastResult(true, intersection, target);
}
return resultRaycast;
}

private static LocalizedCollisionBox entityCollisionBox(final Entity entity, final EntityData data) {
double width = data.width();
double height = data.height();
Expand Down Expand Up @@ -181,11 +213,8 @@ private static BlockRaycastResult checkBlockRaycast(
}

public static BlockOrEntityRaycastResult playerBlockOrEntityRaycast(double blockReachDistance, double entityReachDistance) {
return blockOrEntityRaycastFromPos(
CACHE.getPlayerCache().getX(), CACHE.getPlayerCache().getEyeY(), CACHE.getPlayerCache().getZ(),
CACHE.getPlayerCache().getYaw(), CACHE.getPlayerCache().getPitch(),
blockReachDistance, entityReachDistance
);
var sim = MODULE.get(PlayerSimulation.class);
return blockOrEntityRaycastFromPos(sim.getX(), sim.getEyeY(), sim.getZ(), sim.getYaw(), sim.getPitch(), blockReachDistance, entityReachDistance);
}

public static BlockOrEntityRaycastResult blockOrEntityRaycastFromPos(final double x, final double y, final double z, final float yaw, final float pitch, final double blockReachDistance, final double entityReachDistance) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/zenith/module/impl/PlayerSimulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ public void handleExplosion(final ClientboundExplodePacket packet) {
this.velocity.add(packet.getPushX(), packet.getPushY(), packet.getPushZ());
}

private void syncFromCache(boolean full) {
public void syncFromCache(boolean full) {
this.x = this.lastX = CACHE.getPlayerCache().getX();
this.y = this.lastY = CACHE.getPlayerCache().getY();
this.z = this.lastZ = CACHE.getPlayerCache().getZ();
Expand Down

0 comments on commit d8b2752

Please sign in to comment.