Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 5324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Probably another dumb question (I feel sorta bad asking all sorts of really basic questions) but I''m trying to get some thick lines which span across the screen to have randomly placed holes in them. I have some code which I feel will complete the task but it wont let me see where the hole is cause it keeps flickering? I think that the program is just looping over and over and so its redrawing it. How would I go about changing it? The following is some of the code: glLineWidth(20.0); glColor3f(0.8f, 0.0f, 0.0f); random_integera = (rand()%251); random_integerb = -(rand()%280); random_integera = random_integera/100; random_integerb = random_integerb/100; choice = (rand()%2); if (choice == 0) random_integer = random_integera; else random_integer = random_integerb; glBegin(GL_LINES); glVertex3f(-4.0f, ycoor, 0.0f); glVertex3f(random_integer, ycoor, 0.0f); glVertex3f(random_integer+0.8f, ycoor, 0.0f); glVertex3f(4.0f, ycoor, 0.0f); glEnd(); ycoor = ycoor + 0.0003f;

Share this post

Link to post
Share on other sites
First the flicker problem:
-> even if OpenGL normally does not exceed the fps of your desktop settings (default in graphic card settings is "vertical
synchronization" on by default -> between 60 and 100 Hz), every
change beyond 20Hz looks smooth to a human eye (therefore movies have about 25 - 29 fps).
=> the flickering will occur because the line changes more often than your eye can recognize -> flickering

The second thing is that you should not use rand() like "rand() % 2" - Especially not for small numbers after the modulo (%) operator. If you want to use the rand() function to decide between two states (0 or 1), use it like

int nState = rand () & 1L;

Which does the binary "AND" and therefore returns 0 or 1 (this resulting distribution is better than (rand() % 2)). There exists currently a thread about the rand() function.

I coded your example a bit different:

// --- global stuff ---

double ycoor = 0; // current y position

double ydir = 0.001; // direction of movement (+ or -)

double dGapPos; // current position of our gap

#define WAIT_STEPS 5 // steps until we change the gap pos

int nWaitSteps = 0; // change count down

// --- render function ---

glLoadIdentity (); // default matrix

glTranslatef ( 0.0, 0.0, -0.5 ); // move a bit away

glLineWidth(5.0); // line width

glColor3f(0.8f, 0.0f, 0.0f); // line color

ycoor = ycoor + ydir; // change our line position

if ( (ycoor > 0.1) || // if we are out of this

(ycoor < -0.1) ) // box, change the direction

{ // of our movement

ydir *= -1.0;

// clear background // and delete all lines,

glClear(GL_COLOR_BUFFER_BIT); // -> nice effect


if (--nWaitSteps < 0) // reduce our count down

{ // and check for zero

nWaitSteps = WAIT_STEPS; // reset countdown

// generate random position between -0.5 and 0.5

dGapPos = rand (); //draw a number [0,RAND_MAX]

dGapPos /= RAND_MAX; // -> scale to [0.0,1.0]

dGapPos -= 0.5; // -> move to [-0.5, 0.5]


glBegin(GL_LINES); // draw the lines

// left line

glVertex3f ( -4.0f, ycoor, 0.0f );
glVertex3f ( dGapPos, ycoor, 0.0f );

// right line after gap

glVertex3f ( dGapPos + 0.3f, ycoor, 0.0f );
glVertex3f ( 4.0f, ycoor, 0.0f );



x = rx ( 1 - x ) ; r > 3.5699
... and chaos may begin ...

Share this post

Link to post
Share on other sites
Hey thanks for the help. I will try that code out later. I figured out the flicker problem and found a more substantial problem. Using rand() doesn''t really give random numbers, if I have an srand() command to choose the seed or something like that it is more random. I just put the a test in front of the section of code where it generates the random number so it only goes through that the first time the bars get on the screen. Therefore the hole position stays the same until it is off the screen and then reset. I''m sure this code can be optimized but it works for now. Thanks again for the help. Quick example below where flicker is an int that has been set at 0 at the start of the program.

if (flicker == 0)
generate random number

flicker = 1;

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!