A helping hand for bedroom coders throughout the land.
Bullet Collision

Bullet collision can often be a problem, while a small projectile moving at high velocity will cause a lot of damage in the real world in can be hard to simulate the collision properly using collision methods which would treat the projectile as an ordinary object. As such object to object collision (for example sphere to sphere) collision requires either a very low projectile velocity or a very high framerate (or in XNA's multithreaded case updaterate). Because a realistic simulation of the projectile will have it travelling a large distance between position updates, it is necessary to treat the entire range of positions between the last update position and current update position as potential collision sites. As such line to object collision is highly useful, as you can test a line between the last update position and current bullet position with your impactable targets.

For this example I'm going to explain line to sphere collision, much of collision detection involves working out distances (for instance 3d sphere to sphere collision is essentially a one dimensional distance measurement, just rotated into a 3d plane.) In this example we need four pieces of data: the bullet's last update position, it's current position, the target collision sphere centre and its radius.

Ideally we want the bullet to be travelling along an axis, this is so we can disregard unneccessary dimensions. To do this, you'll need to use trigonometry to find the angles the bullet is travelling at to the axis (in this case the z axis) once this has been done you'll end up essentially with the data which the two diagrams below provide.



The y-x diagram does not include the z axis, and therefore does not factor in two bullet positions, rather the common path of the bullet and what the bullet could potentially or have potentially hit. The y-z diagram however factors in the bullet distance travelled to see whether it intersects with the sphere (or circle) from a side perspective.

Wrapping it up:

  • Find the bullet's last position, it's current position, and the object you're interested in's collision sphere position/radius.
  • Resolve the 3d co-ordinates so that the bullet is travelling along an axis to make detection much simpler.
  • Check the view from behind the bullet to see if a line of infinite length would intersect the sphere.
  • Check the view from the side to see if the actual line would intersect a sphere while ignoring the x axis.
  • If both the two conditions are true then a collision occured.

Posted Sat, Mar 14 2009 4:03 PM by Barnaby Smith
Filed under: , ,

Comments

Steve Conlan wrote re: Bullet Collision
on Sat, Mar 14 2009 5:52 PM

If you are doing a line-sphere ray hit test, why not get rid of all the decomposition, and simply solve the line-sphere hit test in 3 dimensions. Then you only need to check whether bullets current pos, previous pos and next pos interesects the sphere. Far quicker and easier.

Hergonan wrote re: Bullet Collision
on Sun, Mar 15 2009 11:48 AM

Usually with engines like half-life or prey's engine, they went with line traces to see if they hit any objects / targets, and I think line tracing is what you are talking about.

Basically they just get the speed vector of the bullet, then they draw that line and see if it intersects the collision of the target.

eg: www.tehupload.com/.../316023935021071bulletsphere.JPG

Good post!