#### Archived

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

# Depressing obstacle avoision problem.

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

## Recommended Posts

I''ve been working on this with my cousin for a few days now, and am having trouble getting the bot to avoid the various objects strewn in his path. Mainly, he keeps running into walls and getting stuck in the corner of the map. I''d like to fix that problem first, as it should hopefully allow me to finish getting a decent base for movement, or at least point me in the right direction. Does anyone have input on how they solved this problem (if they had it)? I don''t want the code directly, but I will not complain if that''s what I get Pseudo-code or the basic idea would be more than I have right now, and is all I can hope for. Any help will be much appreciated! If you''d like to see our code, we have included it below. A little class we wrote for containment
int numObjects;

class object
{
public:
float distance;
float direction;
int type;
float objDirection;
private:
protected:
};

object objects[20];

Now the Update() function
// Update cycle entry point. Your AI code goes here.

numObjects=g_pCore->GetNumObjectsInSight();
// See all the objects that you can, and store them in an array

for (int i=0; i<numObjects; i++)
{
g_pCore->GetObjectsInSight(i,objects[i].type, objects[i].direction, objects[i].distance, objects[i].objDirection);
}

for (int closest=0, a=0; a<numObjects; a++)
{
if (objects[a].distance <= objects[closest].distance)
closest = a;
}

if(objects[closest].type == OBJ_UNIDENTIFIED)
g_pCore->Taunt("ERROR!");
if(objects[closest].type == OBJ_TREE)
g_pCore->Taunt("Tree");
if(objects[closest].type == OBJ_BIGROCK)
g_pCore->Taunt("Big Rock");
if(objects[closest].type == OBJ_SMALLROCK)
g_pCore->Taunt("Small Rock");
if(objects[closest].type == OBJ_ENEMY)
g_pCore->Taunt("Enemy");
if(objects[closest].type == OBJ_AMMO)
g_pCore->Taunt("Ammo");
if(objects[closest].type == OBJ_WALL)
g_pCore->Taunt("Wall");

g_pCore->SetSpeed(SET_NORMAL_SPEED);

if(objects[closest].distance < 8)
{
if (objects[closest].direction < 0.0f)
{
g_pCore->TurnRight(TURN_FAST);
} else {
g_pCore->TurnLeft(TURN_FAST);
}

} else {
g_pCore->SetSpeed(SET_NORMAL_SPEED);
}


##### Share on other sites
First off, be careful, that won''t compile under certain compilers due to you throwing the int closest=0; in the for loop. It compiles under MSVC because MSVC doesn''t follow the standards properly, but according to the standard, that variable is only good in the for loop, another compiler would give you an error about an unrecognized variable when you try to use it outside the for loop, and I beleive one of the stipulations for this is that it has to be able to compile on other compilers besides msvc .

Ok, my basic obstacle avoidance was this: Store a variable that holds the # of turns I want to do.. then turn that many times while subtracting this variable (make it static or global so it stores it''s value over multiple frames). This way, when you see something, you can rotate more so you don''t get caught on it. Also, the problem of running into walls, simply add a variable that counts how many objecst are in view (not counting walls!), if nothing is in view, turn... if there are no objects in front of you, the other player isn''t there, and you''re running at a wall, so just turn. This will prevent you from running into walls and corners (or at least help tremendously).

##### Share on other sites
quote:
First off, be careful, that won''t compile under certain compilers due to you throwing the int closest=0; in the for loop. It compiles under MSVC because MSVC doesn''t follow the standards properly, but according to the standard, that variable is only good in the for loop, another compiler would give you an error about an unrecognized variable when you try to use it outside the for loop, and I beleive one of the stipulations for this is that it has to be able to compile on other compilers besides msvc .

I know, I (we) declared the variables earlier, just forgot to include them.

• 10
• 19
• 14
• 19
• 15