Get parent camera from code

jimjim
edited November 2013 in Help request
How to get parent camera of an object if defined? I tried but could not find any reference about it.

What I am trying to do is, I am using multiple camera and viewport for different thing. Now I have 3 viewport and Camera, actually I need 3 camera but as 3 camera needs 3 viewport I created 3 viewport with 3 camera. One for GUI, one for game and another for Backgroud, background would be scrollable.

Comments

  • edited November 2013
    Objects can have 4 different kind of parents: objects, cameras, spawners or frames.

    There's only one accessor for getting an object's parent, you'll then need to try to "cast" it into what you expect to see if it's valid or not.

    The code
    orxCAMERA *pstCamera = orxCAMERA(orxObject_GetParent(pstObject));
    

    will return the parent camera of an object if it has one and orxNULL otherwise (ie. if it has no parent or if the parent isn't a camera).

    As a side note, more than one viewport can use the same camera but the opposite isn't true (many cameras can't be linked to the same viewport).

    Out of curiosity, for which purpose do you "need" 3 cameras? Are you doing some compositing?
  • jimjim
    edited November 2013
    Okay, I got that.

    One camera for GUI, as in my game the player would go from left to right, that camera would be responsible for rendering GUI elements.

    Second, for gamneplay element player and other interactive element, this camera would also be fixed

    Third, background, other scrollable element like sky, far objects would be drawn here, as I saw in the scrolling demo, I need to move a camera to make scrolling effect.

    I think, there might be other easy way to do this, also I am not sure about performance hit of '3 camera' for mobile device, I would love to hear other solution.

    Another thing, I am having some difficulty adding multiple viewport with scroll, I think it binds all object with MainViewport , but I need to go more deeper.
  • edited November 2013
    I don't know the detail of your game setup, but the GUI camera, for example, shouldn't be necessary.
    I'm using the main camera for GUI as well in my games, simply setting the GUI objects as child of the camera so that their positions remain fixed on screen.
    That being said, it doesn't hurt having extra cameras for that, I was just curious. :)

    There won't be any performance hit as long as you don't render objects to more than one camera. Using object groups you can make sure groups are only rendered by a single camera.

    As for multiple viewports, I'd need more details as I'm using multiple viewports with Scroll myself and didn't get any trouble. The only thing to keep in mind is that the MainViewport is created by Scroll whereas the other ones will be created directly by you.
  • jimjim
    edited November 2013
    Oh, I forgot about parenting a camera. As for viewport I would provide you more details, as currently I fall back to single camera.

    If I create my own camera, when should I create it, also if I set ParentCamera to 'MyCustomCamera' for Scroll objects in config, I see the object on both main camera of scroll and 'MyCustomCamera', looks like I first have to unlink any camera then set Parent of that object to 'MyCustomCamera' from code.

    Lastly, is it necessary to move a camera to have differential scrolling effect, then I need two camera, one for GUI and game objects, and another for Background with scrolling.
  • edited November 2013
    jim wrote:
    If I create my own camera, when should I create it

    You should never create a camera directly. They'll get created by your viewports.
    also if I set ParentCamera to 'MyCustomCamera' for Scroll objects in config, I see the object on both main camera of scroll and 'MyCustomCamera', looks like I first have to unlink any camera then set Parent of that object to 'MyCustomCamera' from code.

    Parenting isn't linked to rendering. If the object is in the frustum of both cameras, it'll get rendered by both of them. Object groups can be used to make sure objects only get rendered by the appropriate camera. Or old-style Z trick (by playing on the camera depth, which isn't useful anymore, given object groups).
    Lastly, is it necessary to move a camera to have differential scrolling effect, then I need two camera, one for GUI and game objects, and another for Background with scrolling.

    Nope, camera can stay still. The effect is bound to objects. In scroll the toggle will modify all objects, you can can cherry pick which ones you want with differential scrolling by calling orx functions directly.
  • jimjim
    edited November 2013
    You should never create a camera directly. They'll get created by your viewports.
    Well, I meant to say where I should create my viewport, in orxScroll::init() and before I create my scroll objects or after creating scroll objects ?
    Parenting isn't linked to rendering. If the object is in the frustum of both cameras, it'll get rendered by both of them. Object groups can be used to make sure objects only get rendered by the appropriate camera. Or old-style Z trick (by playing on the camera depth, which isn't useful anymore, given object groups).
    Now I realize why it was rendered twice, So, ignore my first question, I had an impression that, objects were linked with both viewports, hence rendered twice. I will definitely use object group, for this purpose.
    Nope, camera can stay still. The effect is bound to objects. In scroll the toggle will modify all objects, you can can cherry pick which ones you want with differential scrolling by calling orx functions directly.
    but in scroll tutorial I have found
    By default, in this tutorial, the attribute AutoScroll is set to 'both'.
    This means a parallax scrolling will happen on both X and Y axis when the camera moves.
    So, I guess if all scrolling objects sets a default object as parent, then moving that parent object would have the same effect. So, I am not moving the camera instead moving the objects, but have the same effect ?

    And thank you for your answers, it was much helpful.
  • edited November 2013
    jim wrote:
    Well, I meant to say where I should create my viewport, in orxScroll::init() and before I create my scroll objects or after creating scroll objects ?

    It shouldn't matter. :)
    Now I realize why it was rendered twice, So, ignore my first question, I had an impression that, objects were linked with both viewports, hence rendered twice. I will definitely use object group, for this purpose.

    I see. :)
    but in scroll tutorial I have found
    By default, in this tutorial, the attribute AutoScroll is set to 'both'.
    This means a parallax scrolling will happen on both X and Y axis when the camera moves.
    So, I guess if all scrolling objects sets a default object as parent, then moving that parent object would have the same effect. So, I am not moving the camera instead moving the objects, but have the same effect ?

    Yes, Scroll doesn't allow you to fine-tune differential scrolling, but you can still do it via orx itself. Moving the camera or the object doesn't matter, it's the resulting relative position that will be used for simulating differential, inside the render plugin.
    And thank you for your answers, it was much helpful.

    My pleasure, glad if it can help you. Don't hesitate if you have any other questions. :)

    Cheers!
Sign In or Register to comment.