[英]Unable to properly calculate a normal/direction vector from pitch and yaw
我的俯仰和偏航都搞砸了。 我有墊的俯仰和偏航,但光束的俯仰和偏航搞砸了。 我如何計算墊的俯仰和偏航的法向量? 我從 stackoverflow 中嘗試了一堆垃圾數學,但到目前為止它們都失敗了。
我首先嘗試的是將 90 添加到打擊墊的音高,但偏航保持混亂:
這就是當我習慣於使用墊子的俯仰和偏航來計算方向向量時發生的情況:
我接下來嘗試做的是將光束的俯仰和偏航與墊的俯仰和偏航分開,並分別計算它們。 這大部分有效,但偏航仍然完全搞砸了。
我用來從橫梁的偏航和俯仰角計算方向向量的是一個實用的 minecraft 用來為怪物這樣做的:
public static Vec3d getVectorForRotation3d(float pitch, float yaw) {
float f = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
float f1 = MathHelper.sin(-yaw * 0.017453292F - (float) Math.PI);
float f2 = -MathHelper.cos(-pitch * 0.017453292F);
float f3 = MathHelper.sin(-pitch * 0.017453292F);
return new Vec3d((double) (f1 * f2), (double) f3, (double) (f * f2));
}
但這顯然失敗了,所以最后,我使用墊的音高嘗試了以下操作:
double pitch = ((te.getPadPitch() + 90) * Math.PI) / 180;
double yaw = ((te.getPadYaw() + 90) * Math.PI) / 180;
double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vec3d lookvec = new Vec3d(x, y, z);
俯仰和偏航都以玩家頭部旋轉的方式計算。 當我在打擊墊本身上使用它們時,打擊墊的俯仰和偏航是 100% 正確的,但當我在橫梁上使用它們時會搞砸。 這些都使用 GL 函數
雖然俯仰和偏航不尊重玩家頭部的方向系統,但它與打擊墊一起工作。
例如,這是這張圖片中鏡子的偏航,它非常適合它的當前值 墊子是這樣旋轉的:
GlStateManager.rotate(te.getPadYaw(), 0, 0, 1);
GlStateManager.rotate(te.getPadPitch(), 1, 0, 0);
這條線是這樣繪制的:
public static void drawConnection(BlockPos pos1, BlockPos pos2, Color color) {
GlStateManager.pushMatrix();
GL11.glLineWidth(1);
GlStateManager.disableTexture2D();
GlStateManager.color(color.getRed(), color.getGreen(), color.getBlue(), 0.7f);
GlStateManager.translate(0.5, 0.7, 0.5);
VertexBuffer vb = Tessellator.getInstance().getBuffer();
vb.begin(GL11.GL_LINES, DefaultVertexFormats.POSITION);
vb.pos(pos2.getX() - pos1.getX(), pos2.getY() - pos1.getY(), pos2.getZ() - pos1.getZ()).endVertex();
vb.pos(0, 0, 0).endVertex();
Tessellator.getInstance().draw();
GlStateManager.enableTexture2D();
GlStateManager.popMatrix();
}
我得到 pos1 和 pos2 像這樣[當前,最近]:
double pitch = ((te.getPadPitch() + 90) * Math.PI) / 180;
double yaw = ((te.getPadYaw() + 90) * Math.PI) / 180;
double x = Math.sin(pitch) * Math.cos(yaw);
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vec3d lookvec = new Vec3d(x, y, z);
Vec3d centervec = new Vec3d(te.getPos().getX() + 0.5, te.getPos().getY() + 0.8, te.getPos().getZ() + 0.5);
Vec3d startvec = centervec.add(lookvec);
Vec3d end = startvec.add(new Vec3d(lookvec.xCoord * 30, lookvec.yCoord * 30, lookvec.zCoord * 30));
RayTraceResult result = te.getWorld().rayTraceBlocks(startvec, end, true, false, true);
Utils.drawConnection(te.getPos(), result.getBlockPos(), Color.RED);
我如何根據墊的俯仰和偏航正確計算法向量或垂直於墊的向量?
我在這一點上不知所措,因為我幾乎嘗試了我在谷歌上找到的所有東西,大部分時間都沒有運氣。
編輯:有人告訴我,沒有必要將光束俯仰和偏航與打擊墊的俯仰和偏航分開,我同意了,但否則我無法讓它工作。 為什么梁繪圖數學與墊數學不同?
我在評論中說這個很難,所以我把它放在一個答案中。 當我看到:
double pitch = ((te.getPadPitch() + 90) * Math.PI) / 180;
double yaw = ((te.getPadYaw() + 90) * Math.PI) / 180;
double x = Math.sin(pitch) * Math.cos(yaw); \\Line 3 is HERE <---
double y = Math.sin(pitch) * Math.sin(yaw);
double z = Math.cos(pitch);
Vec3d lookvec = new Vec3d(x, y, z);
你為什么使用sin(pitch)
和cos(yaw)
? cos()
計算你的x,為什么你使用sin(pitch)
? 它對y
和z
都有意義,但我認為你的數學有點偏,或者只是語法錯誤,或者我完全錯了(這可能是最有可能的)。
我不確定這是否是你的問題,但對我來說,當我處理它們時,mc 返回了一些混亂的偏航位置,這就是我解決問題的方法
if (Yaw < -180.0) Yaw += 360;
else if (Yaw > 180) Yaw -= 360;
Minecraft 偏航旋轉有點奇怪。 它們不會被限制在 0 到 360 度之間。例如,如果您將頭部向右旋轉很長時間,最終可能會旋轉超過 1000 度。 當夾在 0 到 360 度之間時,1000 度變為 280 度。 如果數字是負數,那么你應該從 360 中減去這個數字。 這是一個在 0 和 360 之間夾角的例子:
public static float clamp(float angle) {
angle = angle % 360; // Gets the remainder when dividing angle by 360
if(angle < 0) {
angle = 360 - angle;
}
return angle;
}
如果你用這個設置你的角度,事情總是在 0 到 360 之間。此外,這是一個直接來自 MCP 源代碼的片段,其中旋轉偏航/俯仰被轉換為旋轉向量:
/**
* Creates a Vec3 using the pitch and yaw of the entities rotation.
*/
protected final Vec3 getVectorForRotation(float pitch, float yaw)
{
float f = MathHelper.cos(-yaw * 0.017453292F - (float)Math.PI);
float f1 = MathHelper.sin(-yaw * 0.017453292F - (float)Math.PI);
float f2 = -MathHelper.cos(-pitch * 0.017453292F);
float f3 = MathHelper.sin(-pitch * 0.017453292F);
return new Vec3(f1 * f2, f3, f * f2);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.