在3D世界中尝试发射子弹。经历了x和z轴上奇怪的偏移。

16

好的,所以我正在使用Bukkit API(Minecraft),在这方面应该不是太大的问题,因为它被最小化地使用。所以一个位置包含世界、x、y、z、偏航和俯仰角。这可能会派上用场,但我怀疑。

我的问题是,我使用Shot类(下面)进行射击时,当距离约3个区块时,出现了+/-5的差异,并且HitBox实例化大约在5个区块外(这可能是问题(移动/旋转方法))。我已经试图在纸上计算出来,并使用了半个笔记本,但我还没有能够找到解决方法。我需要的是一个了解三角学和Java的人,他们可以帮忙。

其他有用的信息:

  • + z是0度偏航角,- x是90度,- z是180度,+ x是270度。
  • 变量似乎偶尔有误,并且在某些情况下它们起作用并构成命中。
  • Shot构造函数中的Location(from)参数是玩家在世界中的位置,因此from不是(0,0,0)。
  • ShotData不应影响任何值,因为在我的情况下,风速和风向为0(如果涉及此方面的数学问题,请随时告诉我,哈哈)
  • Pitch似乎没问题,尽管+y为-90,-y为90(很奇怪,对吧?)

所以我的问题是...问题在哪里,我该如何解决?我很抱歉这是一个非常普遍的问题,但这是真正必要的时候。 我试图删除所有不必要的代码,但如果需要,您可以删除更多内容。此外,如果您想查看可能在此引用的其他内容,我可以为您提供。

Shot.java:

private final Location from;
private ShotData data;

public Shot(Location from, ShotData data) {
    this.from = from;
    this.data = data;
}

// TODO - Checking for obstacles
public List<Hit> shoot(List<HitBox> hitBoxes) {
    List<Hit> hits = new ArrayList<Hit>();
    for (HitBox hitBox : hitBoxes) {
        hitBox.update();
        float fromYaw = from.getYaw() % 360;
        float fromPitch = from.getPitch() % 360;
        // making sure the center location is within range
        if (hitBox.getCenter().distanceSquared(from) > Math.pow(data.getDistanceToTravel(), 2)) {
            continue;
        }
        /* TODO Only allow hits on parts of the rectangle that are within range,
         * not just the whole thing if the center is within range. */
        // accounting for wind speed/direction
        float windCompassDirection = data.getWindCompassDirection(from.getWorld());
        float windSpeed = data.getWindSpeedMPH(from.getWorld());
        fromYaw += (windCompassDirection > fromYaw ? 1 : windCompassDirection < fromYaw ? -1 : 0) * windSpeed;
        fromYaw %= 360;
        int[] orderClockwise = new int[] {0, 1, 4, 3};
        Location thisSideCorner = hitBox.getCorner(0);
        Location oppositeSideCorner = hitBox.getCorner(0);
        for (int i = 0; i < orderClockwise.length; i++) {
            int num = orderClockwise[i];
            Location corner = hitBox.getCorner(num);
            Location clockwise = hitBox.getCorner(orderClockwise[(i + 1) % 3]);
            if ((Math.atan2(from.getZ() - corner.getZ(), from.getX() - corner.getX()) * 180 / Math.PI) > 0 && corner.distanceSquared(from) < clockwise.distanceSquared(from)) {
                thisSideCorner = corner;
                int exitCornerClockwiseAmount = (Math.atan2(from.getZ() - clockwise.getZ(), from.getX() - clockwise.getX()) * 180 / Math.PI) < 0 ? 2 : 3;
                oppositeSideCorner = hitBox.getCorner((i + exitCornerClockwiseAmount) % 3);
            }
        }
        Location entrance = getProjectileLocation(thisSideCorner, data, hitBox, fromYaw, fromPitch);
        double distance = entrance.distance(from);
        double deltaX = data.getDeltaX(distance, fromYaw);
        double deltaY = data.getDeltaY(distance, fromPitch);
        double deltaZ = data.getDeltaZ(distance, fromYaw);
        entrance.add(deltaX, deltaY, deltaZ);
        Location exit = getProjectileLocation(oppositeSideCorner, data, hitBox, deltaX, deltaY, deltaZ, fromYaw, fromPitch);
        // hit detection and reaction
        boolean hitX = entrance.getX() <= hitBox.getHighestX() && entrance.getX() >= hitBox.getLowestX();
        boolean hitY = entrance.getY() <= hitBox.getHighestY() && entrance.getY() >= hitBox.getLowestY();
        boolean hitZ = entrance.getZ() <= hitBox.getHighestZ() && entrance.getZ() >= hitBox.getLowestZ();
        if (hitX && hitY && hitZ) {
            hits.add(new Hit(from, entrance, exit, hitBox, data));
        }
    }
    return hits;
}

private Location getProjectileLocation(Location thisSideCorner, ShotData data, HitBox hitBox, float fromYaw, float fromPitch) {
    return getProjectileLocation(thisSideCorner, data, hitBox, 0, 0, 0, fromYaw, fromPitch);
}

private Location getProjectileLocation(Location thisSideCorner, ShotData data, HitBox hitBox, double addX, double addY, double addZ, float fromYaw, float fromPitch) {
    double deltaFromToSideCornerX = thisSideCorner.getX() - from.getX();
    double deltaFromToSideCornerY = thisSideCorner.getY() - from.getY();
    double deltaFromToSideCornerZ = thisSideCorner.getZ() - from.getZ();
    double xzDistFromSideCorner = Math.sqrt(Math.pow(deltaFromToSideCornerX, 2) + Math.pow(deltaFromToSideCornerZ, 2));
    double yawToSideCorner = Math.atan2(deltaFromToSideCornerX, deltaFromToSideCornerZ) * 180 / Math.PI;// flipped x and z from normal
    double theta1 = yawToSideCorner - fromYaw;
    double theta2 = yawToSideCorner - theta1;
    double outerAngle = 180 - yawToSideCorner - 90;// previously theta1
    double outerAngleInShotCone = outerAngle + 90 + hitBox.getYawRotation();
    double lastAngleInShotCone = 180 - theta1 - outerAngleInShotCone;
    double xzDistanceFromHit = (xzDistFromSideCorner * Math.sin(Math.toRadians(outerAngleInShotCone))) / Math.sin(Math.toRadians(lastAngleInShotCone));
    double deltaX = xzDistanceFromHit * Math.sin(Math.toRadians(theta2));// leaves out sin 90 because its just equal to 1...
    double deltaZ = xzDistanceFromHit * Math.sin(Math.toRadians(90 - theta2));// leaves out sin 90 because its just equal to 1...
    double xyzDistFromSideCorner = Math.sqrt(Math.pow(xzDistFromSideCorner, 2) + Math.pow(deltaFromToSideCornerY, 2));
    double theta3 = Math.atan2(Math.abs(deltaFromToSideCornerY), xzDistFromSideCorner) * 180 / Math.PI;
    double theta4 = Math.abs(fromPitch) - theta3;
    double theta5 = 90 + theta3;
    double theta6 = 180 - theta4 - theta5;
    double hitDistance = (xyzDistFromSideCorner * Math.sin(Math.toRadians(theta5))) / Math.sin(Math.toRadians(theta6));
    double deltaY = hitDistance * Math.sin(Math.toRadians(Math.abs(fromPitch)));// leaves out sin 90 because its just equal to 1...
    if (deltaFromToSideCornerX < 0 && deltaX > 0) {
        deltaX *= -1;
    }
    if (fromPitch > 0 && deltaY > 0) {// pitch in minecraft is backwards, normally it would be fromPitch < 0
        deltaY *= -1;
    }
    if (deltaFromToSideCornerZ < 0 && deltaZ > 0) {
        deltaZ *= -1;
    }
    Location hit = from.clone().add(deltaX + addX, deltaY + addY, deltaZ + addZ);
    hit.setYaw(fromYaw);
    hit.setPitch(fromPitch);
    return hit;
}

HitBox.java:

private float yawRotation;
private double x, y, z;
private double[][] additions;
private Location center;
private Location[] corners = new Location[8];
private List<DataZone> dataZones = new ArrayList<DataZone>();
private UUID uuid = UUID.randomUUID();

//@formatter:off
/*
 * O = origin
 * X = x-axis
 * Y = y-axis
 * Z = z-axis
 * C = center
 * 
 *    ---------------------
 *   /                   /|
 *  /                   / |
 * Y--------------------  |
 * |                90 |  |     0 yaw
 * |   ^               |  |    /
 * |   |               |  |
 * |   |               |  |  /
 * | HEIGHT    C       |  |
 * |   |               |  |/
 * |   |               |  Z
 * |   v               | /
 * |   <---WIDTH--->   |/<---LENGTH
 * O-------------------X - - - - - - - - - -270 yaw
 */

/**
 * An invisible box in the world that can be hit with a shot.
 * Additionally, {@link DataZone} instances can be added to this, 
 * allowing for different damage and thickness on an area of the box.
 * 
 * @param center The center of the hit box
 * @param length The length (z axis) of the hit box
 * @param width The width (x axis) of the hit box
 * @param height The height (y axis) of the hit box
 * @param yawRotation The rotation around the center of the origin (or any other point)
 */
public HitBox(Location center, double length, double width, double height, float yawRotation) {  
    corners[0] = center.clone().add(-1 * width / 2, -1 * height / 2, -1 * length / 2);
    this.center = center;
    this.x = width;
    this.y = height;
    this.z = length;
    rotate(yawRotation);
}
//@formatter:on
public Location[] getCorners() {
    return corners;
}

public Location getCorner(int corner) {
    return corners[corner];
}

public Location getOrigin() {
    return corners[0];
}

public void update() {};

public boolean isZoneOpen(DataZone zone) {
    for (DataZone placed : dataZones) {
        boolean Xs = overlap_1D(placed.xFrom, placed.xTo, zone.xFrom, zone.xTo);
        boolean Ys = overlap_1D(placed.yFrom, placed.yTo, zone.yFrom, zone.yTo);
        boolean Zs = overlap_1D(placed.zFrom, placed.zTo, zone.zFrom, zone.zTo);
        if (Xs && Ys && Zs) {
            return true;
        }
    }
    return false;
}

public void rotate(float degrees) {
    Location origin = corners[0];
    this.yawRotation = (yawRotation + degrees) % 360;
    additions = new double[][] { {0, 0, 0}, {x, 0, 0}, {0, y, 0}, {0, 0, z}, {x, 0, z}, {x, y, 0}, {x, y, z}, {0, y, z}};
    for (int i = 0; i < 8; i++) {
        double[] addition = additions[i];
        double xPrime = center.getX() + (center.getX() - (origin.getX() + addition[0])) * Math.cos(Math.toRadians(yawRotation)) - (center.getZ() - (origin.getZ() + addition[2])) * Math.sin(Math.toRadians(yawRotation));
        double zPrime = center.getZ() + (center.getX() - (origin.getX() + addition[0])) * Math.sin(Math.toRadians(yawRotation)) + (center.getZ() - (origin.getZ() + addition[2])) * Math.cos(Math.toRadians(yawRotation));
        corners[i] = new Location(center.getWorld(), xPrime, origin.getY() + addition[1], zPrime, yawRotation, 0);
    }
}

public void move(Location center) {
    double deltaX = center.getX() - this.center.getX();
    double deltaY = center.getY() - this.center.getY();
    double deltaZ = center.getZ() - this.center.getZ();
    for (int i = 0; i < 8; i++) {
        corners[i].add(deltaX, deltaY, deltaZ);
    }
    this.center = center;
}

protected void setY(double y) {
    int[] toChange = new int[] {2, 5, 6, 7};
    for (int i : toChange) {
        corners[i].setY(corners[0].getY() + y);
    }
    this.y = y;
}

public double getHighestX() {
    double highestX = Double.MIN_VALUE;
    for (Location location : corners) {
        if (location.getX() > highestX) {
            highestX = location.getX();
        }
    }
    return highestX;
}

public double getHighestY() {
    return corners[0].getY() + y;
}

public double getHighestZ() {
    double highestZ = Double.MIN_VALUE;
    for (Location location : corners) {
        if (location.getZ() > highestZ) {
            highestZ = location.getZ();
        }
    }
    return highestZ;
}

public double getLowestX() {
    double lowestX = Double.MAX_VALUE;
    for (Location location : corners) {
        if (location.getX() < lowestX) {
            lowestX = location.getX();
        }
    }
    return lowestX;
}

public double getLowestY() {
    return corners[0].getY();
}

public double getLowestZ() {
    double lowestZ = Double.MAX_VALUE;
    for (Location location : corners) {
        if (location.getZ() < lowestZ) {
            lowestZ = location.getZ();
        }
    }
    return lowestZ;
}

public float getYawRotation() {
    return yawRotation;
}

1
你是否在调试器中跟踪代码并检查变量值?你可能只需要几分钟就能找到问题。 - Jim Garrison
我已经详细地打印出了Shot类中的所有值,只是发现有时候其中一些值毫无意义。这个问题包括Eclipse调试器(由于MC)的不可访问性,以及错误变量似乎是零星出现的事实。有时候Shot可以正常工作,然而很多次它都不能正常运行。 - JNorr44
2
这可能是我见过的最好的问题之一,做得好。你可以尝试观察在哪些环境下你的射击有效,并尝试找出为什么在其他环境下无效。 - Josh M
你的代码背后的概念有没有简单的描述方式?我从代码中还没有理解它。 - Daniel S.
Location有一个名为getDirection()的方法,我期望它在你的代码中扮演一个核心角色,但我在那里找不到它。这可能会有所帮助。 - Daniel S.
显示剩余5条评论
2个回答

2

或许考虑画一条与子弹行进方向相同的线,这将提供一个视觉指示器来说明正在发生什么,传递相同的计算等信息。

正如其他人所提到的,还应包含大量调试输出。希望一旦您有了视觉提示,就可以看到问题计算何时/在哪里发生。

此外,您应该使用标准数据类型进行计算,例如float或double,而不是两者都用,因为这可能会导致一些奇怪的舍入和计算问题。


我不能使用for/while循环,因为它们对我的目的来说太慢了,但我不断尝试打印输出。浮点数和双精度混合并不是我能控制的,我正在使用MC,它在偏航方面使用浮点数。 - JNorr44
所以请将所有的计算都改为使用浮点数,这样可以提供最佳精度。在此阶段进行单元测试可能是个好主意,这样您可以将其分解成几个部分并检查数据是否按预期输出,此外,请注释掉一些更复杂的计算(如风力等),首先专注于基础知识。 - Matthew Pigram
2
最准确的类型是浮点数 xD。 - fox_news

1
我知道现在回答已经很晚了(事实上已经过去将近5年),但是几周后我开发出了一种解决方案。重新访问StackOverflow后,我决定为任何可能会发现它有用的人提供我的解决方案。
问题所在是在于问题中找到的大量代码正在计算许多值,并且每次计算都会失去精度,导致某种程度的变化(就像我说的,+/-5个块)。
解决方案源代码可以在这里找到: https://github.com/JamesNorris/MCShot 要计算相交(在util / Plane3D.java中找到):
public Vector getIntersect(LineSegment3D segment) {
    Vector u = segment.getEnd().subtract(segment.getStart());
    Vector w = segment.getStart().subtract(point);
    double D = normal.dot(u);
    double N = -normal.dot(w);
    if (Math.abs(D) < .00000001) {
        /* if N == 0, segment lies in the plane */
        return null;
    }
    double sI = N / D;
    if (sI < 0 || sI > 1) {
        return null;
    }
    return segment.getStart().add(u.multiply(sI));
}

正如您所看到的,这需要更少的计算,从而提供更高的精度。此函数获取3D平面的交点。 3D平面是使用以下值构建的:

point - 在平面内找到的某个点
normal - 平面的法向量

我希望有人能发现这个解决方案有用,或者至少可以将其用作计算精度的课程!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接