[Update] ParallaxLayer

  ... the case you feel the need for a new feature or want to submit one.

Re: [Update] ParallaxLayer

Postby nibbcnoble » Fri Sep 06, 2013 5:55 am

any way to make this work vertically instead of horizontally? I tried just changing all the width and X variables in the class to height and Y but that did NOT work. just wondering If you've made a vertical version. Much appreciated if you did. great piece of code either way though.
Get my new Andengine game: Incognito: Interstellar Empire
Image
nibbcnoble
 
Posts: 25
Joined: Tue Aug 06, 2013 7:02 am

Re: [Update] ParallaxLayer

Postby Superbeni » Thu Sep 12, 2013 8:30 pm

hey jaym,

I´ve been trying to implement a parallax background, and it seems like your code is the only way to do this. But I am using GLES 2 Anchor Center, which seems to not have IAreaShape - is there any way I can still use your class?
Superbeni
 
Posts: 28
Joined: Fri Feb 08, 2013 1:26 pm

Re: [Update] ParallaxLayer

Postby jaym » Sun Sep 15, 2013 6:13 pm

You should just be able to change IAreaShape to IShape.
AndEngine for Android Game Development Cookbook - Based on AndEngine's latest, greatest, GLES2.0 AnchorCenter branch!
jaym
 
Posts: 690
Joined: Wed May 04, 2011 10:08 pm

Re: [Update] ParallaxLayer

Postby Superbeni » Sun Sep 15, 2013 9:08 pm

worked - thanks a lot!
Superbeni
 
Posts: 28
Joined: Fri Feb 08, 2013 1:26 pm

Re: [Update] ParallaxLayer

Postby valka » Thu Oct 17, 2013 11:01 pm

Hey jaym,
Have you tried to draw the entities the same as SpriteBatch does it ? (one draw call for all duplicates)
(updates/spritebatch-2x-performance-improvement-anyone-t3613.html)
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: [Update] ParallaxLayer

Postby cythe » Fri Oct 18, 2013 3:34 pm

Works great. Thanks. :D
cythe
 
Posts: 2
Joined: Thu Oct 10, 2013 8:45 am

Re: [Update] ParallaxLayer

Postby valka » Fri Nov 01, 2013 3:37 am

my parallax was jittering because i was using Box2D and this fix for camera jitter:
https://github.com/nicolasgramlich/AndEngine/commit/c3e81ab7a7b6622d1aa7be286acee8115b7073a9

i've managed to fix that, maybe this could be useful for someone:
(starts from line 90 of ParallaxLayer.class)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.         public void onManagedDraw(GLState pGLState, Camera pCamera) {
  3.                 super.preDraw(pGLState, pCamera);
  4.  
  5.                //we put the parallaxValue calculations here, just before drawing it
  6.    
  7.                 if(mIsScrollable && mCameraPreviousX != this.mCamera.getCenterXNoOffset()){
  8.                                             mCameraOffsetX = mCameraPreviousX - this.mCamera.getCenterXNoOffset();
  9.                                             mCameraPreviousX = this.mCamera.getCenterXNoOffset();
  10.                                             this.mParallaxScrollValue += mCameraOffsetX * this.mParallaxScrollFactor;
  11.                                             mCameraOffsetX = 0;
  12.                             }
  13.                
  14.                 final float parallaxValue = this.mParallaxValue;
  15.                 final float parallaxScrollValue = this.mParallaxScrollValue;
  16.                 final ArrayList<ParallaxEntity> parallaxEntities = this.mParallaxEntities;
  17.  
  18.                 for(int i = 0; i < this.mParallaxEntityCount; i++) {
  19.                         if(parallaxEntities.get(i).mIsScrollable){
  20.                                 parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxScrollValue, mLevelWidth);
  21.                         } else {
  22.                                 parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxValue, mLevelWidth);
  23.                         }
  24.  
  25.                 }
  26.         }
  27.        
  28.         @Override
  29.         protected void onManagedUpdate(float pSecondsElapsed) {
  30.  
  31.                //instead of updating the parallaxValue in onManagedUpdate, i moved it to onDraw^^
  32.                //that might be not so smart, but its only a background, and it works fine.. so i dont care
  33.  
  34.                 /*if(mIsScrollable && mCameraPreviousX != this.mCamera.getCenterXNoOffset()){
  35.                                 mCameraOffsetX = mCameraPreviousX - this.mCamera.getCenterXNoOffset();
  36.                                 mCameraPreviousX = this.mCamera.getCenterXNoOffset();
  37.                                 this.mParallaxScrollValue += mCameraOffsetX * this.mParallaxScrollFactor;
  38.                                 mCameraOffsetX = 0;
  39.                 }*/
  40.                 this.mParallaxValue += this.mParallaxChangePerSecond * pSecondsElapsed;
  41.  
  42.                 super.onManagedUpdate(pSecondsElapsed);
  43.         }
  44.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
User avatar
valka
 
Posts: 97
Joined: Tue Aug 13, 2013 3:17 pm

Re: [Update] ParallaxLayer

Postby GuilhE » Tue Dec 03, 2013 2:03 am

Hello!

I've a background which is created by 3 different textures with 2048x529 and I want to achieve parallax effect.
Problem is the image spawn at certain point isn't accurate so it shows the black background.

This game is in landscape but the camera is in portrait because this is a mini game inside a game which is all in portrait except this one :D . That's why you'll see .setRotation and some Width in Height's place and vice versa.

I'm using Anchor-Center so I've replaced IAreaShape with IEntity, is that correct?

Here's some of the code for the parallax:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LANDSCAPE_ROTATION = 90;
  2. TEXTURE_WIDTH = 800 //camera
  3. TEXTURE_HEIGHT = 1280 //camera
  4.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Entity sky = new Entity();
  2.         skyBg1 = new Sprite(0, 0, bg1TiledRegion.getTextureRegion(0), vertexBufferObjectManager);
  3.         skyBg1.setRotation(LANDSCAPE_ROTATION);
  4.         sky.attachChild(skyBg1);
  5.                
  6.         skyBg2 = new Sprite(0, -skyBg1.getWidth(), bg1TiledRegion.getTextureRegion(1), vertexBufferObjectManager);
  7.         skyBg2.setRotation(LANDSCAPE_ROTATION);
  8.         sky.attachChild(skyBg2);
  9.                
  10.         skyBg3 = new Sprite(0, skyBg2.getY() - skyBg2.getWidth(), bg1TiledRegion.getTextureRegion(2), vertexBufferObjectManager);
  11.         skyBg3.setRotation(LANDSCAPE_ROTATION);
  12.         sky.attachChild(skyBg3);
  13.                
  14.         sky.setSize(C.TEXTURE_WIDTH, skyBg1.getWidth() * 3);
  15.                
  16.         parallaxLayer = new ParallaxLayer(camera, false, 4000, false);
  17.         parallaxLayer.setParallaxChangePerSecond(20);
  18.         parallaxLayer.attachParallaxEntity(new ParallaxLayer.ParallaxEntity(5, skyBg1));
  19.  
  20.         backgroundEntity = new Entity();
  21.         backgroundEntity.setPosition(TEXTURE_WIDTH, TEXTURE_HEIGHT);
  22.         backgroundEntity.attachChild(parallaxLayer);
  23.         attachChild(backgroundEntity);
  24.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


What I'm I missing here? :roll:
Thanks for your time.
GuilhE
 
Posts: 23
Joined: Fri Nov 16, 2012 4:26 pm

Re: [Update] ParallaxLayer

Postby GuilhE » Tue Dec 03, 2013 1:25 pm

GuilhE wrote:Hello!

I've a background which is created by 3 different textures with 2048x529 and I want to achieve parallax effect.
Problem is the image spawn at certain point isn't accurate so it shows the black background.

This game is in landscape but the camera is in portrait because this is a mini game inside a game which is all in portrait except this one :D . That's why you'll see .setRotation and some Width in Height's place and vice versa.

I'm using Anchor-Center so I've replaced IAreaShape with IEntity, is that correct?

Here's some of the code for the parallax:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. LANDSCAPE_ROTATION = 90;
  2. TEXTURE_WIDTH = 800 //camera
  3. TEXTURE_HEIGHT = 1280 //camera
  4.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Entity sky = new Entity();
  2.         skyBg1 = new Sprite(0, 0, bg1TiledRegion.getTextureRegion(0), vertexBufferObjectManager);
  3.         skyBg1.setRotation(LANDSCAPE_ROTATION);
  4.         sky.attachChild(skyBg1);
  5.                
  6.         skyBg2 = new Sprite(0, -skyBg1.getWidth(), bg1TiledRegion.getTextureRegion(1), vertexBufferObjectManager);
  7.         skyBg2.setRotation(LANDSCAPE_ROTATION);
  8.         sky.attachChild(skyBg2);
  9.                
  10.         skyBg3 = new Sprite(0, skyBg2.getY() - skyBg2.getWidth(), bg1TiledRegion.getTextureRegion(2), vertexBufferObjectManager);
  11.         skyBg3.setRotation(LANDSCAPE_ROTATION);
  12.         sky.attachChild(skyBg3);
  13.                
  14.         sky.setSize(C.TEXTURE_WIDTH, skyBg1.getWidth() * 3);
  15.                
  16.         parallaxLayer = new ParallaxLayer(camera, false, 4000, false);
  17.         parallaxLayer.setParallaxChangePerSecond(20);
  18.         parallaxLayer.attachParallaxEntity(new ParallaxLayer.ParallaxEntity(5, skyBg1));
  19.  
  20.         backgroundEntity = new Entity();
  21.         backgroundEntity.setPosition(TEXTURE_WIDTH, TEXTURE_HEIGHT);
  22.         backgroundEntity.attachChild(parallaxLayer);
  23.         attachChild(backgroundEntity);
  24.  
Parsed in 0.011 seconds, using GeSHi 1.0.8.4


What I'm I missing here? :roll:
Thanks for your time.


So I've tried with
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parallaxLayer = new ParallaxLayer(camera, false, 5000, false);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
and with backgrounds with 1/2 of original size and it's working now.
Is this behaviour expectable? Looks like graphic memory issues.
GuilhE
 
Posts: 23
Joined: Fri Nov 16, 2012 4:26 pm

Re: [Update] ParallaxLayer

Postby GuilhE » Tue Dec 03, 2013 1:59 pm

jaym wrote:1. Create your ParallaxLayer object:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ParallaxLayer parallaxLayer = new ParallaxLayer(camera, true, 4000);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

Parameter 1: Camera, needed for Scrolling entities such as hills.
Parameter 2: allow scrolling? this should be enabled if you plan to use parallax entities which move depending on camera movement.
Parameter 3: this is the range in which parallax layer will allow scrolling. Do not include this parameter if you would simply like the parallax layer to use default camera width. The "4000" here means that the parallax entity would scroll for approximately 4000 pixels before it is reverted back to the "starting" point.


Guilhe wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parallaxLayer = new ParallaxLayer(camera, false, 5000, false);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
and with backgrounds with 1/2 of original size and it's working now.
Is this behaviour expectable? Looks like graphic memory issues.


My HD background has 2048*3 = 6144, so if I create my parallax like:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parallaxLayer = new ParallaxLayer(camera, false, 10000, false);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

It works.

If i use LOW background 1024*3 = 3072
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. parallaxLayer = new ParallaxLayer(camera, false, 5000, false);
Parsed in 0.011 seconds, using GeSHi 1.0.8.4

It works too.

Shouldn't I be passing the background exactly size?
I'm missing something here but it's working. I'll read you code as soon as I can.
GuilhE
 
Posts: 23
Joined: Fri Nov 16, 2012 4:26 pm

PreviousNext

Return to Features

Who is online

Users browsing this forum: No registered users and 10 guests