'cursor moves faster then object' problem...

Started by
7 comments, last by swiftcoder 16 years, 4 months ago
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
Advertisement
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();
"Game Maker For Life, probably never professional thou." =)
Hmm.. I might have misunderstood your question.

There is a win API command SetCursorPos that does what you want. :)
"Game Maker For Life, probably never professional thou." =)
well, 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.
Quote:Original post by MMWizard
well, 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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.
-----------------------------------------------“The best, most affordable way to save the most lives and improve overall health is to increase the number of trained local, primary healthcare workers.”Learn how you can help at www.ghets.org
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.
-----------------------------------------------“The best, most affordable way to save the most lives and improve overall health is to increase the number of trained local, primary healthcare workers.”Learn how you can help at www.ghets.org
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...
Quote:Original post by MMWizard
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...


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]

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement