Fire event when player leaves an area


I have an inventory box (stationary) and when the user is near it, he can open it to see the inventory in it. Now when he leaves a certain cubic area around the box (the boxes range) and his box inventory gui is opened, the game should close it.

What is the most efficient way to implement this?

Thank you in advance.

However you are signaling the box to open can you not use that method to signal it to close? This is more dependent on your architecture and how you have wired up all of these items.

Telling it to close is not hard.

I have to detect when the user runs out of its area first. And I’d like to know the most efficient way I can do it with jMonkey’s means.

I see. Just get the location of the player and the location of the box (Vector3f) and do a distance check on them. The most efficient way is to use Vector3f.distanceSquared(), and see if it is less that range*range (range being the range of the inventory box), you avoid a square root operation this way which is important if this is checking every frame.