# Combat collision AI

Printable View

• 04-29-2013, 08:35 AM
leeroijenkiins
Combat collision AI
Hi so I am new to programming like AI and was curious as to how to calculate if it has collided with something, hmm ill go into more detail. so when the user presses the space key a circle generates around his character.(the size is 50x, 50y). I want to know if the AI is within that circles radius. I have never done anything like this before so im alittle confused lol. At first i thought this would work:
Code:

```if(((birdx + aw[0] > knightPositionX)||(birdx - aw[0] < knightPositionX)) && ((birdy + ah[0] > knightPositionY)||(birdy - ah[0] < knightPositionY))){                                 seekerchp[s] = seekerchp[s] - knightd;                                 drawbird = true;                         }```
note the aw[0] is just area wide and ah is area height. both represent 50.
Clearly after i tested it i looked over it and *falmpalm* that is an awful peice of code...

Any ideas? im not asking for you to make it for me, just alittle push in the right direction.

oh also i dont know if its possible to use the:
Code:

`g.drawOval(knightPositionX -5, knightPositionY -5, aw[0], ah[0]);`
to make this collision detection any easier?
Thankyou.
• 04-29-2013, 12:13 PM
Tolls
Re: Combat collision AI
1. You know the location of the character.
2. You know the location of the AI.
3. You know the radius of the circle.

So using 1 and 2 you can calculate the distance between the character and the AI.
If that is less than the value of 3 then the AI is within the circle.
• 04-29-2013, 10:00 PM
leeroijenkiins
Re: Combat collision AI
Quote:

Originally Posted by Tolls
1. You know the location of the character.
2. You know the location of the AI.
3. You know the radius of the circle.

So using 1 and 2 you can calculate the distance between the character and the AI.
If that is less than the value of 3 then the AI is within the circle.

I clearly wasnt in the right state of mind last night.. fixed it thank you!