Mouse Hit Testing when primatives used not meshes

I did search the existing threads and have read them - those that I can understand - but they do not seem to apply - at least with my limited understanding.

I have a "2D" card game started in DirectX3D using managed C#. I read Tom Miller's book as the basis and starting point.

I have several "playing" cards rendered as CustomVertex.PositionedNormalTextured that I add to a VertexBuffer. I draw them with the DrawPrimatives call. The are all "flat" (have the same Z axis) so that should help, and there is nothing "behind" them. Simply non-overlapping cards arrange on the screen.

Everything works great for my simple needs, but the code sample here for detecting mouse hits on 3D objects uses the Mesh object Intersect method. I have the mouse working the old fashion way of hardcoded bounds checking in 2D pixels, but I would like to learn the proper way for 3D.

Any help would appreciated. I am very new to C# and DirectX3D, and math challenged, so please be as verbose in explaining as possible!

Thanks in advance!!




Answer this question

Mouse Hit Testing when primatives used not meshes

  • sgt_b

    Yes sir, I understand I could write this game much easier in C++ not using DirectX at all (or something else). However, I am trying to improve my C# / .NET skills, learn some basic level DirectX3D things and create a "fun" application at the same time.

    I'm not under any false illusions that I can create an eye-popping game with particle effects, and so on. I was just hoping it might be a litte easier!

    Thank you for the support and ideas!



  • TheCompWiz

    Hey, someone who sounds like me. ;)
    If you use MSN would you be interested in hooking up with me We might be able to help each other.

    Particle effects aren't acually that hard to create, just try rendering vertices as Points.


  • alok yadav

    Yes, I have a card class that draws a single card, textured front and back, and handles things like flipping the card, removing it from the game, etc.

    I copied some VB code from this link in one of the threads discussing this. http://dotnetforums.net/showthread.php t=88480 I have changed it to C#, but I have no real understanding of what it does yet. The part I could not reconcile is the "If mesh.Intersect(vNear, vDir, ClosestHit) = True Then" portion. I do not see an equivalent method for vertices.

    This is my first 3D application. I read Tom Miller's book and originally started this application in 2004, so I have lost much of what I had read since then. I apologize for being so dense, but I learn by seeing not by reading. So please forgive me some basic and probably dumb questions.

    If I have a single routine that processes mouse clicks, and I create a vector as you mention (x,y, near plane) - (x,y, far plane) wouldn't it need to be a full polygon (3 verticies ) Does it matter what type of vertex I create

    Do I have to add it to the Vertex buffer Do I have to actually render it

    Thanks for the help!



  • Avishay

    Thank you. That give something else to learn about!

    I have successfully created a mesh on the fly from the vertices I have. I am going to retrofit my card class to represent the cards as a mesh so that I can use the intersect method which is readily supportable by the folks here.

    It made my whole day once the light bulb went on () and the mesh rendered on the screen! Thanks for bearing with me everyone!



  • -Sachin

    Don't forget to look at the "Pick" sample that's in the DirectX Sample Browser. Althought it's in the C++ part of the samples, you should be able to get the general idea of how to do picking in 3D with DirectX.

  • Veniamin

    If you have it working with the 2d coordinates then to be honest keep it that way. The best and fastest and proper way for anything that is 2d is the way you are doing it. Especially for rectangular objects like you have.

    but for 3d....

    I assume you have a single card in a vertex buffer that you draw 52 times by varying the world transform

    The coordinates of the card are object coordinates. The GPU multiplies these by the world matrix to give the world coordinates.

    Then the GPU multiples this by any view matrix you may have (this usually represents the camera) giving view coordinates

    Then it multiples this by the projection matrix giving screen coordinates - the it draw the triangles to the screen.

    As you can imagine the final screen coordinates are 2d (plus a depth for the zbuffer) and are very different from the coordinates in your original vertex buffer.

    Your mouse is in screen coordinates so to see if you have hit a card you have to take the screen coordinates and create a vector going into the screen from the mouse. (x,y,-1000) - (x,y,1000) for example. Then you take this vector back through the inverse of the 3 matrices I mentioned above. This is called unprojecting. When you have done that you will have the mouse or 'pick' vector in object space the same as your vertex buffer and you can use the intersect methods to see if it hit anything.



  • DanielClaici

    One of my issues is I have a day job, leaving me very little time to actually devot to learning it. Because I normally need to sit down with someone already experienced so I can ask question real-time, it makes it hard.

    I also have figured out I need to ask one question at a time on the forums or the major question gets missed in the rush to answer the easy one.

    So my original question still remains unanswered. If I do not have a mesh object (no intersect method) how do I do the hit test Assuming I unproject the vertice correctly, how do I test the intersection

    It's all good....



  • Brehnan

    Y'know...if all you're doing is building simple "card game" applications, you might be better off creating the game using Windows Presentation Framework. If you do decide to take that approach, you might find faster answers to your questions on this forum area: http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=119&SiteID=1


  • AmJS

    Thank you. I will take a look at it.



  • Amherst

     

    I'm probably hopeless at this point!   The code in the Pick example is way beyond my ability to comprehend.  The removal of DrectDraw is probably the end of my simple game writing career.

    Can I build a "mesh" dynamically from the vertex in a VertexBuffer, then call intersect   I just don't see an "intersect" type method available without having a mesh object.

    This is one of those times I wished that an experienced game author lived near me!

    Thanks for all the attempts to help!

     Here is a link to a small image of what the game screen looks like: www.JesusTaxi.com/mikel/game.jpg

     



  • Ferenc Toth

    Apologies... we all apprantly missed that part of the question.

    If you don't have a mesh you will have to walk through each triangle in your vertex buffer and use the Geometry.IntersectTri function

    http://msdn.microsoft.com/library/ url=/library/en-us/directx9_m/directx/ref/ns/microsoft.directx.direct3d/c/geometry/m/intersecttri.asp



  • Mouse Hit Testing when primatives used not meshes