Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7902175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:25:46+00:00 2026-06-03T09:25:46+00:00

I am using XNA 4.0 and trying to take a Vector3 and turn it

  • 0

I am using XNA 4.0 and trying to take a Vector3 and turn it into a Vector2 which represents its coordinates on the screen.
I know you can use Viewport.Project and this gives me perfect results, but I need to make my own method for doing this.
I have to convert every vertex to 2D coords per frame and on CPU instead of GPU.
Using the provided Project method is a little slower than what I’d like.
It returns a “z” value too, which I won’t even use. I just want to return a (x, y) in the simplest way possible.

Here is what I have come up with so far, and it gives nearly correct results. But not quite there yet

    private Vector2 ScreenCoords(Vector3 v)
    {
        return new Vector2(
            viewProj.M11 * v.X + viewProj.M21 * v.Y + viewProj.M31 * v.Z + viewProj.M41,
            viewProj.M12 * v.X + viewProj.M22 * v.Y + viewProj.M32 * v.Z + viewProj.M42);
    }

viewProj is just (viewMatrix * ProjectionMatrix)

A vertex at the center of the screen seems to always come back as (0, 0) but I get slightly messed up results as the vertex changes screen coordinates (as I move the camera)
As the vertex moves left, the X of my projected result decreases, and as the vertex moves down, the Y of my projected result increases.
So at least I have that right, but the rate they change at isn’t correct or consistent.

EDIT: here is how it’s done, probably the fastest way of doing it (note that this returns the coordinates in the range [-1, 1])

    private Vector2 ScreenCoords(Vector3 v) 
    {
        float 
            w = viewProj.M14 * v.X + viewProj.M24 * v.Y + viewProj.M34 * v.Z + viewProj.M44;
        return new Vector2(
            (viewProj.M11 * v.X + viewProj.M21 * v.Y + viewProj.M31 * v.Z + viewProj.M41) / w,
            (viewProj.M12 * v.X + viewProj.M22 * v.Y + viewProj.M32 * v.Z + viewProj.M42) / w);          
    }

Here is a comparison to using Viewport.Project to get the 2D coordinates –
FPS with Project – 81
FPS with ScreenCoords – 143
FPS without either one – 152
So I would recommend something like this for anyone else who is doing lots of projecting per frame.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:25:47+00:00Added an answer on June 3, 2026 at 9:25 am

    You should realize that perspective projections need homogeneus coordinates, you should divide

    the components by the homegeneus factor W.

    This is the math:

    Vector4 position = new Vector4(pos3, 1);
    Vector4.Transform(ref position, ref worldViewProjection, out position);
    position /= position.W;
    

    Also you can find more info here: http://en.wikipedia.org/wiki/Transformation_matrix#Perspective_projection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a 2D array in XNA which I'll be using as
I'm trying to draw on the screen pixel-by-pixel using XNA, but am having problems
I'm trying to develop an app using XNA and for state management I'm using
I am trying to make my first app using XNA, and I am having
Creating a simple RPG game, first time using XNA. Trying to get my character
I'm trying to draw a textured quad in XNA using the sample code from
I'm trying to host an XNA game inside a WPF window using the Windows
I'm trying to output realtime 3D point clouds (307,200 points) using XNA and Kinect;
I'm trying to design a small game using C# and XNA, before I start
I'm using VS2010 express to create a game built with xna . I'm trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.