convert 2D screen coordinates to 3D space coordina

2019-08-12 16:18发布

问题:

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.

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

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.

回答1:

For picking purposes, there are better ways than doing a reverse projection. See this answer: https://stackoverflow.com/a/1114023/252687



回答2:

In general, you will have to scale your finger-movement-distance to use it in a far-away plane (z unit away).

i.e, it l unit is the amount of finger movement and if you want to find the effect z unit away, the length l' = l/z But, please check the effect and adjust the l' (double/halve etc) to get the desired effect.



回答3:

Found the answer at: Wikipedia

It has the following formula:

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

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.