Allowing player to shoot?

Started by
2 comments, last by Supernat02 16 years ago
I've been working on an FPS type game and I want to give the player the ability to shoot at enemies. From browsing these forums, it looks like picking is the way to do this. Is the term picking specific to mesh picking, or is there another type that better suites this type of functionality? I've been messing with unproject but haven't been able to get the behavior I want. I'm wondering if I'm going down the right path or wasting time on the wrong thing.
Advertisement
Unproject is only part of the problem. Once you've managed to project your vector from screen space to world space, you will need to perform a ray-mesh collision check, where the ray will be along this newly acquired vector.
Best regards, Omid
There are a lot of posts discussing D3DXIntersect in this forum, which is probably what you're looking for. Use the Search function for the forum in the upper right-hand corner.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

It depends on how you want the shooting to occur. Do you want the player to aim a crosshair at the center of his view at the target, or do you want the player to move the mouse freely (without changing the view) and click on a target? In either case, generally speaking, you would use ray picking. The origin of the ray in the former case would just always be the center of the screen. If you have the position of your player and you know his orientation, just cast a ray from that position along that orientation. This ray will "hit" whatever is in the center of your current view (where the crosshair is).

Ray picking in general terms is just following a ray until you intersect "something" in space. It could be a mesh, a plane, a triangle, as you get into the details. There are some D3DX routines that help with this (D3DXIntersectMesh or D3DXMeshIntersect I believe, or something similar) if you are testing against a D3DXBaseMesh type. In theory, you would determine if the ray hits the plane that the triangle is in (determine the point on the plane) and then determine if that point lies within the bounds of the triangle.

Don't forget to test all meshes in front of you and keep track of the closest one to you.
Chris ByersMicrosoft DirectX MVP - 2005

This topic is closed to new replies.

Advertisement