Converting world space coordinate to screen space coordinate and getting incorrect range of values

Posted by user1423893 on Game Development See other posts from Game Development or by user1423893
Published on 2012-08-30T15:39:32Z Indexed on 2012/08/30 21:51 UTC
Read the original article Hit count: 476

Filed under:
|

I'm attempting to convert from world space coordinates to screen space coordinates.

I have the following code to transform my object position

Vector3 screenSpacePoint = Vector3.Transform(object.WorldPosition, camera.ViewProjectionMatrix);

The value does not appear to be in screen space coordinates and is not limited to a [-1, 1] range.

What step have I missed out in the conversion process?

EDIT:

Projection Matrix


Perspective(game.GraphicsDevice.Viewport.AspectRatio, nearClipPlaneZ, farClipPlaneZ);

    private void Perspective(float aspect_Ratio, float z_NearClipPlane, float z_FarClipPlane)
    {
        nearClipPlaneZ = z_NearClipPlane;
        farClipPlaneZ = z_FarClipPlane;

        float yZoom = 1f / (float)Math.Tan(fov * 0.5f);
        float xZoom = yZoom / aspect_Ratio;

        matrix_Projection.M11 = xZoom;
        matrix_Projection.M12 = 0f;
        matrix_Projection.M13 = 0f;
        matrix_Projection.M14 = 0f;

        matrix_Projection.M21 = 0f;
        matrix_Projection.M22 = yZoom;
        matrix_Projection.M23 = 0f;
        matrix_Projection.M24 = 0f;

        matrix_Projection.M31 = 0f;
        matrix_Projection.M32 = 0f;
        matrix_Projection.M33 = z_FarClipPlane / (nearClipPlaneZ - farClipPlaneZ);
        matrix_Projection.M34 = -1f;

        matrix_Projection.M41 = 0f;
        matrix_Projection.M42 = 0f;
        matrix_Projection.M43 = (nearClipPlaneZ * farClipPlaneZ) / (nearClipPlaneZ - farClipPlaneZ);
        matrix_Projection.M44 = 0f;
    }

View Matrix


        // Make our view matrix
        Matrix.CreateFromQuaternion(ref orientation, out matrix_View);

        matrix_View.M41 = -Vector3.Dot(Right, position);
        matrix_View.M42 = -Vector3.Dot(Up, position);
        matrix_View.M43 = Vector3.Dot(Forward, position);
        matrix_View.M44 = 1f;

        // Create the combined view-projection matrix
        Matrix.Multiply(ref matrix_View, ref matrix_Projection, out matrix_ViewProj);

        // Update the bounding frustum
        boundingFrustum.SetMatrix(matrix_ViewProj);

© Game Development or respective owner

Related posts about c#

Related posts about coordinates