Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LibGDX Parallax Scrolling  (Read 4181 times)
0 Members and 1 Guest are viewing this topic.
Offline Slyth2727
« Posted 2013-05-24 02:08:55 »

In a 2D topdown space shooter, it is always classic to have multilevel parallax scrolling space backgrounds. Now, in libGDX I know this is possible, because there is a parallax scrolling demo, but this is without repeating the image. Is there a way, with the libGDX library, to create a multilevel parallax scrolling background?
Thanks, -cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline ReBirth
« Reply #1 - Posted 2013-05-24 02:22:52 »

Split your background image into some chunks and load them in queue then move them together like a train.

Offline Slyth2727
« Reply #2 - Posted 2013-05-24 02:26:54 »

I understand the basic idea of parallax scrolling, however I was unsure of how to implement it using libGDX. Is it ok if I get some code?

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2013-05-24 02:40:07 »

My mind is not on this right now, but basically
1  
2  
3  
4  
5  
6  
7  
stage.addAction(forever(delay(time, run(new Runnable(){
  public void run(){
    Building b = new Building();
    stage.addActor(b);
    b.addAction(sequence(moveTo(-b.getRight(), b.getY()), remove()));
  }
}))));

You need to adjust "time" based on the width (or height if vertical scrolling) of building sprite.

Offline relminator
« Reply #4 - Posted 2013-05-24 03:11:51 »

I'm not a libgdx user but if you can get you camera values, you could just divide your camera position by an arbitrary value and apply that to your parallax layer.

Say camx/2.0f would make your parallax layer move at 1/2 the speed of your base layer.
Offline Slyth2727
« Reply #5 - Posted 2013-05-24 20:26:12 »

ah ok, it was the actual doing it that confused me... I saw all of these advanced examples and got to the point of overthinking it. We all know how that gets with programming Wink

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline Slyth2727
« Reply #6 - Posted 2013-05-24 22:35:54 »

Update con un mas question: I am now using this code for my scrolling:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
package com.slyth.angrymasons.Screens;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class ParallaxLayer {

   private TextureRegion region;
   private float xRatio, yRatio;

   public ParallaxLayer(TextureRegion region, float xRatio, float yRatio) {
      super();
      this.region = region;
      this.xRatio = xRatio;
      this.yRatio = yRatio;
   }

   public TextureRegion getRegion() {
      return region;
   }

   public float getxRatio() {
      return xRatio;
   }

   public float getyRatio() {
      return yRatio;
   }

   public void render(float xPosition, float yPosition, float width, float height, SpriteBatch batch) {
      batch.begin();
      batch.draw(region, xPosition, yPosition, width, height);
      batch.end();
   }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
package com.slyth.angrymasons.Screens;

import java.util.ArrayList;
import java.util.List;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class ParallaxBackground {

   private List<ParallaxLayer> layers;
   private float width, height;

   public ParallaxBackground(float width, float height) {
      this.layers = new ArrayList<ParallaxLayer>();
      this.width = width;
      this.height = height;
   }

   public void render(float xPosition, float yPosition, SpriteBatch batch) {
      for (ParallaxLayer layer : layers) {
         float layerOffsetX = (xPosition * layer.getxRatio() % width);
         float layerOffsetY = (yPosition * layer.getyRatio() % height);
         layer.render(xPosition - width / 2f - layerOffsetX, yPosition - height / 2f - layerOffsetY, width, height, batch);
         layer.render(xPosition - width / 2f - layerOffsetX + 5f, yPosition - height / 2f - layerOffsetY, width, height, batch);
      }
   }

   public List<ParallaxLayer> getLayers() {
      return layers;
   }

   public float getWidth() {
      return width;
   }

   public float getHeight() {
      return height;
   }

   public void addLayer(ParallaxLayer parallaxLayer) {
      layers.add(parallaxLayer);
   }

}


and if I send background the width and height of the screen (Gdx.graphics.getHeight()) then it draws the image large to the point of where I can see the individual pixels. If I use 10 and 10, it draws the image at a more reasonable scale, but certainly not 10 pixels. I would like to get this to the point of where the image is the size of the screen and the images repeat themselves. Any suggestions? Thanks, cMp

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Offline relminator
« Reply #7 - Posted 2013-05-25 01:42:58 »

I'm not quite sure what's happenning with your code but why is the width subtracted from your x-position?

Anyways, here's a 2 layer paralkax bg example in java2d.

rel.phatcode.net/junk.php?id=139
Offline Slyth2727
« Reply #8 - Posted 2013-05-25 01:46:15 »

sorry I fixed the problem already. I should've said so, but yes this problem has been solved

Was I before Chuang Tzu who dreamt about being a butterfly, or am I now a butterfly who dreams about being Chuang Tzu?
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (69 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!