Page 1 of 2

Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 8:17 pm
by xFade
How can you set the camera to look at, say... (-250,0)? Is this even possible?

Re: Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 8:25 pm
by Germanunkol
Hi,

Yes, this is possible. It's usually done by calling love.graphics.translate()
So what you're really doing is translating everything to (250, 0) - which has the same effect as moving the camera to (-250, 0).

Here's an example:

Code: Select all

function draw()
    -- Draw the scene at an offset of 250, 0:
    love.graphics.push()
    love.graphics.translate(250, 0)
    love.graphics.rectangle( "fill", 10, 10, 50, 50 )
    love.graphics.pop()   -- restores coordinate system to the one of the previous push()

    -- now draw the UI and everything that should NOT be moved:
    love.graphics.print( "some text", 10, 10 )
end
With that said, there are a few camera libraries out there (vrld's hump being a good example: Documentation here) which do the heavy stuff for you and hide the camera movement, rotation etc and allow for multiple cameras and similar. Check it out!
I recommend trying the way above first though, to get familiar with the concept.

Re: Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 8:30 pm
by xFade
Thank you :D

Re: Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 8:37 pm
by xFade
Wait but I need to access the negative scale.I drew an image from another script at (-250,0) and it doesn't appear :/

Re: Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 9:27 pm
by Robin
Please upload a .love. I'm guessing a module needs to be required or a function called, but it's impossible to tell without seeing your code. (And please don't double post. You could have edited your previous post instead.)

Re: Setting the Camera's view to another position?

Posted: Mon Apr 07, 2014 10:38 pm
by mickeyjm
I have a feeling that in this case it is because the demo Germanunkol posted shifted the camera to positive 250 and you have drawn an object at negative 250. If so just putting a negative sign in front of the 250 in translate() should fix it

Re: Setting the Camera's view to another position?

Posted: Tue Apr 08, 2014 12:37 am
by xFade
Here you go and changing the translate to -250 did not work.

Re: Setting the Camera's view to another position?

Posted: Tue Apr 08, 2014 9:35 am
by Ragzouken
recommend you use the hump library's camera module, it will do all the hard work for you http://vrld.github.io/hump/

Re: Setting the Camera's view to another position?

Posted: Tue Apr 08, 2014 3:32 pm
by Germanunkol
Hi,

Your Main.lua should be named main.lua.
The reason you're not getting any error message is because conf.lua is there, so somehow it seems to load that and then think it's fine. So rename Main.lua to main.lua - at least on linux, I only got a black screen otherwise. Might work on win - since win is not case-sensitive in naming.

About the wrong translation: You need to do two things:
1) Change the -250 to 250 in the Scripts/camera.lua file. What love.graphics.translate does is it simply adds an offset to every following draw call. So If you want an object at -250,0 to be displayed at 0,0 then you need to translate by 250,0 because 250-250 = 0.
2) You're using the love.graphics.pop() function wrong. You need to draw the stuff, before you call the pop() - because pop resets the translation to the state where the push was called - so when you draw things afterwards your translation is no longer active, the way you do it now.
To fix: create a function unsetNewView(), move the love.graphics.pop() call into that and then call that function from love.draw, after drawing the player

Code: Select all

function love.draw()
    setNewView()
    player:draw()
    unsetNewView()
end

...
function setNewView()
    love.graphics.push()
    love.graphics.translate(250, 0)
end

function unsetNewView()
	love.graphics.pop()
end
As a side node: Instead of using .rar, you should make .zip files instead and then rename them to .love before uploading. More people will check out your code when all they have to do is download and run a .love.

Hope this fixes it...

Re: Setting the Camera's view to another position?

Posted: Tue Apr 08, 2014 7:00 pm
by xFade
Thank you for all the tips, I'm new to love2d so I really appreciate it. :D