在 3D 引擎中组合俯仰和偏航旋转

发布于 01-17 05:32 字数 1189 浏览 3 评论 0原文

我正在制作一个 3D 引擎,并且除了俯仰和偏航旋转之外的所有内容都已解决。当只实现一个时,这些工作完美,但当我尝试将它们结合起来时,有些东西就消失了。我的第一个线索是当我直视下方的一个物体并尝试转动偏航轴时。结果看起来与现实生活中的情况完全不同。这是我用于旋转的代码:

    global xAngle
    global yAngle
    global deltaX
    global deltaY
    global deltaZ
    
    xRel = x-camX
    yRel = y-camY
    zRel = z-camZ

    global pressed

    if xRel == 0:
        xAngle = 1.5708
    else:
        xAngle = math.atan(zRel/xRel)
    if xAngle < 0:
        xAngle+=3.14159
    
    if zRel == 0:
        yAngle = 1.5708
    else:
        yAngle = math.atan(yRel/zRel)


    #x and z are changed according the the yaw angle
    newXRel = math.sqrt(xRel*xRel+zRel*zRel) * math.cos(xAngle-math.radians(yaw)) 
    newZRel = math.sqrt(xRel*xRel+zRel*zRel) * math.sin(xAngle-math.radians(yaw))

    xRel = newXRel
    zRel = newZRel
    
    #y and z are changed according to the pitch angle        
    newYRel = math.sqrt(yRel*yRel+zRel*zRel) * math.sin(yAngle-math.radians(pitch)) 
    newZRel = math.sqrt(yRel*yRel+zRel*zRel) * math.cos(yAngle-math.radians(pitch))
    
    yRel = newYRel
    zRel = newZRel

这是使用 pygame,如果需要,我可以添加完整的程序。

显然,我没有以正确的方式组合旋转。有人可以给我一个关于我哪里出错的提示吗?谢谢。

I am making a 3D engine and have everything worked out except the pitch and yaw rotation. These work perfectly when only one is implemented, but when I try to combine them something is off. My first clue of this was when I looked straight down on an object below me and tried to turn on the yaw axis. The result looked nothing like what it would in real life. Here is the code I'm using for rotation:

    global xAngle
    global yAngle
    global deltaX
    global deltaY
    global deltaZ
    
    xRel = x-camX
    yRel = y-camY
    zRel = z-camZ

    global pressed

    if xRel == 0:
        xAngle = 1.5708
    else:
        xAngle = math.atan(zRel/xRel)
    if xAngle < 0:
        xAngle+=3.14159
    
    if zRel == 0:
        yAngle = 1.5708
    else:
        yAngle = math.atan(yRel/zRel)


    #x and z are changed according the the yaw angle
    newXRel = math.sqrt(xRel*xRel+zRel*zRel) * math.cos(xAngle-math.radians(yaw)) 
    newZRel = math.sqrt(xRel*xRel+zRel*zRel) * math.sin(xAngle-math.radians(yaw))

    xRel = newXRel
    zRel = newZRel
    
    #y and z are changed according to the pitch angle        
    newYRel = math.sqrt(yRel*yRel+zRel*zRel) * math.sin(yAngle-math.radians(pitch)) 
    newZRel = math.sqrt(yRel*yRel+zRel*zRel) * math.cos(yAngle-math.radians(pitch))
    
    yRel = newYRel
    zRel = newZRel

This is using pygame, and I can add the full program if necessary.

Obviously, I am not combining the rotations in the correct manner. Could someone please give me a hint on where I am going wrong? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霞映澄塘2025-01-24 05:32:22

当我第一次学习线性代数时,并不清楚我需要多久使用它来解决问题。您发现了一个使用线性代数变得容易得多的问题,但看起来您还没有学习该主题。您正在尝试应用在 1d 或 2d 中运行良好的工具,但在 3d 中却变得非常棘手。

你的直觉是正确的,一个轮换会影响其他轮换。在一定程度上,你可以通过补偿来解决这个问题。如果你继续沿着这条路走下去,在某些物体姿势中,你的三角函数会“爆炸”(在数值上变得不稳定)。

在 3D 空间中对物体的姿态进行建模时,有两种常用的方法可以克服这些挑战:

  1. 四元数< /a>
  2. 变换矩阵

四元数很简洁。它们仅用 4 个数字表示物体的旋转。但他们有点难以理解。但它们在视频游戏和航天器方向中很常见。

3D 变换矩阵是 3x3 矩阵,可以表示各种坐标变换(不仅包括旋转,还包括缩放、拉伸和剪切。)它们比四元数更容易理解,但它们需要 9 个数字,并且您有要小心一点,不要意外引入其他影响。

在这些领域有很多东西需要学习——超出了我在 SO 响应中所能容纳的范围。这两个主题应该可以帮助您找到合适的文档来阅读和 YouTube 视频来观看。

编辑:我已经添加了有关上述特定主题的维基百科链接。鉴于您从哪里开始,我建议您首先查看 关于旋转的维基百科文章部分二维矩阵 一旦您了解了这些旋转矩阵的工作原理,就可以继续推广到 3D 旋转矩阵

When I first learned linear algebra, it wasn't clear how often I would need to use it to solve problems. You've found a problem that gets much easier with linear algebra, but it looks like you haven't learned the subject just yet. You're trying to apply tools that work well in 1d or 2d, but get really tricky in 3d.

Your intuition is right that one rotation affects the others. And you can work around that problem by compensating for it -- up to a point. If you continue down this path, there will be certain object poses where your trig functions "blow up" (become numerically unstable).

When modeling the pose of an object in 3D space, there are two commonly used approaches that overcome these challenges:

  1. Quaternions
  2. Transformation Matrices

Quaternions are concise. They represent the rotation of an object with only 4 numbers. But they're a little hard to understand. But they're common in video games and spacecraft orientations.

3D Transformation matrices are 3x3 matrices that can represent a diverse range of coordinate transformations (including not only rotation, but also scale, stretch, and shear.) They're a little easier to understand than quaternions but they require 9 numbers, and you have to be a little careful not to accidentally introduce other effects.

There's a lot to learn in these areas -- more than I can fit into a SO response. These two topics should help you find the right documentation to read and YouTube videos to watch.

Edit: I've added links to Wikipedia on the specific topics above. Given where you're starting from, I would suggest you start by looking at the Wikipedia article section on rotation matrices in two dimensions Once you can follow how those rotation matrices work, then move on to generalizing to 3D rotation matrices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文