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

查看:221
本文介绍了在C ++中将2D屏幕坐标转换为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特定距离(在我的情况下为100距,因为这是纹理的距离)的3D坐标.

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).

有些取消项目" 函数在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.

推荐答案

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

Found the answer at: Wikipedia

它具有以下公式:

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

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

其中 Bx是屏幕x坐标
轴是模型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.

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

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