In the past I've always worked on 3D games using proper physics engines and such. However I'm starting on my own game now, which will be a simple 2d game a bit like the old Missle Command. I want to do some simple line intersection tests in 2D, but I don't want to use a physics engine just for this simple ray casting as the game has no need for elaborate physics and collision response.
Here is a mockup image I drew of what I want to achieve.
- Red line is the ground that the game objects will sit on.
- Green shapes are the collision volumes for the sprites on the ground.
- Blue lines are what I want to calculate. I would like to be able to specify a 2D start point and a 2D direction vector for the line, then detect the first thing it hits, whether that be the red ground or one of the green shapes.
[attachment=28249:image.png]
So to achieve this I'm thinking I need to find some math for the following 3 scenarios.
- line-box collision check
- line-circle collision check
- line-ground collision check
I imagine scenario 1 and 2 should be pretty straight forward and would just be a matter of looping through the collision shapes and testing the line against each one. Then just returning the hit against the one that was the closest (i.e. the first hit). Does that sound right? Is there any links to this kind of math anywhere or even a basic math library somewhere that can do this?
Scenario 3 has me a little stuck, which is why I'm here. I was thinking that I could define a 1-dimensional array to store the height value of each pixel along the red line from left to right, i.e. left side of the red line in the image would be at index-0 in the array and the right side would be index-n. This would effectively give a 1-dimensional height map in a way. How could I test a line against this height map? Is there math for this sort of thing?