-2

I'm building a server-side(!) for online game and I need to check if bullet hit a target.

Client sends me position and angle of bullet fired, I know all the enemies coordinates and sizes. How can I calculate if bullet hit an enemy after a bullet fired? Enemies have different sizes, bullet is 1px size.

For example, I have player shooting angle of 45 degrees from 0, 0 coordinates, and an enemy on 200, 200 coordinates with 10 width, 15 height. How do I calculate that bullet hit the enemy?

UPDATE: The game is 2d dhooter from the top, like Crimsonland. Projectiles have start speed which decreases slowly.

genpfault
  • 51,148
  • 11
  • 85
  • 139
MurDaD
  • 362
  • 3
  • 10
  • i didnt read any but I am sure that there are complete books about that problem – 463035818_is_not_an_ai Sep 26 '17 at 14:17
  • in short: for each object you need to check at what position it would hit the bullet. The object that hits the bullet first is the one that really hits it. To avoid doing the calculation for each object you can use bounding boxes that allow to detect a non-collision very fast – 463035818_is_not_an_ai Sep 26 '17 at 14:19
  • 3
    How are the projectiles modeled? Instantaneous [hitscan](https://en.wikipedia.org/wiki/Hitscan)? Finite velocity? Is this top-down? Side-view with gravity? Right now you're hovering somewhere between "unclear what you're asking" and "too broad". – genpfault Sep 26 '17 at 14:30
  • @genpfault question updated – MurDaD Sep 26 '17 at 15:21
  • [Line Segment Circle Intersection](https://stackoverflow.com/questions/6091728/line-segment-circle-intersection) – genpfault Sep 26 '17 at 15:49

4 Answers4

1

You should get a glance at the power of a point. It will allow you to know the minimal distance between a line and a point. So if the distance is lower than the width of the target, it's a hit !

Captain'Flam
  • 479
  • 4
  • 12
1

The easiest way is to just check if the bullet is inside the rectangle of the enemy. If it is, dispose the bullet and the enemy.

basically:

while(game.is_on()){
    for(enemy = enemyCollection.begin(); enemy != enemyCollection.end(); ++enemy){
        if(enemy.contains(bullet)){
            enemy.dead();
            bullet.hit();
    }
}
bracco23
  • 2,181
  • 10
  • 28
  • Seems to be the best solution, but what if I need to check which enemy side was hit by bullet? For example if bullet hits from the back, it's another type of damage from if it hits from the face. – MurDaD Sep 26 '17 at 15:07
  • If the time step of the outer loop is small enough, of the four sides of the enemy the one closer to the bullet is the first is the hit side. Some other limit case exists, but it really boils down to just do different things that `enemy.dead()` and `bullet.hit()` inside the if. – bracco23 Sep 26 '17 at 15:12
1

If you need to check whether line intersects rectangle, then apply orientation test based on cross product - if sign of cross product is the same for all rectangle vertices - line does not intersect it.

Line L0-L1, point P

Orientation = Cross(P - L0, L1 - L0)

(instead of L1-L0 components you can use Cos(Fi) and Sin(Fi) of shooting angle)

If you need to get intersection points, use some algorithm for line clipping - for example, Liang-Barsky one.

MBo
  • 77,366
  • 5
  • 53
  • 86
1

It's not quite as easy as it looks. To get a full solution, you need to extrude your objects along their paths of travel. Then you do object to object intersection tests.

That's difficult, as extrusion of an arbitrary shape by an arbitrary path is hard. So it's easier to cheat a little bit. Bullets can have no area, so they are just line segments. Make sure the step size is low enough for the line segment to be relatively short. Then make the ships rectangles, again with relatively short lines. Now it's lots of short line to line intersection tests. You can do these quickly with the planesweep algorithm. The nice thing about planesweep is that you can also extend it to curves or shapes, by returning possible intersections rather than actual intersections.

Once you have an intersection between a bullet and a ship's collision rectangle, you might need to run a timestep to check that a hit actually takes place rather than a narrow miss. You can also determine whether the hit was on front, left, right or back at this point.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18