Enemy Detection

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.

1 Like

Why don’t you want to do this?

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.

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:

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.

1 Like

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.

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.

now there is a

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

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

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.

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?

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.

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.