javaandroidlibgdx

How to create random and Infinite walls with collision and a ball in libgdx?


I'm really new to Android Game Development but I have some experience with the standart Application Development.

What I want to create is a "TutorialGame" for learning purpose. There are no enemies or anything else. It's just plain jumping from side to side wihtout any reason. As I said it's just for learning the code!

I started creating a little ball with LIBGDX's ShapeRenderer:

    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.circle(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, Gdx.graphics.getWidth() / 20);
    shapeRenderer.setColor(1,0,0,1);
    shapeRenderer.end();

The question ist now: Is this even the right way to start with something like this? I read about other classes in LIBGDX like circle and body but as there aren't really any tutorials on those and I'm a beginner I don't really know which class needs to be used here. Of course I will need infinite walls later on and both walls and ball should have collision (I think so?) but I don't really know how to do that.

I appreciate any answer to help me understand LIBGDX a bit more. Thanks in advance!


Solution

  • You have to break your problem down into smaller individual steps.

    First, get your drawing working. You're correct to be using ShapeRenderer, and more info on how to use that class (and many other classes) can be found in the libGDX API and the libGDX wiki. Can you draw a circle in the center of the screen?

    Once you have your drawing working, then worry about the animation. Can you make it so that circle bounces around the screen? You can use pretty basic if statements to figure out when the circle has touched the edge of a screen, and that same logic might be used to add a "wall" to each side of the screen. There really isn't a need for Box2D here.

    Think about it this way: you don't actually need infinite walls, you just need to make it look like you do. You might do that by simply drawing the wall decorations on either side of the screen and moving them down a little bit each frame.

    Since you asked about it, the Circle class is simply a class that holds information about a circle: its location and its radius. That class also contains functions for testing whether a circle intersects with a circle, contains a point, etc. So you can use this class along with your other code, but you don't have to.

    Similarly, the CircleShape class is another class that works with Box2D, the physics engine that comes with libGDX. Although you might use Box2D eventually, I definitely wouldn't recommend using it until you have the basics working.