Page 1 of 1
rotate camera with player?
Posted: Sun Jun 15, 2014 10:51 am
by micronaut
hi,
I've been considering using love for my 1st foray into game making. I would like to have a player sprite in the centre of the screen and have the background rotate under the player as well as move up and down, so that the player appears fixed pointing towards the top of the screen.
Conceptually, would this be achieved by rotating and moving both the player image and the camera simultaneously to make the illusion of a fixed player position on screen?
thanks for any help.
Re: rotate camera with player?
Posted: Sun Jun 15, 2014 1:39 pm
by Sheepolution
I think it's simply love.graphics.rotate( -player_rotation )
If you rotate the player with +5, and rotate the camera back with -5, the player's rotation comes back to 0.
Re: rotate camera with player?
Posted: Sun Jun 15, 2014 1:54 pm
by kikito
It's not that simple, I am afraid. Keeping the player in the center of the screen is somewhat difficult if you are rotating things around. Moreso if you are also zooming in and out at the same time.
I made a library whose purpose is making these calculations so you don't have to. It's called
gamera.
This is how the code could look (adapt the player functions to whatever you use):
Code: Select all
local gamera = require 'gamera'
local cam = gamera.new(0,0, 4000,4000) -- world size
...
local player = ... -- create the player
...
function love.update(dt)
updatePlayer(player, dt)
cam:setPosition(getPlayerPosition(player))
cam:setAngle(getPlayerAngle(player))
cam:setScale(...) -- zoom in/out
end
function love.draw()
cam:draw(function()
drawPlayer(player)
... -- draw everything that needs to be rotated/zoomed here
end)
drawScore()
drawLivesLeft()
end
Re: rotate camera with player?
Posted: Mon Jun 16, 2014 8:55 pm
by micronaut
thanks very much for the replies. I'll take a look at gamera and see how it goes
Re: rotate camera with player?
Posted: Sat Sep 06, 2014 11:20 pm
by Snackrilege
Kikito, I've implemented your camera system, and it's incredibly valuable. The only thing I can't seem to get working is finding mouse coordinates.
How do I find the mouse location with this system?
EDIT: figured it out. I had my understanding of cam:toWorld() and cam:toScreen() backwards. I was using toScreen when I should have been using toWorld. Thanks again for the phenomenal system.
Also, a minor note, it wouldnt let "local gamera = {}" pass the compiler. I had to change it to a global variable.