Making enemies go around an obstacle

I'm making a shooter game and have added random obstacles to it to make it more challenging. The problem is trying to make the enemies go around them. They collide with them and sorta slide around them, but I want them to see that the player is behind it and try to get around it without touching it. I tried to do it, but it doesn't really work. Here is my 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
enemies[i].dir = GetAngle(players[enemies[i].targetPlayer].x, players[enemies[i].targetPlayer].y, enemies[i].x, enemies[i].y);

float checkAngle = enemies[i].dir;
int checkX = enemies[i].x, checkY = enemies[i].y;
bool good = false;

for (int j = 0; j < 200; j++) {
	checkX += cos(enemies[i].dir);
	checkY += sin(enemies[i].dir);
	for (unsigned int k = 0; k < others.size(); k++) {
		if (GetDistance(checkX, checkY, others[k].pos.x, others[k].pos.y) < 32 + enemies[i].radius) {
			if (enemies[i].dir <= GetAngle(others[k].pos.x, others[k].pos.y, enemies[i].x, enemies[i].y))
				enemies[i].dir -= .5;
			else
				enemies[i].dir += .5;
			good = true;
			break;
		}
	}
	if (good)
		break;
}

enemies[i].x += (enemies[i].speed) * cos(enemies[i].dir);
enemies[i].y += (enemies[i].speed) * sin(enemies[i].dir);
uhh, implement the A* algorithm?
That seems like you have to do stuff with grids to make it work and I just want something fast and simple like what I was trying to do. I don't want something super complicated like that. I just want an enemy to see that there is something in the way, figure out if it would be faster to turn clock wise or counter clock wise, turn that way, then just keep doing that until it is around it.
Last edited on
uhh, make a moderately small 2 dimensional pixel by pixel bool array around the AI, using the list of obstacles to flag spots as collidable, and use A* on that. And while outside of that, just use the walk strait to.

Honestly, that you made the AI just slide around obstacles is pretty good. But A* and algorithms like that is the only way I know how to make the AI walk around obstacles ahead of time.
Topic archived. No new replies allowed.