Page 1 of 1

Camera movement

Posted: Sun Jan 17, 2016 9:53 am
by CalebAnder
Hello, how do I make the camera follow my player? In my game you can move the airplane with the arrow keys, but I want the camera to follow it. Any help? Thanks

Re: Camera movement

Posted: Sun Jan 17, 2016 10:43 am
by vrld
Do you use a camera library? Which one? Usually, you have to update the camera position together with the player position.
hump.camera, has some utilities that make these things easier, e.g.:

Code: Select all

function love.update(dt)
    player:update(dt) -- moves player
    cam:lockPosition(player.x, player.y) -- locks the camera on the player
end
If you do not use a camera library, you can center the "camera" on the player with [wiki]love.graphics.translate[/url]:

Code: Select all

function love.draw()
    love.graphics.push()
    love.graphics.translate(-player.x, -player.y)
    draw_world()
    love.graphics.pop()

    draw_hud()
end

Re: Camera movement

Posted: Sun Jan 17, 2016 12:26 pm
by CalebAnder
Ok thanks. So his is my character:

plane = love.graphics.draw("plane.png")

How would I center the camera to the plane?

Re: Camera movement

Posted: Sun Jan 17, 2016 4:11 pm
by zorg
by having

Code: Select all

-- outside any function:
local plane = {}
-- in love.load:
plane.x = 0
plane.y = 0
plane.graphics = love.graphics.newImage("plane.png")
-- in love.update, you modify the x and y members of plane with keypresses or something
-- in love.draw:
love.graphics.draw(plane.graphics, plane.x, plane.y)
-- Note that this won't move the camera, you'll need to add in one of the above codes from vrld.
or similar.

lg.draw can't draw anything from a string parameter (according to the wiki which you should really read through), so what you posted most certainly does not work.

Re: Camera movement

Posted: Sun Jan 17, 2016 4:15 pm
by CalebAnder
Thanks zorg! You are always helping me :awesome: