Advertisement

AI class problem (EVERYONE moves the same!!!!)

Started by July 27, 2010 01:09 PM
12 comments, last by dpadam450 14 years, 3 months ago
........
void main()
{
AIclass a,b,c;
a.Move();
b.Move();
c.Move();
}

And the problem is .... a,b and c moved in the same ways.
They even wait for the others and turn angle together !!!!
move in the same direction, turn around in the same time.
like mimic??

I think it should seperated between a, b, c....
because of I use random()?? but a,b,c gonna random in the same time????
I was confused

why!?
What random number generator are you using? How are you using it? Make sure you only seed it once (for instance, by calling srand).
Advertisement
Quote: Original post by ToohrVyk
What random number generator are you using? How are you using it? Make sure you only seed it once (for instance, by calling srand).


I used srand(GetTickCount());

what is it wrong???
you said Make sure you only seed it once ??
what do you mean??
My knowledge is not enough.

I think srand(GetTickCount()); in main loop for instance
so every time when it is in main program.
it gonna change random all time......
I guess I may understand it wrong......
Call srand() only once, at the beginning of main(). I usually call srand(time(0)), but you can use a different timer (not sure what GetTickCount() returns). After that, call rand() whenever you need a pseudo-random number.

What you are doing is initializing the pseudo-random sequence each time you need a number. If you pass the same value to srand() (and your ticks don't happen fast enough to avoid this), you'll get the same numbers out each time.

Just do what I [and ToohrVyk] said above and you'll be fine.
meaning:

//very important!//this is OUTSIDE the game loopsrand(GetTickCount());while (1){    //just call rand() any time you need a "random" number    float anytimeINeedARand = rand();}


-me
Quote: Original post by alvaro
Call srand() only once, at the beginning of main(). I usually call srand(time(0)), but you can use a different timer (not sure what GetTickCount() returns). After that, call rand() whenever you need a pseudo-random number.

What you are doing is initializing the pseudo-random sequence each time you need a number. If you pass the same value to srand() (and your ticks don't happen fast enough to avoid this), you'll get the same numbers out each time.

Just do what I [and ToohrVyk] said above and you'll be fine.


I 'm not fine....
It doesn't work...
I used the collision detection too. (I guess this also another problem)
When AI hit wall, AI turns.
Now, Every AI wait for everyone hit the walls and turn together.
I think instance should be seperate.

one more thing,
If I declared srand() in Initialize() which we call in 1 time.
Do I need to set time.h to every .cpp that use rand()?
Advertisement
I've got a pretty good idea that this has nothing to do with your randomness and everything to do with your code structure. This is one of those times that I would recommend posting a bit of your code for us to see. That being said, have you actually traced through the execution of your code as it runs to see if things are doing what you are expecting them to do?

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
Author of the book, Behavioral Mathematics for Game AI
Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

hm? is not work??
anyway, this is my code
void CMyAI::Move(){	static float angle = m_rotation.y;		static bool bTurnRight = false;	nTurnRandom = rand()%3000;	if(!m_bTurn){		m_position.x += sinf(m_rotation.y)*m_speed;		m_position.z += cosf(m_rotation.y)*m_speed;		bTurnRight = false;		nTurnRightRand = rand()%4;//so 75:25		if(this->WallCollision() || nTurnRandom%250 == 0)		{			m_position.x -= sinf(m_rotation.y)*m_speed;			m_position.z -= cosf(m_rotation.y)*m_speed;			if(nTurnRightRand == 0 && !bTurnRight)			{				bTurnRight = true;				angle = 2;			}			else			{				angle = -2;			}			m_bTurn = true;		}	}		if(m_bTurn)		Rotation(angle);}


well, it king like this
Quote: Original post by fantasyme
hm? is not work??
anyway, this is my code
void CMyAI::Move(){	static float angle = m_rotation.y;		static bool bTurnRight = false;

I didn't get past this. Why static? Do you really want those variables to basically be global? (and therefore shared by all instances of the class?)

Quote: Original post by alvaro
Quote: Original post by fantasyme
hm? is not work??
anyway, this is my code
void CMyAI::Move(){	static float angle = m_rotation.y;		static bool bTurnRight = false;

I didn't get past this. Why static? Do you really want those variables to basically be global? (and therefore shared by all instances of the class?)



yes, I want static to make it like global variable.
and it shared to all instances....??
I cannot do that?

This topic is closed to new replies.

Advertisement