I am facing a problem in mapping 3d co-ordinate to screen co-ord . I require it because i want to draw text on the model . model is drawn at the 3d-cordinate, so if i know the position of that co-ord on the screen i can draw text at that position.
my view is on z axis test position is on x axis
In the follwing code I did what i said above but the transformation is not happening the way it should i.e, text should appear on the model
float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio; Vector2 screenOrigin = new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height / 2); Vector3 testPosition = new Vector(10,0,0); Vector3 cameraPosition = new vector(0,0,50), Matrix world = Matrix.Identity; Matrix view = Matrix.CreateLookAt(cameraPosition,Vector3.Zero, Vector3.Up); Matrix projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), aspectRatio, 1.0f, 10000.0f); Matrix totalEffect = world * view * projection; Vector2 spritePos = getXYPosition(vecAndMatMultiplication(testPosition, totalEffect))+screenOrigin;
vecAndMatMultiplication() multiplies vector with the matrix and
getXYpostion() should be fairly obvious.