How to check, if a player is inside a certain area


#1

Imagine you have something like cities, defined by x and y coordinates. Everytime a Player goes into a city, there should be a message, informing him that he has jhust entered / left city X.

So without crawling thorugh docs, I know that there is something like a onPlayerMove event, which fires if the player’s / entitiy’s location changes.

Normally I would get the new coordinates / location from the event and validate, if they are inside a certain area, that is also defined by x and y (of course) - probably I would have a list, containing Location objects and everytime a player moves I would have to loop through and check every object’s coordinates to know, if a player is inside it.
If so, a message shows up (I think I made my idea clear).

The thing is, that I’m very concerned, if this is the right way to implement it. Unfortunately, I can’t think of another possible way of realizing this feature.

So is it fine to do this like I initially wanted to, or are there way better solutions?


#2

If your area is a rectangle, you can check if the Player’s XYZ position is in the range of your area. Something like if (x > 100 && x < 200). If your area is not rectangular, maybe you can stick it together with multiple rectangles. Circles could be implemented by checking the distance to a certain center point.
If that all doesn’t work for you, you could theoretically create a list of Vector3i at best (not Location). That vector is a lighter than a whole Location. To optimize even more, you could just have an int[] for the coordinates for each XYZ. That would avoid the whole object creation for a vector.

Though the best solution is still checking if XYZ are in a certain range.

Edit: There are many resources on the internet about collision detection.
Edit2: AABB makes that whole range checking thing simpler.


#3

My ToolBox plugin defines regions, you cam compose them from multiple AABBs and receive events for entering/leaving


#4

Yeah, I know about that and yes, I’m talking about rectangles / building them with multiple rectangles. I know, how to check, if something is inside a rectangle. My problem is, if there is a mroe efficient way of doing this check.

I would do it exactly like you have described it. It’s the most natural way I can think of. Im just asking myself if this produces too much overhead (because I have to do this check on every move event - so quiet often).


#5

Sounds interesting, but basicially you also check everytime a player moves, if a certain area gets entered / left and then you fire a custom event, right?
So essentially you aren’t doing something different than I would do?


#6

yea.
mathematically AABB checking requires 6n*players comparisons, where n is the amount of AABBs. thats super cheap as comparing values is one cpu instruction.
if you are really concerned, you can skip the event and do it interval based. that wont trigger on simply looking around, but you’d always check every player even if they’re in a menu and can’t move or are afk.


#7

You can, to a certain extent, by using a caching mechanism of some sort. There are a few ways to limit the amount of times you actually need to do the position lookup. I know that FoxCore/FoxGuard does this to a large extent by caching which chunks contain the regions, so that you can just check if a player is in that region, and then if so continue down the list.

Beyond those kinds of optimizations, there isn’t a much better way to check if they are within a zone.


#8

That’s a great idea, I also thought about working with chunks additional to my initial idea :slight_smile:


#9

There’s so much that happens every tick in Minecraft, simple a > b checks are definitely fine.