Parallax X & Y Scrolling class

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

Re: Parallax X & Y Scrolling class

Postby cloudHopper » Wed May 18, 2011 3:47 pm

Hi!

First time poster as I'm new to this engine, but already loving what I've been able to do thus far, so many thanks to Nic and all those who have contributed!

I'm after a steer on this parallax class, which on first inspection was just what I was after. I'm looking to pull together a free roaming space scroller, and at present have my basic main sprite facing my SceneTouchEvent location. *mini-woohoo* (easily pleased)...

Initially I swung towards implementing MoveModifiers and Ease'ing to move my main sprite around, but got stuck trying to implement background stitching combined with the camera chasing my sprite, so started to look at parallax, as it seems to lean towards my ideal quiet nicely.

As its a free roaming experience, I need the background to scroll in any and all directions, which the basic parallax is not known for (generally x or y, not both or increments of both), so the classes here, look great...

Am I right in assuming I need to extend these classes further and add my own @Override onUpdate superclass in order to manually trigger a custom movement, or is there more to it? I'm struggling to initially see how I can make this parallax background scroll in a precise direction as my main sprite could be facing any angle from 0-360deg, and the inputs I see revolve around the speed of movement, not the angle the background should be heading in (I guess the speed of each x and y would give that illusion??).

Hoping someone might be able to shed some light on it?

-EDIT-

Ah ha! Just tweaked the AutoHorizontalParallax to handle x and y at the same time (thus it being a Horiz and Vert routine now)....I'm on my way, thanks guys!
cloudHopper
 
Posts: 4
Joined: Wed May 18, 2011 3:30 pm

Re: Parallax X & Y Scrolling class

Postby tajny » Tue May 31, 2011 2:37 pm

Great, can you post source code ?

Paweł P.

--------------------------------------
Portfolio

Run Santa Run Link
Xelorians Link
User avatar
tajny
 
Posts: 216
Joined: Mon May 30, 2011 10:23 pm
Location: Poland

Re: Parallax X & Y Scrolling class

Postby cloudHopper » Mon Jun 06, 2011 12:42 pm

Hasn't turned out how I wanted. From what I could see, setting or offsetting the X/Y details via the methods just made the scrolling jerk or delay by specified increments. From looking at the code, I need to be able to update the pParallaxFactorX and pParallaxFactorY, to make it appear as though the background was changing direction, but they are set as final and have no modifiers.

I can make a background scroll in any direction at a chosen and modifiable speed, but from the point of adding it to the scene, it becomes fixed in that direction. I attempted to add and remove a new parallaxEntity with incremented Factors, but it seemed untidy and laggy for obvious reasons.

I've since gone back to a chaseEntity camera and manually offsetting my background starfield, letting the physicsWorld move my sprite with velocity etc.

I'm still in two minds as to the best solution to be honest. Ideally a method that accepts a velocity (read speed) and angle, that updates the parallax background would be great and could be called from the main game loop. I'm sure it is implementable in a similar way as the autoparallax class, but I'm not gifted enough at present to produce it.

If anyone has a simple answer for this, please let me know, as optimised code is always favourable to a hack in my opinion.
cloudHopper
 
Posts: 4
Joined: Wed May 18, 2011 3:30 pm

Re: Parallax X & Y Scrolling class

Postby lopez1_de » Tue Jun 14, 2011 2:01 pm

Very interesting conversation. Any news on dynamically change the scroll speed?
lopez1_de
 
Posts: 69
Joined: Fri Jun 10, 2011 9:27 am

Re: Parallax X & Y Scrolling class

Postby cloudHopper » Tue Jun 14, 2011 3:31 pm

Actually dynamically changing the scroll speed is quite an easy change to the originally posted AutoParallaxBackground, you can just add a getter/setter for mParallaxChangePerSecond and update it in your game loop/touch events etc.

The issue I'm still having, is dynamically changing the direction of the auto scroll...I've now looked at

- repeatingspritebackgrounds
No good for me, as I'm looking for a very large gaming area. I want my ship to be able to travel for at least 1 minute at full speed in a particular direction ideally (from center of game scene). So I need to find a stitching/streaming solution.
- TMXTiledMaps
An instant no, as this wont be an RPG, up, down, left, right. You can travel in any of the 360 degree directions based on touch.

which always seems to bring me back to parallax backgrounds. All I need, is to work out the math involved to convert the direction my player ship is facing in degrees or rads into appropriate X and Y values used by the parallax class, so the background scrolls in the direction my ship is facing. I'll just have to manually adjust those values once the scene is touched (and add some easing, so it changes direction smoothly).
cloudHopper
 
Posts: 4
Joined: Wed May 18, 2011 3:30 pm

Re: Parallax X & Y Scrolling class

Postby lopez1_de » Tue Jun 14, 2011 4:20 pm

Hi cloudHopper,

thanks for this input. I already added the getters/setters for mParallaxChangePerSecond by myself and come back to this thread to post the solution. ;)
If you change mParallaxChangePerSecond from a positive to a negative value you can change the direction. Works for me. EDIT: after reading again. this is propably not enough for your desires. :lol:

I think the best solution for what you like to do is using tiles. I don't know enough about TMX but I know that most fast Jump & Runs like Mario or Sonic, where the characters are able to move to any direction (360), are using tiles. The are loading them if they come in view and unloading them later. There was a source code for Sonic in Actionscript (flash) anywhere on the web and a page which describes the loading and unloading. Very hard to understand i think. :roll:
lopez1_de
 
Posts: 69
Joined: Fri Jun 10, 2011 9:27 am

Re: Parallax X & Y Scrolling class

Postby cloudHopper » Wed Jun 15, 2011 10:47 am

To better explain the effect I'm looking to reproduce, WarPath seems to be the best example. Its got 3-4 layered Parallax background, and the ship moves via touch. Also the game area is a decent size, although I'm looking to create a game area 4-5 times larger.

I'm looking at math such as:

mParallaxValueX = mParallaxChangePerSecond * cos (angle);
mParallaxValueY = mParallaxChangePerSecond * sin (angle);

to increment the parallax background manually, but having mixed results. I'm guessing I'm confusing degrees and rads and Cartesian and polar coordinates.....I'll keep trying.
cloudHopper
 
Posts: 4
Joined: Wed May 18, 2011 3:30 pm

Re: Parallax X & Y Scrolling class

Postby hungryish » Wed Aug 08, 2012 10:36 pm

I know it's been a while, but if anyone is interested, I've updated the original class to work with the GLES2 branch.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.dustypixels.andengine.entity.scene.background;
  3.  
  4. import java.util.ArrayList;
  5.  
  6. import org.andengine.engine.camera.Camera;
  7. import org.andengine.entity.scene.background.Background;
  8. import org.andengine.entity.shape.IAreaShape;
  9. import org.andengine.opengl.util.GLState;
  10.  
  11.  
  12. /**
  13.  * @author Oldskool73
  14.  *
  15.  * Parallax background that scrolls in both X and/or Y directions.
  16.  *
  17.  * Usage:
  18.  *
  19.  * ...x & y free scrolling tiled background...
  20.  * mParallaxBackground.addParallaxEntity(new ParallaxBackground2d.ParallaxBackground2dEntity(-0.2f,-0.2f, new Sprite(0, 0, this.mParallaxLayerStars)));
  21.  *
  22.  * ...side scroller repeating strip...
  23.  * mParallaxBackground.addParallaxEntity(new ParallaxBackground2d.ParallaxBackground2dEntity(-0.4f, 0.0f, new Sprite(0, 100, this.mParallaxLayerHills),true,false));
  24.  *
  25.  * ...vertical scroller repeating strip...
  26.  * mParallaxBackground.addParallaxEntity(new ParallaxBackground2d.ParallaxBackground2dEntity(-0.0f,-0.4f, new Sprite(100, 0, this.mParallaxLayerHills),false,true));
  27.  *
  28.  * ...non repeating positioned item...
  29.  * mParallaxBackground.addParallaxEntity(new ParallaxBackground2d.ParallaxBackground2dEntity(-0.4f,-0.4f, new Sprite(100, 100, this.mParallaxLayerSun),false,false,true));
  30.  *
  31.  *
  32.  */
  33. public class ParallaxBackground2d extends Background {
  34.         // ===========================================================
  35.         // Constants
  36.         // ===========================================================
  37.  
  38.         // ===========================================================
  39.         // Fields
  40.         // ===========================================================
  41.  
  42.         private final ArrayList<ParallaxBackground2dEntity> mParallaxEntities = new ArrayList<ParallaxBackground2dEntity>();
  43.         private int mParallaxEntityCount;
  44.  
  45.         protected float mParallaxValueX;
  46.         protected float mParallaxValueY;
  47.  
  48.         // ===========================================================
  49.         // Constructors
  50.         // ===========================================================
  51.  
  52.         public ParallaxBackground2d(float pRed, float pGreen, float pBlue) {
  53.                 super(pRed, pGreen, pBlue);
  54.         }
  55.  
  56.         // ===========================================================
  57.         // Getter & Setter
  58.         // ===========================================================
  59.  
  60.         public void setParallaxValue(final float pParallaxValueX, final float pParallaxValueY) {
  61.                 this.mParallaxValueX = pParallaxValueX;
  62.                 this.mParallaxValueY = pParallaxValueY;
  63.         }
  64.        
  65.         public void offsetParallaxValue(final float pParallaxValueX, final float pParallaxValueY) {
  66.                 this.mParallaxValueX += pParallaxValueX;
  67.                 this.mParallaxValueY += pParallaxValueY;
  68.         }
  69.  
  70.         // ===========================================================
  71.         // Methods for/from SuperClass/Interfaces
  72.         // ===========================================================
  73.  
  74.         @Override
  75.         public void onDraw(final GLState pGLState, final Camera pCamera) {
  76.                 super.onDraw(pGLState, pCamera);
  77.  
  78.                 final float parallaxValueX = this.mParallaxValueX;
  79.                 final float parallaxValueY = this.mParallaxValueY;
  80.                 final ArrayList<ParallaxBackground2dEntity> parallaxEntities = this.mParallaxEntities;
  81.  
  82.                 for(int i = 0; i < this.mParallaxEntityCount; i++) {
  83.                         parallaxEntities.get(i).onDraw(pGLState, parallaxValueX, parallaxValueY, pCamera);
  84.                 }
  85.         }
  86.        
  87.         // ===========================================================
  88.         // Methods
  89.         // ===========================================================
  90.  
  91.        
  92.         public void attachParallaxEntity(final ParallaxBackground2dEntity pParallaxEntity) {
  93.                 this.mParallaxEntities.add(pParallaxEntity);
  94.                 this.mParallaxEntityCount++;
  95.         }
  96.  
  97.         public boolean detachParallaxEntity(final ParallaxBackground2dEntity pParallaxEntity) {
  98.                 this.mParallaxEntityCount--;
  99.                 final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
  100.                 if(!success) {
  101.                         this.mParallaxEntityCount++;
  102.                 }
  103.                 return success;
  104.         }
  105.        
  106.        
  107.         // ===========================================================
  108.         // Inner and Anonymous Classes
  109.         // ===========================================================
  110.  
  111.         public static class ParallaxBackground2dEntity {
  112.                 // ===========================================================
  113.                 // Constants
  114.                 // ===========================================================
  115.  
  116.                 // ===========================================================
  117.                 // Fields
  118.                 // ===========================================================
  119.  
  120.                 final float mParallaxFactorX;
  121.                 final float mParallaxFactorY;
  122.                 final Boolean mRepeatX;
  123.                 final Boolean mRepeatY;        
  124.                 final IAreaShape mShape;
  125.                 final Boolean mShouldCull;
  126.  
  127.                 // ===========================================================
  128.                 // Constructors
  129.                 // ===========================================================
  130.  
  131.                 // add a repeating x & y texture fill
  132.                 public ParallaxBackground2dEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape) {
  133.                         this.mParallaxFactorX = pParallaxFactorX;
  134.                         this.mParallaxFactorY = pParallaxFactorY;
  135.                         this.mRepeatX = true;
  136.                         this.mRepeatY = true;
  137.                         this.mShouldCull = false;
  138.                         this.mShape = pShape;
  139.                 }
  140.  
  141.                 // add an x or y only repeating strip
  142.                 public ParallaxBackground2dEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape, final Boolean pRepeatX, final Boolean pRepeatY) {
  143.                         this.mParallaxFactorX = pParallaxFactorX;
  144.                         this.mParallaxFactorY = pParallaxFactorY;
  145.                         this.mRepeatX = pRepeatX;
  146.                         this.mRepeatY = pRepeatY;
  147.                         this.mShouldCull = false;
  148.                         this.mShape = pShape;
  149.                 }                      
  150.                
  151.                 // add an x or y only repeating strip or non repeating feature that may be culled when off screen
  152.                 public ParallaxBackground2dEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape, final Boolean pRepeatX, final Boolean pRepeatY, final Boolean pShouldCull) {
  153.                         this.mParallaxFactorX = pParallaxFactorX;
  154.                         this.mParallaxFactorY = pParallaxFactorY;
  155.                         this.mRepeatX = pRepeatX;
  156.                         this.mRepeatY = pRepeatY;
  157.                         this.mShouldCull = (pRepeatX && pRepeatY)? false : pShouldCull;
  158.                         this.mShape = pShape;
  159.                 }
  160.  
  161.                
  162.                 // ===========================================================
  163.                 // Getter & Setter
  164.                 // ===========================================================
  165.  
  166.                 // ===========================================================
  167.                 // Methods for/from SuperClass/Interfaces
  168.                 // ===========================================================
  169.  
  170.                 // ===========================================================
  171.                 // Methods
  172.                 // ===========================================================
  173.  
  174.                 public void onDraw(final GLState pGLState, final float pParallaxValueX, final float pParallaxValueY, final Camera pCamera) {
  175.                         pGLState.pushModelViewGLMatrix();
  176.                         {
  177.                                 final float cameraWidth = pCamera.getWidth();
  178.                                 final float cameraHeight = pCamera.getHeight();
  179.                                 final float shapeWidthScaled = this.mShape.getWidthScaled();
  180.                                 final float shapeHeightScaled = this.mShape.getHeightScaled();
  181.  
  182.                                 //reposition
  183.                                 float baseOffsetX = (pParallaxValueX * this.mParallaxFactorX);
  184.                                 if (this.mRepeatX) {
  185.                                         baseOffsetX = baseOffsetX % shapeWidthScaled;
  186.                                         while(baseOffsetX > 0) {
  187.                                                 baseOffsetX -= shapeWidthScaled;
  188.                                         }
  189.                                 }                      
  190.                                 float baseOffsetY = (pParallaxValueY * this.mParallaxFactorY);
  191.                                 if (this.mRepeatY) {
  192.                                         baseOffsetY = baseOffsetY % shapeHeightScaled;
  193.                                         while(baseOffsetY > 0) {
  194.                                                 baseOffsetY -= shapeHeightScaled;
  195.                                         }                              
  196.                                 }
  197.                                
  198.                                 //optionally screen cull non repeating items
  199.                                 Boolean culled = false;
  200.                                 if (mShouldCull) {
  201.                                         if (!this.mRepeatX) {
  202.                                                 if ((baseOffsetY + (shapeHeightScaled*2) < 0) || (baseOffsetY > cameraHeight)) {
  203.                                                         culled = true;
  204.                                                 }
  205.                                         }      
  206.                                         if (!this.mRepeatY) {
  207.                                                 if ((baseOffsetX + (shapeWidthScaled*2) < 0) || (baseOffsetX > cameraWidth)) {
  208.                                                         culled = true;
  209.                                                 }
  210.                                         }
  211.                                 }
  212.                                
  213.                                 if (!culled) {
  214.                                         //draw
  215.                                         pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);
  216.                                         float currentMaxX = baseOffsetX;
  217.                                         float currentMaxY = baseOffsetY;
  218.                                         do {                                                                                                            //rows
  219.                                                 this.mShape.onDraw(pGLState, pCamera);
  220.                                                 if (this.mRepeatY) {
  221.                                                         currentMaxY = baseOffsetY;                                                     
  222.                                                         do {                                                                                            //columns
  223.                                                                 pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
  224.                                                                 currentMaxY += shapeHeightScaled;                                              
  225.                                                                 this.mShape.onDraw(pGLState, pCamera);
  226.                                                         } while(currentMaxY < cameraHeight);                            //end columns
  227.                                                         pGLState.translateModelViewGLMatrixf(0, -currentMaxY + baseOffsetY, 0);                                
  228.                                                 }
  229.                                                 pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
  230.                                                 currentMaxX += shapeWidthScaled;
  231.                                         } while (this.mRepeatX && currentMaxX < cameraWidth);           //end rows
  232.                                 }
  233.                         }
  234.                         pGLState.popModelViewGLMatrix();
  235.                 }
  236.  
  237.                 // ===========================================================
  238.                 // Inner and Anonymous Classes
  239.                 // ===========================================================
  240.         }
  241. }
  242.  
  243.  
  244.  
  245.  
Parsed in 0.021 seconds, using GeSHi 1.0.8.4
hungryish
 
Posts: 2
Joined: Fri Jul 13, 2012 7:54 pm

Re: Parallax X & Y Scrolling class

Postby RyugaZ » Sun Dec 23, 2012 11:07 pm

Hi everybody,
after having done one of myself I found this ParallaxBackground2D that has all I needed plus the non repeating items.
So I switched to it and added zoom, which I now post to you. I also found a bug that was afflicting my parallax too and I patched it (it's in the while loop for the x and y displacement).
Still there's some glitch (sometimes) where the shape of a ParallaxBackgroundEntry gets drawn twice. The connection is not perfect and an odd line shows up. This happens only on the X coordinates. I think the glitch results form and rounded result in the addiction of the two floats (currentMaxX += shapeWidthScaled;).
If it will annoy my game I'll try a fix.

Hope you'll use this background and that it becomes the only available in AndEngine (with it's two autoparallax) since it's the most complete one.

If you find some bugs, please let me know.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package game.levels.backgrounds;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.andengine.engine.camera.Camera;
  6. import org.andengine.entity.scene.background.Background;
  7. import org.andengine.entity.shape.IAreaShape;
  8. import org.andengine.opengl.util.GLState;
  9.  
  10. /**
  11.  * @author Oldskool73
  12.  *
  13.  *         Parallax background that scrolls in both X and/or Y directions.
  14.  *
  15.  *         Usage:
  16.  *
  17.  *         ...x & y free scrolling tiled background...
  18.  *         mParallaxBackground.addParallaxEntity(new
  19.  *         ParallaxBackground2d.ParallaxBackground2dEntity(-0.2f,-0.2f, new
  20.  *         Sprite(0, 0, this.mParallaxLayerStars)));
  21.  *
  22.  *         ...side scroller repeating strip...
  23.  *         mParallaxBackground.addParallaxEntity(new
  24.  *         ParallaxBackground2d.ParallaxBackground2dEntity(-0.4f, 0.0f, new
  25.  *         Sprite(0, 100, this.mParallaxLayerHills),true,false));
  26.  *
  27.  *         ...vertical scroller repeating strip...
  28.  *         mParallaxBackground.addParallaxEntity(new
  29.  *         ParallaxBackground2d.ParallaxBackground2dEntity(-0.0f,-0.4f, new
  30.  *         Sprite(100, 0, this.mParallaxLayerHills),false,true));
  31.  *
  32.  *         ...non repeating positioned item...
  33.  *         mParallaxBackground.addParallaxEntity(new
  34.  *         ParallaxBackground2d.ParallaxBackground2dEntity(-0.4f,-0.4f, new
  35.  *         Sprite(100, 100, this.mParallaxLayerSun),false,false,true));
  36.  *
  37.  *
  38.  */
  39. public class ParallaxBackground extends Background {
  40.         // ===========================================================
  41.         // Constants
  42.         // ===========================================================
  43.  
  44.         // ===========================================================
  45.         // Fields
  46.         // ===========================================================
  47.  
  48.         private final ArrayList<ParallaxBackgroundEntity> mParallaxEntities = new ArrayList<ParallaxBackgroundEntity>();
  49.         private int mParallaxEntityCount;
  50.  
  51.         protected float mParallaxValueX;
  52.         protected float mParallaxValueY;
  53.         protected float mZoomFactor;
  54.  
  55.         // ===========================================================
  56.         // Constructors
  57.         // ===========================================================
  58.  
  59.         public ParallaxBackground(float pRed, float pGreen, float pBlue) {
  60.                 super(pRed, pGreen, pBlue);
  61.         }
  62.  
  63.         // ===========================================================
  64.         // Getter & Setter
  65.         // ===========================================================
  66.  
  67.         public void setParallaxValue(final float pParallaxValueX, final float pParallaxValueY) {
  68.                 this.mParallaxValueX = pParallaxValueX;
  69.                 this.mParallaxValueY = pParallaxValueY;
  70.         }
  71.  
  72.         public void setZoomFactor(final float pZoomFactor) {
  73.                 this.mZoomFactor = pZoomFactor;
  74.         }
  75.  
  76.         public void offsetParallaxValue(final float pParallaxValueX, final float pParallaxValueY) {
  77.                 this.mParallaxValueX += pParallaxValueX;
  78.                 this.mParallaxValueY += pParallaxValueY;
  79.         }
  80.  
  81.         // ===========================================================
  82.         // Methods for/from SuperClass/Interfaces
  83.         // ===========================================================
  84.  
  85.         @Override
  86.         public void onDraw(final GLState pGLState, final Camera pCamera) {
  87.                 super.onDraw(pGLState, pCamera);
  88.  
  89.                 final float zoomFactor = this.mZoomFactor;
  90.                 final float parallaxValueX = this.mParallaxValueX;
  91.                 final float parallaxValueY = this.mParallaxValueY;
  92.                 final ArrayList<ParallaxBackgroundEntity> parallaxEntities = this.mParallaxEntities;
  93.  
  94.                 for (int i = 0; i < this.mParallaxEntityCount; i++) {
  95.                         parallaxEntities.get(i).onDraw(pGLState, parallaxValueX, parallaxValueY, zoomFactor, pCamera);
  96.                 }
  97.         }
  98.  
  99.         // ===========================================================
  100.         // Methods
  101.         // ===========================================================
  102.  
  103.         public void attachParallaxEntity(final ParallaxBackgroundEntity pParallaxEntity) {
  104.                 this.mParallaxEntities.add(pParallaxEntity);
  105.                 this.mParallaxEntityCount++;
  106.         }
  107.  
  108.         public boolean detachParallaxEntity(final ParallaxBackgroundEntity pParallaxEntity) {
  109.                 this.mParallaxEntityCount--;
  110.                 final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
  111.                 if (!success) {
  112.                         this.mParallaxEntityCount++;
  113.                 }
  114.                 return success;
  115.         }
  116.  
  117.         // ===========================================================
  118.         // Inner and Anonymous Classes
  119.         // ===========================================================
  120.  
  121.         public static class ParallaxBackgroundEntity {
  122.                 // ===========================================================
  123.                 // Constants
  124.                 // ===========================================================
  125.  
  126.                 // ===========================================================
  127.                 // Fields
  128.                 // ===========================================================
  129.  
  130.                 final float mParallaxFactorX;
  131.                 final float mParallaxFactorY;
  132.                 final Boolean mRepeatX;
  133.                 final Boolean mRepeatY;
  134.                 final IAreaShape mShape;
  135.                 final Boolean mShouldCull;
  136.  
  137.                 // ===========================================================
  138.                 // Constructors
  139.                 // ===========================================================
  140.  
  141.                 // add a repeating x & y texture fill
  142.                 public ParallaxBackgroundEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape) {
  143.                         this.mParallaxFactorX = pParallaxFactorX;
  144.                         this.mParallaxFactorY = pParallaxFactorY;
  145.                         this.mRepeatX = true;
  146.                         this.mRepeatY = true;
  147.                         this.mShouldCull = false;
  148.                         this.mShape = pShape;
  149.                 }
  150.  
  151.                 // add an x or y only repeating strip
  152.                 public ParallaxBackgroundEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape, final Boolean pRepeatX, final Boolean pRepeatY) {
  153.                         this.mParallaxFactorX = pParallaxFactorX;
  154.                         this.mParallaxFactorY = pParallaxFactorY;
  155.                         this.mRepeatX = pRepeatX;
  156.                         this.mRepeatY = pRepeatY;
  157.                         this.mShouldCull = false;
  158.                         this.mShape = pShape;
  159.                 }
  160.  
  161.                 // add an x or y only repeating strip or non repeating feature that may
  162.                 // be culled when off screen
  163.                 public ParallaxBackgroundEntity(final float pParallaxFactorX, final float pParallaxFactorY, final IAreaShape pShape, final Boolean pRepeatX, final Boolean pRepeatY, final Boolean pShouldCull) {
  164.                         this.mParallaxFactorX = pParallaxFactorX;
  165.                         this.mParallaxFactorY = pParallaxFactorY;
  166.                         this.mRepeatX = pRepeatX;
  167.                         this.mRepeatY = pRepeatY;
  168.                         this.mShouldCull = (pRepeatX && pRepeatY) ? false : pShouldCull;
  169.                         this.mShape = pShape;
  170.                 }
  171.  
  172.                 // ===========================================================
  173.                 // Getter & Setter
  174.                 // ===========================================================
  175.  
  176.                 // ===========================================================
  177.                 // Methods for/from SuperClass/Interfaces
  178.                 // ===========================================================
  179.  
  180.                 // ===========================================================
  181.                 // Methods
  182.                 // ===========================================================
  183.  
  184.                 public void onDraw(final GLState pGLState, final float pParallaxValueX, final float pParallaxValueY, final float pZoomFactor, final Camera pCamera) {
  185.                         pGLState.pushModelViewGLMatrix();
  186.                         {
  187.                                 if (this.mParallaxFactorX != 0) this.mShape.setScaleX(pZoomFactor / this.mParallaxFactorX);
  188.                                 if (this.mParallaxFactorY != 0) this.mShape.setScaleY(pZoomFactor / this.mParallaxFactorY);
  189.  
  190.                                 final float cameraWidth = pCamera.getWidth();
  191.                                 final float cameraHeight = pCamera.getHeight();
  192.                                 final float shapeWidthScaled = this.mShape.getWidthScaled();
  193.                                 final float shapeHeightScaled = this.mShape.getHeightScaled();
  194.  
  195.                                 // reposition
  196.                                 float baseOffsetX = (pParallaxValueX * this.mParallaxFactorX);
  197.                                 if (this.mRepeatX) {
  198.                                         baseOffsetX = baseOffsetX % shapeWidthScaled;
  199.                                         while (baseOffsetX > -shapeWidthScaled) {
  200.                                                 baseOffsetX -= shapeWidthScaled;
  201.                                         }
  202.                                 }
  203.                                 float baseOffsetY = (pParallaxValueY * this.mParallaxFactorY);
  204.                                 if (this.mRepeatY) {
  205.                                         baseOffsetY = baseOffsetY % shapeHeightScaled;
  206.                                         while (baseOffsetY > -(2 * shapeHeightScaled)) {
  207.                                                 baseOffsetY -= shapeHeightScaled;
  208.                                         }
  209.                                 }
  210.  
  211.                                 // optionally screen cull non repeating items
  212.                                 Boolean culled = false;
  213.                                 if (mShouldCull) {
  214.                                         if (!this.mRepeatX) {
  215.                                                 if ((baseOffsetY + (shapeHeightScaled * 2) < 0) || (baseOffsetY > cameraHeight)) {
  216.                                                         culled = true;
  217.                                                 }
  218.                                         }
  219.                                         if (!this.mRepeatY) {
  220.                                                 if ((baseOffsetX + (shapeWidthScaled * 2) < 0) || (baseOffsetX > cameraWidth)) {
  221.                                                         culled = true;
  222.                                                 }
  223.                                         }
  224.                                 }
  225.  
  226.                                 if (!culled) {
  227.                                         // draw
  228.                                         pGLState.translateModelViewGLMatrixf(baseOffsetX, baseOffsetY, 0);
  229.                                         float currentMaxX = baseOffsetX;
  230.                                         float currentMaxY = baseOffsetY;
  231.                                         do { // rows
  232.                                                 this.mShape.onDraw(pGLState, pCamera);
  233.                                                 if (this.mRepeatY) {
  234.                                                         currentMaxY = baseOffsetY;
  235.                                                         do { // columns
  236.                                                                 pGLState.translateModelViewGLMatrixf(0, shapeHeightScaled, 0);
  237.                                                                 currentMaxY += shapeHeightScaled;
  238.                                                                 this.mShape.onDraw(pGLState, pCamera);
  239.                                                         } while (currentMaxY < cameraHeight); // end columns
  240.                                                         pGLState.translateModelViewGLMatrixf(0, -currentMaxY + baseOffsetY, 0);
  241.                                                 }
  242.                                                 pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
  243.                                                 currentMaxX += shapeWidthScaled;
  244.                                         } while (this.mRepeatX && currentMaxX < cameraWidth); // end
  245.                                                                                                                                                         // rows
  246.                                 }
  247.                         }
  248.                         pGLState.popModelViewGLMatrix();
  249.                 }
  250.  
  251.                 // ===========================================================
  252.                 // Inner and Anonymous Classes
  253.                 // ===========================================================
  254.         }
  255. }
  256.  
Parsed in 0.022 seconds, using GeSHi 1.0.8.4
RyugaZ
 
Posts: 98
Joined: Fri Mar 09, 2012 4:36 pm

Re: Parallax X & Y Scrolling class

Postby Matthias » Sun Dec 30, 2012 10:02 pm

Hi RyugaZ,

could you maybe provide some example code on how to use it? I'm having a little trouble getting it working...

thanks a lot and best regards,
Matthias
Matthias
 
Posts: 449
Joined: Thu Jun 14, 2012 2:50 pm
Location: Cologne, Germany

PreviousNext

Return to Features

Who is online

Users browsing this forum: No registered users and 7 guests