Sign in to follow this  
JohannesH

2D to 3D position

Recommended Posts

JohannesH    100
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
kloffy    1318
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
Txkun    151
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
JohannesH    100
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this