Jump to content
  • Advertisement
Sign in to follow this  

How to implement drag and drop in Direct3D 9?

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

If you intended to correct an error in the post then please contact us.

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?



Edited by lucky6969b

Share this post

Link to post
Share on other sites

Hi jack. happy.png


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


Hope this can help you.


Share this post

Link to post
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.



Edited by lucky6969b

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!