So I'm using OpenGL for a game in java, but I'm stuck on an extremely confusing problem. Class:

private class TitleBuilding
    {
        private double x1, y1, x2, y2;
        private double percentage, randValue=1;
        private byte windowType, numBuildings;
        private Random random2;
        private byte index;

        public TitleBuilding(double percentage,byte numBuildings,byte index)
        {
            this.percentage=percentage*.01;
            this.numBuildings=numBuildings;
            this.index=index;
            random2=new Random();

            if(index==0)
            {
                while(randValue<1.3)
                    randValue=1+random2.nextDouble();
            }
            else
            {
                while(randValue<2 || randValue>3.2)
                    randValue=(2*random2.nextDouble())+.8;
            }
            updatePosition();
        }

        public void updatePosition()
        {
            x1=Display.getWidth()*percentage;
            x2=(Display.getWidth()/numBuildings)+percentage;
            y1=Display.getHeight()/randValue;
            y2=Display.getHeight();
        }

        public void render()
        {
            glColor3f(0f,0f,0f);
            glBegin(GL_QUADS);
                glVertex2f((float)x1,(float)y1);
                glVertex2f((float)x2,(float)y1);
                glVertex2f((float)x2,(float)y2);
                glVertex2f((float)x1,(float)y2);
            glEnd();
        }
    }

Basically when this object is created, it is assigned a random height by the random2 object. This is how how it will begin drawing the quad from the top of the screen. However, even though they all have different random values each time, many times they are rendered exactly the same! I thought maybe it was some sort of Opengl rounding issue, but it still does the same thing on both double and float. Why are they not drawing correctly?

Have you tried debugging by printing out the values that are used to see what the code is doing? For example print out what the nextDouble method returns and what the randValue is.

This article has been dead for over six months. Start a new discussion instead.