CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2009
    Posts
    3

    How to know if camera is looking at object?

    i am working on a 3D flight simulator game that is based on the 3DVU engine.

    I have created 3D objects in my game, which are cut-in-the-center cubes meant to be flown through as a highway in the sky, which float around the world that I have. I have control over the camera viewing the world and the objects.

    What I am currently trying to do is to have the ability to move the mouse over an object on the screen of the game and to be able to click on them and drag them around.

    Before I get to the dragging part, I need to know when the mouse is over the object.

    I have a 3d vector space where I know the 3d vector position of the object and of the camera. I understand that I could use something called "Ray Tracing" in order to figure out if the camera is looking at the object, or if the object is on screen. I found alot of websites about ray tracing, but I understood those were more about for rendering, not for figuring out if there's just an intersection.
    I've also been told that I should mark my objects in a bounding box, using two positions of the objects (minimum and maximum vector points) to define the bounderies.

    I am confused as how am I supposed to work with ray tracing to figure out if the object is inside the camera's view, or is in the center of the camera's screen.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to know if camera is looking at object?

    Are you sure the engine doesn't has some sort of detection function for that ? I've been playing with the irrLicht engine and it has functionality on board for that kind of stuff.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to know if camera is looking at object?

    That's one way of doing it, but there's an easier way. (EDIT: Easier than ray-tracing. Probably not easier than whatever's built into the engine.)

    Define an alternate rendering mode, where there's zero lighting effects, shadows, etc. Each draggable object should be rendered in a constant, flat color. That color should be computed based off an identifier for the object, say for instance 1 -> (0, 0, 1/256.0), 255 -> (0, 0, 255/256.0), 256 -> (0, 1/256.0, 0), etc.

    Then whenever the mouse is clicked, you can render just a small area around it (or even just the one pixel if you like) to an offscreen buffer (the window's back buffer should be good enough), read back the result into main memory, and simply check which color is present to determine the object ID.

  4. #4
    Join Date
    Sep 2009
    Posts
    3

    Re: How to know if camera is looking at object?

    Quote Originally Posted by Lindley View Post
    That's one way of doing it, but there's an easier way. (EDIT: Easier than ray-tracing. Probably not easier than whatever's built into the engine.)

    Define an alternate rendering mode, where there's zero lighting effects, shadows, etc. Each draggable object should be rendered in a constant, flat color. That color should be computed based off an identifier for the object, say for instance 1 -> (0, 0, 1/256.0), 255 -> (0, 0, 255/256.0), 256 -> (0, 1/256.0, 0), etc.

    Then whenever the mouse is clicked, you can render just a small area around it (or even just the one pixel if you like) to an offscreen buffer (the window's back buffer should be good enough), read back the result into main memory, and simply check which color is present to determine the object ID.


    What if I have several obejcts which are of the same color?

    I don't understand why I need to use a rendering method in order to figure out if I'm looking at the object. This has nothing to do with the lightning, shadow or other stuff. My objects are given simple no-alpha textures.

    I don't think I've mentioned this, but the objects are floating in a world with a terrain built out of images, so scanning this way could 'cause getting wrong info (the terrain).


    I am not sure what other kind of info I should give. There is no method built in to detect what I am asking for yet, I need to build it myself.

  5. #5
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: How to know if camera is looking at object?

    All your elements in your world are stored somewhere in memory (classes/structs etc.)... isn't there a way to simply look at those coordinates and relate them to your own coordinates ?

  6. #6
    Join Date
    Sep 2009
    Posts
    3

    Re: How to know if camera is looking at object?

    Read the right book and got the correct answer -

    I was thinking graphics instead of mathmatics.

    The thing I am looking for is Ray and AABB 'INTERSECTION', and I've coverd most of the principels of these math subjects in HS, so I think I'm good.

    If someone is curious regarding how Ray and AABB intersection works, I'll be happy to explain, but in general, this topic is no longer relevant for me. Thanks for the help anyway.

    The book I used was - 3D Math Primer for Graphics and Game Development by Fletcher Dunn and Ian Parberry

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How to know if camera is looking at object?

    Quote Originally Posted by Superhornet View Post
    What if I have several obejcts which are of the same color?
    You don't understand. The visual appearance of the object has zero to do with the scheme I'm describing. Each object has two rendering modes---it's "real" color, which is what it looks like when rendered on the screen, and a "fake" color which you assign it for purposes of identifying what the mouse clicked on. It's impossible for two objects to have the same fake color because you simply don't assign it that way. Object 1 has color 1, object 2 has color 2, object 100 has color 100, etc.

    The fake colors never show up on the screen, either. They're only to be used for an offscreen render.

    I don't understand why I need to use a rendering method in order to figure out if I'm looking at the object. This has nothing to do with the lightning, shadow or other stuff. My objects are given simple no-alpha textures.
    Naturally all textures, lighting, shadow, etc would be disabled during the false-color render.

    The reason why I suggest it is because doing the math manually can get extremely tricky. The graphics subsystem already does all the math for you, though---so why not leverage that? Instead of saying "My camera is at X facing Y, and the object is at Z, so the mouse coordinates indicate the mouse is on the object", you simply say "Render the area around the mouse and see which object ends up there." You're essentially allowing the computer to decide where the object is on the screen exactly the same way that your eye does, except you're cheating a bit to make it easier.

    I don't think I've mentioned this, but the objects are floating in a world with a terrain built out of images, so scanning this way could 'cause getting wrong info (the terrain).
    You can simply disable terrain rendering for the false-color render. However, note that any objects which would normally be obscured by terrain would then be selectable, so it might be best to render the terrain using a special "no object" color.

    Here's some reading on possible selection strategies. The one above is the second one mentioned, more or less. This is for OpenGL, but the principals apply equally to any system.
    http://www.opengl.org/resources/faq/.../selection.htm
    Last edited by Lindley; September 23rd, 2009 at 11:17 AM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured