How do I go from world space to screen space?

How do I go about converting a point from world space to screen space?

I did a little bit of research and came up with this, but the results are way off.

http://pastebin.com/raw.php?i=6K4U9a7Z

Am I going about this completely the wrong way, or am I missing something small?

UPDATE

TransformFVector4 method: http://pastebin.com/raw.php?i=eFRgwqyv

The manual matrix * vector method: http://pastebin.com/raw.php?i=zVzvRw85

See Project() function.

Why doesn’t Project work for you?
If you want to project from other class rom AHUD, you can implement the logic yourselves.

Refer the code from UCanvas::Project function

https://docs.unrealengine.com/latest/CHN/API/RuntimeModules/Engine/Engine/UCanvas/Project/index.html

I see that you have accessed ViewProjectionMatrix. why dont you use ViewProjectionMatrix.TransformFVector4() function?. You might have to manipulate the retuned value to get the proper output

Hi Chris528! Thanks for sharing your code. I used the last updated version of your code from pastebin and it does what I need! I referred to your post in an other post (which I opened) and I also pasted your code there. Hope you don’t mind!

Hi Chris528. Would you mind separating your solution out into an answer for this question to make it easier for others to find it?

Thanks.

After looking through some code in the engine as well as some code online I was able to figure out what I was doing wrong.

Here’s my current code. http://pastebin.com/raw.php?i=zVzvRw85

The performance improvement of manually multiplying the viewprojection matrix by the world position is most likely negligible, so you may want to use the TransformFVector4 method instead, as its much easier to read.

TransformFVector4 method: http://pastebin.com/raw.php?i=eFRgwqyv

Thanks again! Great solution!

There is also a ConvertScreenLocationToWorldSpace & ConvertWorldLocationToScreenLocation that is callable from a PlayerController which works. These are not documented anywhere so it’s easy to miss them.

Note that these are the friendly blueprint function names and that if you want to call them from code it’s actually these functions:

/** Convert current mouse 2D position to World Space 3D position and direction. **/
	UFUNCTION(BlueprintCallable, Category = "Game|Player", meta = (FriendlyName = "ConvertScreenLocationToWorldSpace"))
	void DeprojectScreenPositionToWorld(float ScreenX, float ScreenY, FVector & WorldLocation, FVector & WorldDirection) const;

	/**
	 * Convert a World Space 3D position into a 2D Screen Space position.
	 * @return true if the world coordinate was successfully projected to the screen.
	 */
	UFUNCTION(BlueprintCallable, Category = "Game|Player", meta = ( FriendlyName = "ConvertWorldLocationToScreenLocation" ))
	bool ProjectWorldLocationToScreen(FVector WorldLocation, FVector2D& ScreenLocation) const;

ProjectWorldLocationToScreen is the function i believe you meant.

You are correct of course! Apologies and I’ve fixed up the answer.

Project world to screen function works only if it’s realy visible - if You want to do quest markers pin to screen edge, then it’s don’t work, and always return 0,0