Moving objects with the mouse in 3D

Started by
10 comments, last by steven katic 15 years, 7 months ago
Hi, I assume no one is going to give the complete code how to move objects with the mouse in 3d space so I ask you at least for the necessary steps. I'd like to move something that the user picked but it has to move exacly the same as the mouse cursor, assuming the view is at any random angle. Axes will be constrained manully. Can someone help me please, and tell me what I should do?
Advertisement
This is how I would do it:
1. Determine which object was picked using ray-bounding shape intersection.
The origin of the ray is the camera position and the direction is determined by the mouse cursor.
2. Unproject the mouse coordinates the get back a 3d position and tell your object to move there.

When using gluUnproject, you supply it 3 coordinates in windows space and it returns a position in object space. obviously the x and y come from the mouse cursor. To get a direction for the first step you would unproject at to spots say z=0 and z=1 and subtract between them. For the second step you will have to specify a z value( either by projecting a known point to get it's window coordinates z value or using some vector math but it depends on your situation)

Hope this helps.
Movement exactly like the mouse pointer means that a particular point of the moved object is to be constrained to a plane that is parallel to the view plane. For that point the co-ordinate frame origin of the object is fine.

So, prepare dragging by computing the object's frame origin in camera local space. In that space you are allowed to alter the horiziontal and vertical co-ordinates, but not the depth co-ordinate (in fact the restriction to the said plane). Use the mouse pointer position, view size, and projection mode, to compute the missing horizontal and vertical co-ordinates. You will presumbly need to transform the resulting position back into the global frame.
Simple case, the camera is looking down along the z-axis, I then presume you would move the object in a XY-plane somewhere. So what you need is:
- the distance from that plane to your camera, in this case the distance of the object along the z-axis.
- You also need to know your projection matrix, for example if its a glu perspective thingy you would have a sort of pyramid with a base angle along one plane corresponding to the field of view you put in and the other angle = asin(ratio * sin(fovAngle));

With this you can calculate the size of the projection pyramids base at that z-value and map that to the screen.

if ratio = 1. fovAngle = 45.

2*(zDistance * cos(fovAngle/2)) = pW (width (and height) of the pyramids base.)

pW/screenWidth now gives some ratio for how many distance units in openGL moving the mouse a certain amount of pixels.

Warning, i wrote this quickly, may be wrong :)
Asking for help in forums is a good exercise in honing you skills at giving precise/unambiguous descriptions. For example:

haegarr says:
Quote:
Movement exactly like the mouse pointer means that a particular point of the moved object is to be constrained to a plane that is parallel to the view plane.


That might be one meaning. But I think ebody has a 3d editing tool type implementation in mind; and probably means he wants the model to follow the user's mouse pointer while still being constrained to a particular set of axes or a plane in the global 3D space: similar to how you can move objects in Max or Maya.

Would that be right ebody?
Gee you ask hard questions (I have some spare time).

I reckon that would be a great programming competition question too.
There is next to no documenation around on it, you could think it's too trivial to bother with or it is too complex to bother with, but in reality it's more of a very specialised domain. There are limited versions of it, with perhaps the stuff you see in Max and Maya being the most sophisticated.

Wouldn't you want to map the viewport plane down onto the 3D world plane (that movement is contrained to at the time e.g. XY Plane)? Like a plane(s)/movement version of the virtual trackball concept (that is popular for rotating objects).
thank you for your replies, I'll try to implement them in my app right away.

I'll let you know if I managed to do it and if necessary I'll ask more questions.


Oh, I've got already one question, what values should I actually use for the third parameter in the gluUnproject function? Can I use any value or 0 and 1 like @CableGuy suggested. What meaning they have and what will happen if I take for instance 0.3 or evern 226?



@steven katic, exacly, I mean moving objecs like in Maya or Max, when an object is faster or slower then the mouse pointer it's pretty easy and that's what I've got now.

[Edited by - ebody on September 10, 2008 2:07:55 AM]
Quote:
@steven katic, exacly, I mean moving objecs like in Maya or Max, when an object is faster or slower then the mouse pointer it's pretty easy and that's what I've got now.


well that might sound like you're half way there.
How did you implement what you have now? i.e. What kind of variables do you use to get the mouse pointer to currently move an object? It may be just a case of extending your current implementation.
since my last post I've made some changes in my app and now I'm using the gluUnProject function with the 3rd (z param) 0.3 (don't ask my why, I've just seen it somewhere) besides I use an additional API for vector calculations, anywhay, I've got this now: (slighly changed to make it more clear, it's c++ but I use some kind of pseudocode here)

here I retrieve mouse coordinates translated to the object coordinates:

Mouse2Modelview(old_mouse_pos, [out] mousePos_old);
Mouse2Modelview(new_mouse_pos, [out] mousePos_new);

then using another API I calculate the movement vector:

CINSVector movemet = mousePos_old.objectCoordiantes - mousePos_new.objectCoordiantes;

finally using the same API I set the translation (x-translation in this case):

transformMatrix.SetTranslation(movemet.x, 0.0f, 0.0f);

if I move the mouse pointer along the x axis (I have something like this RGB arrows for moving in maya) the object moves a little bit faster but not as fast as before and in the opossite direction if I rotate the scene 180 degrees

hmm, and if I tried to set the mouse pointer to the new object's position it wouldn't be faster any more, but the I had to project the object coordinates back to windows coordinates, could it work? then the mouse pointer would be snapped to the move-arrow of the "trackball"

model1en4.th.jpg

http://img98.imageshack.us/my.php?image=model1en4.jpg

strange, no preview availible :(

this is how I understand it, on the right side (screen) I've got the mouse coordinates (XY) that are translated by the gluUnProject function into model coordinates on the left side

if the screen is paralell to the Y-axis the movement of an object is the same as that of the mouse pointer (the right red line) but if the screen (camera, eyes) are at some angle to the Y-axis the value of the translation is the orange line although the distance computed for the translation comes frome the model coordinates

(let's also say that the buttom end of the red an orangle lines is the start position of the mouse pointer and the upper end is the end of the movement)

[Edited by - ebody on September 10, 2008 5:43:59 AM]
Take a look at this:



OK, then...I don't know how to refer to images either...
its here: http://img183.imageshack.us/my.php?image=moveobjectsmalad4.jpg

Looks overly contrived?...probably because it is.

Here's the context:
It's a form a collision detection we want to do (which is usually a staple ingredient of games). And we want our interaction to be 'realtime'; i.e very responsive to the users mouse. All other similar examples of this type of interaction are based on some sort of reference space or object, be it visible
(such as a terrain in a game that is clicked on with the mouse so that an object moves to that position), or virtual (like the virtual track ball used to rotate objects in the 3D space).

So we can take one more little step and apply these well known concepts to the problem (Not for any special reason...except maybe for its initial simplicity..and it's interesting?). I am pretty sure this can be done at a lower level by using a bunch of matrix manipulation concepts if you like.

So, back to the pretty little picture and a bit of explanation.

It's a snapshot of moving the (cube) object from point B to point I in the 3D space, which to the user (looking thru the camera/viewport on the computer screen) will look like the object is following the mouse pointer from point A to E.

to be continued...(be back soon).

[Edited by - steven katic on September 11, 2008 4:33:51 PM]

This topic is closed to new replies.

Advertisement