试图在3D世界中射击子弹。在 x 和 z 中遇到奇数偏移
好吧,所以我使用的是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(如果涉及此的数学有问题,请随时让我知道,哈哈)
- 音高似乎很好,尽管+y是-90,-y是90(奇怪吗?
所以我的问题是...问题出在哪里,我该如何解决?我很抱歉,这是一个如此普遍的问题,而且是一个非常普遍的问题,但这是真正必要的时候之一。我试图删除所有不必要的代码,但如果需要,您可以删除更多代码。另外,如果您想查看此处可能引用的任何其他内容,我可以为您获取。
镜头.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;
}