Jump to content
  • Advertisement
Sign in to follow this  

C# SharpDX convert 3D coordinate to 2D

This topic is 401 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to select my meshes with mouse on screen. I think I wiil need 2d coords of mesh vertices I use vector4 for it. Is there a simple way to do it ?


CoordinateConverter class

        public Vector2 Convert_3Dto2D(Vector3 position, Matrix viewProj, RenderForm_EX form)
            var project = Vector3.Project(position, 0, 0, form.ClientSize.Width, form.ClientSize.Height, 0, 1, viewProj);
            Console.WriteLine(project.X+" "+ project.Y);
            return new Vector2(project.X, project.Y);

Inside RenderLoop

if (MEListener.MouseDown)
   var viewProj = Matrix.Multiply(Meshes.MeshCollection[0].ViewLayer.Camera.getView(), proj);
   for (int _iV = 0; _iV < Meshes.MeshCollection[0].VertexData.Length; _iV++)
     CoordConverter.Convert_3Dto2D((Vector3)Meshes.MeshCollection[0].VertexData[_iV].Position + Meshes.MeshCollection[0].Position, viewProj, form);

I think I did it correct. When I move mesh I can get still correct result I think. But what happens if I rotate or scale mesh ?

For translation I do it like: 

So I +add Position of mesh

(Vector3)Meshes.MeshCollection[0].VertexData[_iV].Position + Meshes.MeshCollection[0].Position,...

What should I Do to get coorect results ?

Edited by gomidas

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!