Jump to content
  • Advertisement
Sign in to follow this  

Screen distance to 'level' distance conversion?

This topic is 2363 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

Maybe my brain is being dead today but im really having trouble sorting this out in my head.

If my camera points down the Z axis, I have an object Z distance away from the camera, and I want the user to be able to drag the object around (the X-Y axis only) with their mouse, then I need some way of converting the distance they drag their mouse on the screen, to the equivalent distance on the plane I will need to move the object.

Because the object is located at 1000 on the z axis or something like that, then 10 pixels of screen distance will actually be a larger distance [say, 100] for the object if it is going to move enough to keep up with the mouse.

I am pretty sure this is related to the projection matrix, and/or the screen dimensions/ratio I am using but I just end up confusing myself when I think about it :(

How can I convert screen pixel distances to equivalent distances on an XY plane that is a certain distance away? What is the relationship between the two distances?

Im using opengl, doubt that is important though?
Thanks a lot, hope I was clear enough...

Share this post

Link to post
Share on other sites
Hi esaptonor,
you can calculate intersection of 3D ray going from viewer position through specified point on screen with XY plane lying at specified distance.
This will basically map 2D position from screen into 3D position (world space), so you can move your object easily.

Here is code snippet to perform screen-space position to world-space direction calculation:

Vector3 CalcScreenRayDir(const CCamera &cam,const Vector2 &normScreenPos) const
NX_ASSERT(normScreenPos[0] >= 0 && normScreenPos[0] <= 1);
NX_ASSERT(normScreenPos[1] >= 0 && normScreenPos[1] <= 1);
Matrix4 mViewProj(cam.GetViewProjMatrix());
Matrix4 mViewProjInv(mViewProj.GetInverseGeneral());

Vector3 projPos( RemapValue<Vector3::T_ValueType>(normScreenPos[0],0,1,-1,1),
Vector3 dir(UnProjectVect<Vector3,Vector3,Matrix4>(projPos,mViewProjInv) - cam.GetOrigin());
return dir;


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!