VB.NET/Direct X 3D object selection using the mouse click.

I realize that I'm in the wrong forum, but there dosn't seem to be one for non-game applications that use Direct X. I'm not a C++ programmer. I've used Visual Basic and VB.NET to write applications that display objects in 3D. I want to able to allow the user to select one of several 3D objects via the mouse. Can someone supply some sample code in VB or VB.NET that does this.

Thanks in advance.



Answer this question

VB.NET/Direct X 3D object selection using the mouse click.

  • Jeffrey.Ye

    Probably not the cleanest implementation but I found the code hanging around on my harddrive and I thought it might help you.
    Here is the core of the Intersection. It will return bool if a mesh was selected.


    public bool Intersect(Microsoft.DirectX.Direct3D.Device device, Mesh mesh)
    {
    //intersection information
    IntersectInformation closest;
    //the near vector
    Vector3 near;
    //the far vector
    Vector3 far;
    near = new Vector3(cursor.X, cursor.Y, 0); //the mouse coordinates
    far = new Vector3(cursor.X, cursor.Y, 1); //the mouse coordinates

    near.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, entity.MatWorld);
    far.Unproject(device.Viewport, device.Transform.Projection, device.Transform.View, entity.MatWorld);

    far.Subtract(near);
    if(mesh.Intersect(near, far, out closest))
    return true;
    return false;
    }



    This method just makes sure that the coordinates you get for the mouse is perfect. Method name is self explanatory.
    [code language = "c#"]
    private void ValidateMouseCoordinates(Form window)
    {
    //Computes the screen space coordinates to client coordinates
    cursor = window.PointToClient(Cursor.Position);
    if(cursor.X < 0) //if the x position is less than 0. set it to the window width
    cursor.X = window.Width;
    if(cursor.Y < 0) //if the y position is less than 0. set it to the window height
    cursor.Y = window.Height;
    }
    [/code]

    I hope this helps.
    Take care.


  • BennyAndGisela

    The Pick sample in the SDK does this. To expand it to multiple meshes, just repeat the ray intersection determination for each mesh that you want to check.


  • learning_to_code

    Which SDK I was not aware of any VB examples for DirectX.
  • dhtroy

    The Feb 06 SDK contains c++ version, but indeed no managed version. Sorry!

    I still recommend looking at it's source, as the concepts are the same in both managed and unmanaged dx, even though the syntax differs somewhat.



  • VB.NET/Direct X 3D object selection using the mouse click.