Search code examples
javaarrayslibgdx

Using an array of Box2dbodies to create multiple of the same Body at different times


I'm currently making my first libgdx game and I'm not sure how to use arrays properly in the context of libgdx/box2d. What I'm aiming to do is to create a group of box2dbodies that are identical but spawn in at different times. This will be done to make the game harder as time goes on.

I looked online on how to do this and the most relevant search result was written in C++ and was about spawning a group of 100 bodies in at the same time. I used a converter to change it to Java and changed the code so it suited what I wanted. I eventually managed to get it to work but now I have a problem, one of my methods essentially respawns the object to the other side of the screen at a new random position but unfortunately I have no idea how to get this to apply to the b2body that was just created.

TLDR; I need help in either fixing my array code so that my Respawn() method works, and/or reworking my array completely so it does what I intend. Any help is greatly appreciated!

My arrays:

Shape[] objects = new PolygonShape[4];
Body[] bBody = new Body[4];

How I define my body:

private Body[] makeObstacle(World world, BodyDef.BodyType type, PolygonShape shape) {

    BodyDef bdef = new BodyDef();
    bdef.type = type;
    bdef.position.set(7, randomYPos);
    Body body = world.createBody(bdef);


    body.setLinearVelocity(-4, 0);

    shape = new PolygonShape();

    shape.setAsBox(60 / RadiationPigeon.PPM, 25 / RadiationPigeon.PPM);

    FixtureDef fdef = new FixtureDef();
    fdef.shape = shape;


    body.createFixture(fdef);


    return new Body[]{body}
}

Here's how I spawn the body:

public void bodies(){

    if(!alreadyExecuted){

        if(DistanceM == 10 && DistanceC == 0) {
            bBody = makeObstacle(world, BodyDef.BodyType.KinematicBody, (PolygonShape) objects[2]);
            alreadyExecuted = true;
        }
    }

}

Here's my Respawn() method:

private void Respawn(){
    if(bBody[2].getPosition().x <= -3){

        minWorldY = pigeoncam.position.y - pigeoncam.viewportHeight/2;
        maxWorldY = pigeoncam.position.y + pigeoncam.viewportHeight/2;
        randomYPos =  MathUtils.random(minWorldY, maxWorldY);
        bBody[2].setTransform(7, randomYPos, 0);

    }
}

Using the above code gives a null object reference error regarding the respawn() method


Solution

  • Due to lack of knowledge on Java Arrays and the above body code being from a block of c++ code; my code was completely wrong, after learning arrays properly I managed to fix it by:

    Changing the makeObstacle() method to return a body and getting rid of the 'objects' array:

    private Body makeObstacle(World world, BodyDef.BodyType type) {
    
        BodyDef bdef = new BodyDef();
        bdef.type = type;
        bdef.position.set(7, randomYPos);
        Body body = world.createBody(bdef);
    
    
        body.setLinearVelocity(-4, 0);
    
        PolygonShape shape = new PolygonShape();
    
        shape.setAsBox(60 / RadiationPigeon.PPM, 25 / RadiationPigeon.PPM);
    
        FixtureDef fdef = new FixtureDef();
        fdef.shape = shape;
    
    
        body.createFixture(fdef);
    
        return body;
    }
    

    Defining which bbody I want to recieve the body:

    if(DistanceM == 10 && DistanceC == 0) {
                bBody[0] = makeObstacle(world, BodyDef.BodyType.KinematicBody);
                alreadyExecuted = true;
            }
    

    Making my Respawn() method check if the bBody which is created is not null

     private void Respawn(){
        if(bBody[0] != null && bBody[0].getPosition().x <= -3){
    
            minWorldY = pigeoncam.position.y - pigeoncam.viewportHeight/2;
            maxWorldY = pigeoncam.position.y + pigeoncam.viewportHeight/2;
            randomYPos =  MathUtils.random(minWorldY, maxWorldY);
            bBody[0].setTransform(7, randomYPos, 0);
    
        }
    }