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 frame rate (or in XNA's multi-threaded case update rate). Because a realistic simulation of the projectile will have it traveling 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 center and its radius.
Ideally we want the bullet to be traveling along an axis, this is so we can disregard unnecessary dimensions. To do this, you'll need to use trigonometry to find the angles the bullet is traveling 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:
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.