# Enemy Detection

**URL:** <https://discourse.processing.org/t/enemy-detection/10509>\
**Category:** Coding Questions\
**Created:** [April 20, 2019, 10:19pm UTC](https://discourse.processing.org/t/enemy-detection/10509 "2019-04-20T22:19:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [April 20, 2019, 10:19pm UTC](https://discourse.processing.org/t/enemy-detection/10509/1 "2019-04-20T22:19:23Z")

</div>

Hey I’m trying to make my first AI game. Is there a way to check to see if any item is within a certain range? I want my AI to only see 8 directions and whatever is in the radius of its x point and the furthest it can see. I would rather not loop through all items to check if within radius.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 21, 2019, 1:37am UTC](https://discourse.processing.org/t/enemy-detection/10509/2 "2019-04-21T01:37:11Z")

</div>

> [@ctrembla](#):
>
> I would rather not loop through all items to check if within radius.

Why don’t you want to do this?

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [April 21, 2019, 1:39am UTC](https://discourse.processing.org/t/enemy-detection/10509/3 "2019-04-21T01:39:28Z")

</div>

Just in case I place several items in then I would need to loop them all. I remember one programming thing had a dist() that could see if there was anything between you and the point.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 21, 2019, 1:52am UTC](https://discourse.processing.org/t/enemy-detection/10509/4 "2019-04-21T01:52:44Z")

</div>

Right, you can use the `dist()` function to check the distance between two points.

Shameless self-promotion: here is a tutorial on collision detection in Processing:

> **[Processing Collision Detection](https://happycoding.io/tutorials/processing/collision-detection)**
>
> Learn how to trigger an action when objects overlap in Processing!

I think it’s probably fine for you to loop over your enemies to find the correct one. If you really need something more advanced, you might look into data structures like quadtrees, but honestly that’s probably overkill. Stick with the basic solution for now.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [April 21, 2019, 2:08am UTC](https://discourse.processing.org/t/enemy-detection/10509/5 "2019-04-21T02:08:53Z")

</div>

ty, i guess this language wasnt the one that has a dist that can tell you if somthing overlapping the line between point a and b. i’ll just loop through everything. then feed that info to my ai.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [April 21, 2019, 2:21am UTC](https://discourse.processing.org/t/enemy-detection/10509/6 "2019-04-21T02:21:56Z")

</div>

You can google something like “circle line segment collision detection” or “determine if circle overlaps line segment” for a ton of results, but you’re still going to have to loop through your enemies to do that.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 21, 2019, 2:34am UTC](https://discourse.processing.org/t/enemy-detection/10509/7 "2019-04-21T02:34:10Z")

</div>

> [@ctrembla](#):
>
> I remember one programming thing had a dist() that could see if there was anything between you and the point

> [@ctrembla](#):
>
> if somthing overlapping the line between point a and b

now there is a

> **[dist() / Reference](https://processing.org/reference/dist_.html)**
>
> Calculates the distance between two points.

function that can calculate a distance ( means you need to know both points )  
but sorry, that has nothing to do with the detection of other ? unknown objects  
between those points.  
and all the collision coding here mentioned work on math and also on knowing  
objects position and dimension.

but there is a very different ? collision detection?  
that is that you rely on the only memory you have,  
the canvas and its colors  
means from point a you can check

- in a line of any direction
- or in a circle with fix distance
- or all pixels around ( slowest )

for a color change.  
that might be the only way to detect a ?unknown? object  
a easy way is the

> **[get() / Reference](https://processing.org/reference/get_.html)**
>
> Reads the color of any pixel or grabs a section of an image. If no parameters are specified, the entire image is returned. Use the x and y parameters to get the value of one pixel. Get a…

color of a position, and compare its  
red, green, blue, hue, saturation, brightness  
with a limit value.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [April 21, 2019, 2:35am UTC](https://discourse.processing.org/t/enemy-detection/10509/8 "2019-04-21T02:35:24Z")

</div>

i know that kind of collision and atm i’m using for loop but i wanted my ai to see only things surrounding it not know every block.

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [April 23, 2019, 4:44pm UTC](https://discourse.processing.org/t/enemy-detection/10509/9 "2019-04-23T16:44:58Z")

</div>

> [@ctrembla](#):
>
> I want my AI to only see 8 directions

Can you explain this better? Do you mean that its vision works like a laser in 8 direction, and any object not triggering the ray is invisible? Is the 8 directions infinite, or limited in distance?

> [@ctrembla](#):
>
> not know every block

Is your map / space continuous, or is it tiled? You could index your enemies by what tile they are in, and only check the tile the ai is in – that gives you fast lookup. But that doesn’t sound like what you are doing with the “8 directions” thing.

Unless you have a really large numbers of enemies or a very expensive collision algorithm, looping over the list is probably going to be just fine – not worth complex optimization.

---

<div class="post-metadata">

**Author:** ![ctrembla](https://avatars.discourse-cdn.com/v4/letter/c/aeb1de/32.png) [@ctrembla](https://discourse.processing.org/u/ctrembla)\
**Post date:** [April 23, 2019, 7:01pm UTC](https://discourse.processing.org/t/enemy-detection/10509/10 "2019-04-23T19:01:32Z")

</div>

I found code that gave me what I want and its lines that run it 8 directions and if something breaks the line. the enemy will know something is there. I got all the walls in a grid format. Straight lines.
