## Setting the Camera's view to another position?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

### Setting the Camera's view to another position?

How can you set the camera to look at, say... (-250,0)? Is this even possible?
(ง'̀-'́)ง

Germanunkol
Party member
Posts: 710
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

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

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.
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de

Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

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

Thank you
(ง'̀-'́)ง

Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

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

Wait but I need to access the negative scale.I drew an image from another script at (-250,0) and it doesn't appear :/
(ง'̀-'́)ง

Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

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

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.)

mickeyjm
Party member
Posts: 237
Joined: Thu Dec 29, 2011 11:41 am

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

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
Your screen is very zoomed in...

Prole
Posts: 41
Joined: Mon Dec 23, 2013 6:04 pm

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

Here you go and changing the translate to -250 did not work.
(ง'̀-'́)ง

Ragzouken
Citizen
Posts: 84
Joined: Fri Aug 10, 2012 7:59 am
Contact:

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

recommend you use the hump library's camera module, it will do all the hard work for you http://vrld.github.io/hump/

Germanunkol
Party member
Posts: 710
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

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

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...
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de