Can't seed rand() inside array of objects?

Hello. I have some pixels that should run around on the screen in a random fashion. They don't. They all run around in exsact the same rout so that they look as one. I have srand()'ed only once. I won't bother posting all the code. since it is quite much. But I think I know the problem must be around the code I'm posting. This is some of the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

// from the class
void creature::move(void)
{	
	int x=rand()%2;
	int y=rand()%2;

	if(x==0)
		x_pos=x_pos+speed;
	if(x==1)
		x_pos=x_pos-speed;
	if(y==0)
		y_pos=y_pos+speed;
	if(y==1)
		y_pos=y_pos-speed;
	
}



//Main

creature microbe[STARTING];
	
	srand((unsigned)time(NULL));
	

	for(i=0;i<STARTING; i++)
	{
		microbe[i].init();

	}
	
//Inside mainloop

for(i=0; i<STARTING;i++)
	{	
		microbe[i].move();
		microbe[i].draw();
							
	}

SwapBuffers(hDC);	


If someone can see what I do wrong I would appreciate a word or two.
I can't really see anything wrong in the posted code.

Are you sure they just look as one, rather than actually just being one due to faulty initialization?
I don't see anything wrong here too. Do you think creature::draw() uses x_pos/y_pos that are motified by creature::move()?
This is the draw function member:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

void creature::draw(GLvoid)

{	
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	
	glLoadIdentity();
	glTranslatef(x_pos,y_pos,z_pos);
	glColor3f(r,g,b);

                 glBegin(GL_QUADS);              			
	glVertex3f(-maxCellSize, maxCellSize, 0.0f);              
                glVertex3f( maxCellSize, maxCellSize, 0.0f);              
                glVertex3f( maxCellSize,-maxCellSize, 0.0f);                 
                glVertex3f(-maxCellSize,-maxCellSize, 0.0f);             
	
glEnd();                         


}


And this is the init():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

void creature::init(void)
{

	state=ALIVE;
	foodTactics=CARBEATER;
	age=0;
	mode=MOVE;	
	spiecementId=1;
	evolutionSteps=0;
	r=0.0f; g=1.0f; b=0.0f;

	energy=10000;
	life=5000;
	scope=10;
	strength=10;
	speed=0.05f;
	maxCellSize=0.1f;
		
	x_pos=0.0f; y_pos=0.0f, z_pos=-50.0f;
}
Last edited on
Ah, at last I found it my self. Thank you Gaminic and EricDu for asking the right questions.

The glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); statement should be moved out of creatur::draw(), and into main() !!!
Last edited on
sorry, I am really not familiar with GL interfaces. How about try this way, print out x/y values in move(), then we know if it's rand() issue.
It was apparently not a rand() problem. I was sure it was - so I was looking at the same code over and over ... again thanks. :-)
Topic archived. No new replies allowed.