Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

So I'm currently working on a game where multiple meteors spawn and you have to avoid them. The problem is the meteors sometimes spawn overlapping each other, which I don't want. I want them to spawn at a certain distance from each other.

I know what to do in pseudo code but I don't know how to put it in the Libgdx/java language. I know how to work out the distance to point formula of two points but I don't know how to do that in an array. Also I want the point to be the centre of the sprite which I can workout. Here is my code bellow:

Meteor Class

public class Meteors {    
    private Texture bigMeteor;
    private Vector2 posBigMeteor;
    private Random yrand;

    //Constructor
    public Meteors(float x){
        bigMeteor = new Texture("meteor.png");
        yrand = new Random();

        //Spawn location of meteor
        posBigMeteor = new Vector2(x, yrand.nextInt(AstroDemo.HEIGHT/2 - bigMeteor.getHeight()));
    }

    public Texture getBigMeteor() {
        return bigMeteor;
    }

    public Vector2 getPosBigMeteor() {
        return posBigMeteor;
    }

    public void reposition(float x){
        posBigMeteor.set(x, yrand.nextInt(AstroDemo.HEIGHT/2 - bigMeteor.getHeight()));
    }
}

Playstate Class

  public class PlayState extends State {
     private static final int METEOR_COUNT = 4;

        private Naught naught;
        private Texture bg;
        private Random xrand;
        private Array <Meteors> meteors;

        public double x1;
        public double x2;
        public double y1;
        public double y2;
        public double distance;

        public PlayState(GameStateManager gsm) {
            super(gsm);
            //Starting co-ordinates of main character (Naught)
            naught = new Naught(50, 100);
            //Setting viewport of the camera
            cam.setToOrtho(false, AstroDemo.WIDTH/2, AstroDemo.HEIGHT/2);
            bg = new Texture("bg.png");
            xrand = new Random();

            meteors = new Array <Meteors>();

            //Spawn meteors randomly off screen
            for (int i = 1; i <= METEOR_COUNT; i++){
                meteors.add(new Meteors(AstroDemo.WIDTH/2 + (xrand.nextInt(300))));

               //I know this is where I have to do the distance formula and I know this is incorrect but I'm not sure how to find points in an array. This is my attempt.
               x1 = x + (bigMeteor.getWidth()/2);
               x2 = x + (bigMeteor.getWidth()/2);
               y1 = y + (bigMeteor.getHeight()/2);
               y2 = y + (bigMeteor.getHeight()/2);
               distance = Math.sqrt(Math.pow(x2 - x1, 2) + (Math.pow(y2 - y1, 2)));
            }
        }


        @Override
        protected void handleInput() {
            //If screen/mouse is held
            if(Gdx.input.isTouched()){
                //Main Character jumps/flys
                naught.jump();
            }
        }

        @Override
        public void update(float dt) {
            handleInput();
            naught.update(dt);
            //If meteors are left side of the screen, re-position to the right side of the screen
            for(Meteors meteor : meteors){
                if (cam.position.x - (cam.viewportWidth/2) > meteor.getPosBigMeteor().x + meteor.getBigMeteor().getWidth()){
                    meteor.reposition(meteor.getPosBigMeteor().x + (AstroDemo.WIDTH/2 + 20 + (xrand.nextInt(300))));
                }
           }
            cam.position.x = naught.getPosition().x + 80;

            cam.update();
        }

        @Override
        public void render(SpriteBatch sb) {
            //Adjust the spritebatch for co-ordinate system in relation to camera
            sb.setProjectionMatrix(cam.combined);
            sb.begin();
            //Draw background where the camera is
            sb.draw(bg, cam.position.x - (cam.viewportWidth/2), 0);
            sb.draw(naught.getTexture(), naught.getPosition().x, naught.getPosition().y);
            for (Meteors meteor : meteors) {
                sb.draw(meteor.getBigMeteor(), meteor.getPosBigMeteor().x, meteor.getPosBigMeteor().y);
            }
            sb.end();
        }

        @Override
        public void dispose() {

        }
    }
share|improve this question

closed as off-topic by Vogel612, Mat's Mug Jan 15 at 21:35

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – Vogel612, Mat's Mug
If this question can be reworded to fit the rules in the help center, please edit the question.