Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How to use the Viewport? — Gideros Forum

How to use the Viewport?

stetsostetso Member
edited March 2017 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"))
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... <img class="emoji" src="http://forum.giderosmobile.com/resources/emoji/anguished.png" title="D:" alt="D:" height="20" />
	-- view:lookAt(aaaaaaaarrrrrrrgh!!!!) ???
end
 
stage:addEventListener(Event.ENTER_FRAME, update)
I also add the whole project.

Thanks for your help!

Comments

  • 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 ?
    +1 -1 (+4 / -0 )Share on Facebook
  • @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 Member
    edited March 2017
    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
    zip
    zip
    simple camera.zip
    11K

    Likes: stetso

    +1 -1 (+1 / -0 )Share on Facebook
  • @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

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