3rd person shooter crosshairs screen location

im trying to add crosshairs for my third person view game.

when i raycast the shots the player makes i use the gun’s barrel as the origin and use the camera’s direction for the direction of the ray. I’d like to add crosshairs so the player can know what he might hit. Just putting crosshairs in the middle of the screen doesnt give such a great result because the camera is higher up than the characater.

Ive tried using cam.getScreenCoordinates like this.

[java]
//this is run when the player pushes e to “aim” the gun
Vector3f loc = gun.getWorldTranslation().clone();
Vector3f dir = world.getApplication().getCamera().getDirection().mult(50); // effective range of the weapon is 50 units
loc.addLocal(dir);

Vector3f screenCoordinates = world.getApplication().getCamera().getScreenCoordinates(loc);
crosshairs.setText("+");
crosshairs.setLocalTranslation(screenCoordinates);
[/java]

this however often gives me coordinate that arent even on the screen. or when it gives me viewable coordiantes theyre clearly in the wrong spot (decals are left in the world where the bullet hits the scenery so i can see where the crosshairs SHOULD be). I am possibly just not using that function correctly… ive also tried using various “ranges” closer and further than 50 to see if id get a relatively close crosshair but nothing worked out.

does anyone know how i could find this location on the screen?

How do you know the coordinates are not even on the screen? Did you print them out or something?

The only problem I see with your code is that you haven’t set the z value of screenCoordinates to something sensible and that maybe crossHairs is attached to something weird.

Either way, I’m not sure an arbitrary location 50 meters off of the gun barrel is going to be any better than an arbitrary crosshair in the center of the screen. If you aren’t pointing your gun in the same direction as the camera then any location you pick on the screen is only going to be valid for one very specific distance and just wrong for any other.

hmm so games like grand theft auto or splinter cell just ray cast from the center of the screen like it was an FPS?

i always assumed they had some way to find a “reasonable” cursor location and ray cast from the gun, especially since i think nearby obstacles will usually block your shot even if your crosshair is over an enemy.

after some thinking on the issue im thinking that actually might be the way to go, with an added change to it

i’ll first cast a ray from the center of the screen (where the crosshair is) and find the first piece geom it hits. then use that contact point to do a second raycast from the players gun to that position that was previously “picked” by the crosshair to do the actual collision test for the actual shot.

it requires an extra raycast, but i think it might work. I’m quite interested if anyone has any further ideas though.