   I need to do inverse picking - world coordinates to pixel coordinates!
  Hey there guys.I have this function that works for picking.   private bool doPicking(ref Device device, Vector3 position, ref Matrix transform, float scaling, ref float size, short MouseX, short MouseY)   {  BoundingSphere sphere = new BoundingSphere(position, size * scaling);   SlimDX.Vector3 v = new SlimDX.Vector3();  v.X = ((((2f * MouseX) / device.Viewport.Width) - 1) / (Game_memory.Values.PROJECTION.M11));  v.Y = (-(((2f * MouseY) / device.Viewport.Height) - 1) / (Game_memory.Values.PROJECTION.M22));  v.Z = (1);  SlimDX.Matrix m = SlimDX.Matrix.Invert(Game_memory.Values.VIEW);  SlimDX.Vector3 rayOrigin = new SlimDX.Vector3();  SlimDX.Vector3 rayDir = new SlimDX.Vector3();  rayDir.X = (v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31);  rayDir.Y = (v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32);  rayDir.Z = (v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33);  rayOrigin.X = m.M41;  rayOrigin.Y = m.M42;  rayOrigin.Z = m.M43;  SlimDX.Matrix matInverse = SlimDX.Matrix.Invert(Matrix.Identity);  SlimDX.Vector3 rayObjOrigin = SlimDX.Vector3.TransformCoordinate(rayOrigin, matInverse);  SlimDX.Vector3 rayObjDirection = SlimDX.Vector3.TransformNormal(rayDir, matInverse);  rayObjDirection = SlimDX.Vector3.Normalize(rayObjDirection);  float distance = 0;  return BoundingSphere.Intersects(sphere, new SlimDX.Ray(rayObjOrigin, rayObjDirection), out distance);   }However, this method converts pixel coordinates into 3D world space coordinates.What if I have 3D world space coordinates that I would like to convert into pixel coordinates?Any suggestions?[Edited by - Mathy on May 6, 2010 10:01:23 AM]
You can multiply your Vector3 containing world space coordinates with a world * view * projection matrix, which should give you 'clip space' coordinates (screenspace in the range of -1 to 1 for x and y). You can then convert them to real screen coordinates using your viewport width & height, basically:0.5 * (clipSpace.x + 1) * width0.5 * (clipSpace.y + 1) * heightI think that should do the trick.;  What remigus said :)Basically you're asking how to do the most common thing in 3D graphics. How to go from a point in the 3D world (like a vertex) to a point on your screen.   ;  I tried doing this, but that doesn't seem to work.On a resolution of 1024X768, it always just returns X:512 Y:384.What am I doing wrong?This is the code:Matrix clipSpaceMatrix = Matrix.Identity * Game_memory.Values.VIEW * Game_memory.Values.PROJECTION;Vector2 clipSpaceCoordinates = new Vector2(clipSpaceMatrix.M11 * clipSpaceMatrix.M21 * clipSpaceMatrix.M31, clipSpaceMatrix.M12 * clipSpaceMatrix.M22 * clipSpaceMatrix.M32);_pixelPosition = new Vector2((float)(0.5 * (clipSpaceCoordinates.X + 1) * Game_memory.Values.DEVICE.Viewport.Width), (float)(0.5 * (clipSpaceCoordinates.Y + 1) * Game_memory.Values.DEVICE.Viewport.Height));;Your matrix math seems to be going wrong... either that, or you're doing something brilliant I'm not following [smile]  Anyway, my guess is that this line isn't quite right:Vector2 clipSpaceCoordinates = new Vector2(clipSpaceMatrix.M11 * clipSpaceMatrix.M21 * clipSpaceMatrix.M31,   clipSpaceMatrix.M12 * clipSpaceMatrix.M22 * clipSpaceMatrix.M32);What you'd typically want is something like this:Matrix worldViewProjection = Matrix.Identity * Game_memory.Values.VIEW * Game_memory.Values.PROJECTION;// your own xyz worldspace coordinates go in hereVector3 worldSpaceCoordinates = new Vector3(x, y, z); Vector3 clipSpaceCoordinates = Vector3.Transform(worldSpaceCoordinates, worldViewProjection);_pixelPosition = new Vector2((float)(0.5 * (clipSpaceCoordinates.X + 1) * Game_memory.Values.DEVICE.Viewport.Width),   (float)(0.5 * (clipSpaceCoordinates.Y + 1) * Game_memory.Values.DEVICE.Viewport.Height));Vector3.Transform performs a common vector * matrix multiplication, as you might have seen in linear algebra. Multiplying by a matrix conceptually means the vector you supply is transformed from one space to another, in this case from world space (technically from model space, but since you use Matrix.Identity as the world matrix, these are the same) to clip space. If you'd like to know more, this excellent primer should get you started.;  Is there any reason you aren't using Vector3.Project()?   ;Quote:Original post by SiCraneIs there any reason you aren't using Vector3.Project()?You mean aside from the fact that Vector3.Project is for pussies? [grin]I wasn't sure if SlimDX had that method to be quite honest, so I thought I'd try and explain the steps a bit. 