Quick Links: Download Gideros Studio | Gideros Documentation | Ultimate Guide | Gideros Development Center
How to use the Viewport? - Gideros Forum

How to use the Viewport?

stetso Member
edited March 1 in General questions
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"))
-- move the ship around with arrow keys
local ship = Ship.new()
local view = Viewport.new()
view:setClip(0,0, screenW, screenH)
-- how to follow the ship with the viewport!? possible?
function update(ev)
local dt = ev.deltaTime
view:setTransform(ship:getMatrix()) -- this transforms the viewport position and rotation itself... D:
-- view:lookAt(aaaaaaaarrrrrrrgh!!!!) ???
stage:addEventListener(Event.ENTER_FRAME, update)

I also add the whole project.

Thanks for your help!


  • hgy29hgy29 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 ?

    Aye: stetso

    +1 -1 (+1 / -0 )
  • stetso 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!
    +1 -1
  • antixantix Member
    edited March 1
    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

    Aye: stetso

    +1 -1 (+1 / -0 )
  • stetso 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...

    Aye: antix

    +1 -1 (+1 / -0 )
Sign In or Register to comment.