Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
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  
  Parallax scrolling background with zoom functionality  (Read 1064 times)
0 Members and 1 Guest are viewing this topic.
Offline LWS

Innocent Bystander





« Posted 2009-09-24 22:18:02 »

First of all hi everyone. I'm a new member but long-time fan of the various java games out there.

So a while back I decided I wanted to make a game of my own, a space shooter game to be precise (yes I know it's been done  Wink). Naturally I decided to go with Java as a the programming language, and started learning about the various libraries out there; in the end choosing to use JOGL for graphics and JBox2D for physics simulation. But this is just in way of an introdution, and is not what my question is about.

I have already implemented a few things, but I now need to create a background for the game (after all, it'd be dull to fly on black background all the time  Grin), and since the game is to be 2D I wanted to make a parallax scrolling background of stars, where each star is presented as a sprite.

Here's a few more details: The view into the world is represented by an instance of the ViewPort class which contains the world coordinates of the edges, the centre and additionally the width and height of the current view (as well as pixels per world unit variable used for scaling). When the view is moved all these variables are updated accordingly. I have already implemented this for simple moving the ViewPort centre, but I am still working on changing the width and height of the ViewPort - i.e. zooming in and out.

At the moment each star is represented by a particle class like so

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  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
       /**
    * The Particle class is used to represent single particles on the Sprite Layer. Particle coordinates are
    * relative to the ViewPort position and size (floating point coordinates going from 0 to 1, where 0.5,0.5
    * is the center of the ViewPort).
    *
    * @author LWS
    *
    */

   private class Particle implements Comparable<Particle> {
      public float size;
      public float xPos, yPos;
      public int textureIndex;
      public float[] color;
      public float relSpeed;

      public String toString() {
         return String.format("Particle at %.2,%.2 scaled to %.1 original size.");
      }

      public Particle(float x, float y, float scale, float relSpeed, int texIndex, float[] color) {
         xPos = x;
         yPos = y;
         this.size = scale;
         this.relSpeed = relSpeed;
         textureIndex = texIndex;
         this.color = color;
      }

      /**
       * Draw the particle in the ViewPort relative to the ViewPorts dimensions and the particles location.
       * Ensure there is a sufficient border below and left of the ViewPort so particles don't just 'pop in'.
       *
       * @param gl
                 * @param v
       */

      public void drawParticle(GL gl, ViewPort v) {
         float xAddm = xAdd * v.pixelFactor;
         float yAddm = yAdd * v.pixelFactor;
         float x0 = xPos * (v.width + xAddm) - xAddm;
         float x1 = x0 + size/v.pixelFactor;
         float y0 = yPos * (v.height + yAddm) - yAddm;
         float y1 = y0 + size/v.pixelFactor;

                        //Draw
        gl.glColor3f(color[0], color[1], color[2]);
         gl.glBegin(GL.GL_QUADS);
         gl.glTexCoord2f(0, 0);
         gl.glVertex3f(x0, y0, 0);
         gl.glTexCoord2f(1, 0);
         gl.glVertex3f(x1, y0, 0);
         gl.glTexCoord2f(1, 1);
         gl.glVertex3f(x1, y1, 0);
         gl.glTexCoord2f(0, 1);
         gl.glVertex3f(x0, y1, 0);
         gl.glEnd();
      }

      @Override
      public int compareTo(Particle o) {
         return textureIndex - o.textureIndex;
      }
   }


They are initialised in an array structure of predetermined size by a factory which assigns random values - inside specified ranges - to the variables. This works fine when only moving the ViewPort without zooming, but now I'm a bit stumped as to how I might implement zooming functionality. I was envisioning something like adding an alpha value to the particel properties and changing the vertex color with it; decreasing the value when zooming out for example. Whenever an alpha value should drop below a threshold (due to a sufficient zoom out) the particle would be removed and replaced (or more likely have its variables changed accordingly) by a Particle that would be positioned in the area that has now become visible because of the zoom out. The opposite would happen when zooming in, where particles outside the smaller ViewPort would get replaced by new particles inside the ViewPort at a very low alpha value. I just get confused about which coordinate system to use for the sprites now. As it is above I'm using a per unit coordinate system of screen space, but this is probably not suited for zooming.

At this stage I'm just thinking about a design for this, brainstorming really  Wink. I am grateful for any ideas or references you have that may help me  Smiley.
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.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!