# How to implement drag and drop in Direct3D 9?

This topic is 1611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello and Happy New Year everybody,

I wonder how I can implement drag and drop function in Direct3D because

I am interested in building a Game editor...

So there would be a toolbox of primitives on the left hand side of the application,

Then I drag them onto the scene,

Is there any good readings on the web that you would recommend?

Also, when I "pick" one of several objects in the scene that are overlapping,

Do I loop thru them to get the closest object that is facing toward me?

Thanks

Jack

Edited by lucky6969b

##### Share on other sites

Hi jack.

I've had this exact problem myself, though I can't say it's fully solved, but, it's close.

So, the first thing you have to consider is that once you click the screen, you have to make some kind of world space ray, which could be done like this:

// PS. I always use the transposed versions of my view and projection matrices.

//getting projection matrix
D3DXMATRIX projMat = *Camera->getProjection();

float vx = (+2.0f*mouseX/viewport.Width  - 1.0f)/projMat._11;
float vy = (-2.0f*mouseY/viewport.Height + 1.0f)/projMat._22;

CEVector3f Origin(0.0f, 0.0f, 0.0f);
CEVector3f Direction(vx, vy, 1.0f);

D3DXMATRIX viewMat = *Camera->getView();
D3DXMatrixTranspose(&viewMat, &viewMat); // Transposing the view matrix (To the original one)

D3DXMATRIX iviewMat;
D3DXMatrixInverse(&iviewMat, 0, &viewMat);
D3DXVec3TransformCoord(&Origin, &Origin, &iviewMat);
D3DXVec3TransformNormal(&Direction, &Direction, &iviewMat);

D3DXVec3Normalize(&Direction, &Direction);


Now the next step can be optimized in many ways, but I'll present a simple way:

• Loop over all object
•     Does this objects bounding box/sphere collide with this ray?
•            Add it to my vector
• Loop over all objects in vector
•      Loop over all triangles in this object
•           Does the ray collide with this triangle
•                  Store the triangle and the distance (ray start to ray end)
• Then find the triangle for which the ray had to travel the shortest distance, and we assume that that's our users preferred drop position.

Now this is only one way of accomplishing this, but, it should work.

-MIGI0027

##### Share on other sites

Just wondering, in case sometimes I need to stack an object on the top of another or or placing thru the others

For example, a box on top of a sphere. Because the box overlapping the sphere

and on top of it are both valid, wonder which one should prevail,

Should I make up an option for the user to select which method they are using?

Sometimes, when I look at 3ds max, both of these two operations are valid.

The method they use is to draw a base, and dragging upward.

Thanks

Jack

Edited by lucky6969b

• 10
• 17
• 9
• 14
• 41