Quick Links: Gideros Home | Download Gideros | Developer Guide
How to use the Viewport?
  • stetso +1 -1
    Member
    Hi everyone,

    I tried to understand the new Viewport but failed miserably. I want it to follow an entity around (like a camera) but I am not sure how to do that or even if the Viewport is the right thing for it.
    Of course, an alternative would be to just not use a Viewport and move the sprite hierarchy around but if possible I would rather use the new Viewport (also because splitscreens are much easier, I hope...) and it kind of... seems cleaner.
    Anyway, I could not make much out of the documentation unfortunately. I think :lookAt() is the correct thing to use but I do not understand the parameters.

    Any help would be appreciated :) Here is some code that uses :setTransform() but this moves the viewport itself around, not the view inside the sprite hierarchy (well, at least the effect is kind of psychedelic...) @-)
     
    local screenW = application:getContentWidth()
    local screenH = application:getContentHeight()
     
    local bg = Bitmap.new(Texture.new("background.png"))
    bg:setPosition(0,0)
    bg:setScale(2)
    stage:addChild(bg)
     
    -- move the ship around with arrow keys
    local ship = Ship.new()
    ship:setPosition(10,10)
    bg:addChild(ship)
     
    local view = Viewport.new()
    view:setClip(0,0, screenW, screenH)
    view:setContent(bg)
    stage:addChild(view)
     
    -- how to follow the ship with the viewport!? possible?
     
    function update(ev)
    local dt = ev.deltaTime
    ship:update(dt)
    view:setTransform(ship:getMatrix()) -- this transforms the viewport position and rotation itself... D:
    -- view:lookAt(aaaaaaaarrrrrrrgh!!!!) ???
    end
     
    stage:addEventListener(Event.ENTER_FRAME, update)


    I also add the whole project.

    Thanks for your help!
    viewport question.zip
    5K
  • hgy29hgy29 +1 -1 (+1 / -0 )
    Maintainer
    Hi @steso,

    lookAt() is dedicated to 3D, it may not be easy to use for 2D. You are right in using setTransform(), but I don't get why you pass it ship:getMatrix()... maybe you meant passing the inverse matrix instead ?

    Likes: stetso

  • stetso +1 -1
    Member
    @hgy29 thanks, that could certainly be it. Still new to all the math related to graphics, so sorry for the confusion. I will test it once I am back home!
  • antixantix +1 -1 (+1 / -0 )
    Member
    Personally I find a simple camera works best for 2D stuff. All you need to do is create a sprite for the camera, add that to the stage, and attach all objects (that move about the game world) to the camera. When you want to set the camera to any objects position just set the cameras position as the objects -x, -y positions (you need to adjust for centering).

    The attached example will create a bunch box objects and move them about a world at random speeds. Clicking the screen makes the camera track the next box in the list :bz
    simple camera.zip
    11K

    Likes: stetso

    Check out my DevBlog, my GitHub, and my games Falling Animals | Breaky Wall | Exetor
  • stetso +1 -1 (+1 / -0 )
    Member
    @antix Thank you! Yes, this is what I have been doing as well. I thought the Viewport may be an alternative to that, but I couldnt get it to work :(( so I will also stick to the traditional way for now... at least until I understand the math better or venture into 3D territory...

    Likes: antix

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Login with Facebook Sign In with Google Sign In with OpenID

In this Discussion

Top Posters