Camera movement

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Camera movement

Post 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
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Camera movement

Post 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Re: Camera movement

Post by CalebAnder »

Ok thanks. So his is my character:

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

How would I center the camera to the plane?
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Camera movement

Post 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.
Last edited by zorg on Sun Jan 17, 2016 4:20 pm, edited 1 time in total.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
CalebAnder
Prole
Posts: 16
Joined: Fri Jan 15, 2016 6:33 pm

Re: Camera movement

Post by CalebAnder »

Thanks zorg! You are always helping me :awesome:
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests