Dragging Objects in 3D world...

Dragging Objects in 3D world...

Post by a.m. » Mon, 30 Jun 2003 02:06:08



Hi there.

I have a Question reguarding how to drag shapes in a 3D world.

I have methods that allow shapes to be "picked" properly but
I need to know how to calculate the NEW position of a
selected (picked) object .

Im posting a sample code for the translation method (x-y plane only).
It works but its not accurate and thats why im here :)

/*
    incx and incy are the amounts taken from the Mouse
    position minus Old mouse position , after an object has
    been picked and the mouse has moved.

    A CObject3D instance is an object oriented shape
    and cx, cy ,cz are its center coordinates.

    WorldCenter[2] : world center position on z axis
*/

void Translate(float incx, float incy)
{
    if(toolkit.m_SelectedObject3D != null )
    {
        float w = surface.Width; //display size
        float h = surface.Height; //display size
        float vx = 0;
        float vy = 0;
        CObject3D obj = toolkit.m_SelectedObject3D;
        vx = -incx * ((obj.cz + surface.WorldCenter[2]) / w);
        vy = -incy * ((obj.cz + surface.WorldCenter[2]) / h);
        obj.cx -= vx;
        obj.cy -= vy;
    }
    surface.RePaint();

Quote:}

ps : this sample is from a C# wrapper for OpenGL i wrote and its
free to try here : www.headbits.com
 
 
 

Dragging Objects in 3D world...

Post by Alex Mizrah » Mon, 30 Jun 2003 19:19:21


Hello, a.m.a!
You wrote  on Sat, 28 Jun 2003 13:06:08 -0400:

 aa> I have a Question reguarding how to drag shapes in a 3D world.

 aa> I have methods that allow shapes to be "picked" properly but
 aa> I need to know how to calculate the NEW position of a selected
 aa> (picked) object .

 aa> Im posting a sample code for the translation method (x-y plane
 aa> only).
 aa> It works but its not accurate and thats why im here :)

using following code you can get object coordinates that correspond to
window coordinates(mouse coordinates). here `proj` is projection matrix,
`modelview` is modelview one, if you need eye coords (if your object has
identity before it's own xform is applied) use identity for `modelview`.
mouse coords are _state->x and _state->y here. `depth` specifies x-y plane.

  int vwprt[4];
  glGetIntegerv(GL_VIEWPORT,vwprt);
  double winx,winy,winz;
  gluProject(0,0,depth,(double*)&modelview
,(double*)&proj,vwprt,&winx,&winy,&winz);
//this is only needed to get winz that corresponds to depth

  double objx,objy,objz;

gluUnProject(_state->x,_state->y,winz,(double*)&modelview,(double*)&proj,vwp
rt,&objx,&objy,&objz);
  FPos.x=objx;FPos.y=-objy; //y is flipped because of different origins
                //of OpenGL and windows coord systems
FPos.z=objz;

so, to perform dragging you should:
1) when object is first clicked record its z coord as depth, and calculate
position that corresponds to that mouse position. calculate delta values
between mouse and object origin:
delta_x = ox - mx; delta_y = oy - my;
2) when mouse is moved to recalculate objects position, unproject mouse
position and add delta_x and delta_y to coordinates.

this should be 100% accuarate, i think.

With best regards, Alex Mizrahi aka killer_storm.

 
 
 

1. Doing mouse drag on 3d objects accurately

Hi does anyone know how to make dragging of objects accurate....The problem
I'm facing is that  the object moves but it doesn't stop directly under the
mouse cursor.....Probably because an object in 3d space when mapped to 2d
screen coordinates varies  when the camera distance changes or perspective
changes.
So I like to find out if there is a way of mapping each screen X,Y into
world X,Y....

Thanks in advanced

2. batch printing Illustr for Mac

3. Dragging an object into a 3D scene.

4. Lets compare terrain engines

5. Doing mouse drag on 3d objects accurately

6. Newbie question about glViewport.

7. LW MIDI input

8. Object culling in 3d world?

9. Moving objects across floor of 3D world

10. Help: Rotate 3D object using World Axis...

11. Picking objects in 3D world

12. Selecting objects in a 3D World