# Projecting a shape into screen space coordinates gives odd results.

This topic is 2775 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I would like to project a shape from its object coordinates into screen space so it will always face a camera.

I start by drawing a yellow triangle in world space using a perspective transform:

 Vector3 a = new Vector3(0, 0, 0); Vector3 b = new Vector3(0, 2, 0); Vector3 c = new Vector3(1, 0, 0); game.WireShapeDrawer.Begin(game.ActiveCamera.ViewMatrix, game.ActiveCamera.ProjectionMatrix); game.WireShapeDrawer.DrawWireTriangle(a, b, c, Color.Yellow); game.WireShapeDrawer.End(); 

I now use the built in XNA function to project the vertices a, b and c to screen space:

 a = viewport.Project(a, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); b = viewport.Project(b, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); c = viewport.Project(c, camera.ProjectionMatrix, camera.ViewMatrix, Matrix.Identity); 

Each coordinate requires dividing by the width and height in order to be in the [-1, 1] range and the y axis is flipped:

 a.X /= viewport.Width; a.Y /= -viewport.Height; b.X /= viewport.Width; b.Y /= -viewport.Height; c.X /= viewport.Width; c.Y /= -viewport.Height; 

The projected triangle is now drawn to the screen using no transformation (Matrix.Identity):

 game.WireShapeDrawer.Begin(Matrix.Identity, Matrix.Identity); game.WireShapeDrawer.DrawWireTriangle(a, b, c, Color.Purple); game.WireShapeDrawer.End(); 

The final result produces a triangle that doesn't face the camera and doesn't appear to project onto 2D correctly:

My projection matrix is created as follows:

 private void Perspective(float aspect_Ratio, float z_NearClipPlane, float z_FarClipPlane) { nearClipPlaneZ = z_NearClipPlane; farClipPlaneZ = z_FarClipPlane; float yZoom = 1f / (float)Math.Tan(MathHelper.ToRadians(GlobalSettings.FOV_X) * 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; } 

The view matrix is as follows:

 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; 

Where might this be going wrong?

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633700
• Total Posts
3013426
×