Parallax scrolling background with zoom functionality

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 ;)). 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 ;D), 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


       /**
	 * 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 ;). I am grateful for any ideas or references you have that may help me :).