将 2D 屏幕坐标转换为 C++ 中的 3D 空间坐标? [英] convert 2D screen coordinates to 3D space coordinates in C++?

查看:39
本文介绍了将 2D 屏幕坐标转换为 C++ 中的 3D 空间坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 OpenGLES 的包装器编写 2D 游戏.有一个相机瞄准一堆纹理,这是游戏的精灵.用户应该能够通过在屏幕上移动手指来移动视图.问题是,相机距离纹理大约 100 个单位,因此当手指在屏幕上滑动以平移相机时,由于视差效应,精灵的移动速度比手指快.

I'm writing a 2D game using a wrapper of OpenGLES. There is a camera aiming at a bunch of textures, which are the sprites for the game. The user should be able to move the view around by moving their fingers around on the screen. The problem is, the camera is about 100 units away from the textures, so when the finger is slid across the screen to pan the camera, the sprites move faster than the finger due to parallax effect.

所以基本上,我需要将 2D 屏幕坐标转换为特定 z 距离处的 3D 坐标(在我的情况下为 100,因为这是纹理的距离).

So basically, I need to convert 2D screen coordinates, to 3D coordinates at a specific z distance away (in my case 100 away because that's how far away the textures are).

有一些Unproject" 函数在 C# 中,但我使用的是 C++ 所以我需要这个函数背后的数学.我对 3D 东西非常陌生,而且我的数学很差,所以如果你能像向 10 岁的孩子解释一样解释,那将不胜感激.

There are some "Unproject" functions in C#, but I'm using C++ so I need the math behind this function. I'm extremely new to 3D stuff and I'm very bad at math so if you can explain like you are explaining to a 10 year old that would be much appreciated.

如果我能做到这一点,我就可以以这样的速度平移相机,使远处的精灵看起来像在用用户的手指平移.

If I can do this, I can pan the camera at such a speed so it looks like the distant sprites are panning with the users finger.

推荐答案

在以下位置找到答案:维基百科

它有以下公式:

要确定哪个屏幕 x 坐标对应于 Ax,Az 处的点,将点坐标乘以:

To determine which screen x-coordinate corresponds to a point at Ax,Az multiply the point coordinates by:

哪里Bx 是屏幕 x 坐标
Ax 是模型的 x 坐标
Bz 是焦距——从相机中心到图像平面的轴向距离
Az 是被摄体距离.

where Bx is the screen x coordinate
Ax is the model x coordinate
Bz is the focal length—the axial distance from the camera center to the image plane
Az is the subject distance.

这篇关于将 2D 屏幕坐标转换为 C++ 中的 3D 空间坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆