Java 三维三角;“免费”;照相机

Java 三维三角;“免费”;照相机,java,3d,camera,trigonometry,Java,3d,Camera,Trigonometry,很抱歉,如果以前有人问过这个问题,我确实搜索过,但没有找到答案 我的问题是,我想在所有3个轴上移动,相机的X和Y旋转是相关的 这就是我所做的: private static void fly(int addX, int addY){ //parameters are the direction change relative to the current rotation float angleX = rotation.x + addX; //angle is basically

很抱歉,如果以前有人问过这个问题,我确实搜索过,但没有找到答案

我的问题是,我想在所有3个轴上移动,相机的X和Y旋转是相关的

这就是我所做的:

private static void fly(int addX, int addY){ //parameters are the direction change relative to the current rotation

    float angleX = rotation.x + addX;   //angle is basically the direction, into which we will be moving(when moving forward this is always the same as our actual rotation, therefore addX and addY would be 0, 0)
    float angleY = rotation.y + addY;


    float speed = (moveSpeed * 0.0002f) * delta;
    float hypotenuse = speed; //the length that is SUPPOSED TO BE moved overall on all 3 axes


    /* Y-Z side*/
    //Hypotenuse, Adjacent and Opposite side lengths of a triangle on the Y-Z side
    //The point where the Hypotenuse and the Adjacent meet is where the player currently is.
    //OppYZ is the opposite of this triangle, which is the ammount that should be moved on the Y axis.
    //the Adjacent is not used, don't get confused by it. I just put it there, so it looks nicer.
    float HypYZ = speed;
    float AdjYZ = (float) (HypYZ * Math.cos(Math.toRadians(angleX))); //adjacent is on the Z axis
    float OppYZ = (float) (HypYZ * Math.sin(Math.toRadians(angleX))); //opposite is on the Y axis

    /* X-Z side*/
    //Side lengths of a triangle on the Y-Z side
    //The point where the Hypotenuse and the Adjacent meet is where the player currently is.
    float HypXZ = speed;
    float AdjXZ = (float) (HypXZ * Math.cos(Math.toRadians(angleY)));  //on X
    float OppXZ = (float) (HypXZ * Math.sin(Math.toRadians(angleY)));  //on Z

    position.x += AdjXZ;
    position.y += OppYZ;
    position.z += OppXZ;
}
我只在向前移动(参数:0,90)或向后移动(参数:180,270)时才实现此方法,因为侧向移动时不会在Y轴上发生移动,因为不会在Z轴上旋转。(侧向(扫射)的方法工作得很好,所以我不想补充。)

问题是,当我向上看90度或向下看-90度,然后向前移动时,我应该只在Y轴上移动(垂直),但出于某种原因,我也向前移动(这意味着在Z轴上,因为X轴是扫射)。


我确实意识到这样的运动速度不是恒定的。如果你有一个解决方案,我也很乐意接受。

我认为你的错误在于你没有在水平面和垂直面上完全投射你的距离(你的运动量
假设值

换句话说,无论选择的方向是什么,您现在所做的是在水平面X-Z上移动
抵押
点,即使您已经在垂直方向Y上移动了
抵押
的一部分

您可能想做的是移动您的
抵押点作为总数

所以你必须评估有多少运动发生在水平面上,有多少运动发生在垂直轴上。你的方向给了你答案

现在,我不清楚你的两个角度代表什么。我强烈建议您在这种情况下使用(只使用哈欠和俯仰,因为您似乎不需要滚动-您称之为Z旋转),以简化计算

在此配置中,打呵欠角度显然类似于您对
角度的定义,而俯仰角度是水平面和
假设
向量之间的角度(而不是平面Y-Z中的投影角度)

要澄清的模式:

与:

  • s
    您从初始位置到
    P\u 0
    P\u 1
    抵押
  • a_y
    打哈欠的角度和
    a_p
    俯仰的角度
  • D_x
    D_y
    D_z
    各轴的位移(添加到
    位置
    ,即
    AdjXZ
    OppYZ
    OppXZ
所以如果你看这个表示,你可以看到你的三角形在X-Z中没有斜边,而是它的投影。这个距离的计算非常简单:如果你把自己放在三角形
P_0
P_1
P_1xz
,你可以看到
s_xz=s*cos(a_P)
。这给了你:

float HypXZ = speed * Math.cos(Math.toRadians(angleP))); // s_xz
float AdjXZ = (float) (HypXZ * Math.cos(Math.toRadians(angleY)));  // D_x
float OppXZ = (float) (HypXZ * Math.sin(Math.toRadians(angleY)));  // D_z
至于
D_y
ie
OppYZ
,再次将自己置于三角形
p_0
p_1
p_1xz
,您将获得:

float OppYZ = (float) (speed * Math.sin(Math.toRadians(angleP))); // D_y
现在,如果你所说的角度x实际上是指仰角,就像我想的那样,那么你的代码中的角度p=angleX
HypXZ=AdjYZ

通过此校正,如果
angleX=90
angleX=-90
,则

HypXZ = speed * cos(angleX) = speed * cos(90deg) = speed * 0;
。。。因此,
AdjXZ=0
OppXZ=0
。在水平面上没有移动


注:

要检查您的计算是否正确,您可以验证您是否确实移动了所需移动量的点(
hypotenus
ie
speed
ie
s
)。使用毕达哥拉斯定理:

s² = s_xz² + D_z² // Applied in the triangle P_0 P_1 P_1xz
   = D_x² + D_y² + D_z² // Applied in the triangle P_0 P_1x P_1xz
根据上述位移定义:

D_x² + D_y² + D_z²
   = (s * cos(a_p) * cos(a_y))² + (s * cos(a_p) * sin(a_y))² + (s * sin(a_p))²
   = s² * (cos(a_p)² * cos(a_y)² + cos(a_p)² * sin(a_y)² + sin(a_p)²)
   = s² * (cos(a_p)² * (cos(a_y)² + sin(a_y)²) + sin(a_p)²)
   = s² * (cos(a_p)² * 1 + sin(a_p)²)
   = s² * (cos(a_p)² + sin(a_p)²)
   = s² * 1 // Correct

希望对你有帮助。。。再见

这令人困惑。您是在3D中,但只能在2D中移动/旋转?这难道不意味着你真的只是在2D中吗?不。你可以用这段代码在3D中移动,但其中有一些逻辑错误,导致意外的行为。但是,您只能在两个轴上旋转相机是正确的,但在Z轴上旋转相机是不寻常的。(这就像把头靠在肩膀上一样)。然而,您的位置在所有3个方向上都会发生变化,正如您在最后3行中看到的那样。还是我误解了你的问题?我只能说哇!!非常感谢您提供了这个非常详细的答案,我明白我做错了什么,现在它起作用了!非常感谢您花了这么多时间和精力来写这个漂亮的答案!:你就是那个人,好先生!我的荣幸。祝你今天愉快!:)