# Simple AI theory question

## Recommended Posts

mattor    154
Hi. I read the last post here: http://www.gamedev.net/community/forums/topic.asp?topic_id=308897 and decided to do the steps he suggested as an exercise. I got it up and running just fine, but I have a question about AI theory as it pertains to this. First, my movement code (C++ using allegro):
void moveletter(CLetter &Let)
{
//pick one of eight random directions for the letter to move in
int dir = rand()%8;

//These guys indicate how much each char 'jumps' each movement
int tW = Let.W;
int tH = Let.H;

//blank out old char
rectfill(screen, Let.x,Let.y, Let.x+Let.W, Let.y+Let.H,BLACK);

//Store current position for resetting if there's a collision
Let.oldX = Let.x;
Let.oldY = Let.y;

switch(dir)
{
case 0:
Let.x -= tW;
Let.y -= tH;
break;
case 1:
Let.y -= tH;
break;
case 2:
Let.x += tW;
Let.y -= tH;
break;
case 3:
Let.x -= tW;
break;
case 4:
Let.x += tW;
break;
case 5:
Let.x -= tW;
Let.y += tH;
break;
case 6:
Let.y += tH;
break;
case 7:
Let.x += tW;
Let.y += tH;
break;
default:
break;
}
}


Now, the question. It seems to me, based on my code here, that the letters will have a tendency to spread out towards the edges of the screen and stay out there. My theory is that since they are bounded by the edges of the screen right now, but when they hit the wall, they are still given one of 8 possible directions to move, they are more likely to hit the wall again. Is this theory correct? I am very new to AI and so I'm just trying to understand a bit. Also, if it is true, can anyone suggest a more random movement algorithm than the one I've posted here?

##### Share on other sites
Guest Anonymous Poster
A common way to implement random motion in robotics is to pick a random direction, move in that direction for a set amount of time, repeat.

##### Share on other sites
WeirdoFu    205
If your random number generator doesn't mess up, I would say that there's no reason for them to all go towards the edges and not come back. There shouldn't be such a tendency unless there's a biased random number generator, since every direction is given equal chance.

To resolve the stuck at edges problem, you may want to try a bounce back. Where if it choose to move out of the screen you bounce it back.