# XNA Texture2D positioning problem

I'm currently stuck on something that just seems extremely odd. I'm creating a game with kinect, and when the player performs an action I want to place a texture onto the screen where their hand is.

I currently have that code implemented. I have something like:

var myVector = Skeleton[JointType.RightHand].Position;


When I get the vector coordinates I store them in an array and that allows me to reference it later if/when I need to. That is the HitVectors array.

The numbers that are recorded into the array are the actual coordinates of the [RightHand] in relation to it's pixel coords on the screen. So for instance your hand is at 100X and 400Y. the array gets a new Vector2(100f, 400f), and so on...

public override void Draw(GameTime gameTime)
{
// This game has a blue background. Why? Because!
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target,
Color.CornflowerBlue, 0, 0);

SpriteBatch spriteBatch = ScreenManager.SpriteBatch;

spriteBatch.Begin();

spriteBatch.Draw(targetTexture, hostileOne.CollisionBox, Color.White);

if (hostileOne != null && hostileOne.HitVectors.Count > 0)
{
foreach (var pos in hostileOne.HitVectors)
{
//trying to get the textures to not draw in a diagonal line
//spriteBatch.Draw(impactTexture, pos, Color.White);
spriteBatch.Draw(impactTexture, new Rectangle((int)(pos.X), (int)(pos.Y), 256, 256), Color.White);
}
}

spriteBatch.End();
}


but the texture isn't drawn to those coordinates on the screen. Instead they are essentially only drawing on a diagonal line, at a 45 degree angle. It seems like the oddest thing to me. I've double checked to make sure that the position coords being passed into the sprite batch are correct, and they are. I've checked to make sure that the coordinates of the hand joint that i'm tracking is at the correct X and Y coordinates when I record its position. So to me, everything seems good until the spritebatch.Draw() method is executed.

For extra info, my texture is a 256x256 png file, the spriteBatch being used is from the GameStateManagement example. It's in the ScreenManager class.

How I convert my Kinect joint position in 3D space to 2D:

protected Vector2 CalculatePointPosition(SkeletonPoint point)
{
var pointx = point.X;
var pointy = point.Y;
var pointz = point.Z;
var pointnormx = pointx / pointz;
var pointnormy = pointx / pointz;

return new Vector2((pointnormx + 0.5f) * (int)screenSize.X, (pointnormy + 0.5f) * (int)screenSize.Y);
}


• I don't think we could find your problem just from the two lines of code you've posted. Is it possible you have some matrix-manipulation math somewhere that's not being cleaned up properly? It's possible just from the graphics device being in an unexpected state, that it would draw a sprite differently. – Katana314 Jun 18 '13 at 13:31
• Also its a whole lot easier to debug graphical issues when there are images of whats occuring. – ClassicThunder Jun 18 '13 at 13:35
• @Katana314, I'm not using the Matrix class at all if that's what you're hinting at. One thing that i'm doing is I'm taking my joints in 3D space and converting them to 2D so that I can get pixel coordinates of the joint on the screen. That code is like this (I'll add it to the question) – CSMHowitzer Jun 18 '13 at 15:31
• ahhhhhh. I see my issue now. My normal for the y coord was calculated by (pointx / pointz) which is the same as the normal for the x coord. It was a syntax problem. var pointnormx = pointx / pointz; var pointnormy = pointx / pointz; should've been: var pointnormx = pointx / pointz; var pointnormy = pointy / pointz; – CSMHowitzer Jun 18 '13 at 15:55

Use Viewport.Project to project the 3D point to 2D.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport.project.aspx

the code I was using was:

protected Vector2 CalculatePointPosition(SkeletonPoint point)
{
var pointx = point.X;
var pointy = point.Y;
var pointz = point.Z;
var pointnormx = pointx / pointz;
var pointnormy = pointx / pointz;

return new Vector2((pointnormx + 0.5f) * (int)screenSize.X, (pointnormy + 0.5f) * (int)screenSize.Y);
}


and the issue was a syntax error where I had coded in var pointnormy = pointx / pointz; and it should've been var pointnormy = pointy / pointz;

so the correct code in this case was:

protected Vector2 CalculatePointPosition(SkeletonPoint point)
{
var pointx = point.X;
var pointy = point.Y;
var pointz = point.Z;
var pointnormx = pointx / pointz;
var pointnormy = -(pointy / pointz);

return new Vector2((pointnormx + 0.5f) * (int)screenSize.X, (pointnormy + 0.5f) * (int)screenSize.Y);
}


this function is taking the real world measurements of a point in space (in my case it's the user's right hand) and takes the normal vectors and multiplies them by the screen width and height respectively.