Jump to content
  • Advertisement
Sign in to follow this  
JohannesH

2D to 3D position

This topic is 3017 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 am a beginner in 3D and XNA and can't get my head around 2D to 3D conversion. I need to convert a mouse X,Y position to a position in my world view at Z=0. I tried to use Viewport.Unproject twice: Vector3 near = new Vector3(_2D_point.X, _2D_point.Y, 0); Vector3 far = new Vector3(_2D_point.X, _2D_point.Y, 1); near=GraphicsDevice.Viewport.Unproject(near, projectionMatrix, viewMatrix,effect.World ); far=GraphicsDevice.Viewport.Unproject(far, projectionMatrix, viewMatrix, effect.World); Given that I get 2 Vector points from this, how to I calculate the Vector3 position at Z=0 ??? THANKS

Share this post


Link to post
Share on other sites
Advertisement
You will get two vectors, one with Z at the near plane and one with Z at the far plane.
These two vectors uniquely determines an infinite line, which you can intersect against the Z=0 plane.

Share this post


Link to post
Share on other sites
Edit: Nevermind, I just realized you only need the position on a certain plane. Zao has posted the right approach.

Share this post


Link to post
Share on other sites
I agree with Zao.

If there is something that feels wrong, check the value of the "effect.World" matrix. You didn't talk about any object so I think it should be identity.

Share this post


Link to post
Share on other sites
Hi Zao,
>These two vectors uniquely determines an infinite line, which you can intersect against the Z=0 plane.

Thanks! It may be simple, but how do I calculate that intersection point with Z=0?

near: X=-7.82 Y=0.5307 z=-4.4464
far: X=4996.03857 Y=2655.054 Z=1237.23633

What formula do i use to get the intersection with Z=0 ?

Johannes

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!