Thursday, March 1, 2018

xna - 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.CurrentTechnique = effect.Techniques["ColoredNoShading"];

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

// rotating like a radar
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 :)


Thank you for your assistance



Answer



I found that the issue was related with the usage of viewports. When defining viewports for 3d we need to define 2 extra props minDepth and maxDepth.


If we forget to define these two... the viewport is still valid for 2d but using unproject in 3d will return the NaN issue


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...