Camera movement to follow player problems

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
TheSquareRootOf3
Prole
Posts: 15
Joined: Wed Jun 26, 2013 5:41 pm

Camera movement to follow player problems

Post 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.
Attachments
camera.lua
(822 Bytes) Downloaded 267 times
main.lua
(1.22 KiB) Downloaded 205 times
Windwaker.love
(992.49 KiB) Downloaded 183 times
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Camera movement to follow player problems

Post 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
TheSquareRootOf3
Prole
Posts: 15
Joined: Wed Jun 26, 2013 5:41 pm

Re: Camera movement to follow player problems

Post 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
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Camera movement to follow player problems

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Camera movement to follow player problems

Post by kikito »

In case this helps, you might want to try gamera (disclaimer: I'm gamera's author)
When I write def I mean function.
TheSquareRootOf3
Prole
Posts: 15
Joined: Wed Jun 26, 2013 5:41 pm

Re: Camera movement to follow player problems

Post by TheSquareRootOf3 »

thanks guys! problem solved!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 99 guests