Camera moving

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.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Camera moving

Post 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.
Attachments
game.love
(266.1 KiB) Downloaded 291 times
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Camera moving

Post 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)
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Camera moving

Post 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..
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Camera moving

Post 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)
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Camera moving

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Camera moving

Post 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.
Help us help you: attach a .love.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Camera moving

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Camera moving

Post by bartbes »

Probably the playerx.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Camera moving

Post 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.
Help us help you: attach a .love.
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Camera moving

Post 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.
Attachments
game.love
(442.01 KiB) Downloaded 266 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot] and 57 guests