camera.getScreenCoordinates(Vector3f worldLoc) returns an on-screen position when worldLocation argument is off camera

I am not sure if this is intended results or a potential bug, but here you can see a GUI element being placed with the camera.getScreenCoordinates() method, except the GUI Element (the floating ! ) can still be seen floating around the screen whenever the camera is turned away from the world location passed into the getScreenCoordinates method.

Is this supposed to work this way? If so then I can just do a dot product check to determine when the camera is facing away from the GUI element and then cull it to prevent it from floating around like this. However in the case its a bug it could be worthwhile to submit a PR to fix the method.

It’s just using the projection matrix to project the 3d coordinate into 2D space… it’s not doing any extra frustum checks.

So, yeah, when the object is behind the camera, it’s a perfectly valid question and result (mathematically speaking) to figure out how that projects onto the flat plane using the matrix4f projection matrix.

Dot product is the way.

1 Like

Now that I’ve got this all working, I have another general question about which way is better for making GUI elements like the ! and overhead health bars:

The two options I’m aware of are:

  1. Attach the element to the GUI node and use getScreenCoordinates() to position them (that’s how I’m doing the gold ! in this scene)

  2. Or use an unshaded material with a bill-board geometry that is attached to the rootNode and always faces the camera (that’s how the over-head health bar is done in my video)

On one hand I don’t like that the 1st option makes the overhead exclamation mark appear overtop of everything in the scene, which is unavoidable since its attached to the gui node

But on the other hand I don’t like that the 2nd option causes health bars to clip through other scene objects when the camera rotates.

I wonder If I am better off using an unshaded over-head billboard for the exclamation point, and make the health bars attached to the GUI node.

Or I wonder if I am just better off attaching them all to the GUINode and then do a ray-cast towards the camera to determine if the exclamation point is behind a wall/tree in order to temporarily cull it until its back in line of sight of the camera.

Note: this part is easily avoided… but then you end up with them writing on top of things like in option 1.

Personally, I go with the second option because I don’t have to update positions all the time… everything is automatic. And if I don’t want things to clip then I put them in the translucent bucket… and/or turn off depth test/write.

Edit: everything is automatic because I just attach them to the entity/node of the model and let the scene graph manage them.

1 Like