# Does anyone suggest me how to do bullet detection?

I am currently working on the first person shooter.
Everthing works fine…now comes to the stage that to implement bullet detection. As you guys know, thats impossible to turn 2D inputs into 3D coordinates. So, what could I do?
I have been searching on Google. I cant find any suitable web site for reference. I would be very appreciate if anyone could help me! Superb thanks!

As you guys know, thats impossible to turn 2D inputs into 3D coordinates.
Not really. For example, when the user clicks on a point at the screen, you can find the 3D position on the near plane where he clicked.

Then just project a ray from the camera through the clicked point, and intersect it with the world geometry. You can accelerate this by checking bounding volumes first.

This works fine for infinitely fast and small bullets (e.g. guns). For slower bullets (e.g. rockets), use the ray as a movement path of your bullet geometry and do intersection tests.

“you can find the 3D position on the near plane where he clicked”…well, i am in a little bit confused in that, could u elaborate it more?

i am implementing the view of angle by just subtracting the mouse position. that means i am not using the MFC, the mouse is not always positioned at the middle. Then,…i am confusing how can i do it…

“bullet detection” is not 2D coordinate to 3D coordinate :
Think about it like “intersection of the bullet trajectory with any potential target”.

but you know…i am trying to capture the users mouse input, in case which only got the x and y 2d coordinates, so…how could i turn in into 3d in order to do the trajectory tracing, thats what i am confusing about
of coz, if i could trace the trajectory of the bullet, its rather easy in the implementation, just simply by collision detection

Yes, it’s completely a 3D thing.
You are the viewer who is at {0, 0, 0} in camera space. The gun may be a little elsewhere so you need to know the position of the gun and orientation.
So you will have a start point and direction.

So the problems comes down to ray to triangle intersection test.
The ray should be in the same space as the object. It is usually faster to transform the ray into object space, for each object and then do the test.

You can add to that more optimizations later.

BTW, your question is not GL related. It’s just math.

thanks v-man,

indeed the position of gun is the same as the camera, however, the orientation is something i am not sure about, as i want the user use the mouse’cursor to shoot
in case i am using mfc, i can position the mouse cursor at the center of the screen, thats makes everything fine, however, i am using purely opengl, in this case, the mouse position is not necessarily at the center, therefore, the orientation algorithm may easily run into fault.

is there any case that i could use the user input of mouse position, thats the x and y only, to do it? that is, where the user is clicking, where he/she is shooting at…

thanks v-man,
i have checked many comments from internet, it seems most people are using ray casting
let me have a try first…
by the way, thank you very much

Have a look at gluUnProject. For that you need x,y and z, but you can just set z=0, then you get a point at the near plane (or z=1 for the far plane, it really doesn’t matter).

From this point, together with the camera position, you can construct a ray.

SOOO thanks for your suggestion, overmind
i dont know such a thing! it just looked its spec up in the google

indeed i am looking for implementation by the select mode of the opengl, i havent finished it yet, however, your suggestion seems worth trying, that will be my backup plan, superb thanks, man!

hey, overmind

i am checking out this
http://www.3dkingdoms.com/selection.html#point
however, it doesnt return the right point…is that anything that i should take care??
thanks!

It is important to use the correct modelview matrix with gluUnProject, because that determines what coordinate system the returned coordinates are in.

Most of the time, you should have only the camera transformations on the modelview matrix. Then you will get the point back in world coordinates.

how do i know whether my modelview matrix is a correct one?

That depends on what you expect as result.

If you want exactly the same coordinates that you input to OpenGL, you need to use exactly the same modelview matrix as was used when drawing the object.

If you want the output in global world coordinates, you have to use the modelview matrix with camera transformations.

OK, THX!
let me have a try!

hey, Overmind, my friend!
i finally did it!
but i am not using the unproject method
i am not familiar with it still
however, i try the picking, and i did
YES!!!