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

Re: Camera moving

Post by bartbes »

I guess that the problem is that the condition is still true.

And:
WHAT HAVE YOU DONE?! Why fullscreen?! It crashed on me, leaving me with this crappy resolution!
User avatar
eliasaif
Prole
Posts: 25
Joined: Sat May 02, 2009 4:04 pm
Location: Sweden

Re: Camera moving

Post by eliasaif »

Oh! I've done it again! I'm sorry for that. I'm just used to run thing the way they are ment to be.
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 »

What fun:

Code: Select all

    if playerx % width >= 2 then
        x = originx + width
        getCamera():setOrigin(x, y)
    elseif playerx % width <= width-2 then
        x = originx - width
        getCamera():setOrigin(x, y)
    else
        x = 0
        getCamera():setOrigin(x, y)
    end
Two mistakes:
  1. The if and elseif should switch comparison.
  2. You compare playerx, while you should compare playerviewx (i.e.: "playerx - viewx").
Change it:

Code: Select all

    if playerviewx % width <= 2 then
        x = originx + width
        getCamera():setOrigin(x, y)
    elseif playerviewx % width >= width-2 then
        x = originx - width
        getCamera():setOrigin(x, y)
    else
        x = 0
        getCamera():setOrigin(x, y)
    end
Calculating playerviewx is left as an exercise to the reader.
Last edited by Robin on Sat Jun 06, 2009 12:06 pm, edited 1 time in total.
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 »

Isn't originx what you call playerviewx? It's the current origin of the camera.
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:Isn't originx what you call playerviewx? It's the current origin of the camera.
Oh, I didn't see originx. But no, it's a tad different. I think:

Code: Select all

playerviewx = playerx - originx
It's where the playerx is on the screen.
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 »

I tried to do like this:

Code: Select all

    
    playerx = playerbody:getX()
    playery = playerbody:getY()
    
    originx, originy = getCamera():getOrigin()
    
    width = love.graphics.getWidth()
    height = love.graphics.getHeight()
    
    playerviewx = playerx - originx
    
    if playerviewx % width <= 1280/50 then
        x = originx + width
        getCamera():setOrigin(x, y)
    elseif playerviewx % width >= width-1280/50 then
        x = originx - width
        getCamera():setOrigin(x, y)
    end
But it's still doing this loop that gets the camera to fly away all the time.. I know it's because playerviewx % width <= 1280/50 or playerviewx % width >= width-1280/50 is still true. I still can't figure this out. :P
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, the case here is that you move your origin to the next screen, where it will be within the 'back' zone, so it will go back, where it will be in the 'forward' zone....
So... maybe move half a screen, or just find a better solution :P.
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 »

Another approach would be like I did in Jump Game. Instead of changing the view, I set viewdx = 20 (or something like that), and in update, I put the following code:

Code: Select all

if viewdx > .1 then
    setview(+dt)
    viewdx = viewdx - dt
elseif viewdx < -.1 then
    setview(-dt)
    viewdx = viewdx + dt
end
(replace setview() with your camera moving code)
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 »

Thanks for all good ideas! Now I have solved my problem. It's not really anything like the above, but it works fine. Here's my code if someone's interested.

Code: Select all

    playerx = playerbody:getX()
    playery = playerbody:getY()
    
    originx, originy = getCamera():getOrigin()
    
    width = love.graphics.getWidth()
    height = love.graphics.getHeight()
    
    if playerx > originx+(width/2) then
        x = x + width
        getCamera():setOrigin(x, y)
    elseif playerx < originx-(width/2) then
        x = x - width
        getCamera():setOrigin(x, y)
    end
    
    if playery > originy+height then
        y = y + height
        getCamera():setOrigin(x, y)
    elseif playery < originy-height then
        
    elseif playery < originy then
        y = y - height
        getCamera():setOrigin(x, y)
    end
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 »

Let me try and describe it with one word:
better

Well, that went well... maybe I can start typing sentences next...
Post Reply

Who is online

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