Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: easy picking

  1. #1

    easy picking

    hi! I was reading some of the picking tecnique, to see where the mouse is pointing in a 3d game.
    Now i've a very simple problem: to see in which tile the mouse is pointing.
    The tiles are laid horizontally on the x,z plane, so the problem is simply to see where the mouse intersect the horizontal plane..
    What's the easiest way to do this ? Thanks!
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  2. #2

    easy picking

    I have never tried working out how to do picking in OpenGL ( actually not done much at all in OpenGL ), but I found this article http://www.lighthouse3d.com/opengl/picking/ which seems to cover different techniques for doing it.
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  3. #3

    easy picking

    A more universal API independant way would be to shoot a ray from eye to mouse position and get the closest intersection with pickable objects
    bounding spheres(or other primitives if you really need them)

  4. #4

    easy picking

    I'm not sure how much help this will be. It is a function that I wrote several years ago just after starting here at Krome that helped to work out which 3D object was selected with the mouse. Basically, it works out the world coordinate on the near clip plane, the world coordinate on the far clip plane, then casts a ray between those points from near to far. If the ray collides with an object, it returns the world position and normal of that collision point. It could also return the object that it collided with if it was extended a bit more.

    BG was the name of our proprietary engine back then. BGU was my little utility library for common functions. Yes, it's in C.

    vpRight and vpBottom were the right and bottom edges of the viewport, in this case, the width and height of the viewport.

    Code:
    //---------------------------------------------------------------------------
    //  Function&#58; BGU_ProjectScreenToWorld
    //  Purpose&#58;  Projects a ray from the screen coordinate into the world and
    //            checks for a collision
    //  Input&#58;    sx, sy      Screen coordinate
    //            world       Pointer to a vector that will receive the world
    //                        coordinate of the collision point
    //            normal      Pointer to a vector that will receive the normal of
    //                        the polygon that was involved in the collision. Can
    //                        be NULL if no normal is required
    //  Output&#58;   int         0 if no collision, !0 if a collision
    
    int BGU_ProjectScreenToWorld&#40;int sx, int sy, Vector *world, Vector *normal&#41;
    &#123;
      Vector nearPoint, farPoint, farView;
      float px, py;
      float fovx, fovy;
      BG_GetFOV&#40;&fovx, &fovy&#41;;
    
      // Get near point in worldspace
      px = &#40;&#40;&#40;float&#41;sx / &#40;float&#41;vpRight&#41; * 2 - 1&#41; * &#40;nearPlane + 1.0f&#41; / fovx;
      py = &#40;&#40;&#40;float&#41;sy / &#40;float&#41;vpBottom&#41; * 2 - 1&#41; * &#40;nearPlane + 1.0f&#41; / fovy;
    
      BG_WorldPoint&#40;px, py, &#40;nearPlane + 1.0f&#41;, &nearPoint.x, &nearPoint.y, &nearPoint.z&#41;;
    
      // Get far point in worldspace
      px = &#40;&#40;&#40;float&#41;sx / &#40;float&#41;vpRight&#41; * 2 - 1&#41; * &#40;farPlane - 1.0f&#41; / fovx;
      py = &#40;&#40;&#40;float&#41;sy / &#40;float&#41;vpBottom&#41; * 2 - 1&#41; * &#40;farPlane - 1.0f&#41; / fovy;
    
      BG_WorldPoint&#40;px, py, &#40;farPlane - 1.0f&#41;, &farPoint.x, &farPoint.y, &farPoint.z&#41;;
    
      // Now we have the near point and the far point, test for collision between them
      if &#40;BG_SurfaceHit&#40;WORLD_OBJECT, nearPoint.x, nearPoint.y, nearPoint.z, farPoint.x, farPoint.y, farPoint.z&#41;&#41;
      &#123;
        if &#40;world&#41;
          BG_GetLastPoint&#40;&world->x, &world->y, &world->z&#41;;
        if &#40;normal&#41;
          BG_GetLastVector&#40;&normal->x, &normal->y, &normal->z&#41;;
        return 1;
      &#125;
      else
      &#123;
        return 0;
      &#125;
    &#125;

  5. #5

    easy picking

    Thanks everybody!
    I only need some really basic picking, ie the intersection with the y panel.
    I think Sly function may help.. if i have the line that's beneath the mouse, i can do a line/plane intersection.
    The problem is that that function is not so clear without the called functions
    For example the how the BG_GetFov work ? and the BG_WorldPoint ?
    Also, where the nearPlane and farPlane came from ?

    Thanks
    If you save your data in a proprietary format, the owner of the format owns your data.
    <br /><A href="http://msx80.blogspot.com">http://msx80.blogspot.com</A>

  6. #6

    easy picking

    BG_GetFOV(): Returns the current field of view.
    BG_WorldPoint(): Transforms the point from local (view) space back to world space. This is an inverse of the world-to-local transformation.
    The near and far plane came from another function in my BGU library (BGU_SetClipPlanes).

    As long as you know what you have set your fov, near and far planes to, you should be able to use those values in this function.

  7. #7

    easy picking

    I'm thinking of doing it this way:

    1) Use gluUnproject to transform window coordinates (winx, winy, winz) to object coordinates (objx, objy, objz). Use it twice: once with winx = 0.0 for near plane; and once with winz = 1.0 for far plane. Subtract these two points from each other to retrieve a ray vector.

    2) Use the ray vector to test for intersection with each selectable object in your frustum. There are plenty of sources for intersection algorithms on the net and in books. Use the nearest intersection (i.e. the closest object) for your selection.

    I haven't tried it yet, though. Maybe I'll try it tomorrow and post some code here. It looks interesting enough to warrant the effort.
    [size=10px]&quot;In science one tries to tell people, in such a way as to be understood by everyone, something that no one ever knew before. But in poetry, it&#39;s the exact opposite.&quot; -- Paul Dirac[/size]

  8. #8

    easy picking

    That's basically what my function does.

  9. #9

    easy picking

    There's a new article on picking over @
    http://www.bookofhook.com/Article/Ga.../mousepick.pdf
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  10. #10

    easy picking

    If you understand all your mathematical equations, that article may be of some assistance. It left me dumbfounded, primarily because I do not understand mathematical equations. There is no code at all, not even pseudo-code. Give me code in an article, then I'll understand it.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •