• News

• Industries

• Learning & Support

• Community

• Marketplace

# Converting 2D to 3D Space

I’m trying to figure out what objects/position is under a location on my screen. This location is not related to the position/direction of my character, and this location is not my cursor/finger. I’m looking to use “Single Line Trace For Objects”, but I cannot figure out how to get my start/end points.

I have seen code that uses something called “deproject”, but I cannot find the blueprint version.

This is what I currently have (for moving to the object under that point)

I feel like I am just not finding something that must exist.

Can someone point me in the correct direction?

You have the right idea. You need to supply locations for the “Start” and “end” of the trace. You have them both as 0. For start you should probably have the location of the player in the world. Then you need to create a point in space in front of the character to set the end of the trace. You should use “Get Rotation Xvector” get get a vector of the forward rotation, multiply it by some scalar to set the trace length, and then add that to the player world location.

I am not sure what you mean about converting from 2D to 3D though.

In this case the line I am tracing does not relate to the position/direction of the character in the world, so I sadly cannot use it to figure out my start/end points.

I would have tried to use the camera’s eye as the start, but again my location is not related to that point. There is a point on my screen (not centered) that I need to know what object is under, so I want to convert the 2D location on screen to a 3D position and direction, and then use that to figure out my start/end point. I was going to look at using the Far-Near for the scalar to apply to the vector.

Ah, in that case you should be able to just get the location of your playercamera and subtract a few Z units depending on how far down you want the trace, and use the same forward projecting trick I mentioned.

You can enable debug drawing to render a line between the points and just adjust your Z offset until it comes from where you want. I’m not sure about a way to convert between 2D screen space and 3d space. I think I could figure it out in the material editor but not the blueprint editor.

The location on screen that I need to de-project is not locked. It moves around the screen based on player input. Also, changing the Z of the camera would not directly relate to how this cursor would move on the screen (if the camera were tilted for example). This is why I am unable to use the cameras position (eye or target) to build the 3D line.

For you start and end points, you can use Vector Variables. If you make the variable public, by clicking the eyeball beside the variable name, you can then turn on the Show 3D Widget checkbox in the variables Details panel. With that on, you can click on them in the 3d viewport, and drag them around to you desired locations.

Ah, you could create those editable vectors within your camera or pawn BP, and since they would be specified in local space, you should be able to write their direction using screenposition style coordinates.

Then you could use transform to transform their positions from localspace to worldspace for the trace locations.

Thanks.

After lots of digging and fighting, I finally went ahead and added code to my project to expose the De-project function through my HUD class, enabling me to convert any 2D location in my HUD to a 3D line.

As a final note, I still feel that the ability to convert a 2D point to a 3D line should be exposed to blueprints.

Yes, Deproject should be exposed in UE4, it just hasn’t been yet. It was in UE3. I think I have already added a TTP for it a while back, I’ll double check.

I see there is already a ConvertMouseLocationToWorldSpace function in PlayerController, but not quite what you need. I’ll go ahead and add the following right now (they should be in 4.2):