Download

C++ equivalent of "Convert Mouse Location To World Space"

Hello All,

I asked this question in the Unreal AnswerHub : C++ equivalent of "Convert Mouse Location To World Space" - UE4 AnswerHub

I am stumped on how to “Convert Mouse Location To World Space” in C++. Can you folks point me to the right direction?

Thanks.

I dont know if there is a function to do it in Unreal, but basicly its…

First get the Vector of Mouse position, then you have to set the vector to range -1/1 (cube coords) , its FVector cubeCoord ((mousePos.x/width2) - 1, -((mousePos.y/height2) -1), -0.5); where width and height are the size of the viewport of the camera. Then use that cubeCoord the the camera to get world position like this:

First get the inverse Matrix of the camera JUST LIKE THIS, DONT CHANGE THE ORDER OR IT WONT WORKS, FMatrix inv = (CameraProjectionMatrix * CameraViewMatrix) * Inverse, here you have to get the projection matrix of the camera times view matrix of the camera and then get the inverse of the resulted matrix. Last thing create another FVector multiplying the inv time cubeCoord ( FVector(inv * cubeCoord)) and that vector should be the world position of the mouse location

I made this in Ogre3d, but the idea its the same maybe there is an easy way to do it in Unreal but if its not, you can do it with this. I made this long time ago and i dont know if its entirely correct hehe.

EDIT: maybe the Y coord of the cubeCoord its wrong, depending if the position of height 0 its up or down and its the same of Ogre

**[FONT=Comic Sans MS]Welcome to the forums Benjamin-WSS !!
**
Rama

Just use the following function of the PlayerController:

void DeprojectMousePositionToWorld(FVector & WorldLocation, FVector & WorldDirection) const;

:smiley:

Cheers,
Moss

@Rama : Thanks !!

@All :
Moss has already answered my question and IT WORKS !! WOOHOO !!

Thanks, y’all.

ya welcome :smiley: