Picking objects with the mouse

edited May 2012 in Help request
Long version:
Hello guys, I am now Trying to select objects with the click of the mouse.
I managed to do it using the physics system, creating a very small object of size 1x1 and placing it where the mouse is clicked, the problem is that the information is handled in other function (the physics event handler), this may lead to some very confusing code.
Reading through the API documentation I found this OObox module, but I could not find how to create one, so I took the one frommy 1x1 object and tried to use the orxObject_BoxPick to pick the objects under the mouse. Still it always return the 1x1 object. Is there any easy way to find which object I clicked on?

Short version:
Is there any easy way to find which object I clicked on?

Thanks in advance.

Comments

  • edited May 2012
    Yes, there's a way and you don't need to create any object to do this kind of stunt! :)

    It's called orxObject_Pick(Pos). The position you give it is in world space and orx will return the first object under that position (so the first object whose Z coordinate is >= Pos.Z).

    To get the world coordinate from the screen space one (mouse position is in screen space), simply call orxRender_GetWorldPosition().
    If it returns orxNULL, it means your mouse position is either-offscreen or isn't inside any viewport.

    There's an example of this transformation in the tutorial #5: viewport & camera.
  • edited May 2012
    Also if a point is too precise for picking, as you mentioned there's the box version of it: orxObject_BoxPick.

    orxOBOX is a POD structure that you can initialize with a call to orxOBox_2DSet(), or simply sets its member yourself.
  • edited May 2012
    Thanks a lot for the reply, can't believe I missed that function while reading the API (should stop programming till 4 AM after eating a ton of pizza :blush: ).

    The GetWorldPosition I was already using, I saw in the API docs the other day, really useful.

    About the OBOX, the API docs say that this function (as wall as all the ones of the module) is static, is the API wrong?
  • edited May 2012
    No worries, the object API is getting pretty big and I should probably add sub-categories for a more structured doxygen organization. I'll try to do that tonight or tomorrow.

    As for the OBox API, all the functions are static inline, yes, I'm not sure what you mean by the API being wrong.
  • edited May 2012
    Well, afaik, you can't call an static function outside the source code it was decleared, unless you return a pointer to the function and invoke it from the pointer.
  • edited May 2012
    That would be for a function compiled in a .c/.cpp file: being static means it's local to the translation unit.

    Here's the function is in a header hence defined everywhere the header is included. orx.h includes all the public headers so this function will be defined basically everywhere.

    Were the compiler to decide it doesn't want to inline the function, a local version (to the translation unit) will be stored.
    If the function wasn't static and didn't get inlined, all the translation modules using it would have their own public copy, leading to a symbol conflict at link time. Here the static keyword ensures that this will never happen, if the code doesn't get inline, the local version will not be public and there won't be any symbol clash at link time.
Sign In or Register to comment.