'cursor moves faster then object' problem...

Recommended Posts

MMWizard    122
Hi, Im building an 3D app where the user can drag objects around. Problem is: I dont know what kind of calculation i need to use, to keep the cursor at the same spot on the object while dragging. I know that it depends on the distance from the object to the camera. But currently it does not what i want... Can someone help me out please. I would have searched it at this forum but i dont know the english term for it. thanx in advance

Share on other sites
You could calculate an offset x,y value for the object, and then simply position the object at the cursorpos+offset.

Also, you could try to keep the clicked object in a variable and keep it there until you release the button?

pseudo code:

if (objectSelected==0){objectSelected = getObjectUnderCursor();}objectSelected.pos = getCursorPos();

Share on other sites
Hmm.. I might have misunderstood your question.

There is a win API command SetCursorPos that does what you want. :)

Share on other sites
MMWizard    122

In my 3D viewport the user can select and drag a object during a mousedrag. therefor i have and need a speed variabele that keeps the object at the same position as the mousecursor. for now the object moves slower if the object is further away from the camera and faster if the object is closer to the camera.

Share on other sites
swiftcoder    18426
Quote:
 Original post by MMWizardwell, hi Rasmadrak,In my 3D viewport the user can select and drag a object during a mousedrag. therefor i have and need a speed variabele that keeps the object at the same position as the mousecursor. for now the object moves slower if the object is further away from the camera and faster if the object is closer to the camera.

I am going to assume for the moment that you are using OpenGL (but I am sure someone can translate this into D3D). So, during object picking, you presumably transformed the object position into screen space with gluProject() or similar. gluProject returns a 3-element vector, so the x and y elements are your screen position, and save the z element somewhere. When the user drags, form a 3-element vector from the new x and y, and the old z, and transform it back to world space with gluUnproject(), and use this as the new position for the object. This will move the object parallel to the surface of the screen, at the same speed as the cursor.

Share on other sites
ItsDan    104
Exactly. Don't try to move the object by some value that you calculate. Just figure out which direction the mouse pointer is pointing in, and put the object there.

Share on other sites
ItsDan    104
Exactly. Don't try to move the object by some value that you calculate. Just figure out which direction the mouse pointer is pointing in, and put the object there.

Share on other sites
MMWizard    122
hmm okay. thats to bad. Because i'm experimenting with the 3D-API from the Windows Presentation Foundation. And there is nothing like glu-project. And i thought it would'nt be a difficult calculation to perform. Maybe someone can help me where to look for the math involved for this particular translation issue.

By the way, Thanx for you're quick replys...

Share on other sites
swiftcoder    18426
Quote:
 Original post by MMWizardhmm okay. thats to bad. Because i'm experimenting with the 3D-API from the Windows Presentation Foundation. And there is nothing like glu-project. And i thought it would'nt be a difficult calculation to perform. Maybe someone can help me where to look for the math involved for this particular translation issue. By the way, Thanx for you're quick replys...

All you need is the combined model-view-projection matrix, which you multiply by the object position to get screen space, and its inverse, which you multiply by the screen space vector to get back to world space.

Edit: That should be object space, rather than world space. If you need world space, just use the view-projection matrix and its inverse.

[Edited by - swiftcoder on November 26, 2007 10:31:34 AM]

Create an account

Register a new account