Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / 3d X/Y/Z worldspace position converted to X/Y camera screen position

Author
Message
Philip
21
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 25th Aug 2004 08:00
I've recently seen a few requests for a function that converts any X/Y/Z worldspace position into its equivalent X/Y camera screen position.

Phaelax and DmitryK have previously provided two excellent solutions for this problem. I thought for completeness I'd post mine as well.

In the case of the code below x vector3(1) and y vector3(1) contain the resulting X/Y camera screen coordinates. If they are larger than the size of the screen or are minus numbers then the X/Y/Z worldspace position is outside the camera's screen. If they are smaller than the size of the screen then the X/Y/Z worldspace position is either within the camera screen or behind it.

The code below also has some added functionality in that product# tells you whether the X/Y/Z worldspace position is in front of or behind the camera screen. Product# varies between 1.0 and -1.0. If product# is positive then the position is somewhere in front of the camera screen. The closer that product# gets to being 1.0 the closer the X/Y/Z worldspace position is to being bang in the centre of the camera screen. Vice versa is also true. In other words, if product# is negative then the X/Y/Z position is somewhere behind the camera screen. The closer product# gets to being -0.1 the closer the X/Y/Z position gets to being directly behind the very centre of the camera screen.

Cheers

Philip



What do you mean, bears aren't supposed to wear hats and a tie? P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home

Login to post a reply

Server time is: 2024-11-27 13:26:26
Your offset time is: 2024-11-27 13:26:26