bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

it should be b*b -4*a*c here?

zyxia opened this issue · comments

The quadratic arises from:

dot(linePosition, linePosition) - radius * radius + t * 2 * dot(linePosition, lineDirection) + t^2 * dot(lineDirection, lineDirection) = 0

By inspection, one would expect the coefficient b to then be 2 * dot(linePosition, lineDirection, but in the code, it's computed as dot(linePosition, lineDirection).

So, the 'true' discriminant would be:
4 * dot(linePosition, lineDirection)^2 - 4 * dot(linePosition, linePosition) * dot(lineDirection, lineDirection).

But note that the sign of the discriminant, and thus intersection state, does not change when scaling by a positive scalar. The common factor 4 can be pulled out.

You could write the quadratic equation to match like so: (b +- 2 * sqrt(b^2 / 4 - a * c)) / (2 * a). We can go further: (2 * (b/2) +- 2 * sqrt(b^2 / 4 - a * c)) / (2 * a), which simplifies to (b / 2 +- sqrt(b^2/4 - a * c)) / a. Since b here includes a factor of 2, the divisions disappear when actually calculating the result.