Background with randomly moving elements in LibGDX

Started by
1 comment, last by devO6 8 years, 3 months ago

Hello there!

I'm looking to implement a 'dynamic' background (meaning, with objects moving randomly) in my libgdx game, something similar to what you can see in this video (from the Google Play game 'Finger Dodge'):

In the above example, the background has a bunch of moving squares and triangles (and some blinks) that move randomly, independently from each other. So, what exactly should be the best approach to get something like that? I'm guessing that those background elements aren't just a bunch of single, independent textures that all get rendered at the same time with a random direction, because that would seem a bit TOO much stuff to render... Or am i wrong and that's what it might be?

So, is there any way to consistently and efficiently achieve this effect in libgdx (or if not, any other way)?

Thanks in advance! happy.png

Advertisement
I'm guessing that those background elements aren't just a bunch of single, independent textures that all get rendered at the same time with a random direction, because that would seem a bit TOO much stuff to render...

They probably are, it's really not all that intensive to render 2D textures. I've seen people render over a few thousand independent entities in Java. What you really want to do is utilize psudo-random numbers.

Simply do something like this below:

NOTE: I do not add a function to destroy old objects, you will need to do this if you want to reduce lag over its lifespan.

ALSO NOTE: This is using Java2D, which comparatively to OpenGL is much slower. So, even using a less optimized way to draw things you still don't really see any lag.

Here is a link to the executable jar using the code below. Example.jar

'Falling Object' class.


import java.awt.*;

/**
 * Created by Brendyn Todd on 1/14/2016.
 */
public class FallingObject {

    public int x,y;

    public FallingObject(int x){
        this.x=x;
    }

    public void render(Graphics g, double delta){

        //Draw the square down by 2 every step.
        y += 2;

        //Render a white square on the screen.
        g.setColor(Color.white);
        g.drawRect(x,y, 25, 25);
    }
}

'Main' class


import com.framework.framework.Framework;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

import com.framework.display.Window;

/**
 * Created by Brendyn Todd on 1/14/2016.
 */
public class Main extends Framework {

    ArrayList<FallingObject> objects = new ArrayList<FallingObject>();
    double time, lastTime = 15;

    public Main(){

        //Do not mind these boilerplate lines of code. They are from my own personal Framework.
        register(new Window("Example", 480, 320), null);
        start();

    }

    public void update(Graphics g, double delta) {

        //Set the background color.
        g.setColor(Color.black);
        
        //Draw the black background to our window size. 480w x 320h
        g.fillRect(0,0,480,320);

        //Allows us to control time. Woooooo.
        time += delta;

        //Checks if enough time has passed.
        if(time > lastTime){

            addObject();

            //Update the last time we created a new object.
            lastTime = time + 15;
        }

        //Loop through and render the objects.
        for(int i = 0; i < objects.size(); i++){
            FallingObject fo = objects.get(i);

            fo.render(g,delta);
        }
    }

    public void addObject(){
        
        /Initialize the Random class.
        Random random = new Random();

        //Set the objects X Position to a random location within the windows width.
        int xPos = random.nextInt(480);

        //Add it to the array list to render it, also setting its random xPosition.
        objects.add(new FallingObject(xPos));
    }

    //BEGIN!
    public static void main(String[] args){
        new Main();
    }
}

I'm guessing that those background elements aren't just a bunch of single, independent textures that all get rendered at the same time with a random direction, because that would seem a bit TOO much stuff to render...

They probably are, it's really not all that intensive to render 2D textures. I've seen people render over a few thousand independent entities in Java. What you really want to do is utilize psudo-random numbers.

Simply do something like this below:

NOTE: I do not add a function to destroy old objects, you will need to do this if you want to reduce lag over its lifespan.

ALSO NOTE: This is using Java2D, which comparatively to OpenGL is much slower. So, even using a less optimized way to draw things you still don't really see any lag.

Here is a link to the executable jar using the code below. Example.jar

'Falling Object' class.


import java.awt.*;

/**
 * Created by Brendyn Todd on 1/14/2016.
 */
public class FallingObject {

    public int x,y;

    public FallingObject(int x){
        this.x=x;
    }

    public void render(Graphics g, double delta){

        //Draw the square down by 2 every step.
        y += 2;

        //Render a white square on the screen.
        g.setColor(Color.white);
        g.drawRect(x,y, 25, 25);
    }
}

'Main' class


import com.framework.framework.Framework;

import java.awt.*;
import java.util.ArrayList;
import java.util.Random;

import com.framework.display.Window;

/**
 * Created by Brendyn Todd on 1/14/2016.
 */
public class Main extends Framework {

    ArrayList<FallingObject> objects = new ArrayList<FallingObject>();
    double time, lastTime = 15;

    public Main(){

        //Do not mind these boilerplate lines of code. They are from my own personal Framework.
        register(new Window("Example", 480, 320), null);
        start();

    }

    public void update(Graphics g, double delta) {

        //Set the background color.
        g.setColor(Color.black);
        
        //Draw the black background to our window size. 480w x 320h
        g.fillRect(0,0,480,320);

        //Allows us to control time. Woooooo.
        time += delta;

        //Checks if enough time has passed.
        if(time > lastTime){

            addObject();

            //Update the last time we created a new object.
            lastTime = time + 15;
        }

        //Loop through and render the objects.
        for(int i = 0; i < objects.size(); i++){
            FallingObject fo = objects.get(i);

            fo.render(g,delta);
        }
    }

    public void addObject(){
        
        /Initialize the Random class.
        Random random = new Random();

        //Set the objects X Position to a random location within the windows width.
        int xPos = random.nextInt(480);

        //Add it to the array list to render it, also setting its random xPosition.
        objects.add(new FallingObject(xPos));
    }

    //BEGIN!
    public static void main(String[] args){
        new Main();
    }
}

Damn i wasn't aware that, as you said, rendering multiple 2D textures isn't that expensive really ^^. Got the main idea now, thanks a lot for the detailed example! :)

This topic is closed to new replies.

Advertisement