Page 1 of 1

Accurate cross hairs

Posted: Wed Feb 16, 2011 11:53 pm
by lazerblade
Lot's of 3d game have them, too many want them. The objective is quite simple, create accurate cross hairs and draw them on the HUD. It may sound easy, but there is something I don't quite get.

Let's say I draw the cross hairs in the center of the screen as one pixel. I then line up the gun to face the center of the player's line of sight. This is no problem for things like handguns where ray-tracing can be used. But for a rocket launcher for example, things can get a bit icky. The rocket will only strike on the cross hairs if one of two scenarios occur. Either the rocket launcher being Perfectly lined up with the camera, or the player being the exact right distance from the object being struck.

This poses a problem which I have seen solved in many FPS and other games, but have failed to solve myself. How do you get accurate cross hairs for a projectile weapon like a rocket launcher?

I'm all ears. :mrgreen:

Posted: Thu Feb 17, 2011 12:23 am
by shadowslair
Try to:
1) cast a ray from camera origin towards 0,0,1 (towards the crosshair)
2) get the penetration point (if any)
3) your weapon-towards-target-dir will be the normalized(penetrPoint - weaponTipPosition); or sth alike.

Posted: Thu Feb 17, 2011 3:25 am
by lazerblade
So the rotation of the gun would be changed to face the cross hairs collision point based on ray tracing? True you could change the projectile's direction, but that might occasionally lead to unrealistic flight paths. Probably a compromise would be best.

Thx! :D

Posted: Thu Feb 17, 2011 3:36 am
by Lonesome Ducky
I solved this by casting a ray from the crosshair, and seeing where it hits. Then the projectile travels from the gun to the hit point. It can create clipping in a few instance, but it's almost never noticeable.

Posted: Thu Feb 17, 2011 5:44 am
by lazerblade
Sounds good, and looks like what I see happening in MechWarrior 4 based on the projectile overshooting the cross hairs if the target has moved. ;)

Posted: Thu Feb 17, 2011 10:03 am
by shadowslair
lazerblade wrote:So the rotation of the gun would be changed to face the cross hairs collision point based on ray tracing? True you could change the projectile's direction, but that might occasionally lead to unrealistic flight paths. Probably a compromise would be best.
Rotating the gun will be ugly- if you aim with the rocket launcher standing in front of a wall it may rotate that much, so it`ll cover almost the whole screen. You just shoot the projectile in the direction you get. Ducky does absolutely the same, and as he says- the clipping is minimal and somewhat what you want, plus it`s probably the best way to do it.

Posted: Thu Feb 17, 2011 3:34 pm
by lazerblade
Agreed.