Skip to main content
GameDev.net gamedev.net
🔒 Locked

raytracing for collision detection?

Started by silverphyre673 Oct 11, 2005 at 6:24 PM 4 replies 9.1k views
Original Post
silverphyre673
silverphyre673
Say I wanted to develop a simple 3D first person shooter. How would I calculate when the bullets hit a wall? I mean, I understand that you would test to see if they collide with any section of a wall or player or something, but since bullets generally travel the entire distance from barrel to wall in one frame, how would you check whether they are going to hit something? Understand what I mean? I thought this had something to do with "ray tracing", which I have understood to me a graphics technique. I'm not sure what it is, or how to apply it to this situation. If anyone can enlighten me on this topic, I'd be very grateful. Thanks.
my siteGenius is 1% inspiration and 99% perspiration
Wyrframe
Wyrframe
Just drop a line segment into the scene, from the barrel to infinity in the aiming direction, and test its intersection against the world. The nearest hit to the start, the gun barrel, is where the bullet will hit.

The line is the ray. The intersection-test is the tracing. Done deal.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
silverphyre673
silverphyre673
OK... I'm still wondering how you avoid the problems that might come from "step increments". You can't test for a collision at each point from the barrel to the edge of the map, since there are an infinite number of points to test. Seems like you might wind up testing for a collision on one side of a wall, then on the other side, and assuming that the bullet missed the wall entirely, when it actually went straight through the wall. Get what I'm saying?
my siteGenius is 1% inspiration and 99% perspiration
FlyingDodo
FlyingDodo
I found this article very useful http://www.flipcode.com/articles/article_basiccollisions.shtml
It assumes that you have basic vector math knowledge.
silverphyre673
silverphyre673
I think that's going to be helpful. Thanks for the link!
my siteGenius is 1% inspiration and 99% perspiration
SimmerD
SimmerD
silverphyre673, by testing a mathematical line segment or ray, you are solving an equation for the exact time of intersection, so in effect, you are finding the zero, one, or infinitely many intersections that an infinite line can have with a plane.

The problem you describe is known as 'tunneling', and can be avoided with simple shapes such as a point, sphere or simple convex shapes. More complex objects suffer from the problem you mention without extra work, even in the most advanced modern physics systems.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.