Touch Input location distorted by perspective camera

I have a top down game that uses a camera with 3d perspective. I’ve been working on my touch input controls and was going along pretty well until I hit this road block.

I need to calculate the distance of the player’s touch from the player’s pawn and as I was massaging the ranges etc I noticed that the distances that appear to be similar in value from the player’s pawn are actually much different. I realize now that that’s because of the perspective of the camera and the trace offsetting more or less based on the distance from the camera, further the touch is from the center of the camera the greater the distortion.

I’m guessing I’ll have to some how compensate for the perspective but I’m not quite sure how to go about that.
I’m really hoping I can keep the perspective camera system, but if I can’t solve this I guess I’ll have to move to orthographic which I’d rather not do.

Thanks

Hello, Sarchasm,

If I understand right, I had a similar problem to yours: Getting 3D coordinates from screen space but at an arbitrary distance from the camera and represented as a plane instead of a distorted segment of a sphere, ie correcting the camera perspective. You might think of it as the inverse problem to yours. After a few misguided attempts I think I found an answer.

If you had a plane that intersected the position of your player pawn and was perpendicular to the forward vector to the camera, you would like to get the 3D coordinates corresponding to the perceived touch position, but on that plane, correct? Then you would have the distance from your pawn to the point of that plane where you appeared to touch.

If you try Deproject Screen to World you will see that it gives you a range of 3D coordinates that represent a plane at 10 unreal units ahead of the camera. And you also get a World Direction vector, that you can multiply by a distance and add to the world position, to get a 3D coordinate that corresponds to the 2D point but a certain distance ahead. The problem is that if you trace that and see it in one of the ortographic cameras you will notice that you are no longer getting a plane, but a curve or a sphere, varying on depth depending on where you are in 2D relative to the center of the screen. That is because the World Direction is calculated from the position of the camera to the position of the 2D projected point, and the distance is fixed and not compensating that.

What I found is you can intersect that trace with a Line Plane Intersection (the plane easily represented as an origin & normal). The Plane Origin would be your pawn position, and the plane normal would be the camera’s forward vector multiplied by -1.

The distance by which you want to multiply the World Direction will need to be larger than the distance from the pawn to the camera. You can get that vector length and you multiply it by two, so that your line will always be able to reach the plane.

Then, on the Line Plane Intersection you can use the camera’s position as the line start and the deprojected coordinate as the line end. You will now get an intersection point corresponding and limited to that plane where your pawn is located. You can test that by tracing from the camera to the intersection point. You will notice that you now get a plane corresponding to the visible pixels of the screen but at an arbitrary distance, instead of a sphere.

Subtract the intersection point and the pawn’s position, and the length of the resulting vector is the distance you want.

Here are my blueprints:

Here, assume that the var Distance from Camera is the distance from your pawn to the camera, on the axis corresponding to the forward vector of the camera.

This is the result of trace of all the possible coordinates viewed trough an orthographic camera. You will notice that i am now getting a nice plane with no camera distortion.

Thank you for the detailed response. I bit the bullet after I posted this and begrudgingly turned my camera into an orthographic camera and it pretty much solved all my issues. But armed with this new knowledge I might take a stab at it again and switch back to 3d, we’ll see. I was desperately trying to deproject my camera but I hadn’t discovered any information as detailed as your post.

I’m actually wondering if some of these issues might still linger because I’ve been having a lengthy chat with an Unreal engine programmer on why my TouchInput node’s location vector seems to be radically off from where its suppose to be. Maybe putting these two together will reveal some solution. Going to investigate after work.

Glad to be of help! I hope you can manage to turn your game the way you originally intended.

After reading the other thread, I am quite sure that by using the location of the input touch or some other method of getting the touch coordinate but in 2D screen space, and combining that with this method, you will get what you want.

I will take the opportunity to note that my second image has a mistake in the blueprint. I should be substracting the intersection point and the plane origin, not using the deproject to world coordinate. But you get the idea.

Hey, Sarchasm! If you try the method and it works, please remember to mark the answer as accepted so other people can find it easier! I am sure someone else will find this information useful as well, either as a solution or a starting point.

Thank you.