Vector3.Project() doesn't work.

I'm trying to project the location of a mesh in world space back to screen coords. I've been searching around with no real luck. I've seen people say that you should use Matrix.Identity instead of your world matrix, but that makes no difference for me at the moment.

It's not that the code causes an error, it doesn't. It's just that after projecting pos, it remains almost identical to what it was, off by about 0.5. Considering my values are typically in the hundreds, this is a minor rounding error. I get the exact same results using Unproject(). I would have expected that projecting into 2D space would have removed the z component, but it doesn't. The coords don't even get transformed by the view matrix.

I am setting up all of the matrices prior to this code block. I'm also using all the latest software, april 2006 sdk, VS 2005 and MDX 1.1.

So as usual, I find myself here, asking what haven't I done properly Project returns a void, so it's not lack of assignment to a new vector.

Any ideas would be much appreciated.

name_bar.Begin(SpriteFlags.AlphaBlend | SpriteFlags.SortDepthBackToFront | SpriteFlags.SortTexture);
LinkedListNode<GameClient> nbclient = game.clients.First;
while (nbclient != null)
{
Vector3 pos = new Vector3(nbclient.Value.x, nbclient.Value.y, nbclient.Value.z);
pos.Project(
Graphics.device.Viewport, Graphics.device.Transform.Projection, Graphics.device.Transform.View, Matrix.Identity);
int sx = (int) pos.X + 400;
int sy = (int) (300 - pos.Y);

dxfont.DrawText(name_bar, nbclient.Value.name, sx, sy, Color.Yellow);
nbclient = nbclient.Next;
}
name_bar.End();



Answer this question

Vector3.Project() doesn't work.

  • Optima Warehouse Solutions

    The Z value that is output should be in the range of 0.0 to 1.0. Projecting does not get rid of Z, that is the value that gets written to the depth buffer.

    It does sound like the Project function isn't returning the proper values, though... have you tried using the static version of the Project function, i.e. "pos=Vector3.Project(pos,...)" instead of "pos.Project(...)"

    Also, if you created your device using the PureDevice behavior flag, you would not be able to retrieve valid transformation matrices from the device, and this could cause the results that you are seeing.

    Robert Dunlop
    Microsoft DirectX MVP
    www.directxzone.org


  • SQL User 2005

    Robert Dunlop wrote:

    The Z value that is output should be in the range of 0.0 to 1.0. Projecting does not get rid of Z, that is the value that gets written to the depth buffer.

    It does sound like the Project function isn't returning the proper values, though... have you tried using the static version of the Project function, i.e. "pos=Vector3.Project(pos,...)" instead of "pos.Project(...)"

    Also, if you created your device using the PureDevice behavior flag, you would not be able to retrieve valid transformation matrices from the device, and this could cause the results that you are seeing.

    Robert Dunlop
    Microsoft DirectX MVP
    www.directxzone.org

    I ended up getting it to work by storing Device.Transform.World etc. in a variable first isntead of using them inline in the function. I tried the static version, I also tried Unproject, which gave identical results. The results I was getting appeared to be x / 2 and -(y / 2). Close to it anyway.

    I'm not using a pure device, but I am using Software Vertex Processing. I'm not sure why I am using software when I have a GeForce 6800 GT, it must have been something I was testing and forgot to put it back...


  • Vector3.Project() doesn't work.