Page 1 of 1

Camera movement to follow player problems

Posted: Wed Jun 26, 2013 7:57 pm
by TheSquareRootOf3
hey guys! first off id like to say IM NEW! so im sorry if i break some rules or piss some people off, i promise im not trying to :p im just trying to get this problem solved! alright moving on to the problem, i want the camera to start following my player once he reaches half way through the screen, now it does that when the player moves right, but it does not when he moves up, left, or down, like i said im new, ive been using this stuff for like a few hours max, so maybe someone could help me understand? im sure its a stupid mistake.

Re: Camera movement to follow player problems

Posted: Wed Jun 26, 2013 8:27 pm
by jjmafiae
okay first off your map picture is too big not all computers can run such big maps i would make a props.lua/house.lua and use tables

secound, the camera problem:

main.lua

Code: Select all

	if player.y > love.graphics.getWidth() / 2 then
		camera.y = player.y - love.graphics.getHeight() / 2
	end

Re: Camera movement to follow player problems

Posted: Wed Jun 26, 2013 11:50 pm
by TheSquareRootOf3
alright man, i did that, but now the problem is that when the game first starts, the player walks all the way to the bottom of the screen, it glitches, and then it does it properly, any ideas? this happens every time

Re: Camera movement to follow player problems

Posted: Thu Jun 27, 2013 5:47 am
by micha
I'd implement a camera like this: By default, make the camera center around the player. Then check if the camera is within the bounds of the map and if not, correct it. So here you go (this by the way has to be inside the 'if gamestate == "player"'-part not in the general love.update. The menu does not have camera movement)

Code: Select all

camera.x = player.x - love.graphics.getWidth()/2
camera.y = player.y - love.graphics.getHeight()/2
if camera.x < 0 then camera.x = 0 end
if camera.y < 0 then camera.y = 0 end
if camera.x > mapWidth - love.graphics.getWidth() then
  camera.x = mapWidth - love.graphics.getWidth()
end
if camera.y > mapHeight - love.graphics.getHeight() then
  camera.y = mapHeight - love.graphics.getHeight()
end
In love.load I added these lines:

Code: Select all

mapHeight = 1988
mapWidth = 2720
Later, when you work more on your project you should then implement a function that reads the height and width of the map, instead of hard coding it, like I have done it now.

And you should really make the window smaller.

Re: Camera movement to follow player problems

Posted: Thu Jun 27, 2013 7:38 am
by kikito
In case this helps, you might want to try gamera (disclaimer: I'm gamera's author)

Re: Camera movement to follow player problems

Posted: Thu Jun 27, 2013 6:00 pm
by TheSquareRootOf3
thanks guys! problem solved!