0
\$\begingroup\$

I am currently creating an environment to train aim-bot AI's for my game. It is a 2d world made of complex polygons, and the goal is to have the AI's shoot ricocheting bullets. Using a ray-casting algorithm, I can tell whether the bullets are inside or outside the shape, but I do not know how to calculate the nearest edge of the polygon (so I can send the edge to a reflection function to reflect the vector of the bullet). Is this process even the best way of doing things:

  • if the bullet will end up inside of the polygon in the next move
  • reflect the bullet's vector based on the slope of the nearest edge
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You have the bullet source (origin); the nearest poly point is the "near" end of 2 possible edges. The other (end) point is on the same side relative to the bullet. You now have two angles (to each end of the edge), and the bullet angle in between to use as a ratio to interpolate the collision point on the chosen edge (end to end distance). The angle of reflection can be worked out using the collision point as an origin (back to the bullet's source and an end point).

Is this the "best" way? It's one way.

\$\endgroup\$
4
  • \$\begingroup\$ thanks for the help. You said The other (end) point is on the same side relative to the bullet, how do I calculate this? \$\endgroup\$
    – Beluker
    Commented Apr 10 at 2:04
  • \$\begingroup\$ If the first end point is "left" of the bullet (angle), the other (desired) end point is "right" of the bullet; if it's also left, you missed the edge, but picked the wrong edge in the first place. (I use a routine to find the "direction" of an angle relative to another; left, right, behind, etc.) \$\endgroup\$ Commented Apr 10 at 2:13
  • \$\begingroup\$ Thank you for the further explanation! : D \$\endgroup\$
    – Beluker
    Commented Apr 10 at 2:22
  • \$\begingroup\$ You're welcome! \$\endgroup\$ Commented Apr 10 at 2:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .