Issues with viewport.Project

Started by
4 comments, last by adam.jones 9 years, 1 month ago

First off sorry if this is to wrong section.

I am currently working on a way to to take the world space coords of a model and turn them in to screen space coords. Now I am still new to XNA and what i found as I can use the viewport.project method to convert the world space to screen space. I have set this all up but every time i get can the same numbers which are the center on the window. It makes no difference what the world space values are.

Now i am think I have made a mistake with the matrices and they are not calculating correctly but I have no idea any more and after 24 hours of tiring to fix this it is now killing me.


 Console.WriteLine("X: " + min.X + "  :" + max.X);
 Console.WriteLine("Y: " + min.Y + "  :" + max.Y);

 Min2D = graphics.GraphicsDevice.Viewport.Project(min, project, view, world);
 Max2D = graphics.GraphicsDevice.Viewport.Project(max, project, view, world);

//test project
 Vector3 temp3 = graphics.GraphicsDevice.Viewport.Project(new Vector3(0, 0, 0), project, view, world);

 Console.WriteLine("X: " + Min2D.X + "  :" + Max2D.X);
 Console.WriteLine("Y: " + Min2D.Y + "  :" + Max2D.Y);

 flames = new BoundingBox(Min2D, Max2D);

This is the where I am projecting the values. The matrices are past in from the main Initialize.


private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
private Matrix view = Matrix.CreateLookAt(new Vector3(0, 0, 10), new Vector3(0, 0, 0), Vector3.UnitY);
private Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 1f, 100f);

This is the creation of them. I have check over them and I cant see what is wrong.

If anyone can help me with this I will be in your debt. For clarity I am using XNA with monogame and the values I am passing into project are

minX = -3 maxX = 3

minY = -3 maxY = 3

Advertisement

private Matrix world = Matrix.CreateTranslation(new Vector3(0, 0, 0));
…
graphics.GraphicsDevice.Viewport.Project(new Vector3(0, 0, 0), project, view, world);

How are you expecting to get different results if you are always hard-coding the input the same way?

It makes no difference what the world space values are.

Prove it. What other values have you tried? For testing purposes, [0,0,0] is the worst value you can possibly use.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You are correct, it is not giving me the same numbers and if I use the numbers passed from the model I also get different answer so it looks like there is a issues with my code elsewhere.

Ok so I have not had a lot of time to work on my project but I spent a good part of today looking into this error i have.

So I took L.Spiro and changed the numbers around. After checking every number over and over I finally worked out that the coords that i am getting from project are all wrong because somewhere it is adding 24.5 to them all. I am currently searching through my code to work out why and at what point that it being added but so far the only point i can see is from the viewport.project.

Has anyone else had this problem before?


it is adding 24.5 to them all.

That's not much help, really, as it's not clear what value 24.5 is being added to. The Z value? All 3 values?

Suggestion:

Set your world matrix to Matrix.Identity, and your view and projections as you've posted above. Using just one or two lines of code, determine a world position that converts correctly to screen coordinates. E.g., testVector = Vector3( 0, 0, 0 ) should convert to center-of-screen coordinates (x==0, y==0, z== some depth value).

It appears you're using a viewpoint 10 units along the +Z axis, looking at the origin. If so, then change testVector by a very small amount in the +X direction (only) and see if it converts reasonably.

E.g.,


testVector = Vector3( 1, 0, 0 ); // 1 unit along the +X axis
resultVector = graphics.GraphicsDevice.Viewport.Project( testVector, project, view, world );

resultVector should be slightly to the left of center-of-screen. That is, viewed from +Z, X = 1 should be just to the left of the world origin, and just to the left of the screen-center.

If that works, then change the values of testVector by small amounts, working by small changes toward one of the values for your model until you encounter a problem.

If that doesn't work, check your viewport values (width, height, etc.) to make sure the viewport has been setup correctly.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

OK first off thank you for all the help.

So I have fixed the problem and here is what was wrong. The main reason I wanted to get the screen coords was to set up collision for a 3D object with 2D sprites. So mistake number one was my view matrix, this was not set to the correct view port size. The biggest mistake was passing the z value of my shape in. This was calculating the coords with that as well and returning off values.

This topic is closed to new replies.

Advertisement