I'm trying to detect, whether a projectile is going to hit a shield - and where will the collision happen.

enter image description here

On this image, you can see the situation. Though the A and B projectiles are in approximately the same distance from shield centre S, one of them will collide with the shield and other one will not.

enter image description here

Solution that is mathematically straightforward would use the mathematical equation of circle and the equation of the line of the projectile path.

With a pen and a paper, I can calculate that. However, I have a very bad experience with implementing analytic geometry directly.

Also, this would return positives for projectiles leaving the shield. How do I filter them?

enter image description here

Second approach, friends idea, is measuring the minimal distance between projectile path and the shield centre - if l is smaller than r, a collision happens.

It seems easier to implement, but doesn't let me know where will the collision happen.

I work in 2 dimensions. I'm using C# and the Unity Engine, however universal solutions are welcome.

The projectile is, of course, considered a point with zero size.

有帮助吗?

解决方案

Use the parametric equation of the trajectory: X= X0 + t.P + X0, Y = t.Q + Y0 (starting from (X0, Y0) in direction (P, Q)).

Plug this into the circle equation (X - Xc)^2 + (Y - Yc)^2 = R^2, and get:

(t.P + Dx)^2 + (t.Q + Dy)^2 = (P^2+Q^2).t^2 + 2.(P.Dx+Q.Dy).t + Dx^2+Dy^2 = R^2.

This is a quadratic equation in t. The shield is hit whenever there is a positive root.

If your projectile has nonzero radius r, just imagine you deflate the projectile to 0 and inflate the target to R + r.

其他提示

Your shield can be described as:

(X - X1)^2 + (Y - Y1)^2 = R^2

The line of the projectile can be described as:

Y - Y3 = ((Y4 - Y3) / (X4 - X3)) * (X - X3)

From here,

Y = ((Y4 - Y3) / (X4 - X3)) * (X - X3) + Y3

Extending the first equation using the one above, we get:

(X - X1)^2 + (((Y4 - Y3) / (X4 - X3)) * (X - X3) + Y3 - Y1)^2 = R^2

This is a quadratic equation which, if solved will give you the X values of the intersections. Read the link for the solution of the quadratic equation, it will get you the first impulse to solve your problem with a single formula. Of course, if the discriminant is negative, then there is no intersection, as the equation does not have a real solution. If the discriminant is 0, then the projectile only touches the shield and if the discriminant is positive, then you get the entry and exit X with the solution of the equation.

Knowing the value(s) of X you can calculate the value(s) of Y with the following formula:

Y = ((Y4 - Y3) / (X4 - X3)) * (X - X3) + Y3

Finally, note that this works only if the line of the projectile is not vertical, because then X4 would be equal with X3 which would make the fundamental equation useless. For the case when the projectile traverses a vertical line, the line's equation would be:

X = X1

and you can use the equation of

Y = ((Y4 - Y3) / (X4 - X3)) * (X - X3) + Y3

to get the possible solutions (again, this is a quadratic equation)

So, the implementation should check whether the line of the projectile is vertical and you should choose the according solution. I hope this helped you.

If you made the shield as a GameObject, you could use the Bounds to determine if the projectile's future path is contained inside the shield area(Link to bounds). Calculate the future path of the projectile. Then see if any of those future points of the projectile are inside the shield bounds by using Bounds.Contains. If it is, then it will hit.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top