## Doing mouse drag on 3d objects accurately

### 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....

### 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....

The mouse forms a ray between your eye and the world.  Calculate
where this ray intersects some plane in your world and place your object
there.

Marc Hernandez

### Doing mouse drag on 3d objects accurately

I need a couple more pointers.....I assume the middle of the screen is the
camera position....so if the cursor is at the middle i can project a ray.
But how do i project a ray when the cursor is not in the middle.

Also how do i determine which plane is most accurate to place my model
against...I guess that this plane must be pependicular at the point where
the camera ray hits it....

Thanks

Quote:> The mouse forms a ray between your eye and the world.  Calculate
> where this ray intersects some plane in your world and place your object
> there.

> Marc Hernandez

### Doing mouse drag on 3d objects accurately

: I need a couple more pointers.....I assume the middle of the screen is the
: camera position....so if the cursor is at the middle i can project a ray.
: But how do i project a ray when the cursor is not in the middle.

The middle of the screen is actually:
Pc : camera position
La : camera vector (where the camera is pointing)
F  : front clipping plane distance

Pc+La*F

The details of what ray the mouse<->camera pos forms is dependant
on the API you are using.  The maths are fairly simple.  Take your matrix
that take World->screen and invert it.  This matrix * your mouse point is
the point is world space.  This point - camera point is the ray vector
(you will probably want to normalize this). The camera point and this
vector form the ray.

With this ray you can do a lot.  Intersect with arbitrary planes
or objects (although for many objects you will probably want to put the
ray into object space).

: Also how do i determine which plane is most accurate to place my model
: against...I guess that this plane must be pependicular at the point where
: the camera ray hits it....

Interesting question.  I project down to the y=0 plane.

Marc Hernandez

### Doing mouse drag on 3d objects accurately

Quote:> I need a couple more pointers.....I assume the middle of the screen is the
> camera position....so if the cursor is at the middle i can project a ray.
> But how do i project a ray when the cursor is not in the middle.

If (x,y) is the device coordinate point under the cursor, then:

GLdouble x, y, nx, ny, nz, fx, fy, fz, vecx, vecy, vecz;
GLdouble m[16], p[16];
GLint vp[4];

// First, get m, p, and vp using glGet functions
...

// Now compute the ray
gluUnProject(x,y,0.,m,p,vp,&nx,&ny,&nz);
gluUnProject(x,y,1.,m,p,vp,&fx,&fy,&fz);
vecx=fx-nx; vecy=fy-ny; vecz=fz-nz;

(vecx,vecy,vecz) is the ray under the mouse cursor pointing away from the
viewer.

BTW, that code is off the top of my head. I wouldn't be surprised if it
doesn't compile as-is. But the concept should be right. Just unproject the
point under the cursor at the near and far planes, and compute a ray from
the unprojected values.

Quote:> Also how do i determine which plane is most accurate to place my model
> against...I guess that this plane must be pependicular at the point where
> the camera ray hits it....

This is application dependant. Any plane will work. Which plane do you want
your object to move along in response to user input? Use that plane. It
doesn't have to be perpendicular to the ray.
--

Hewlett Packard Technical Solutions Lab
OpenGL FAQ: http://www.frii.com/~martz/oglfaq

### Doing mouse drag on 3d objects accurately

Alritey......a thank you note for the nice folks who helped.
Really appreciated :)

Cya

### Doing mouse drag on 3d objects accurately

sure, you may want to keep the mapping linear. In that case, 1 unit in
window coordinate might mean 0.1 in object coordinates. You can adjust
those numbers, and even find an equation to make it non linear.

easy enough

V-man

> 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....

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....