PDA

View Full Version : drag and place 3d object



adityask
03-14-2002, 08:25 PM
hello,
i have done the coding for selection of an object from different objects present on screen. i have to drag and place it somewhere else with a mouse.how to code it?
can anyone help? i am using MFC with OpenGL.
thanx in advance.

Gorg
03-14-2002, 08:45 PM
Depends how you want to move your object, but assuming this :

when the mouse moves up or down, the object moves vertically parallel to the screen.

when the mouse moves left or right, the object moves horizontally parallel to the screen.

To do this, you only need a couple of informations.

1. Relative displacement of the mouse.
2. The Up and Right axis of the view.

When you got that, if your mouse as some horizontal movement you do :

objPos += mouseMovementx*scale*RightAxis

scale maps the screen mouse coordinates to your software display units.

Simply replace mouseMovementX by mouseMovementX and RightAxis by UpAxis to get vertical motion.

[This message has been edited by Gorg (edited 03-14-2002).]

adityask
03-14-2002, 11:45 PM
hey,
thanks for the info. but it is a 3d object.
any change in the code?



Originally posted by Gorg:
Depends how you want to move your object, but assuming this :

when the mouse moves up or down, the object moves vertically parallel to the screen.

when the mouse moves left or right, the object moves horizontally parallel to the screen.

To do this, you only need a couple of informations.

1. Relative displacement of the mouse.
2. The Up and Right axis of the view.

When you got that, if your mouse as some horizontal movement you do :

objPos += mouseMovementx*scale*RightAxis

scale maps the screen mouse coordinates to your software display units.

Simply replace mouseMovementX by mouseMovementX and RightAxis by UpAxis to get vertical motion.

[This message has been edited by Gorg (edited 03-14-2002).]

Gorg
03-15-2002, 10:28 PM
Originally posted by adityask:
hey,
thanks for the info. but it is a 3d object.
any change in the code?




No. My example was with 3d in my mind. You don't need the camera frame to move an object in 2d.

You define how you want your object to moves from the input that's all. I simply explained one case.

In my case, the object was simply moving in relation with the camera. And I simply defined my mouse moving left to move the object along the right axis of the camera and moving the mouse up with up axis of the camera.

You could also have picked moving the mouse up is going into the screen which would mean you move along the camera forward(direction) axis.


[This message has been edited by Gorg (edited 03-15-2002).]

adityask
03-16-2002, 01:10 AM
hey,
thanx again.but how to get the scale and axis information. Also, do u have any idea on how to move a selected object ie. with which function glPushMatrix etc.? in other words my question is how to move a single object without affecting the display of other objects on the screen?


Originally posted by Gorg:
No. My example was with 3d in my mind. You don't need the camera frame to move an object in 2d.

You define how you want your object to moves from the input that's all. I simply explained one case.

In my case, the object was simply moving in relation with the camera. And I simply defined my mouse moving left to move the object along the right axis of the camera and moving the mouse up with up axis of the camera.

You could also have picked moving the mouse up is going into the screen which would mean you move along the camera forward(direction) axis.


[This message has been edited by Gorg (edited 03-15-2002).]

Gorg
03-16-2002, 09:33 AM
Well the scale could simply be the ratio of viewvolume dimension over the screen dimension. This has some problem if you are using a perspective projection, because if your object is far away the ratio will be incorrect because the dimension are bigger and the object won't stay under the mouse. But it work correctly for an ortho projection.

Some co
ncrete numbers. Say you are using an ortho project with with following values :

left = -100, right = 100, top = 100, bottom = -100, near and far do not matter. The view volume dimension will always be :

horizontally abs( left - right ) = 200 in our case
vertically abs( top - bottom ) = 200 in our case

And your screen dimension are 1024 X 768. So our movement scale is horizontally = 200 / 1024 and 200/768.

Great, now if your mouse moves 300 units to the left and 100 units up.




objPos += ( cameraRightAxis*300*200/1024 ) + ( cameraUpAxis*100*200/1024 )


That is for an ortho projection. For a perspective projection, we need to adjust the scale depending on the distance of the object to the near plane. I won't put any number, it should become obvious now.
ViewFrustum = ( left, right, bottom, top, znear, zfar )




XScale = abs( left - right ) / XScreenDimension * ( distance( object, camera ) / znear )
YScale = abs( top - bottom ) / YScreenDimension * ( distance( object, camera ) / znear )



Now for the camera axis. By default opengl coordinate system is like this : direction = (0, 0, -1), up = ( 0, 1, 0 ) and right = ( 1, 0, 0 ) with position ( 0,0,0). when the camera moves, the position changes, when the camera rotate, the axis rotates. So you keep those values direction, up, right and position and you update them accordingly when you move or rotate your camera.