bepu / bepuphysics2

Pure C# 3D real time physics simulation library, now with a higher version number.
Apache License 2.0
2.25k stars 261 forks source link

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

Closed zyxia closed 3 months ago

zyxia commented 3 months ago

https://github.com/bepu/bepuphysics2/blob/05e3e8d33f5cbfd78064182313f9ad416cff9276/BepuPhysics/CollisionDetection/CollisionTasks/BoxCylinderTester.cs#L27

RossNordby commented 3 months ago

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.