How to get a random position on terrain for an AI?

I am looking for an efficient solution to get a random position on random terrain that AIs can path to for something like a “wandering” movement.
I am deciding the AI position on the server, and I am building the AI and making it pathfind to the position that the server gives that AI on the client, so I cannot just use bodymovers and raycasting to make the AI wander aimlessly.

I have not yet started doing this problem, but right now I am thinking of selecting a random X and Z value and raycasting down to check if it is a viable position.

The terrain would look something like this (with more foliage):


So, my goal would be to get a random position on the terrain, out of the water, and not on a tree.

I’m not sure if this is even a some-what decent way of doing this, but depending on how large your map is, perhaps you could create a bunch of decently-sized parts that cover different areas of the terrain you wish for your AI’s to be able to travel to, and then you can use math.random on each axis to get a random position out of the part.

Can clarify further if you’re at all interested in this method. :slight_smile:

3 Likes

That would work for open areas, but I’m not sure about forests and such. Thanks for the idea!

2 Likes

You can always just cast more rays if you hit something like a tree, it’s the size of the ray not the amount that makes the difference.

1 Like

Alright, thanks! I wasn’t sure of how fast rays were.

1 Like

“This is not really rocket science. We do use a dynamic spatial partitioning structure; we use it both for broadphase for physics simulation and for ray casting. The structure is reasonably efficient (although the longer the ray is the slower it is to query it so shorter rays are better than longer rays).” - zeuxcg

1 Like

Just so I don’t have to test this: if a ray starts “inside” a part, is that part returned as the hit?

1 Like

No, that’s why u can use rays to detect noclip.

Player moves > you track position

Raycast from old position to now position > no hit not noclipping
Hit > Raycast from current pos to the first position we cast from > if hit then we went through a part, check angles and normals as we might’ve clipped a corner
Other hit option > raycast from current pos to original cast pos > no hit > currently inside the part

Think of parts as one way mirrors.

1 Like

Oh, I’ve always wondered how people detected noclipping. Thanks!

1 Like

You do it from the server, as the client can just delete parts and completely negate it. In this case they can’t really avoid it. Now since rays can be costly the bigger they are, if a player teleports that’s a hit. Instead of taking it do a check to see the distance between the position before doing the rays. If it’s too far apart just skip the rays as they teleported and go straight to whatever ur punishment is.

I recommend just teleporting them straight back to where they were, doesn’t punish laggy players with a ban and annoys exploiters.

3 Likes

Interesting. If I ever make something reliant on positioning, I’ll be sure to refer back to this.

1 Like