# 3d world vertex translation to go to 2d screen coords

My technical english is a little rusty so to avoid misunderstands please be patient with me :)
I will try to be brief and clear

Situation:
- I have a 2d sprite character on the screen
- I've just start learning how to draw 3d primitives and work with the cameras
- I'm working on a 2d isometric environment
- I made a 3d isometric triangle which I want to center in character
- I'm trying to do something similar to a flashlight

Problem:
- The triangle is not centered on character (probably because of the scales between 2d and 3d workspace)
-the triangle speed does not match the character speed.

Code:

player vision

``````        playerVision = new VertexPositionColor[3];

playerVision[0].Position = new Vector3(-15f, -30f, 0f);
playerVision[0].Color = Color.Transparent;
playerVision[1].Position = new Vector3(0f, 0f, 0f);
playerVision[1].Color = new Color(70, 102, 25, 20);
playerVision[2].Position = new Vector3(15f, -30f, 0f);
playerVision[2].Color = Color.Transparent;
``````

-

``````private void SetUpCamera()
{
// isometric angle is 35º , for 50y = -107z
viewMatrix = Matrix.CreateLookAt(new Vector3(0, -107, 50), new Vector3(0, 0, 0), new Vector3(0, 1, 0));

// TODO: use viewport for whole window or just the draw area ?
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, GameInterface.vpWholeWindow.AspectRatio, 1.0f, 300.0f);
}

private void drawPlayerVision(GameTime gameTime, SpriteBatch spriteBatch, Vector2 playerDrawPos)
{
// 50 is related with the camera position in the axis
// TODO: this probably is incorrect since it should be a module of the distance? btw points
Vector2 playerPos = playerDrawPos / 50;

effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);

Matrix worldMatrix = Matrix.CreateRotationZ(3 * visionAngle);

// moving to the character
worldMatrix *= Matrix.CreateTranslation(new Vector3(playerPos.X, playerPos.Y, 0));

effect.Parameters["xWorld"].SetValue(worldMatrix);
//effect.Parameters["xWorld"].SetValue(Matrix.Identity);

foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
Game.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, playerVision, 0, 1, VertexPositionColor.VertexDeclaration);
}
}
``````

if you need more details please ask me I've been trying to figure out all by myself so its a little hard for me :)

-
Look into Viewport.Project and Viewport.Unproject methods, they might help you – NDraskovic Aug 1 '12 at 9:17
thanks im looking into them now – Navy Seal Aug 1 '12 at 16:54
I've still not been able to implement unproject... seems the primitve isn't drawn and also the values Im getting from unproject are Nan (not a number), ive been searching for an example but all i could find was an old xna example which isnt working anymore on xna 4... this seems so easy to implement but its just not working :( – Navy Seal Aug 2 '12 at 14:37
Well the first thing I can tell you is that working with primitives can be very frustrating, so you might try to make a model of the object you are trying to make (there are free modeling programs like Blender, Wings3D...). Working with models is a much easier (the basic stuff) than working with primitives. As for your problems with 2D-3D coordinates, loock into the picking algorithm, it might not solve your problem, but it might point you in the right direction. – NDraskovic Aug 4 '12 at 8:43