Game Development Reference
In-Depth Information
3.4U NPROJECT (S CREEN TO W ORLD )
While project is a function to bring a world space coordinate into screen space, unproject
is the opposite operation, given a screen space position, we want to calculate a correspond-
ing world space position. The use of unproject seems less intuitive at first glance, as you
can imagine there may be infinite coordinates in world space under any given point on the
screen, however, rather than passing a 2D vector of screen coordinates, we can pass a 3D
vector, using the z component as a ratio into the depth of the view frustum.
A common use for this is for picking or determining whether your input cursor, whether a
mouse click, or a finger tap on the display intersects with any objects in the 3D world.
Theideabehinditis,beginbyunprojecting ascreenpositionatdepth0.0,thisrepresentsthe
nearclippingplaneoftheviewfrustum;keeptheworldspacepositionyouretrieved,wewill
callthis p0 .Next, unproject thesamescreenpositionatadepthof1.0,thisrepresentsthefar
clipping plane of the view frustum, we will also keep this position, and call it p1. Now we
have two positions in 3D space from which we can construct the direction vector
At this point you can create a ray using p0 and v which you can use to perform intersection
testing against the objects in the game's world.
Thefirststepto unproject ascreenspacepositionistoframetheworldspacepositionwithin
the viewport, in normalized coordinates from -1 to 1.
There is a special case to be aware of when calculating p' , it is possible for depth max
- depth min to yield 0.0, which would result in a division by zero, if this is the case, we
set p' z to 0.0.
Search WWH ::




Custom Search