What is the API that gets the mouse focus .. I am looking to point a player and find out their ID for interactions. Not a GUI thing.
mouse focus or object
-
- English
- angriff
- Thread is marked as Resolved.
-
-
Ok I guess the Raycast method is the way.. Yes?
Just what performance hit does it do?
void raycast(int collisionType, Callback<RayCastResult> callback)
-
If you want to find out which object the player is currently looking at, the raycast() method is indeed what you're looking for The method "shoots" a ray from the player camera position and checks if it collides with any solid objects. Since the raycast is performed clientside, a callback is required (if any collisions occurred, the result contains these information). You can find an example in the Javadoc.
A raycast is rather "expensive" in terms of performance (not expensive for the server, but for the particular client), however, it matters how often you actually perform it. It will only be a problem if it's performed too frequently, e.g. every tick, so using it in the PlayerChangePositionEvent or the UpdateEvent is not advisable. If you really have to perform a raycast frequently, there should be a delay of 50 or 100 ms (at least) between every raycast (per player). -
If you want to find out which object the player is currently looking at, the raycast() method is indeed what you're looking for The method "shoots" a ray from the player camera position and checks if it collides with any solid objects. Since the raycast is performed clientside, a callback is required (if any collisions occurred, the result contains these information). You can find an example in the Javadoc.
A raycast is rather "expensive" in terms of performance (not expensive for the server, but for the particular client), however, it matters how often you actually perform it. It will only be a problem if it's performed too frequently, e.g. every tick, so using it in the PlayerChangePositionEvent or the UpdateEvent is not advisable. If you really have to perform a raycast frequently, there should be a delay of 50 or 100 ms (at least) between every raycast (per player).Thanks Red.. ya I just finished writing a test routine that works using your examples. They work great. I do have a couple of questions
- Is the normal raycast easier (the one that sends just along the x direction) on the performance than the others? I have the concept that it is only sending one ray out..
- getting the name of the npc is an interesting exercise in typing correctly but works fine but if I wanted to get a players information would it be Object obj = result.getCollisionObject(); PLAYER player = (PLAYER) obj;? So I can later get the UID and other information from the target player. I can't test this without help so was looking to save some development time for my code.
Thanks
-
Is the normal raycast easier (the one that sends just along the x direction) on the performance than the others? I have the concept that it is only sending one ray out..
The performance is the same for these methods. The only thing that really matters is the "CollisionType", the more collision types you set, the bigger the impact on performance. For example, if you only want to check if the player is looking at another player, you should only define "CollisionType.PLAYERS" (in this case, the game only checks if the ray collides with other players, so there is no need for expensive checks if the ray also collides with npcs, vegetations etc)
getting the name of the npc is an interesting exercise in typing correctly but works fine but if I wanted to get a players information would it be Object obj = result.getCollisionObject(); PLAYER player = (PLAYER) obj;? So I can later get the UID and other information from the target player. I can't test this without help so was looking to save some development time for my code.
Yes, if the ray collides with a player, the "getCollisionObject()" function returns this particular player object. For example:
Java -
Wow thanks Red.. the instanceof part was not in my vocabulary.. .. so would have struggled as I cant test without peeps.. extremely helpful. Just made my day.
Participate now!
Don’t have an account yet? Create a new account now and be part of our community!