0
\$\begingroup\$

I think I am almost there, but not quite. I am trying to transform a 2D point, (in the following example (100,100)) into 3D world coordinates, in order to draw my 3D Model named TestModel in XNA/Monogame. Here is my attempt:

Matrix[] transforms = new Matrix[TestModel.Bones.Count];
TestModel.CopyAbsoluteBoneTransformsTo(transforms);
ModelMesh mesh = TestModel.Meshes[0];
BasicEffect effect = (BasicEffect)mesh.Effects[0];
float Scale = 1.2f;

effect.View = Matrix.CreateLookAt(new Vector3(0.0f, 0.0f, 6115.0f), 
     Vector3.Zero, Vector3.Up);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 
    aspectRatio, 1.0f, 10000.0f);
effect.EnableDefaultLighting();

//here i am using the function to calculate the world coordinates from (100,100) Point
//it gives me modelPosition = {X:284,0316 Y:384,2054 Z:0,9999364} in this example
Vector3 modelPosition = graphics.GraphicsDevice.Viewport.Project(new Vector3(100, 100, 1),
   effect.Projection, effect.View, Matrix.Identity);

effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(Scale) * 
   Matrix.CreateTranslation(modelPosition);

//finally i draw the model
mesh.Draw();

I suppose this is the correct function to do it graphics.GraphicsDevice.Viewport.Project, so what exactly am I missing here ? Right now the model is drawn around (393, 444) in the 2D world (instead of (100,100) which is where i am trying to draw it)

Edit: I had wrong numbers before
Edit2: I think the correct function is Unproject, but i still can't get it drawn at (100,100)

\$\endgroup\$
2
  • \$\begingroup\$ This mesh is meant to be drawn in 3D? It isn't, say, a billboard? And your world is 2D based, not 3D? \$\endgroup\$
    – jzx
    Commented Feb 22, 2015 at 10:37
  • \$\begingroup\$ @jzx my game is 2D, but the model is being drawn multiple times in the screen (in different locations) and it is 3D (it is the brick in an arkanoid type game) \$\endgroup\$
    – dimitris93
    Commented Feb 22, 2015 at 10:41

1 Answer 1

1
\$\begingroup\$

You are right that the correct function is Unproject. Try this:

Vector3 modelPosition = graphics.GraphicsDevice.Viewport.Unproject(
    new Vector3(100.0f, 100.0f, 0.01f), // Note the 0.01f!
    effect.Projection, 
    effect.View,
    Matrix.Identity);

The 0.01f means you want a point that is 1% of the way between your near and far clip planes (the values you passed into Matrix.CreatePerspectiveFieldOfView). This will work out to 1.0f + 0.01f * (10000.0f - 0.01f) = 100.99f away from the camera position. It should be centered at pixel position (100, 100). Try decreasing the 0.01f value or decreasing your far clip plane distance if the model is too small or not visible.

\$\endgroup\$
5
  • \$\begingroup\$ I actually had already tried this. With this code, the modelPosition is {X:-0,1395457 Y:0,3138523 Z:6113,989} and it is out of screen. With Z = 1f it is drawn at (139,147) and it is really tiny (like 2,5 times smaller than what it should have been with the current view/aspect ratio/angle etc) and with Z = 1.01f or Z = 0.99f or Z = 0.001f it is also out of screen. When i change far distance from 10000f to 1000 or 100, the difference is less than 0.0001f on X/Y of modelPosition, so it stays out of screen. \$\endgroup\$
    – dimitris93
    Commented Feb 24, 2015 at 18:38
  • \$\begingroup\$ Sounds strange. Perhaps just use effect.World = matrix.CreateTranslation(modelPosition); \$\endgroup\$
    – Aranda
    Commented Feb 26, 2015 at 2:42
  • \$\begingroup\$ this messes it up as well, i tried it now \$\endgroup\$
    – dimitris93
    Commented Feb 26, 2015 at 2:45
  • \$\begingroup\$ Alright, I'm outa ideas. In any case, you should really not bother with Unprojecting pixel positions. Follow nkast's instructions here: community.monogame.net/t/vector2-to-3d-world-coordinates/2008/… \$\endgroup\$
    – Aranda
    Commented Feb 26, 2015 at 5:36
  • \$\begingroup\$ i will try that also i suppose \$\endgroup\$
    – dimitris93
    Commented Feb 26, 2015 at 10:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .