Changing the drag amount based on cam pos

I want to drag the objects in 3D consistently based on the current camera position. Currently if i setup a constant amount like this

valX = (x - oldX)/300; 
valY = (oldY - y)/300; 

This works and the object follows the cursor fine. If i move the camera, the drag amount is slow/fast depnding on if I zoom in/out? Any ideas on how to make it generalized for any zoom value?

For that you will have to gluUnProject the mouse position in the 2D window, and intersect the resulting line with your 3D XY plane.

Hi Zbuffer,
Thanks for ur reply. Well i have the point already calculated now i need to contol the drag amount such that on zooming in/out the drag amount stays the same.

If you did what I tried to explain, you would not need adjusting at all :slight_smile:
Do you need that to work in a generic perspective view, or only 2D views (simpler) ?

Well its for perspective view and in opengl3.3 thus i need to clacl the unProject stuff which i did already here it is

void UnProject(float winx, float winy, float winz, GLint* viewport, float* objXYZ) {
   M3DVector4f tmp;
   tmp[0] = (2.0f*((winx-viewport[0])/float(viewport[2]))) -1;
   tmp[1] = (2.0f*((winy-viewport[1])/float(viewport[3]))) -1;
   tmp[2] = (2.0f*winz)-1.0;
   tmp[3] = 1.0;

Once i have the object space xyz i loop through all my objects and return the first object intersected with this object space xyz (simple aabb test). Are u suggesting that I cast a ray back frm the camera? But I dont understand how this will help me in obtaining the drag amount?

The ray is only needed if you don’t have the winz value.

Form mouse positions A and B you want the world positions A’ and B’ right ? So UnProject both and A and B, then compute the A’B’ distance for world space ‘drag amount’.

Both A and B can have the same winz for a 2D move parallel to the current window.