Shot tracing

I’m writing some sort of FPS. I was wondering how do I make an instant hit weapon, like a pistol or chaingun.
The question is: how do I get to know the first object that can be found between two points?
Some sort of function with this kind of header:
object traceshot(vector start, vector end, <whatever may be necessary…>);

Well, the way I would do it would be to think of the bullet as a person and use the direction your camera is viewing and move the bullet forward just like the person and use collision detection to find if you hit anything. If you want it to be instant than just use a loop that keeps moving the bullet forward until it hits something, then display the next scene. This may not be the fastest way, but it’s an idea.

P.S. If you use the loop make sure you use a variable that will stop the loop if it takes too long other wise it will freeze your program.

what about getting the distances from the player’s position to all objects intersected by your ray, then the object which has the smallest distance is the first object hit by your ray.
Instead of moving the “bullet” and checking for collisions every frame you should check only at the time you shoot(e.g. click mouse button).

Thanks Terminatore3. But don’t you think that’s a bit heavy on processor load? Maybe I’m wrong, but that’s what I think.
BoSoft: Well, that seems to be a good idea, but there’s still a problem: how do I know which objects are intersected by my ray? I could check it for all objects, but I was looking for something less expensive on CPU load (no, I’m not obsessed!). Isn’t there anything more efficient?

First check if your ray intersects with the bounding boxes/spheres of the objects. Then you only need to check one object(the nearest one) for intersections.

Yes, my process would be slow if you were shooting a long ways, but it also depends on how much you move the bullet such as testing every 0.1 units would be faster than testing every 0.01 units.

A raycast is always going to be faster than successive approximations.
That’s because it takes only a couple of calculations to get the intersection of a ray with a triangle.

Bounding spheres or bounding boxes are also quick to test against, so these would be useful to test before testing triangles.

But if you want to really speed things up, you should organise your scene into zones:

  1. put every object into one or more zones - you can either place them all into one, which means you always need to check neighbouring zones, or place them into multiple zones.
  2. when tracing, determine which zones the ray traverses, and process those in order of traversal. Since you always trace from a known position (a camera object or a character), it’s also possible to know which zone that is, so determining that is not difficult.
  3. per traversed zone, check all objects and walls for a collision, using bounding spheres/boxes/both to dismiss objects early.
  4. It’s up to you to decide whether you want to stop with the first collision or continue.

Obviously an editor won’t have this many options for optimisation.

Thanks guys.