PhysicsWorld.CastRay does not hit if distance is large

I am doing some tests with PhysicsWorld.CastRay and it looks like the rays consistently do not hit something when I the End point is far away.

For example RayCastInput

End = origin + direction*1e10f // does not hit
End = origin + direction*1e6f // does hit

// just  for sake of completeness - not really usable without attaching an example project
Start = (0.1, 49.7, 0.0)
End = float3(437697,6f, -896598,6f, 66654,33f) // 1e6f

// Result:
Fraction = 0.000139309908 // 1e6f

This might be related to float having only about 6-9 significant digits, which are already reached when calculating the direction over 10 digits (end1e10-start1e0).

I assume there is no way to force double precision for Unity.Physics?

Scenario: Data from a scientific area, which easily spans orders larger than that.

Physics uses the general Translation component so, if possible, forcing double precision won’t just be a Unity Physics thing.
Is the usecase focused on querying a static world or trying to simulate a massive world as well?
To somewhat mitigate the precision issues, you’ll need some custom query code. For example, you could break the really long raycast into a series of AABB overlap queries. Then each partial ray segment can be converted to local space of any overlapping collider, and a raycast performed against the collider directly.

1 Like