[Perspective] Continous Parallax in different directions

Hi,

I am not able to post any comments on https://developer.coronalabs.com/code/perspective so I had to create a new topic about my question.

I am currently trying to create a function which enables me to create a parallax effect based on 3 or 4 different images which works with the perspective module. The normal parallax effect works very well but I want to make the images move continuously to the right and the left dependent on the player. In order to safe performance I do not want to place hundreds of images next to each other and hope that the player does not reach the “end” at all :wink:

There is an interesting article about different parallax techniques but I do not really know how to implement “technique 2” into the perspective module.

http://illogictree.com/blog/2009/01/how-to-implement-parallax-scrolling/

@Caleb: If you read this, may you think about implementing something like this? :slight_smile:

Max / CineTek

like the background in this video

[media]

http://www.youtube.com/watch?v=XR7J41PuxEE

[/media]

No, I forgot to mention that I am working on a 2D platformer :wink:

I want to move a few background images in a continuous parallax style.

Max

thats actually 5 background images moving in 4 different directions all at different speeds

Hm yes, but the camera does not move the parallax at all right? But the way you are spawning the images might be part of the solution - May I ask how you did that?

Max

http://j-strahan.com/main/?p=127

Thank you! :slight_smile: I will try this out.

“has been deleted or changed. Please refresh the page.” I am getting this error :confused: Or do I have to donate first? (I would have done it anyway :wink: )

ill check it it should be a free download

hold on

not sure why yet tried re uploading but didnt work

msg me your email and ill send to you

Message sent

It´s me again. I have made my own (pretty easy) function to solve this problem and this is what I´ve got so far:

function updateBackgrounds() --check whether the player is running or not: if player.isRunning == true then if player.x \<= \_W/2 then else if player.xScale == 1 then if (ground1.x \<= player.x - 495) then ground1.x = player.x + 465 end if (ground2.x \<= player.x - 495) then ground2.x = player.x + 465 end if (brownMountains1.x \<= player.x - 495) then brownMountains1.x = player.x + 465 end end end end

This code works as long as I do not change the parallax-ratio in my camera settings (again, I am using the perspective module!)

So my ground1 and ground 2 object are both moving by a ratio of 1. But I want to have other background images like “brownMountains1” that are moving with a ratio of 0.6 for example.

So I need to edit the above function in order to spawn the other layers accordingly to the player.

As far as I understand Perspective, it only moves the focussed object, the player, so only his x-values are changing.

Ok, sorry for not noticing sooner :slight_smile:

Perspective actually doesn’t move the focus itself - it moves each layer around the player. When the player moves left, all the layers move right; when the player moves up, all the layers move down, etc. You might want to check out the :contentToLocal() and :localToContent() functions. They give the position of an object in comparison to the screen without taking position into account.

Perhaps like so:

(Warning: This is from the top of my head; it’s not even tested once)

[lua]

local objWrapTable={ – Put all your objects you want to wrap inside of here

    brownMountains1,

    ground1,

    ground2

}

local wrapBoundX1=display.screenOriginX – Wrapping bound 1 (low)

local wrapBoundX2=display.contentWidth-display.screenOriginX – Wrapping bound 2 (high)

local wrapIncr=wrapBoundsX2-wrapBoundX1 – Don’t edit

local function wrapObjects()

    for i=1, #objWrapTable do

        local obj=objWrapTable[i]

        local contentX, contentY=obj:localToContent(0, 0)

        if contentX<=wrapBoundsX1-obj.contentWidth*0.5 then

            obj:translate(wrapIncr, 0)

        elseif contentX>=wrapBoundsX2+obj.contentWidth*0.5 then

            obj:translate(-wrapIncr, 0)

        end

    end

end

[/lua]

  • C

Hey Caleb,

This kind of works, I have to change the paramaters though :wink: But the general concept works just fine. Thank you very much!

local function wrapObjects() for i=1, #objWrapTable do local obj = objWrapTable[i] local contentX, contentY = obj:localToContent(0, 0) if contentX \<= wrapBoundX1 - obj.contentWidth\*0.5 then obj:translate(wrapIncr\*2, 0) print(obj) elseif contentX \>= wrapBoundX2 + obj.contentWidth\*0.5 then obj:translate(-wrapIncr\*2, 0) end end end

Well, after some trial and error I got this to work. I doubled the wrapIncr value and now it works as expected! :slight_smile:

Glad you got it working, and thanks for using Perspective :slight_smile:

  • C

like the background in this video

[media]

http://www.youtube.com/watch?v=XR7J41PuxEE

[/media]

No, I forgot to mention that I am working on a 2D platformer :wink:

I want to move a few background images in a continuous parallax style.

Max

thats actually 5 background images moving in 4 different directions all at different speeds

Hm yes, but the camera does not move the parallax at all right? But the way you are spawning the images might be part of the solution - May I ask how you did that?

Max