Click to Rate and Give Feedback
MSDN
MSDN Library
XNA Game Studio
XNA Game Studio 3.1
Viewport Structure
Viewport Methods
 Unproject Method
Collapse All/Expand All Collapse All
XNA Game Studio 3.1
Viewport.Unproject Method
Converts a screen space point into a corresponding point in world space.

Namespace: Microsoft.Xna.Framework.Graphics
Assembly: Microsoft.Xna.Framework (in microsoft.xna.framework.dll)

C#
public Vector3 Unproject (
         Vector3 source,
         Matrix projection,
         Matrix view,
         Matrix world
)

Parameters

source
The vector to project.
projection
The projection matrix.
view
The view matrix.
world
The world matrix.

Return Value

The vector in object space.

A common use for Unproject is determining if the current cursor location intersects with an object in 3D world space. First, calculate two Vector3 values that differ only by their Z value. For instance, assume that the cursor location is currently (100, 100). Therefore, the first vector (located at the near clip plane) becomes (100, 100, 0) and the second (located at the far clip plane) becomes (100, 100, 1).

Call Unproject for each point, and store the result. For example, minPointSource stores the result of "unprojecting" (100,100,0), and maxPointSource stores the result of "unprojecting" (100, 100, 1). Determine the direction vector by subtracting maxPointSource from minPointSource.

Finally, normalize the direction vector, and create a Ray with minPointSource and the now-normalized direction vector. You can now use this ray in a simple intersect test case (for example, Intersects) with the model.

For more usage examples of Unproject, see the following:

Xbox 360, Windows XP SP2, Windows Vista, Zune
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker