Page 1 of 2

Camera moving

Posted: Sat May 30, 2009 9:24 am
by eliasaif
Hi! I'm trying to do a platform game where the camera only should move when the character gets outside of the current screen. Then the camera should move one screen width/height in the character's moving direction. I've almost got it to work, but as you can see, it's not working exactly as I want it to. I hope someone knows how to do it.

Re: Camera moving

Posted: Sat May 30, 2009 11:02 am
by bartbes
Well, you could start by describing the problem, I don't see anything that's clearly wrong (though there is a flicker when switching to the next screen)

Re: Camera moving

Posted: Sat May 30, 2009 11:33 am
by eliasaif
Well, the problem is that when the character goes to the left more than one screen and then goes back again, the camera doesn't follow. I don't know if my way of doing this screen movement is the best so I'm wondering if someone any better idea that actually works without any x and y valuses hard coded, as it is right now. And the flicker is also a problem. I assume that it has something to do with the way I have done the coding..

Re: Camera moving

Posted: Sat May 30, 2009 11:48 am
by bartbes
Hmm, right it seems to be the hardcoding, can't you use some function of camera to get the correct height and width? love.graphics.getHeight()/getWidth()? And, you might want to do something like:

Code: Select all

if x % width <= 2 then
--move right
elseif x % width >= width-2 then
--move left
end
(this is with a 2-'unit' area in which you can move to the next screen)

Re: Camera moving

Posted: Sat May 30, 2009 1:00 pm
by eliasaif
Ok, so what is this code really doing? Can't figure out what the percent sign is doing. In my current code i move the camera with getCamera():setOrigin(x, y). So what is supposed to be the x that the camera is moving to?

Re: Camera moving

Posted: Sat May 30, 2009 1:24 pm
by Robin
eliasaif wrote:Ok, so what is this code really doing? Can't figure out what the percent sign is doing. In my current code i move the camera with getCamera():setOrigin(x, y). So what is supposed to be the x that the camera is moving to?
% means modulus: the rest after an integer division: 17 / 4 = 4 modulus 1, so 17 % 4 = 1.

The x in bartbes's code is (I think) the player position.

This method is still somewhat inflexible. I would suggest looking at Jump Game for a more flexible solution.

Re: Camera moving

Posted: Thu Jun 04, 2009 7:51 am
by eliasaif
Hi again! I still can't find out how to do this.. I've tried something like this code:

Code: Select all

if playerx % width <= 2 then
  getCamera():setOrigin(x, y)
elseif playerx % width >= width-2 then
  getCamera():setOrigin(x, y)
end
The problem is that I can't figure out what I should set the camera origin to. In this code, it sais x and y, but I don't know what the x and y should be. Does anyone have any idea how I can get this working?

Re: Camera moving

Posted: Thu Jun 04, 2009 2:19 pm
by bartbes
Probably the playerx.

Re: Camera moving

Posted: Thu Jun 04, 2009 2:59 pm
by Robin
I would say something like x = current_x + width / 2 or x = current_x + width. It depends on whether you still want to see a bit of the old position.

The y stays the same, I think.

Re: Camera moving

Posted: Thu Jun 04, 2009 5:45 pm
by eliasaif
It's like it's looping the moving process by it self. Something must still be wrong in the code. I've attached the game below.