I'm working on a video game and I'm struggling with the math behind one of the enemies. The enemy is a grenade launcher mounted on a vertical rail, which can slide up and down, and lob a grenade at any angle with any amount of force. The grenade's path will be a parabola which must hit the player, but there are line segment boundaries in the way represented by their two endpoints which the parabola must avoid.
Here is a drawing:
What I'd like to do is calculate the equation for the parabola of the grenade which hits a target and misses all of the boundaries, from which I can figure out the position, angle, and force for the launcher to use. The parabola must be subject to these three constraints:
- The parabola must pass through the target point $(x_T, y_T)$
- The parabola must pass through the line segment $\overline{RS}$
- For each boundary $\overline{EF}$, if the parabola passes through the segment, it must not happen between the line $\overline{RS}$ and the target point.
Depending on where the target is, there may be no solution, in which case I'd like it to return that information. If there is any solution there will be multiple; I would only need one.
What I've tried so far:
We can represent the parabola as $y-y_T=A(x-x_T)^2+B(x-x_T)$, which takes care of the first constraint, and means we need to find values for $A$ and $B$ that satisfy the other two constraints (we know $A$ must be negative because of the direction of gravity). Then for each boundary $\overline{EF}$ on the map, with endpoints $(x_E,y_E)$ and $(x_F,y_F)$, we can represent the boundary line as $(y_E-y_F)x+(x_F-x_E)y+(x_E y_F-x_F y_E)=0$. From that I can find the points of intersection between the line and the parabola, and make sure that for every boundary, the x-values of the points are either not between $x_E$ and $x_F$, or they are not between $x_S$ and $x_T$. This quickly becomes a nasty quadratic equation, which then creates a system of linear inequalities that I don't know how to solve. Can anyone think of a better way to approach the problem?