[SOLVED] Move 3D object with 2D mouse

Started by
21 comments, last by donlucacorleone 14 years, 6 months ago
Quote:
It's a simple matter of determining a point in 3D space

Yes! I'm asking how can I find the (x,y) 3D world coordinates from a given 2D mouse point and a given 3D Z value...

[input]
2D mouse point
Z value
[output]
The 3D World coordinates in which I've to put my object, so the user sees it under the mouse

The code you've posted is right (only now I understand it).
POINT mousePt;D3DXVECTOR3 v0, v1;D3DXVECTOR3 rayPos, rayDir;v0 = D3DXVECTOR3(mousePt.x, mousePt.y, 0);v1 = D3DXVECTOR3(mousePt.x, mousePt.y, 1);D3DXVec3Unproject(&rayPos, &v0, &vp, &proj, &view, &world);D3DXVec3Unproject(&rayDir, &v1, &vp, &proj, &view, &world);rayDir -= rayPos;D3DXVec3Normalize(&rayDir,&rayDir);// rayDir is a vector in 3D world from eye point to infinity// define D3DXVECTOR3 wPos = rayPos+factor*rayDir with wPos a point in the plane// you want to solve for factor// wPos.z = rayPos.z + factor*rayDir.z// wPos.z = objectZCoord // since it's in the plane// CHECK FOR RAYDIR.Z==0 to avoid division by zero// This happens when you're looking parallel to the plane// factor = (wPos.z - rayPos.z)/rayDir.z// wPos.x = rayPos.x + rayDir.x*factor // desired x value// wPos.y = rayPos.y + rayDir.y*factor // desired y value


The issue is related to that damned "worldMatrix" :-).
Really, I don't understand why I've to put an Identity matrix instead of the matrix that describes the world in which I'd put the object.

Please be patient with me, I'm only an unexpert DirectX developer and all these stuff are getting me crazy.

Again, thank you for your help.
Advertisement
First, your object has nothing to do with the calculation of the point (except that's where you got the z-value).

The identity matrix is the world matrix into which you put your object. The object or model matrix moves the object within that world. Don't think of the model matrix as a "world" matrix. It describes where the model's local space is located in the world.

One way to think of it (until you understand the math better):

When you created your object, you created it at (0,0,0), at the origin of the world. If you used an identity model matrix, that's where it would be located. But you didn't want it located at the origin, so you specified a location in the world described by the model matrix.

To pick an object, you create a ray in the world space. Then you determine if it intersects an object located in the world at a particular location (described by the model matrix).

Now that you've located a point on the object in the world, you want to find another point in the world near that location in the world because the mouse moved. At this point, it doesn't matter how you found the location. So you do the calculation of the new point in the world near the old one.

You have a new location and you move the model in the world based on that result.

[Edited by - Buckeye on September 25, 2009 10:35:38 AM]

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Quote:Original post by Buckeye
First, your object has nothing to do with the calculation of the point (except that's where you got the z-value).

The identity matrix is the world matrix into which you put your object. The object or model matrix moves the object within that world. Don't think of the model matrix as a "world" matrix. It describes where the model's local space is located in the world.

One way to think of it (until you understand the math better):

When you created your object, you created it at (0,0,0), at the origin of the world. If you used an identity model matrix, that's where it would be located. But you didn't want it located at the origin, so you specified a location in the world described by the model matrix.

To pick an object, you create a ray in the world space. Then you determine if it intersects an object located in the world at a particular location (described by the model matrix).

Now that you've located a point on the object in the world, you want to find another point in the world near that location in the world because the mouse moved. At this point, it doesn't matter how you found the location. So you do the calculation of the new point in the world near the old one.

You have a new location and you move the model in the world based on that result.


Now I really understand!
And now my routine works as expected!

Thank you very much Buckeye!

This topic is closed to new replies.

Advertisement