# How do I create an AI that avoids obstacles

Get help using Construct 2

### » Wed Aug 19, 2015 2:24 pm

Hello fellas,

How can I program an AI that can avoid obstacles and try to score using different angle options, e.g. Billiards game.

The AI on its turn should analyze and consider (between all the balls on its way) if there's any possible way or angle to shoot the ball and score, if not, to shoot it (with a proportional force) in a direction which can place the ball closer to the goal.

I appreciate in advance any help, once again, thanks!
Last edited by Indomitus on Sat Aug 22, 2015 11:46 pm, edited 1 time in total.
B
4
S
1
Posts: 34
Reputation: 416

### » Sat Aug 22, 2015 11:38 pm

Bumping this thread. (If by any chance there's already a post -I could have missed- on the forums concerning to this topic, please let me know)

B
4
S
1
Posts: 34
Reputation: 416

### » Sun Aug 23, 2015 2:58 am

There isn't much in C2 that is specific to this problem, you could look at answers on SO for ideas: http://stackoverflow.com/questions/2799 ... illiard-ai

With some maths you can work out some shot angles to try, e.g. hitting off the wall to hit the target ball if there is something in the way. How do you tell if there's something in the way? You can use the algorithm for the distance from a point to a line segment to see if it is less than the radius of the two balls added together to see if it is 'in the way'. Use it to check against every other ball on the table and if none are in the way then it's a clear shot.

If you're using Physics behaviors, I'm not sure if you can predict the outcome of a shot meaningfully. However, for an OK AI you don't need to be perfect. Trace angles and use geometry/physics to get an approximation of what will happen and use that to score the outcome of a shot. http://i.ytimg.com/vi/QXsfwLFX4iI/maxresdefault.jpg shows a kind of example of what you have to take into account: where the cue ball will hit another ball, and the angles of both those balls afterwards.

In short, I don't think there's an easy way.
B
10
S
2
Posts: 33
Reputation: 710

### » Wed Aug 26, 2015 9:23 am

Hi @tgfcoder,

Sorry for the delay on my response. I'll take a look at your links, and get some inspiration for the code I need. I know this is not an easy task, but very challenging.

I must say I'm looking forward to develop the code for this kind of AI!

Thanks a lot mate.
B
4
S
1
Posts: 34
Reputation: 416