I am planning to make a Laser Squad clone and I think I have most of the aspects covered. But the major headache is the projectiles shot/thrown.
The easy way would be to figure out the probability of hit and just mark miss/hit. But I want to be able to have the projectile to hit something eventually (collateral damage!).
Currently everything is flat 2D tile map and there would be full (wall, door) and half height (desk, chair, window) obstacles.
My idea is to draw an imaginary line from the shooter to the target and add some horizontal&vertical error based on the player skills. Then I would trace the modified path until it hits something. This is basically what the original Laser Squad seems to do.
Can you recommend any algorithms or other approaches for this?
Answer
The idea you have is to raytrace into the scenery to see what it hits. This is viable because your game is (presumably) tile based. When the bullet starts, you must know its 3D position, even if it is fake. Once you know the direction of the bullet, you can use a Bresenham algorithm (more commonly used for line drawing) to see which squares the bullet will "touch". You can then do collision checking only for the objects touched by the Bresenham line.
Basically:
- Determine 3D starting position of bullet
- Determine 3D direction of bullet
- Determine in which square the bullet will land (if it hits nothing)
- Draw a line (in data) with the direction of the bullet using Bresenham
- For each square the bullet enters, check if the bullet hits anything
No comments:
Post a Comment